Skip to content

Commit

Permalink
Reflect differences between the two libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Nov 20, 2020
1 parent ff026ef commit ebe0729
Show file tree
Hide file tree
Showing 23 changed files with 195 additions and 165 deletions.
2 changes: 1 addition & 1 deletion commands/completion_command.go
Original file line number Diff line number Diff line change
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
63 changes: 19 additions & 44 deletions internal/lsp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,17 @@ import (
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
}
4 changes: 2 additions & 2 deletions internal/lsp/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ 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
2 changes: 1 addition & 1 deletion internal/lsp/file.go
Original file line number Diff line number Diff line change
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),
}
}
13 changes: 0 additions & 13 deletions internal/lsp/file_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion internal/lsp/file_handler.go
Original file line number Diff line number Diff line change
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
20 changes: 8 additions & 12 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"
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,
}
}
8 changes: 4 additions & 4 deletions internal/lsp/position.go
Original file line number Diff line number Diff line change
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),
}
}
24 changes: 12 additions & 12 deletions internal/lsp/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ func fsRangeToLSP(fsRng *filesystem.Range) lsp.Range {

return lsp.Range{
Start: lsp.Position{
Character: fsRng.Start.Column,
Line: fsRng.Start.Line,
Character: float64(fsRng.Start.Column),
Line: float64(fsRng.Start.Line),
},
End: lsp.Position{
Character: fsRng.End.Column,
Line: fsRng.End.Line,
Character: float64(fsRng.End.Column),
Line: float64(fsRng.End.Line),
},
}
}
Expand All @@ -30,25 +30,25 @@ func lspRangeToFsRange(rng *lsp.Range) *filesystem.Range {

return &filesystem.Range{
Start: filesystem.Pos{
Line: rng.Start.Line,
Column: rng.Start.Character,
Line: int(rng.Start.Line),
Column: int(rng.Start.Character),
},
End: filesystem.Pos{
Line: rng.End.Line,
Column: rng.End.Character,
Line: int(rng.End.Line),
Column: int(rng.End.Character),
},
}
}

func HCLRangeToLSP(rng hcl.Range) lsp.Range {
return lsp.Range{
Start: lsp.Position{
Line: rng.Start.Line - 1,
Character: rng.Start.Column - 1,
Line: float64(rng.Start.Line - 1),
Character: float64(rng.Start.Column - 1),
},
End: lsp.Position{
Line: rng.End.Line - 1,
Character: rng.End.Column - 1,
Line: float64(rng.End.Line - 1),
Character: float64(rng.End.Column - 1),
},
}
}
2 changes: 1 addition & 1 deletion internal/lsp/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func ConvertSymbols(uri lsp.DocumentURI, sbs []decoder.Symbol) []lsp.SymbolInfor
for i, s := range sbs {
symbols[i] = lsp.SymbolInformation{
Name: s.Name(),
Kind: lsp.SKClass, // most applicable kind for now
Kind: lsp.Class, // most applicable kind for now
Location: lsp.Location{
Range: HCLRangeToLSP(s.Range()),
URI: uri,
Expand Down
52 changes: 52 additions & 0 deletions internal/lsp/text_edits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package lsp

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

func TextEditsFromDocumentChanges(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 textEdits(tes []lang.TextEdit, snippetSupport bool) []lsp.TextEdit {
edits := make([]lsp.TextEdit, len(tes))

for i, te := range tes {
edits[i] = *textEdit(te, snippetSupport)
}

return edits
}

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

return &lsp.TextEdit{
NewText: te.NewText,
Range: HCLRangeToLSP(te.Range),
}
}

func insertTextFormat(snippetSupport bool) lsp.InsertTextFormat {
if snippetSupport {
return lsp.SnippetTextFormat
}

return lsp.PlainTextTextFormat
}
18 changes: 17 additions & 1 deletion langserver/handlers/cancel_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@ package handlers

import (
"context"
"fmt"

"github.com/creachadair/jrpc2"
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

func CancelRequest(ctx context.Context, params lsp.CancelParams) error {
id := params.ID.String()
id, err := decodeRequestID(params.ID)
if err != nil {
return err
}

jrpc2.CancelRequest(ctx, id)
return nil
}

func decodeRequestID(v interface{}) (string, error) {
if val, ok := v.(string); ok {
return val, nil
}
if val, ok := v.(float64); ok {
return fmt.Sprintf("%d", int64(val)), nil
}

return "", fmt.Errorf("unable to decode request ID: %#v", v)
}
4 changes: 2 additions & 2 deletions langserver/handlers/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
lsp "github.com/hashicorp/terraform-ls/internal/protocol"
)

func (h *logHandler) TextDocumentComplete(ctx context.Context, params lsp.CompletionParams) (ilsp.CompletionList, error) {
var list ilsp.CompletionList
func (h *logHandler) TextDocumentComplete(ctx context.Context, params lsp.CompletionParams) (lsp.CompletionList, error) {
var list lsp.CompletionList

fs, err := lsctx.DocumentStorage(ctx)
if err != nil {
Expand Down
Loading

0 comments on commit ebe0729

Please sign in to comment.