Skip to content

Commit

Permalink
Merge pull request #217 from vekatze/lsp-completion-narrowing
Browse files Browse the repository at this point in the history
lsp: stop suggesting ambiguous candidates
  • Loading branch information
vekatze authored Sep 30, 2024
2 parents 182d95a + 2f793c4 commit 16d4ae0
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Scene/LSP/Complete.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Data.Containers.ListUtils (nubOrd)
import Data.HashMap.Strict qualified as Map
import Data.List (sort)
import Data.List.NonEmpty qualified as NE
import Data.Maybe (maybeToList)
import Data.Maybe (mapMaybe, maybeToList)
import Data.Set qualified as S
import Data.Text qualified as T
import Entity.BaseName qualified as BN
Expand Down Expand Up @@ -126,7 +126,7 @@ topCandidateToCompletionItem importSummaryOrNone candIsInCurrentSource presetSum
let fullyQualified = FullyQualified locator ll
let bare = Bare locator ll
let prefixed = map (`Prefixed` ll) prefixList
let cands = fullyQualified : bare : prefixed
let cands = mapMaybe (removeAmbiguousCand importSummaryOrNone) (fullyQualified : bare : prefixed)
flip map cands $ \cand -> do
let inPresetImport = isInPresetImport presetSummary cand
let needsTextEdit = not candIsInCurrentSource && not inPresetImport
Expand All @@ -141,6 +141,30 @@ data Cand
| Prefixed T.Text T.Text
| Bare T.Text T.Text

removeAmbiguousCand :: Maybe RawImportSummary -> Cand -> Maybe Cand
removeAmbiguousCand summaryOrNone cand = do
case cand of
Bare gl ll ->
case summaryOrNone of
Nothing ->
Just cand
Just (summary, _) -> do
if shouldRemoveLocatorPair (gl, ll) summary
then Nothing
else Just cand
_ ->
Just cand

shouldRemoveLocatorPair :: (T.Text, T.Text) -> [(T.Text, [T.Text])] -> Bool
shouldRemoveLocatorPair (gl, ll) summary = do
case summary of
[] ->
False
(gl', lls) : rest -> do
let b1 = gl /= gl' && ll `elem` lls
let b2 = shouldRemoveLocatorPair (gl, ll) rest
b1 || b2

isInPresetImport :: FastPresetSummary -> Cand -> Bool
isInPresetImport presetSummary cand =
case cand of
Expand Down

0 comments on commit 16d4ae0

Please sign in to comment.