Skip to content

Commit e07a5d7

Browse files
authored
lsp: format root directory files (#1232)
This had been disabled by mistake, now files in the project root will still be formatted using the configured formatter but will still not be templated. Signed-off-by: Charlie Egan <[email protected]>
1 parent aeec96b commit e07a5d7

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

internal/lsp/server.go

+9-14
Original file line numberDiff line numberDiff line change
@@ -2079,24 +2079,22 @@ func (l *LanguageServer) handleTextDocumentFormatting(
20792079

20802080
var oldContent string
20812081

2082-
var ok bool
2083-
20842082
// Fetch the contents used for formatting from the appropriate cache location.
20852083
if l.ignoreURI(params.TextDocument.URI) {
2086-
oldContent, ok = l.cache.GetIgnoredFileContents(params.TextDocument.URI)
2084+
oldContent, _ = l.cache.GetIgnoredFileContents(params.TextDocument.URI)
20872085
} else {
2088-
oldContent, ok = l.cache.GetFileContents(params.TextDocument.URI)
2089-
}
2090-
2091-
// disable the templating feature for files in the workspace root.
2092-
if filepath.Dir(uri.ToPath(l.clientIdentifier, params.TextDocument.URI)) ==
2093-
uri.ToPath(l.clientIdentifier, l.workspaceRootURI) {
2094-
return []types.TextEdit{}, nil
2086+
oldContent, _ = l.cache.GetFileContents(params.TextDocument.URI)
20952087
}
20962088

20972089
// if the file is empty, then the formatters will fail, so we template
20982090
// instead
20992091
if oldContent == "" {
2092+
// disable the templating feature for files in the workspace root.
2093+
if filepath.Dir(uri.ToPath(l.clientIdentifier, params.TextDocument.URI)) ==
2094+
uri.ToPath(l.clientIdentifier, l.workspaceRootURI) {
2095+
return []types.TextEdit{}, nil
2096+
}
2097+
21002098
newContent, err := l.templateContentsForFile(params.TextDocument.URI)
21012099
if err != nil {
21022100
return nil, fmt.Errorf("failed to template contents as a templating fallback: %w", err)
@@ -2116,10 +2114,7 @@ func (l *LanguageServer) handleTextDocumentFormatting(
21162114
return ComputeEdits(oldContent, newContent), nil
21172115
}
21182116

2119-
if !ok {
2120-
return nil, fmt.Errorf("failed to get file contents for uri %q", params.TextDocument.URI)
2121-
}
2122-
2117+
// opa-fmt is the default formatter if not set in the client options
21232118
formatter := "opa-fmt"
21242119

21252120
if l.clientInitializationOptions.Formatter != nil {

0 commit comments

Comments
 (0)