Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace sourcegraph/go-lsp with gopls' internal/lsp/protocol #311

Merged
merged 3 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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