This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
textDocument/completion support via gocode #219
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff980ce
lsp: Add completion definitions v3
Contextualist 9416c6a
langserver/internal/gocode: vendor nsf/gocode@e990796
Contextualist c236682
lsp: add CompletionItemKind.String()
Contextualist 2eb00ea
Add generic test for completion
Contextualist 536ea11
langserver: Add textDocument/completion support
Contextualist 8c4f57a
langserver/internal/gocode: update to nsf/gocode@0444ce1
Contextualist 2300863
support completion for built-ins
Contextualist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package langserver | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/sourcegraph/go-langserver/langserver/internal/gocode" | ||
"github.com/sourcegraph/go-langserver/pkg/lsp" | ||
"github.com/sourcegraph/jsonrpc2" | ||
) | ||
|
||
var ( | ||
GocodeCompletionEnabled = false | ||
CIKConstantSupported = lsp.CIKVariable // or lsp.CIKConstant if client supported | ||
) | ||
|
||
func (h *LangHandler) handleTextDocumentCompletion(ctx context.Context, conn jsonrpc2.JSONRPC2, req *jsonrpc2.Request, params lsp.CompletionParams) (*lsp.CompletionList, error) { | ||
if !isFileURI(params.TextDocument.URI) { | ||
return nil, &jsonrpc2.Error{ | ||
Code: jsonrpc2.CodeInvalidParams, | ||
Message: fmt.Sprintf("textDocument/completion not yet supported for out-of-workspace URI (%q)", params.TextDocument.URI), | ||
} | ||
} | ||
|
||
// In the case of testing, our OS paths and VFS paths do not match. In the | ||
// real world, this is never the case. Give the test suite the opportunity | ||
// to correct the path now. | ||
vfsURI := params.TextDocument.URI | ||
if testOSToVFSPath != nil { | ||
vfsURI = pathToURI(testOSToVFSPath(uriToFilePath(vfsURI))) | ||
} | ||
|
||
// Read file contents and calculate byte offset. | ||
contents, err := h.readFile(ctx, vfsURI) | ||
if err != nil { | ||
return nil, err | ||
} | ||
filename := h.FilePath(params.TextDocument.URI) | ||
offset, valid, why := offsetForPosition(contents, params.Position) | ||
if !valid { | ||
return nil, fmt.Errorf("invalid position: %s:%d:%d (%s)", filename, params.Position.Line, params.Position.Character, why) | ||
} | ||
|
||
ca, rangelen := gocode.AutoComplete(contents, filename, offset) | ||
citems := make([]lsp.CompletionItem, len(ca)) | ||
for i, it := range ca { | ||
var kind lsp.CompletionItemKind | ||
switch it.Class.String() { | ||
case "const": | ||
kind = CIKConstantSupported | ||
case "func": | ||
kind = lsp.CIKFunction | ||
case "import": | ||
kind = lsp.CIKModule | ||
case "package": | ||
kind = lsp.CIKModule | ||
case "type": | ||
kind = lsp.CIKClass | ||
case "var": | ||
kind = lsp.CIKVariable | ||
} | ||
citems[i] = lsp.CompletionItem{ | ||
Label: it.Name, | ||
Kind: kind, | ||
Detail: it.Type, | ||
TextEdit: &lsp.TextEdit{ | ||
Range: lsp.Range{ | ||
Start: lsp.Position{Line: params.Position.Line, Character: params.Position.Character - rangelen}, | ||
End: lsp.Position{Line: params.Position.Line, Character: params.Position.Character}, | ||
}, | ||
NewText: it.Name, | ||
}, | ||
} | ||
} | ||
return &lsp.CompletionList{ | ||
IsIncomplete: false, | ||
Items: citems, | ||
}, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
*.8 | ||
*.a | ||
*.out | ||
gocode | ||
gocode.exe | ||
goremote | ||
gocodetest | ||
*.swp | ||
listidents | ||
showcursor | ||
showsmap | ||
rename |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (C) 2010 nsf <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you are setting a global variable here, but langserver supports multiple instances running at once in the same process.