1
0
mirror of https://github.com/golang/go synced 2024-10-01 18:18:32 -06:00
go/internal/lsp/imports.go
Rebecca Stambler 57eff0d8ac 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>
2018-12-17 20:01:16 +00:00

35 lines
822 B
Go

// 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
}