Skip to content

Commit a9333fd

Browse files
authored
Replace sourcegraph/go-lsp with gopls' internal/lsp/protocol (#311)
* Generate structs from golang/tools internal/lsp/protocol It would be best to import this like a normal package, but gopls team doesn't feel confident about the stability of their generator and so this remains under internal for now. That said this is still currently the most accurate and up to date structs we can get for Go today with little effort. * Replace sourcegraph/go-lsp with internal/protocol * Reflect differences between the two libraries
1 parent 53fb526 commit a9333fd

38 files changed

+4817
-190
lines changed

commands/completion_command.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212

1313
"github.com/hashicorp/terraform-ls/internal/filesystem"
1414
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
15+
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
1516
"github.com/hashicorp/terraform-ls/internal/terraform/rootmodule"
1617
"github.com/hashicorp/terraform-ls/logging"
1718
"github.com/mitchellh/cli"
18-
lsp "github.com/sourcegraph/go-lsp"
1919
)
2020

2121
type CompletionCommand struct {
@@ -77,7 +77,7 @@ func (c *CompletionCommand) Run(args []string) int {
7777
c.Ui.Error(fmt.Sprintf("Error parsing column: %s (expected number)", err))
7878
return 1
7979
}
80-
lspPos := lsp.Position{Line: line, Character: col}
80+
lspPos := lsp.Position{Line: float64(line), Character: float64(col)}
8181

8282
logger := logging.NewLogger(os.Stderr)
8383

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ require (
2222
github.com/pmezard/go-difflib v1.0.0
2323
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
2424
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
25-
github.com/sourcegraph/go-lsp v0.0.0-20200117082640-b19bb38222e2
2625
github.com/spf13/afero v1.3.2
2726
github.com/stretchr/testify v1.4.0
2827
github.com/vektra/mockery/v2 v2.3.0
2928
)
30-
31-
replace github.com/sourcegraph/go-lsp => github.com/radeksimko/go-lsp v0.1.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
301301
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
302302
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
303303
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
304-
github.com/radeksimko/go-lsp v0.1.0 h1:evTibJ/0G2QtamSdA6mi+Xov7t5RpoGmMcTK60bWp+E=
305-
github.com/radeksimko/go-lsp v0.1.0/go.mod h1:tpps84QRlOVVLYk5QpKYX8Tr289D1v/UTWDLqeguiqM=
306304
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
307305
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
308306
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=

internal/context/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"time"
66

77
"github.com/hashicorp/terraform-ls/internal/filesystem"
8+
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
89
"github.com/hashicorp/terraform-ls/internal/terraform/rootmodule"
910
"github.com/hashicorp/terraform-ls/internal/watcher"
1011
"github.com/hashicorp/terraform-ls/langserver/diagnostics"
11-
"github.com/sourcegraph/go-lsp"
1212
)
1313

1414
type contextKey struct {

internal/lsp/completion.go

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,20 @@ package lsp
33
import (
44
"github.com/hashicorp/hcl-lang/lang"
55
"github.com/hashicorp/terraform-ls/internal/mdplain"
6-
lsp "github.com/sourcegraph/go-lsp"
6+
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
77
)
88

9-
type CompletionList struct {
10-
IsIncomplete bool `json:"isIncomplete"`
11-
Items []CompletionItem `json:"items"`
12-
}
13-
14-
type CompletionItem struct {
15-
lsp.CompletionItem
16-
Command *lsp.Command `json:"command,omitempty"`
17-
}
18-
19-
func ToCompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCapabilities) CompletionList {
20-
list := CompletionList{
21-
Items: make([]CompletionItem, len(candidates.List)),
9+
func ToCompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCapabilities) lsp.CompletionList {
10+
list := lsp.CompletionList{
11+
Items: make([]lsp.CompletionItem, len(candidates.List)),
2212
IsIncomplete: !candidates.IsComplete,
2313
}
2414

2515
snippetSupport := caps.Completion.CompletionItem.SnippetSupport
2616

2717
markdown := false
2818
docsFormat := caps.Completion.CompletionItem.DocumentationFormat
29-
if len(docsFormat) > 0 && docsFormat[0] == "markdown" {
19+
if len(docsFormat) > 0 && docsFormat[0] == lsp.Markdown {
3020
markdown = true
3121
}
3222

@@ -37,23 +27,23 @@ func ToCompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCap
3727
return list
3828
}
3929

40-
func toCompletionItem(candidate lang.Candidate, snippet, markdown bool) CompletionItem {
30+
func toCompletionItem(candidate lang.Candidate, snippet, markdown bool) lsp.CompletionItem {
4131
doc := candidate.Description.Value
4232

43-
// TODO: revisit once go-lsp supports markdown in CompletionItem
33+
// TODO: Revisit when MarkupContent is allowed as Documentation
34+
// https://github.com/golang/tools/blob/4783bc9b/internal/lsp/protocol/tsprotocol.go#L753
4435
doc = mdplain.Clean(doc)
4536

4637
var kind lsp.CompletionItemKind
4738
switch candidate.Kind {
4839
case lang.AttributeCandidateKind:
49-
kind = lsp.CIKProperty
40+
kind = lsp.PropertyCompletion
5041
case lang.BlockCandidateKind:
51-
kind = lsp.CIKClass
42+
kind = lsp.ClassCompletion
5243
case lang.LabelCandidateKind:
53-
kind = lsp.CIKField
44+
kind = lsp.FieldCompletion
5445
}
5546

56-
te, format := textEdit(candidate.TextEdit, snippet)
5747
var cmd *lsp.Command
5848
if candidate.TriggerSuggest {
5949
cmd = &lsp.Command{
@@ -62,29 +52,14 @@ func toCompletionItem(candidate lang.Candidate, snippet, markdown bool) Completi
6252
}
6353
}
6454

65-
return CompletionItem{
66-
CompletionItem: lsp.CompletionItem{
67-
Label: candidate.Label,
68-
Kind: kind,
69-
InsertTextFormat: format,
70-
Detail: candidate.Detail,
71-
Documentation: doc,
72-
TextEdit: te,
73-
},
74-
Command: cmd,
55+
return lsp.CompletionItem{
56+
Label: candidate.Label,
57+
Kind: kind,
58+
InsertTextFormat: insertTextFormat(snippet),
59+
Detail: candidate.Detail,
60+
Documentation: doc,
61+
TextEdit: textEdit(candidate.TextEdit, snippet),
62+
Command: cmd,
63+
AdditionalTextEdits: textEdits(candidate.AdditionalTextEdits, snippet),
7564
}
7665
}
77-
78-
func textEdit(te lang.TextEdit, snippetSupport bool) (*lsp.TextEdit, lsp.InsertTextFormat) {
79-
if snippetSupport {
80-
return &lsp.TextEdit{
81-
NewText: te.Snippet,
82-
Range: HCLRangeToLSP(te.Range),
83-
}, lsp.ITFSnippet
84-
}
85-
86-
return &lsp.TextEdit{
87-
NewText: te.NewText,
88-
Range: HCLRangeToLSP(te.Range),
89-
}, lsp.ITFPlainText
90-
}

internal/lsp/diagnostics.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package lsp
22

33
import (
44
"github.com/hashicorp/hcl/v2"
5-
lsp "github.com/sourcegraph/go-lsp"
5+
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
66
)
77

88
func HCLSeverityToLSP(severity hcl.DiagnosticSeverity) lsp.DiagnosticSeverity {
99
var sev lsp.DiagnosticSeverity
1010
switch severity {
1111
case hcl.DiagError:
12-
sev = lsp.Error
12+
sev = lsp.SeverityError
1313
case hcl.DiagWarning:
14-
sev = lsp.Warning
14+
sev = lsp.SeverityWarning
1515
case hcl.DiagInvalid:
1616
panic("invalid diagnostic")
1717
}

internal/lsp/file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package lsp
22

33
import (
4+
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
45
"github.com/hashicorp/terraform-ls/internal/source"
5-
lsp "github.com/sourcegraph/go-lsp"
66
)
77

88
type File interface {
@@ -59,6 +59,6 @@ func FileFromDocumentItem(doc lsp.TextDocumentItem) *file {
5959
return &file{
6060
fh: FileHandlerFromDocumentURI(doc.URI),
6161
text: []byte(doc.Text),
62-
version: doc.Version,
62+
version: int(doc.Version),
6363
}
6464
}

internal/lsp/file_change.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package lsp
22

33
import (
44
"github.com/hashicorp/terraform-ls/internal/filesystem"
5-
"github.com/sourcegraph/go-lsp"
5+
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
66
)
77

88
type contentChange struct {
@@ -26,19 +26,6 @@ func DocumentChanges(events []lsp.TextDocumentContentChangeEvent, f File) (files
2626
return changes, nil
2727
}
2828

29-
func TextEdits(changes filesystem.DocumentChanges) []lsp.TextEdit {
30-
edits := make([]lsp.TextEdit, len(changes))
31-
32-
for i, change := range changes {
33-
edits[i] = lsp.TextEdit{
34-
Range: fsRangeToLSP(change.Range()),
35-
NewText: change.Text(),
36-
}
37-
}
38-
39-
return edits
40-
}
41-
4229
func (fc *contentChange) Text() string {
4330
return fc.text
4431
}

internal/lsp/file_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"path/filepath"
66
"strings"
77

8+
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
89
"github.com/hashicorp/terraform-ls/internal/uri"
9-
"github.com/sourcegraph/go-lsp"
1010
)
1111

1212
func FileHandlerFromDocumentURI(docUri lsp.DocumentURI) *fileHandler {
@@ -86,7 +86,7 @@ type versionedFileHandler struct {
8686
func VersionedFileHandler(doc lsp.VersionedTextDocumentIdentifier) *versionedFileHandler {
8787
return &versionedFileHandler{
8888
fileHandler: fileHandler{uri: string(doc.URI)},
89-
v: doc.Version,
89+
v: int(doc.Version),
9090
}
9191
}
9292

internal/lsp/file_handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package lsp
33
import (
44
"testing"
55

6-
"github.com/sourcegraph/go-lsp"
6+
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
77
)
88

99
var (

0 commit comments

Comments
 (0)