@@ -65,7 +65,6 @@ import Language.LSP.Types.Capabilities
65
65
import qualified Language.LSP.VFS as VFS
66
66
import Text.Fuzzy.Parallel (Scored (score ),
67
67
original )
68
- import Safe (fromJustNote )
69
68
70
69
-- Chunk size used for parallelizing fuzzy matching
71
70
chunkSize :: Int
@@ -588,10 +587,7 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
588
587
$ (if T. null enteredQual then id else mapMaybe (T. stripPrefix enteredQual))
589
588
allModNamesAsNS
590
589
591
- filtCompls = Fuzzy. filter chunkSize maxC prefixText ctxCompls (label . snd )
592
- where
593
-
594
- mcc = case maybe_parsed of
590
+ maybeContext = case maybe_parsed of
595
591
Nothing -> Nothing
596
592
Just (pm, pmapping) ->
597
593
let PositionMapping pDelta = pmapping
@@ -600,8 +596,10 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
600
596
hpos = upperRange position'
601
597
in getCContext lpos pm <|> getCContext hpos pm
602
598
599
+ filtCompls = Fuzzy. filter chunkSize maxC prefixText ctxCompls (label . snd )
600
+ where
603
601
-- completions specific to the current context
604
- ctxCompls' = case mcc of
602
+ ctxCompls' = case maybeContext of
605
603
Nothing -> compls
606
604
Just TypeContext -> filter ( isTypeCompl . snd ) compls
607
605
Just ValueContext -> filter (not . isTypeCompl . snd ) compls
@@ -637,8 +635,15 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
637
635
, enteredQual `T.isPrefixOf` original label
638
636
]
639
637
640
- getModuleName line = let ws = filter (/= " qualified" ) (T. words line)
641
- in if List. length ws >= 2 then Just (ws !! 1 ) else Nothing
638
+ moduleImportListCompletions :: String -> [Scored CompletionItem ]
639
+ moduleImportListCompletions moduleNameS =
640
+ let moduleName = T. pack moduleNameS
641
+ funcs = HM. lookupDefault HashSet. empty moduleName moduleExportsMap
642
+ funs = map (show . name) $ HashSet. toList funcs
643
+ in filterModuleExports moduleName $ map T. pack funs
644
+
645
+ -- manually parse in case we don't have completion context ("import [qualified ]ModuleName")
646
+ getModuleName line = filter (/= " qualified" ) (T. words line) !? 1
642
647
filtImportCompls = filtListWith (mkImportCompl enteredQual) importableModules
643
648
filterModuleExports moduleName = filtListWith $ mkModuleFunctionImport moduleName
644
649
filtKeywordCompls
@@ -647,20 +652,32 @@ getCompletions plId ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qu
647
652
648
653
if
649
654
-- TODO: handle multiline imports
655
+ | Just (ImportListContext moduleName) <- maybeContext
656
+ -> pure $ moduleImportListCompletions moduleName
657
+
658
+ | Just (ImportHidingContext moduleName) <- maybeContext
659
+ -> pure $ moduleImportListCompletions moduleName
660
+
661
+ -- TODO: Is manual parsing ever needed or is context always present for module?
662
+ -- If possible only keep the above.
650
663
| " import " `T.isPrefixOf` fullLine
651
- && isJust (getModuleName fullLine)
652
- && " (" `T.isInfixOf` fullLine
653
- -> do
654
- let moduleName = fromJustNote " NEVER FAILS: module name checked above" $ getModuleName fullLine
655
- funcs = HM. lookupDefault HashSet. empty moduleName moduleExportsMap
656
- funs = map (show . name) $ HashSet. toList funcs
657
- return $ filterModuleExports moduleName $ map T. pack funs
664
+ , Just moduleName <- getModuleName fullLine
665
+ , " (" `T.isInfixOf` fullLine
666
+ -> pure $ moduleImportListCompletions $ T. unpack moduleName
667
+
668
+ | Just (ImportContext _moduleName) <- maybeContext
669
+ -> return filtImportCompls
670
+
671
+ -- TODO: Can we avoid this manual parsing?
672
+ -- If possible only keep the above.
658
673
| " import " `T.isPrefixOf` fullLine
659
674
-> return filtImportCompls
675
+
660
676
-- we leave this condition here to avoid duplications and return empty list
661
677
-- since HLS implements these completions (#haskell-language-server/pull/662)
662
678
| " {-# " `T.isPrefixOf` fullLine
663
679
-> return []
680
+
664
681
| otherwise -> do
665
682
-- assumes that nubOrdBy is stable
666
683
let uniqueFiltCompls = nubOrdBy (uniqueCompl `on` snd . Fuzzy. original) filtCompls
0 commit comments