Skip to content

Commit

Permalink
Don't suggest if or contains following import (#834)
Browse files Browse the repository at this point in the history
Also:
- improve handling of manually invoked suggestions
- don't suggest "import" after "import "

Fixes #830

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Jun 13, 2024
1 parent 53dbce6 commit 3af7d06
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ linters:
- varnamelen
- nonamedreturns
- testpackage
- goconst
- gochecknoinits
- gomnd
- mnd
Expand Down
7 changes: 5 additions & 2 deletions internal/lsp/completions/providers/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ func (*Import) Run(c *cache.Cache, params types.CompletionParams, _ *Options) ([

_, currentLine := completionLineHelper(c, fileURI, params.Position.Line)

if params.Context.TriggerKind == completion.Invoked && params.Position.Character == 0 {
// the user manually invoked completions at the beginning of an empty line
if params.Position.Character == 0 && strings.TrimSpace(currentLine) == "" {
return importCompletionItem(params), nil
}

// the user must type i before we provide completions
if params.Position.Line != 0 && strings.HasPrefix(currentLine, "i") {
if params.Position.Character != 0 &&
strings.HasPrefix(currentLine, "i") &&
!strings.HasPrefix(currentLine, "import ") {
return importCompletionItem(params), nil
}

Expand Down
9 changes: 6 additions & 3 deletions internal/lsp/completions/providers/packagerefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ func (*PackageRefs) Run(c *cache.Cache, params types.CompletionParams, _ *Option
continue
}

// only suggest packages that match the last word
// the user has typed.
if !strings.HasPrefix(ref.Label, lastWord) {
// if the user has not typed in any word, they've triggered
// completion suggestions manually
if strings.TrimSpace(lastWord) != "" &&
// otherwise, only suggest packages that match the last
// word the user has typed.
!strings.HasPrefix(ref.Label, lastWord) {
continue
}

Expand Down
5 changes: 5 additions & 0 deletions internal/lsp/completions/providers/ruleheadkeyword.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func (*RuleHeadKeyword) Run(c *cache.Cache, params types.CompletionParams, _ *Op
return []types.CompletionItem{}, nil
}

firstWord := strings.TrimSpace(words[0])
if firstWord == "package" || firstWord == "import" {
return []types.CompletionItem{}, nil
}

lastWord := words[len(words)-1]

const keyWdContains = "contains"
Expand Down

0 comments on commit 3af7d06

Please sign in to comment.