Skip to content

Commit

Permalink
feat(lsp): import members
Browse files Browse the repository at this point in the history
  • Loading branch information
vknabel committed Nov 11, 2022
1 parent 00b568c commit ee7f98d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
1 change: 0 additions & 1 deletion examples/greeter/cmd/test.lithia
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module example

import tests
import results

tests.test "example", { fail =>
unless True, fail "should never happen"
Expand Down
43 changes: 42 additions & 1 deletion langsrv/handler-completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ func textDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
if err != nil {
return nil, err
}
switch targetNode.Type() {
case "{", "}":
if targetNode.Parent() != nil {
targetNode = targetNode.Parent()
}
}

insertAsStatement := false
contextReferenceType := targetNode.Type()
Expand All @@ -37,6 +43,42 @@ func textDocumentCompletion(context *glsp.Context, params *protocol.CompletionPa
completionItems := []protocol.CompletionItem{}

switch targetNode.Type() {
case "import_members":
globalDeclarations := rc.sourceFile.Declarations
for _, decl := range globalDeclarations {
decl, ok := decl.(ast.DeclImport)
if !ok || !includesAstSourcePosition(decl.Meta().Source, rc.position) {
continue
}
exportedDecls, err := rc.moduleDeclarationsForImportDecl(decl)
if err != nil {
return nil, err
}
for _, exportedDecl := range exportedDecls {
skip := false
for _, alreadyImported := range decl.Members {
if alreadyImported.Name == exportedDecl.DeclName() {
skip = true
break
}
}
if skip {
continue
}
detail := "import member"
insertText := fmt.Sprintf("%s,\n", exportedDecl.DeclName())
completionItem := protocol.CompletionItem{
Label: string(exportedDecl.DeclName()),
Kind: completionItemKindForDecl(exportedDecl),
Documentation: documentationMarkupContentForDecl(exportedDecl),
Detail: &detail,
InsertText: &insertText,
CommitCharacters: []string{" "},
}
completionItems = append(completionItems, completionItem)
}
}
return completionItems, nil
case parser.TYPE_NODE_IMPORT_DECLARATION, "import":
kind := protocol.CompletionItemKindModule
mods, err := ls.resolver.ImportableModules(rc.module.Package())
Expand Down Expand Up @@ -233,7 +275,6 @@ func (rc *ReqContext) textDocumentMemberAccessCompletionItems(context *glsp.Cont
if string(decl.DeclName()) != accessedExpr {
continue
}
// TODO: support imported module
moduleDecls, err := rc.moduleDeclarationsForImportDecl(decl)
if err != nil {
return nil, err
Expand Down

0 comments on commit ee7f98d

Please sign in to comment.