Skip to content

Commit

Permalink
Fix import location
Browse files Browse the repository at this point in the history
  • Loading branch information
kitagry committed Apr 29, 2022
1 parent 458eeea commit 5efa205
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
10 changes: 9 additions & 1 deletion langserver/internal/source/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,21 @@ func (p *Project) listImportCompletionItems(location *ast.Location) []Completion
}
}

locationForImport := &ast.Location{
Text: location.Text,
File: location.File,
Row: location.Row,
Col: 1,
Offset: location.Offset,
}

result := make([]CompletionItem, 0, len(refs))
for _, r := range refs {
if !inRef(r, alreadyExistPackages) {
result = append(result, CompletionItem{
Label: fmt.Sprintf("import %s", r.String()),
Kind: ImportItem,
TextEdit: createTextEdit(location, fmt.Sprintf("import %s", r.String())),
TextEdit: createTextEdit(locationForImport, fmt.Sprintf("import %s", r.String())),
})
}
}
Expand Down
48 changes: 48 additions & 0 deletions langserver/internal/source/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type completionTestCase struct {
files map[string]source.File
updateFile map[string]source.File
createLocation createLocationFunc
expectItems []source.CompletionItem
}
Expand Down Expand Up @@ -40,6 +41,39 @@ func TestProject_ListCompletionItemsStrict(t *testing.T) {
},
},
},
"Should list import library location is 1": {
files: map[string]source.File{
"src.rego": {
RawText: `package src
`,
},
"lib.rego": {
RawText: `package lib`,
},
},
updateFile: map[string]source.File{
"src.rego": {
RawText: `package src
im
`,
},
},
createLocation: createLocation(3, 2, "src.rego"),
expectItems: []source.CompletionItem{
{
Label: "import data.lib",
Kind: source.ImportItem,
TextEdit: &source.TextEdit{
Row: 3,
Col: 1,
Text: "import data.lib",
},
},
},
},
"Should not list already imported library": {
files: map[string]source.File{
"src.rego": {
Expand Down Expand Up @@ -184,6 +218,13 @@ violation[msg] {
t.Fatal(err)
}

for path, file := range tt.updateFile {
err := project.UpdateFile(path, file.RawText, file.Version)
if err != nil {
t.Fatal(err)
}
}

location := tt.createLocation(tt.files)
got, err := project.ListCompletionItems(location)
if err != nil {
Expand Down Expand Up @@ -508,6 +549,13 @@ default is_test = true`,
t.Fatal(err)
}

for path, file := range tt.updateFile {
err := project.UpdateFile(path, file.RawText, file.Version)
if err != nil {
t.Fatal(err)
}
}

location := tt.createLocation(tt.files)
got, err := project.ListCompletionItems(location)
if err != nil {
Expand Down

0 comments on commit 5efa205

Please sign in to comment.