cmd/golsp: support formatting in golsp
This commit adds support for some basic commands necessary for integration with VSCode. It also adds support for the "textDocument/format" method. Change-Id: I8fd0e33ca544ab65d3233efe2fef9716446ad4ff Reviewed-on: https://go-review.googlesource.com/138135 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
parent
308f0c7c09
commit
d457fc8054
|
@ -12,6 +12,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -26,6 +27,10 @@ var (
|
||||||
cpuprofile = flag.String("cpuprofile", "", "write CPU profile to this file")
|
cpuprofile = flag.String("cpuprofile", "", "write CPU profile to this file")
|
||||||
memprofile = flag.String("memprofile", "", "write memory profile to this file")
|
memprofile = flag.String("memprofile", "", "write memory profile to this file")
|
||||||
traceFlag = flag.String("trace", "", "write trace log to this file")
|
traceFlag = flag.String("trace", "", "write trace log to this file")
|
||||||
|
|
||||||
|
// Flags for compatitibility with VSCode.
|
||||||
|
logfile = flag.String("logfile", "", "filename to log to")
|
||||||
|
mode = flag.String("mode", "", "no effect")
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -80,6 +85,15 @@ func main() {
|
||||||
f.Close()
|
f.Close()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *logfile != "" {
|
||||||
|
f, err := os.Create(*logfile)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to create log file: %v", err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
log.SetOutput(io.MultiWriter(os.Stderr, f))
|
||||||
|
}
|
||||||
if err := run(context.Background()); err != nil {
|
if err := run(context.Background()); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,11 @@ package lsp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"go/format"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"golang.org/x/tools/internal/jsonrpc2"
|
"golang.org/x/tools/internal/jsonrpc2"
|
||||||
"golang.org/x/tools/internal/lsp/protocol"
|
"golang.org/x/tools/internal/lsp/protocol"
|
||||||
|
@ -14,7 +19,9 @@ import (
|
||||||
// RunServer starts an LSP server on the supplied stream, and waits until the
|
// RunServer starts an LSP server on the supplied stream, and waits until the
|
||||||
// stream is closed.
|
// stream is closed.
|
||||||
func RunServer(ctx context.Context, stream jsonrpc2.Stream, opts ...interface{}) error {
|
func RunServer(ctx context.Context, stream jsonrpc2.Stream, opts ...interface{}) error {
|
||||||
s := &server{}
|
s := &server{
|
||||||
|
activeFiles: make(map[protocol.DocumentURI]string),
|
||||||
|
}
|
||||||
conn, client := protocol.RunServer(ctx, stream, s, opts...)
|
conn, client := protocol.RunServer(ctx, stream, s, opts...)
|
||||||
s.client = client
|
s.client = client
|
||||||
return conn.Wait(ctx)
|
return conn.Wait(ctx)
|
||||||
|
@ -22,26 +29,74 @@ func RunServer(ctx context.Context, stream jsonrpc2.Stream, opts ...interface{})
|
||||||
|
|
||||||
type server struct {
|
type server struct {
|
||||||
client protocol.Client
|
client protocol.Client
|
||||||
|
|
||||||
|
initializedMu sync.Mutex
|
||||||
|
initialized bool // set once the server has received "initialize" request
|
||||||
|
|
||||||
|
activeFilesMu sync.Mutex
|
||||||
|
activeFiles map[protocol.DocumentURI]string // files
|
||||||
}
|
}
|
||||||
|
|
||||||
func notImplemented(method string) *jsonrpc2.Error {
|
func (s *server) cacheActiveFile(uri protocol.DocumentURI, changes []protocol.TextDocumentContentChangeEvent) error {
|
||||||
return jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not yet implemented", method)
|
s.activeFilesMu.Lock()
|
||||||
|
defer s.activeFilesMu.Unlock()
|
||||||
|
|
||||||
|
for _, change := range changes {
|
||||||
|
if change.RangeLength == 0 {
|
||||||
|
s.activeFiles[uri] = change.Text
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Initialize(context.Context, *protocol.InitializeParams) (*protocol.InitializeResult, error) {
|
func (s *server) readActiveFile(uri protocol.DocumentURI) (string, error) {
|
||||||
return nil, notImplemented("Initialize")
|
s.activeFilesMu.Lock()
|
||||||
|
defer s.activeFilesMu.Unlock()
|
||||||
|
|
||||||
|
content, ok := s.activeFiles[uri]
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("file not found: %s", uri)
|
||||||
|
}
|
||||||
|
return content, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *server) Initialize(ctx context.Context, params *protocol.InitializeParams) (*protocol.InitializeResult, error) {
|
||||||
|
s.initializedMu.Lock()
|
||||||
|
defer s.initializedMu.Unlock()
|
||||||
|
if s.initialized {
|
||||||
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server already initialized")
|
||||||
|
}
|
||||||
|
s.initialized = true
|
||||||
|
return &protocol.InitializeResult{
|
||||||
|
Capabilities: protocol.ServerCapabilities{
|
||||||
|
TextDocumentSync: protocol.TextDocumentSyncOptions{
|
||||||
|
Change: float64(protocol.Full), // full contents of file sent on each update
|
||||||
|
},
|
||||||
|
DocumentFormattingProvider: true,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Initialized(context.Context, *protocol.InitializedParams) error {
|
func (s *server) Initialized(context.Context, *protocol.InitializedParams) error {
|
||||||
return notImplemented("Initialized")
|
return nil // ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Shutdown(context.Context) error {
|
func (s *server) Shutdown(context.Context) error {
|
||||||
return notImplemented("Shutdown")
|
s.initializedMu.Lock()
|
||||||
|
defer s.initializedMu.Unlock()
|
||||||
|
if !s.initialized {
|
||||||
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeInvalidRequest, "server not initialized")
|
||||||
|
}
|
||||||
|
s.initialized = false
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Exit(context.Context) error {
|
func (s *server) Exit(ctx context.Context) error {
|
||||||
return notImplemented("Exit")
|
if s.initialized {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) DidChangeWorkspaceFolders(context.Context, *protocol.DidChangeWorkspaceFoldersParams) error {
|
func (s *server) DidChangeWorkspaceFolders(context.Context, *protocol.DidChangeWorkspaceFoldersParams) error {
|
||||||
|
@ -68,7 +123,8 @@ func (s *server) DidOpen(context.Context, *protocol.DidOpenTextDocumentParams) e
|
||||||
return notImplemented("DidOpen")
|
return notImplemented("DidOpen")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) DidChange(context.Context, *protocol.DidChangeTextDocumentParams) error {
|
func (s *server) DidChange(ctx context.Context, params *protocol.DidChangeTextDocumentParams) error {
|
||||||
|
s.cacheActiveFile(params.TextDocument.URI, params.ContentChanges)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,8 +212,31 @@ func (s *server) ColorPresentation(context.Context, *protocol.ColorPresentationP
|
||||||
return nil, notImplemented("ColorPresentation")
|
return nil, notImplemented("ColorPresentation")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) Formatting(context.Context, *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) {
|
func (s *server) Formatting(ctx context.Context, params *protocol.DocumentFormattingParams) ([]protocol.TextEdit, error) {
|
||||||
return nil, notImplemented("Formatting")
|
data, err := s.readActiveFile(params.TextDocument.URI)
|
||||||
|
if err != nil {
|
||||||
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "unable to format %s: %v", params.TextDocument.URI, err)
|
||||||
|
}
|
||||||
|
fmted, err := format.Source([]byte(data))
|
||||||
|
if err != nil {
|
||||||
|
return nil, jsonrpc2.NewErrorf(jsonrpc2.CodeInternalError, "unable to format %s: %v", params.TextDocument.URI, err)
|
||||||
|
}
|
||||||
|
// Get the ending line and column numbers for the original file.
|
||||||
|
line := strings.Count(data, "\n")
|
||||||
|
col := len(data) - strings.LastIndex(data, "\n")
|
||||||
|
if col < 0 {
|
||||||
|
col = 0
|
||||||
|
}
|
||||||
|
// TODO(rstambler): Compute text edits instead of replacing whole file.
|
||||||
|
return []protocol.TextEdit{
|
||||||
|
{
|
||||||
|
Range: protocol.Range{
|
||||||
|
Start: protocol.Position{0, 0},
|
||||||
|
End: protocol.Position{float64(line), float64(col)},
|
||||||
|
},
|
||||||
|
NewText: string(fmted),
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) RangeFormatting(context.Context, *protocol.DocumentRangeFormattingParams) ([]protocol.TextEdit, error) {
|
func (s *server) RangeFormatting(context.Context, *protocol.DocumentRangeFormattingParams) ([]protocol.TextEdit, error) {
|
||||||
|
@ -175,3 +254,7 @@ func (s *server) Rename(context.Context, *protocol.RenameParams) ([]protocol.Wor
|
||||||
func (s *server) FoldingRanges(context.Context, *protocol.FoldingRangeRequestParam) ([]protocol.FoldingRange, error) {
|
func (s *server) FoldingRanges(context.Context, *protocol.FoldingRangeRequestParam) ([]protocol.FoldingRange, error) {
|
||||||
return nil, notImplemented("FoldingRanges")
|
return nil, notImplemented("FoldingRanges")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func notImplemented(method string) *jsonrpc2.Error {
|
||||||
|
return jsonrpc2.NewErrorf(jsonrpc2.CodeMethodNotFound, "method %q not yet implemented", method)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue