@@ -2079,24 +2079,22 @@ func (l *LanguageServer) handleTextDocumentFormatting(
2079
2079
2080
2080
var oldContent string
2081
2081
2082
- var ok bool
2083
-
2084
2082
// Fetch the contents used for formatting from the appropriate cache location.
2085
2083
if l .ignoreURI (params .TextDocument .URI ) {
2086
- oldContent , ok = l .cache .GetIgnoredFileContents (params .TextDocument .URI )
2084
+ oldContent , _ = l .cache .GetIgnoredFileContents (params .TextDocument .URI )
2087
2085
} 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 )
2095
2087
}
2096
2088
2097
2089
// if the file is empty, then the formatters will fail, so we template
2098
2090
// instead
2099
2091
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
+
2100
2098
newContent , err := l .templateContentsForFile (params .TextDocument .URI )
2101
2099
if err != nil {
2102
2100
return nil , fmt .Errorf ("failed to template contents as a templating fallback: %w" , err )
@@ -2116,10 +2114,7 @@ func (l *LanguageServer) handleTextDocumentFormatting(
2116
2114
return ComputeEdits (oldContent , newContent ), nil
2117
2115
}
2118
2116
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
2123
2118
formatter := "opa-fmt"
2124
2119
2125
2120
if l .clientInitializationOptions .Formatter != nil {
0 commit comments