Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tidy: Use cmp.Or in a few places #1337

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions cmd/languageserver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"cmp"
"context"
"fmt"
"os"
Expand Down Expand Up @@ -35,12 +36,12 @@ func init() {
if err != nil {
fmt.Fprintln(os.Stderr, "error getting executable path:", err)
} else {
v := version.Version
if v == "" {
v = "Unknown"
}

fmt.Fprintf(os.Stderr, "Regal Language Server (path: %s, version: %s)", absPath, v)
fmt.Fprintf(
os.Stderr,
"Regal Language Server (path: %s, version: %s)",
absPath,
cmp.Or(version.Version, "Unknown"),
)
}
}

Expand Down
7 changes: 2 additions & 5 deletions internal/lsp/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package lsp

import (
"cmp"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -181,14 +182,10 @@ func buildSendHandler(
}

case resp != nil:
method := getMethod(resp.ID)
method := cmp.Or(getMethod(resp.ID), "(no previous request)")

deleteMethod(resp.ID)

if method == "" {
method = "(no previous request)"
}

if !cfg.ShouldLog(method) {
return
}
Expand Down
5 changes: 2 additions & 3 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package lsp

import (
"cmp"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -1220,9 +1221,7 @@ func (l *LanguageServer) templateContentsForFile(fileURI string) (string, error)
}

// if we are in the root, then we can use main as a default
if pkg == "" {
pkg = "main"
}
pkg = cmp.Or(pkg, "main")

if strings.HasSuffix(fileURI, "_test.rego") {
pkg += "_test"
Expand Down
8 changes: 2 additions & 6 deletions pkg/fixer/fixes/directorypackagemismatch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fixes

import (
"cmp"
"fmt"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -32,12 +33,7 @@ func (d *DirectoryPackageMismatch) Fix(fc *FixCandidate, opts *RuntimeOptions) (
return nil, err
}

rootPath := opts.BaseDir
if rootPath == "" {
rootPath = filepath.Dir(fc.Filename)
}

rootPath = filepath.Clean(rootPath)
rootPath := filepath.Clean(cmp.Or(opts.BaseDir, filepath.Dir(fc.Filename)))

newPath := filepath.Join(rootPath, pkgPath, filepath.Base(fc.Filename))

Expand Down
Loading