Skip to content

Commit

Permalink
Replace sourcegraph/go-lsp with gopls' internal/lsp/protocol (#311)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
radeksimko authored Nov 20, 2020
1 parent 53fb526 commit a9333fd
Show file tree
Hide file tree
Showing 38 changed files with 4,817 additions and 190 deletions.
4 changes: 2 additions & 2 deletions commands/completion_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (

"github.com/hashicorp/terraform-ls/internal/filesystem"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/terraform/rootmodule"
"github.com/hashicorp/terraform-ls/logging"
"github.com/mitchellh/cli"
lsp "github.com/sourcegraph/go-lsp"
)

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

logger := logging.NewLogger(os.Stderr)

Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ require (
github.com/pmezard/go-difflib v1.0.0
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
github.com/sourcegraph/go-lsp v0.0.0-20200117082640-b19bb38222e2
github.com/spf13/afero v1.3.2
github.com/stretchr/testify v1.4.0
github.com/vektra/mockery/v2 v2.3.0
)

replace github.com/sourcegraph/go-lsp => github.com/radeksimko/go-lsp v0.1.0
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,6 @@ github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/radeksimko/go-lsp v0.1.0 h1:evTibJ/0G2QtamSdA6mi+Xov7t5RpoGmMcTK60bWp+E=
github.com/radeksimko/go-lsp v0.1.0/go.mod h1:tpps84QRlOVVLYk5QpKYX8Tr289D1v/UTWDLqeguiqM=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
Expand Down
2 changes: 1 addition & 1 deletion internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"time"

"github.com/hashicorp/terraform-ls/internal/filesystem"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/terraform/rootmodule"
"github.com/hashicorp/terraform-ls/internal/watcher"
"github.com/hashicorp/terraform-ls/langserver/diagnostics"
"github.com/sourcegraph/go-lsp"
)

type contextKey struct {
Expand Down
65 changes: 20 additions & 45 deletions internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,20 @@ package lsp
import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/mdplain"
lsp "github.com/sourcegraph/go-lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

type CompletionList struct {
IsIncomplete bool `json:"isIncomplete"`
Items []CompletionItem `json:"items"`
}

type CompletionItem struct {
lsp.CompletionItem
Command *lsp.Command `json:"command,omitempty"`
}

func ToCompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCapabilities) CompletionList {
list := CompletionList{
Items: make([]CompletionItem, len(candidates.List)),
func ToCompletionList(candidates lang.Candidates, caps lsp.TextDocumentClientCapabilities) lsp.CompletionList {
list := lsp.CompletionList{
Items: make([]lsp.CompletionItem, len(candidates.List)),
IsIncomplete: !candidates.IsComplete,
}

snippetSupport := caps.Completion.CompletionItem.SnippetSupport

markdown := false
docsFormat := caps.Completion.CompletionItem.DocumentationFormat
if len(docsFormat) > 0 && docsFormat[0] == "markdown" {
if len(docsFormat) > 0 && docsFormat[0] == lsp.Markdown {
markdown = true
}

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

func toCompletionItem(candidate lang.Candidate, snippet, markdown bool) CompletionItem {
func toCompletionItem(candidate lang.Candidate, snippet, markdown bool) lsp.CompletionItem {
doc := candidate.Description.Value

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

var kind lsp.CompletionItemKind
switch candidate.Kind {
case lang.AttributeCandidateKind:
kind = lsp.CIKProperty
kind = lsp.PropertyCompletion
case lang.BlockCandidateKind:
kind = lsp.CIKClass
kind = lsp.ClassCompletion
case lang.LabelCandidateKind:
kind = lsp.CIKField
kind = lsp.FieldCompletion
}

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

return CompletionItem{
CompletionItem: lsp.CompletionItem{
Label: candidate.Label,
Kind: kind,
InsertTextFormat: format,
Detail: candidate.Detail,
Documentation: doc,
TextEdit: te,
},
Command: cmd,
return lsp.CompletionItem{
Label: candidate.Label,
Kind: kind,
InsertTextFormat: insertTextFormat(snippet),
Detail: candidate.Detail,
Documentation: doc,
TextEdit: textEdit(candidate.TextEdit, snippet),
Command: cmd,
AdditionalTextEdits: textEdits(candidate.AdditionalTextEdits, snippet),
}
}

func textEdit(te lang.TextEdit, snippetSupport bool) (*lsp.TextEdit, lsp.InsertTextFormat) {
if snippetSupport {
return &lsp.TextEdit{
NewText: te.Snippet,
Range: HCLRangeToLSP(te.Range),
}, lsp.ITFSnippet
}

return &lsp.TextEdit{
NewText: te.NewText,
Range: HCLRangeToLSP(te.Range),
}, lsp.ITFPlainText
}
6 changes: 3 additions & 3 deletions internal/lsp/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package lsp

import (
"github.com/hashicorp/hcl/v2"
lsp "github.com/sourcegraph/go-lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

func HCLSeverityToLSP(severity hcl.DiagnosticSeverity) lsp.DiagnosticSeverity {
var sev lsp.DiagnosticSeverity
switch severity {
case hcl.DiagError:
sev = lsp.Error
sev = lsp.SeverityError
case hcl.DiagWarning:
sev = lsp.Warning
sev = lsp.SeverityWarning
case hcl.DiagInvalid:
panic("invalid diagnostic")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/file.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lsp

import (
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/source"
lsp "github.com/sourcegraph/go-lsp"
)

type File interface {
Expand Down Expand Up @@ -59,6 +59,6 @@ func FileFromDocumentItem(doc lsp.TextDocumentItem) *file {
return &file{
fh: FileHandlerFromDocumentURI(doc.URI),
text: []byte(doc.Text),
version: doc.Version,
version: int(doc.Version),
}
}
15 changes: 1 addition & 14 deletions internal/lsp/file_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lsp

import (
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/sourcegraph/go-lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

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

func TextEdits(changes filesystem.DocumentChanges) []lsp.TextEdit {
edits := make([]lsp.TextEdit, len(changes))

for i, change := range changes {
edits[i] = lsp.TextEdit{
Range: fsRangeToLSP(change.Range()),
NewText: change.Text(),
}
}

return edits
}

func (fc *contentChange) Text() string {
return fc.text
}
Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/file_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"path/filepath"
"strings"

lsp "github.com/hashicorp/terraform-ls/internal/protocol"
"github.com/hashicorp/terraform-ls/internal/uri"
"github.com/sourcegraph/go-lsp"
)

func FileHandlerFromDocumentURI(docUri lsp.DocumentURI) *fileHandler {
Expand Down Expand Up @@ -86,7 +86,7 @@ type versionedFileHandler struct {
func VersionedFileHandler(doc lsp.VersionedTextDocumentIdentifier) *versionedFileHandler {
return &versionedFileHandler{
fileHandler: fileHandler{uri: string(doc.URI)},
v: doc.Version,
v: int(doc.Version),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/file_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lsp
import (
"testing"

"github.com/sourcegraph/go-lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/file_handler_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package lsp
import (
"testing"

"github.com/sourcegraph/go-lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

func TestFileHandler_valid_unix(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/file_handler_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lsp
import (
"testing"

"github.com/sourcegraph/go-lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

var (
Expand Down
22 changes: 9 additions & 13 deletions internal/lsp/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@ package lsp

import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/mdplain"
"github.com/sourcegraph/go-lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

func HoverData(data *lang.HoverData, cc lsp.TextDocumentClientCapabilities) lsp.Hover {
mdSupported := cc.Hover != nil &&
len(cc.Hover.ContentFormat) > 0 &&
mdSupported := len(cc.Hover.ContentFormat) > 0 &&
cc.Hover.ContentFormat[0] == "markdown"

value := data.Content.Value
if data.Content.Kind == lang.MarkdownKind && !mdSupported {
value = mdplain.Clean(value)
}

content := lsp.RawMarkedString(value)
rng := HCLRangeToLSP(data.Range)
// In theory we should be sending lsp.MarkedString (for old clients)
// when len(cc.Hover.ContentFormat) == 0, but that's not possible
// without changing lsp.Hover.Content field type to interface{}
//
// We choose to follow gopls' approach (i.e. cut off old clients).

return lsp.Hover{
Contents: []lsp.MarkedString{content},
Range: &rng,
Contents: markupContent(data.Content, mdSupported),
Range: HCLRangeToLSP(data.Range),
}
}
25 changes: 25 additions & 0 deletions internal/lsp/markup_content.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package lsp

import (
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/terraform-ls/internal/mdplain"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

func markupContent(content lang.MarkupContent, mdSupported bool) lsp.MarkupContent {
value := content.Value

kind := lsp.PlainText
if content.Kind == lang.MarkdownKind {
if mdSupported {
kind = lsp.Markdown
} else {
value = mdplain.Clean(value)
}
}

return lsp.MarkupContent{
Kind: kind,
Value: value,
}
}
10 changes: 5 additions & 5 deletions internal/lsp/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package lsp
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/filesystem"
lsp "github.com/sourcegraph/go-lsp"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

type filePosition struct {
Expand Down Expand Up @@ -40,16 +40,16 @@ func FilePositionFromDocumentPosition(params lsp.TextDocumentPositionParams, f F
return &filePosition{
fh: FileHandlerFromDocumentURI(params.TextDocument.URI),
pos: hcl.Pos{
Line: params.Position.Line + 1,
Column: params.Position.Character + 1,
Line: int(params.Position.Line) + 1,
Column: int(params.Position.Character) + 1,
Byte: byteOffset,
},
}, nil
}

func lspPosToFsPos(pos lsp.Position) filesystem.Pos {
return filesystem.Pos{
Line: pos.Line,
Column: pos.Character,
Line: int(pos.Line),
Column: int(pos.Character),
}
}
Loading

0 comments on commit a9333fd

Please sign in to comment.