@@ -33,6 +33,7 @@ import Data.Function (on)
33
33
import Data.Functor
34
34
import qualified Data.HashMap.Strict as HM
35
35
import qualified Data.HashSet as HashSet
36
+ import Data.Ord (Down (Down ))
36
37
import qualified Data.Set as Set
37
38
import Development.IDE.Core.Compile
38
39
import Development.IDE.Core.PositionMapping
@@ -540,6 +541,10 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
540
541
enteredQual = if T. null prefixModule then " " else prefixModule <> " ."
541
542
fullPrefix = enteredQual <> prefixText
542
543
544
+ -- Boolean labels to tag suggestions as qualified (or not)
545
+ qual = not (T. null prefixModule)
546
+ notQual = False
547
+
543
548
{- correct the position by moving 'foo :: Int -> String -> '
544
549
^
545
550
to 'foo :: Int -> String -> '
@@ -556,7 +561,7 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
556
561
$ (if T. null enteredQual then id else mapMaybe (T. stripPrefix enteredQual))
557
562
allModNamesAsNS
558
563
559
- filtCompls = Fuzzy. filter chunkSize maxC prefixText ctxCompls " " " " label False
564
+ filtCompls = Fuzzy. filter chunkSize maxC prefixText ctxCompls " " " " ( label . snd )
560
565
where
561
566
562
567
mcc = case maybe_parsed of
@@ -571,11 +576,11 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
571
576
-- completions specific to the current context
572
577
ctxCompls' = case mcc of
573
578
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
577
582
-- 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'
579
584
580
585
infixCompls :: Maybe Backtick
581
586
infixCompls = isUsedAsInfix fullLine prefixModule prefixText pos
@@ -595,9 +600,9 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
595
600
thisModName = Local $ nameSrcSpan name
596
601
597
602
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)
601
606
602
607
filtListWith f list =
603
608
[ fmap f label
@@ -629,13 +634,26 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
629
634
-> return []
630
635
| otherwise -> do
631
636
-- 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
638
646
]
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)
639
657
640
658
uniqueCompl :: CompItem -> CompItem -> Ordering
641
659
uniqueCompl x y =
0 commit comments