1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:44:42 -07:00

internal/lsp: add support for running goimports as a code action

This change adds support for goimports as a code action that can be run
on save. However, there do appear to be issues with the propagation of
the context.Only field of the CodeActionParams, so we treat every
codeAction as an organizeImports action - this should be fixed in the
next vscode-languageclient release
(https://github.com/Microsoft/vscode-languageserver-node/issues/442).

Change-Id: I64ca0034c393762248fde6521aba86ed9d41bf70
Reviewed-on: https://go-review.googlesource.com/c/154338
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2018-12-14 17:00:24 -05:00
parent 17661a9724
commit 57eff0d8ac
3 changed files with 69 additions and 6 deletions

34
internal/lsp/imports.go Normal file
View File

@ -0,0 +1,34 @@
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lsp
import (
"context"
"golang.org/x/tools/internal/lsp/cache"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/source"
)
func organizeImports(ctx context.Context, v *cache.View, uri protocol.DocumentURI) ([]protocol.TextEdit, error) {
f := v.GetFile(source.URI(uri))
tok, err := f.GetToken()
if err != nil {
return nil, err
}
r := source.Range{
Start: tok.Pos(0),
End: tok.Pos(tok.Size()),
}
content, err := f.Read()
if err != nil {
return nil, err
}
edits, err := source.Imports(ctx, tok.Name(), content, r)
if err != nil {
return nil, err
}
return toProtocolEdits(tok, content, edits), nil
}

View File

@ -56,6 +56,7 @@ func (s *server) Initialize(ctx context.Context, params *protocol.InitializePara
return &protocol.InitializeResult{ return &protocol.InitializeResult{
Capabilities: protocol.ServerCapabilities{ Capabilities: protocol.ServerCapabilities{
CodeActionProvider: true,
CompletionProvider: protocol.CompletionOptions{ CompletionProvider: protocol.CompletionOptions{
TriggerCharacters: []string{"."}, TriggerCharacters: []string{"."},
}, },
@ -250,8 +251,22 @@ func (s *server) DocumentSymbol(context.Context, *protocol.DocumentSymbolParams)
return nil, notImplemented("DocumentSymbol") return nil, notImplemented("DocumentSymbol")
} }
func (s *server) CodeAction(context.Context, *protocol.CodeActionParams) ([]protocol.CodeAction, error) { func (s *server) CodeAction(ctx context.Context, params *protocol.CodeActionParams) ([]protocol.CodeAction, error) {
return nil, notImplemented("CodeAction") edits, err := organizeImports(ctx, s.view, params.TextDocument.URI)
if err != nil {
return nil, err
}
return []protocol.CodeAction{
{
Title: "Organize Imports",
Kind: protocol.SourceOrganizeImports,
Edit: protocol.WorkspaceEdit{
Changes: map[protocol.DocumentURI][]protocol.TextEdit{
params.TextDocument.URI: edits,
},
},
},
}, nil
} }
func (s *server) CodeLens(context.Context, *protocol.CodeLensParams) ([]protocol.CodeLens, error) { func (s *server) CodeLens(context.Context, *protocol.CodeLensParams) ([]protocol.CodeLens, error) {

View File

@ -13,9 +13,10 @@ import (
"go/format" "go/format"
"golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/imports"
) )
// Format formats a document with a given range. // Format formats a file with a given range.
func Format(ctx context.Context, f File, rng Range) ([]TextEdit, error) { func Format(ctx context.Context, f File, rng Range) ([]TextEdit, error) {
fAST, err := f.GetAST() fAST, err := f.GetAST()
if err != nil { if err != nil {
@ -54,11 +55,24 @@ func Format(ctx context.Context, f File, rng Range) ([]TextEdit, error) {
if err := format.Node(buf, fset, node); err != nil { if err := format.Node(buf, fset, node); err != nil {
return nil, err return nil, err
} }
// TODO(rstambler): Compute text edits instead of replacing whole file. return computeTextEdits(rng, buf.String()), nil
}
// Imports formats a file using the goimports tool.
func Imports(ctx context.Context, filename string, content []byte, rng Range) ([]TextEdit, error) {
content, err := imports.Process(filename, content, nil)
if err != nil {
return nil, err
}
return computeTextEdits(rng, string(content)), nil
}
// TODO(rstambler): Compute text edits instead of replacing whole file.
func computeTextEdits(rng Range, content string) []TextEdit {
return []TextEdit{ return []TextEdit{
{ {
Range: rng, Range: rng,
NewText: buf.String(), NewText: content,
}, },
}, nil }
} }