Skip to content

Commit

Permalink
Guard rather than use error control flow
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 committed Oct 7, 2024
1 parent 932aa9c commit 0936ca7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
38 changes: 21 additions & 17 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -986,12 +986,16 @@ func (l *LanguageServer) StartTemplateWorker(ctx context.Context) {
case <-ctx.Done():
return
case job := <-l.templateFileJobs:
// disable the templating feature for files in the workspace root.
if filepath.Dir(uri.ToPath(l.clientIdentifier, job.URI)) ==
uri.ToPath(l.clientIdentifier, l.workspaceRootURI) {
continue
}

// determine the new contents for the file, if permitted
newContents, err := l.templateContentsForFile(job.URI)
if err != nil {
if !errors.Is(err, &templatingInRootError{}) {
l.logError(fmt.Errorf("failed to template new file: %w", err))
}
l.logError(fmt.Errorf("failed to template new file: %w", err))

continue
}
Expand Down Expand Up @@ -1085,13 +1089,14 @@ func (l *LanguageServer) StartWebServer(ctx context.Context) {
l.webServer.Start(ctx)
}

type templatingInRootError struct{}

func (*templatingInRootError) Error() string {
return "templating is not supported in the root of the workspace"
}

func (l *LanguageServer) templateContentsForFile(fileURI string) (string, error) {
// this function should not be called with files in the root, but if it is,
// then it is an error to prevent unwanted behavior.
if filepath.Dir(uri.ToPath(l.clientIdentifier, fileURI)) ==
uri.ToPath(l.clientIdentifier, l.workspaceRootURI) {
return "", errors.New("this function does not template files in the workspace root")
}

content, ok := l.cache.GetFileContents(fileURI)
if !ok {
return "", fmt.Errorf("failed to get file contents for URI %q", fileURI)
Expand All @@ -1101,11 +1106,6 @@ func (l *LanguageServer) templateContentsForFile(fileURI string) (string, error)
return "", errors.New("file already has contents, templating not allowed")
}

if filepath.Dir(uri.ToPath(l.clientIdentifier, fileURI)) ==
uri.ToPath(l.clientIdentifier, l.workspaceRootURI) {
return "", &templatingInRootError{}
}

diskContent, err := os.ReadFile(uri.ToPath(l.clientIdentifier, fileURI))
if err == nil {
// then we found the file on disk
Expand Down Expand Up @@ -2047,13 +2047,17 @@ func (l *LanguageServer) handleTextDocumentFormatting(
oldContent, ok = l.cache.GetFileContents(params.TextDocument.URI)
}

// disable the templating feature for files in the workspace root.
if filepath.Dir(uri.ToPath(l.clientIdentifier, params.TextDocument.URI)) ==
uri.ToPath(l.clientIdentifier, l.workspaceRootURI) {
return []types.TextEdit{}, nil
}

// if the file is empty, then the formatters will fail, so we template
// instead
if oldContent == "" {
newContent, err := l.templateContentsForFile(params.TextDocument.URI)
if err != nil && errors.Is(err, &templatingInRootError{}) {
return nil, nil
} else if err != nil {
if err != nil {
return nil, fmt.Errorf("failed to template contents as a templating fallback: %w", err)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/lsp/server_formatting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lsp
import (
"context"
"encoding/json"
"path/filepath"
"testing"

"github.com/sourcegraph/jsonrpc2"
Expand Down Expand Up @@ -30,7 +31,7 @@ func TestFormatting(t *testing.T) {
t.Fatalf("failed to create and init language server: %s", err)
}

mainRegoURI := fileURIScheme + tempDir + mainRegoFileName
mainRegoURI := fileURIScheme + filepath.Join(tempDir, "main/main.rego")

// Simple as possible — opa fmt should just remove a newline
content := `package main
Expand Down
3 changes: 1 addition & 2 deletions internal/lsp/server_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package lsp
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -145,7 +144,7 @@ func TestTemplateContentsForFileInWorkspaceRoot(t *testing.T) {
t.Fatalf("expected error")
}

if !errors.Is(err, &templatingInRootError{}) {
if !strings.Contains(err.Error(), "this function does not template files in the workspace root") {
t.Fatalf("expected error to be templatingInRootError, got %T", err)
}
}
Expand Down

0 comments on commit 0936ca7

Please sign in to comment.