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

lsp: format root directory files #1232

Merged
merged 1 commit into from
Oct 31, 2024
Merged
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
23 changes: 9 additions & 14 deletions internal/lsp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2079,24 +2079,22 @@ func (l *LanguageServer) handleTextDocumentFormatting(

var oldContent string

var ok bool

// Fetch the contents used for formatting from the appropriate cache location.
if l.ignoreURI(params.TextDocument.URI) {
oldContent, ok = l.cache.GetIgnoredFileContents(params.TextDocument.URI)
oldContent, _ = l.cache.GetIgnoredFileContents(params.TextDocument.URI)
} else {
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
oldContent, _ = l.cache.GetFileContents(params.TextDocument.URI)
}

// if the file is empty, then the formatters will fail, so we template
// instead
if oldContent == "" {
// 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
}

newContent, err := l.templateContentsForFile(params.TextDocument.URI)
if err != nil {
return nil, fmt.Errorf("failed to template contents as a templating fallback: %w", err)
Expand All @@ -2116,10 +2114,7 @@ func (l *LanguageServer) handleTextDocumentFormatting(
return ComputeEdits(oldContent, newContent), nil
}

if !ok {
return nil, fmt.Errorf("failed to get file contents for uri %q", params.TextDocument.URI)
}

// opa-fmt is the default formatter if not set in the client options
formatter := "opa-fmt"

if l.clientInitializationOptions.Formatter != nil {
Expand Down