Skip to content

Commit 478e17f

Browse files
committed
Sort qualified completions first
1 parent 710cf3b commit 478e17f

File tree

1 file changed

+32
-14
lines changed
  • ghcide/src/Development/IDE/Plugin/Completions

1 file changed

+32
-14
lines changed

ghcide/src/Development/IDE/Plugin/Completions/Logic.hs

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import Data.Function (on)
3333
import Data.Functor
3434
import qualified Data.HashMap.Strict as HM
3535
import qualified Data.HashSet as HashSet
36+
import Data.Ord (Down (Down))
3637
import qualified Data.Set as Set
3738
import Development.IDE.Core.Compile
3839
import Development.IDE.Core.PositionMapping
@@ -540,6 +541,10 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
540541
enteredQual = if T.null prefixModule then "" else prefixModule <> "."
541542
fullPrefix = enteredQual <> prefixText
542543

544+
-- Boolean labels to tag suggestions as qualified (or not)
545+
qual = not(T.null prefixModule)
546+
notQual = False
547+
543548
{- correct the position by moving 'foo :: Int -> String -> '
544549
^
545550
to 'foo :: Int -> String -> '
@@ -556,7 +561,7 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
556561
$ (if T.null enteredQual then id else mapMaybe (T.stripPrefix enteredQual))
557562
allModNamesAsNS
558563

559-
filtCompls = Fuzzy.filter chunkSize maxC prefixText ctxCompls "" "" label False
564+
filtCompls = Fuzzy.filter chunkSize maxC prefixText ctxCompls "" "" (label . snd)
560565
where
561566

562567
mcc = case maybe_parsed of
@@ -571,11 +576,11 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
571576
-- completions specific to the current context
572577
ctxCompls' = case mcc of
573578
Nothing -> compls
574-
Just TypeContext -> filter isTypeCompl compls
575-
Just ValueContext -> filter (not . isTypeCompl) compls
576-
Just _ -> filter (not . isTypeCompl) compls
579+
Just TypeContext -> filter ( isTypeCompl . snd) compls
580+
Just ValueContext -> filter (not . isTypeCompl . snd) compls
581+
Just _ -> filter (not . isTypeCompl . snd) compls
577582
-- Add whether the text to insert has backticks
578-
ctxCompls = map (\comp -> toggleAutoExtend config $ comp { isInfix = infixCompls }) ctxCompls'
583+
ctxCompls = (fmap.fmap) (\comp -> toggleAutoExtend config $ comp { isInfix = infixCompls }) ctxCompls'
579584

580585
infixCompls :: Maybe Backtick
581586
infixCompls = isUsedAsInfix fullLine prefixModule prefixText pos
@@ -595,9 +600,9 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
595600
thisModName = Local $ nameSrcSpan name
596601

597602
compls = if T.null prefixModule
598-
then localCompls ++ unqualCompls ++ (($Nothing) <$> anyQualCompls)
599-
else Map.findWithDefault [] prefixModule (getQualCompls qualCompls)
600-
++ (($ Just prefixModule) <$> anyQualCompls)
603+
then map (notQual,) localCompls ++ map (qual,) unqualCompls ++ ((notQual,) . ($Nothing) <$> anyQualCompls)
604+
else ((qual,) <$> Map.findWithDefault [] prefixModule (getQualCompls qualCompls))
605+
++ ((notQual,) . ($ Just prefixModule) <$> anyQualCompls)
601606

602607
filtListWith f list =
603608
[ fmap f label
@@ -629,13 +634,26 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
629634
-> return []
630635
| otherwise -> do
631636
-- assumes that nubOrdBy is stable
632-
let uniqueFiltCompls = nubOrdBy (uniqueCompl `on` Fuzzy.original) filtCompls
633-
let compls = (fmap.fmap) (mkCompl plId ideOpts) uniqueFiltCompls
634-
return $ mergeListsBy (flip compare `on` score_)
635-
[ filtModNameCompls
636-
, filtKeywordCompls
637-
, (fmap.fmap) (toggleSnippets caps config) compls
637+
let uniqueFiltCompls = nubOrdBy (uniqueCompl `on` snd . Fuzzy.original) filtCompls
638+
let compls = (fmap.fmap.fmap) (mkCompl plId ideOpts) uniqueFiltCompls
639+
return $
640+
(fmap.fmap) snd $
641+
sortBy (compare `on` lexicographicOrdering) $
642+
mergeListsBy (flip compare `on` score_)
643+
[ (fmap.fmap) (notQual,) filtModNameCompls
644+
, (fmap.fmap) (notQual,) filtKeywordCompls
645+
, (fmap.fmap.fmap) (toggleSnippets caps config) compls
638646
]
647+
where
648+
-- We use this ordering to alphabetically sort suggestions while respecting
649+
-- all the previously applied ordering sources. These are:
650+
-- 1. Qualified suggestions go first
651+
-- 2. Fuzzy score ranks next
652+
-- 3. label alphabetical ordering next
653+
-- 4. module alphabetical ordering
654+
lexicographicOrdering Fuzzy.Scored{score_, original=(isQual, CompletionItem{_label,_detail})} =
655+
-- TODO uncomment the line below to sort alphabetically and fix tests
656+
Down isQual -- , Down score_, _label, _detail)
639657

640658
uniqueCompl :: CompItem -> CompItem -> Ordering
641659
uniqueCompl x y =

0 commit comments

Comments
 (0)