Skip to content

Commit 661e769

Browse files
committed
refactor
1 parent 52bc706 commit 661e769

File tree

1 file changed

+24
-42
lines changed
  • ghcide/src/Development/IDE/Plugin/Completions

1 file changed

+24
-42
lines changed

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

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import Data.Either (fromRight)
3131
import Data.Function (on)
3232
import Data.Functor
3333
import qualified Data.HashMap.Strict as HM
34-
import qualified Data.Map.Strict as M
3534

3635
import qualified Data.HashSet as HashSet
3736
import Data.Monoid (First (..))
@@ -70,10 +69,10 @@ import qualified Language.LSP.VFS as VFS
7069
import Text.Fuzzy.Parallel (Scored (score),
7170
original)
7271

73-
import Data.Coerce (coerce)
7472
import Development.IDE
7573

7674
import qualified Data.Rope.UTF16 as Rope
75+
import Development.IDE.Spans.AtPoint (pointCommand)
7776

7877
-- Chunk size used for parallelizing fuzzy matching
7978
chunkSize :: Int
@@ -617,52 +616,35 @@ getCompletions plugins ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls,
617616
hpos = upperRange position'
618617
in getCContext lpos pm <|> getCContext hpos pm
619618

620-
dotFieldSelectorToCompl :: T.Text -> (Bool, CompItem)
621-
dotFieldSelectorToCompl label = (True, CI CiVariable label (ImportedFrom T.empty) Nothing label Nothing emptySpanDoc False Nothing)
622619

623620
-- we need the hieast to be fresh
624621
-- not fresh, hasfield won't have a chance. it would to another larger change to ghc IfaceTyCon to contain record fields
625-
tst :: [(Bool, CompItem)]
626-
tst = case maybe_ast_res of
627-
Just (HAR {hieAst = hieast, hieKind = HieFresh},_) -> concat $ pointCommand hieast (completionPrefixPos prefixInfo) (theFunc HieFresh)
622+
recordDotSyntaxCompls :: [(Bool, CompItem)]
623+
recordDotSyntaxCompls = case maybe_ast_res of
624+
Just (HAR {hieAst = hieast, hieKind = HieFresh},_) -> concat $ pointCommand hieast (completionPrefixPos prefixInfo) nodeCompletions
628625
_ -> []
629-
630-
getSels :: GHC.TyCon -> [T.Text]
631-
getSels tycon = let f fieldLabel = printOutputable fieldLabel
632-
in map f $ tyConFieldLabels tycon
633-
634-
theFunc :: HieKind Type -> HieAST Type -> [(Bool, CompItem)]
635-
theFunc kind node = concatMap g (nodeType $ nodeInfoH kind node)
636626
where
627+
nodeCompletions :: HieAST Type -> [(Bool, CompItem)]
628+
nodeCompletions node = concatMap g (nodeType $ nodeInfo node)
637629
g :: Type -> [(Bool, CompItem)]
638-
g (TyConApp theTyCon _) = map dotFieldSelectorToCompl $ getSels theTyCon
630+
g (TyConApp theTyCon _) = map (dotFieldSelectorToCompl (printOutputable $ GHC.tyConName theTyCon)) $ getSels theTyCon
639631
g _ = []
640-
641-
nodeInfoH :: HieKind a -> HieAST a -> NodeInfo a
642-
nodeInfoH (HieFromDisk _) = nodeInfo'
643-
nodeInfoH HieFresh = nodeInfo
644-
645-
pointCommand :: HieASTs t -> Position -> (HieAST t -> a) -> [a]
646-
pointCommand hf pos k =
647-
catMaybes $ M.elems $ flip M.mapWithKey (getAsts hf) $ \fs ast ->
648-
-- Since GHC 9.2:
649-
-- getAsts :: Map HiePath (HieAst a)
650-
-- type HiePath = LexialFastString
651-
--
652-
-- but before:
653-
-- getAsts :: Map HiePath (HieAst a)
654-
-- type HiePath = FastString
655-
--
656-
-- 'coerce' here to avoid an additional function for maintaining
657-
-- backwards compatibility.
658-
case selectSmallestContaining (sp $ coerce fs) ast of
659-
Nothing -> Nothing
660-
Just ast' -> Just $ k ast'
661-
where
662-
sloc fs = mkRealSrcLoc fs (fromIntegral $ line+1) (fromIntegral $ cha+1)
663-
sp fs = mkRealSrcSpan (sloc fs) (sloc fs)
664-
line = _line pos
665-
cha = _character pos
632+
getSels :: GHC.TyCon -> [T.Text]
633+
getSels tycon = let f fieldLabel = printOutputable fieldLabel
634+
in map f $ tyConFieldLabels tycon
635+
dotFieldSelectorToCompl :: T.Text -> T.Text -> (Bool, CompItem)
636+
--dotFieldSelectorToCompl label = (True, CI CiVariable label (ImportedFrom T.empty) Nothing label Nothing emptySpanDoc False Nothing)
637+
dotFieldSelectorToCompl recname label = (True, CI
638+
{ compKind = CiField
639+
, insertText = label
640+
, provenance = DefinedIn recname
641+
, typeText = Nothing
642+
, label = label
643+
, isInfix = Nothing
644+
, docs = emptySpanDoc
645+
, isTypeCompl = False
646+
, additionalTextEdits = Nothing
647+
})
666648

667649
-- completions specific to the current context
668650
ctxCompls' = case mcc of
@@ -692,7 +674,7 @@ getCompletions plugins ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls,
692674

693675
compls
694676
| T.null prefixScope = map (notQual,) localCompls ++ map (qual,) unqualCompls ++ ((notQual,) . ($ Nothing) <$> anyQualCompls)
695-
| not $ null tst = tst
677+
| not $ null recordDotSyntaxCompls = recordDotSyntaxCompls
696678
| otherwise = ((qual,) <$> Map.findWithDefault [] prefixScope (getQualCompls qualCompls))
697679
++ ((notQual,) . ($ Just prefixScope) <$> anyQualCompls)
698680

0 commit comments

Comments
 (0)