forked from haskell/haskell-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogic.hs
966 lines (853 loc) · 40.2 KB
/
Logic.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiWayIf #-}
-- Mostly taken from "haskell-ide-engine"
module Development.IDE.Plugin.Completions.Logic (
CachedCompletions
, cacheDataProducer
, localCompletionsForParsedModule
, getCompletions
, fromIdentInfo
, getCompletionPrefix
) where
import Control.Applicative
import Data.Char (isAlphaNum, isUpper)
import Data.Generics
import Data.List.Extra as List hiding
(stripPrefix)
import qualified Data.Map as Map
import Data.Maybe (catMaybes, fromMaybe,
isJust, listToMaybe,
mapMaybe)
import qualified Data.Text as T
import qualified Text.Fuzzy.Parallel as Fuzzy
import Control.Monad
import Data.Aeson (ToJSON (toJSON))
import Data.Either (fromRight)
import Data.Function (on)
import Data.Functor
import qualified Data.HashMap.Strict as HM
import qualified Data.HashSet as HashSet
import Data.Monoid (First (..))
import Data.Ord (Down (Down))
import qualified Data.Set as Set
import Development.IDE.Core.Compile
import Development.IDE.Core.PositionMapping
import Development.IDE.GHC.Compat hiding (ppr)
import qualified Development.IDE.GHC.Compat as GHC
import Development.IDE.GHC.Compat.Util
import Development.IDE.GHC.Error
import Development.IDE.GHC.Util
import Development.IDE.Plugin.Completions.Types
import Development.IDE.Spans.Common
import Development.IDE.Spans.Documentation
import Development.IDE.Spans.LocalBindings
import Development.IDE.Types.Exports
import Development.IDE.Types.HscEnvEq
import Development.IDE.Types.Options
#if MIN_VERSION_ghc(9,2,0)
import GHC.Plugins (Depth (AllTheWay),
defaultSDocContext,
mkUserStyle,
neverQualify,
renderWithContext,
sdocStyle)
#endif
import Ide.PluginUtils (mkLspCommand)
import Ide.Types (CommandId (..),
IdePlugins (..),
PluginId)
import Language.LSP.Types
import Language.LSP.Types.Capabilities
import qualified Language.LSP.VFS as VFS
import Text.Fuzzy.Parallel (Scored (score),
original)
import qualified Data.Text.Utf16.Rope as Rope
import Development.IDE
import Development.IDE.Spans.AtPoint (pointCommand)
-- Chunk size used for parallelizing fuzzy matching
chunkSize :: Int
chunkSize = 1000
-- From haskell-ide-engine/hie-plugin-api/Haskell/Ide/Engine/Context.hs
-- | A context of a declaration in the program
-- e.g. is the declaration a type declaration or a value declaration
-- Used for determining which code completions to show
-- TODO: expand this with more contexts like classes or instances for
-- smarter code completion
data Context = TypeContext
| ValueContext
| ModuleContext String -- ^ module context with module name
| ImportContext String -- ^ import context with module name
| ImportListContext String -- ^ import list context with module name
| ImportHidingContext String -- ^ import hiding context with module name
| ExportContext -- ^ List of exported identifiers from the current module
deriving (Show, Eq)
-- | Generates a map of where the context is a type and where the context is a value
-- i.e. where are the value decls and the type decls
getCContext :: Position -> ParsedModule -> Maybe Context
getCContext pos pm
| Just (L (locA -> r) modName) <- moduleHeader
, pos `isInsideSrcSpan` r
= Just (ModuleContext (moduleNameString modName))
| Just (L (locA -> r) _) <- exportList
, pos `isInsideSrcSpan` r
= Just ExportContext
| Just ctx <- something (Nothing `mkQ` go `extQ` goInline) decl
= Just ctx
| Just ctx <- something (Nothing `mkQ` importGo) imports
= Just ctx
| otherwise
= Nothing
where decl = hsmodDecls $ unLoc $ pm_parsed_source pm
moduleHeader = hsmodName $ unLoc $ pm_parsed_source pm
exportList = hsmodExports $ unLoc $ pm_parsed_source pm
imports = hsmodImports $ unLoc $ pm_parsed_source pm
go :: LHsDecl GhcPs -> Maybe Context
go (L (locA -> r) SigD {})
| pos `isInsideSrcSpan` r = Just TypeContext
| otherwise = Nothing
go (L (locA -> r) GHC.ValD {})
| pos `isInsideSrcSpan` r = Just ValueContext
| otherwise = Nothing
go _ = Nothing
goInline :: GHC.LHsType GhcPs -> Maybe Context
goInline (GHC.L (locA -> r) _)
| pos `isInsideSrcSpan` r = Just TypeContext
goInline _ = Nothing
importGo :: GHC.LImportDecl GhcPs -> Maybe Context
importGo (L (locA -> r) impDecl)
| pos `isInsideSrcSpan` r
= importInline importModuleName (fmap (fmap reLoc) $ ideclHiding impDecl)
<|> Just (ImportContext importModuleName)
| otherwise = Nothing
where importModuleName = moduleNameString $ unLoc $ ideclName impDecl
importInline :: String -> Maybe (Bool, GHC.Located [LIE GhcPs]) -> Maybe Context
importInline modName (Just (True, L r _))
| pos `isInsideSrcSpan` r = Just $ ImportHidingContext modName
| otherwise = Nothing
importInline modName (Just (False, L r _))
| pos `isInsideSrcSpan` r = Just $ ImportListContext modName
| otherwise = Nothing
importInline _ _ = Nothing
occNameToComKind :: Maybe T.Text -> OccName -> CompletionItemKind
occNameToComKind ty oc
| isVarOcc oc = case occNameString oc of
i:_ | isUpper i -> CiConstructor
_ -> CiFunction
| isTcOcc oc = case ty of
Just t
| "Constraint" `T.isSuffixOf` t
-> CiInterface
_ -> CiStruct
| isDataOcc oc = CiConstructor
| otherwise = CiVariable
showModName :: ModuleName -> T.Text
showModName = T.pack . moduleNameString
mkCompl :: Maybe PluginId -- ^ Plugin to use for the extend import command
-> IdeOptions -> CompItem -> CompletionItem
mkCompl
pId
IdeOptions {..}
CI
{ compKind,
isInfix,
insertText,
provenance,
typeText,
label,
docs,
additionalTextEdits
} = do
let mbCommand = mkAdditionalEditsCommand pId =<< additionalTextEdits
let ci = CompletionItem
{_label = label,
_kind = kind,
_tags = Nothing,
_detail =
case (typeText, provenance) of
(Just t,_) | not(T.null t) -> Just $ colon <> t
(_, ImportedFrom mod) -> Just $ "from " <> mod
(_, DefinedIn mod) -> Just $ "from " <> mod
_ -> Nothing,
_documentation = documentation,
_deprecated = Nothing,
_preselect = Nothing,
_sortText = Nothing,
_filterText = Nothing,
_insertText = Just insertText,
_insertTextFormat = Just Snippet,
_insertTextMode = Nothing,
_textEdit = Nothing,
_additionalTextEdits = Nothing,
_commitCharacters = Nothing,
_command = mbCommand,
_xdata = Nothing}
removeSnippetsWhen (isJust isInfix) ci
where kind = Just compKind
docs' = imported : spanDocToMarkdown docs
imported = case provenance of
Local pos -> "*Defined at " <> pprLineCol (srcSpanStart pos) <> " in this module*\n"
ImportedFrom mod -> "*Imported from '" <> mod <> "'*\n"
DefinedIn mod -> "*Defined in '" <> mod <> "'*\n"
colon = if optNewColonConvention then ": " else ":: "
documentation = Just $ CompletionDocMarkup $
MarkupContent MkMarkdown $
T.intercalate sectionSeparator docs'
pprLineCol :: SrcLoc -> T.Text
pprLineCol (UnhelpfulLoc fs) = T.pack $ unpackFS fs
pprLineCol (RealSrcLoc loc _) =
"line " <> printOutputable (srcLocLine loc) <> ", column " <> printOutputable (srcLocCol loc)
mkAdditionalEditsCommand :: Maybe PluginId -> ExtendImport -> Maybe Command
mkAdditionalEditsCommand (Just pId) edits = Just $ mkLspCommand pId (CommandId extendImportCommandId) "extend import" (Just [toJSON edits])
mkAdditionalEditsCommand _ _ = Nothing
mkNameCompItem :: Uri -> Maybe T.Text -> OccName -> Provenance -> Maybe Type -> Maybe Backtick -> SpanDoc -> Maybe (LImportDecl GhcPs) -> CompItem
mkNameCompItem doc thingParent origName provenance thingType isInfix docs !imp = CI {..}
where
compKind = occNameToComKind typeText origName
isTypeCompl = isTcOcc origName
label = stripPrefix $ printOutputable origName
insertText = case isInfix of
Nothing -> case getArgText <$> thingType of
Nothing -> label
Just argText -> if T.null argText then label else label <> " " <> argText
Just LeftSide -> label <> "`"
Just Surrounded -> label
typeText
| Just t <- thingType = Just . stripForall $ printOutputable t
| otherwise = Nothing
additionalTextEdits =
imp <&> \x ->
ExtendImport
{ doc,
thingParent,
importName = showModName $ unLoc $ ideclName $ unLoc x,
importQual = getImportQual x,
newThing = printOutputable origName
}
stripForall :: T.Text -> T.Text
stripForall t
| T.isPrefixOf "forall" t =
-- We drop 2 to remove the '.' and the space after it
T.drop 2 (T.dropWhile (/= '.') t)
| otherwise = t
getArgText :: Type -> T.Text
getArgText typ = argText
where
argTypes = getArgs typ
argText :: T.Text
argText = mconcat $ List.intersperse " " $ zipWithFrom snippet 1 argTypes
snippet :: Int -> Type -> T.Text
snippet i t = case t of
(TyVarTy _) -> noParensSnippet
(LitTy _) -> noParensSnippet
(TyConApp _ []) -> noParensSnippet
_ -> snippetText i ("(" <> showForSnippet t <> ")")
where
noParensSnippet = snippetText i (showForSnippet t)
snippetText i t = "${" <> T.pack (show i) <> ":" <> t <> "}"
getArgs :: Type -> [Type]
getArgs t
| isPredTy t = []
| isDictTy t = []
| isForAllTy t = getArgs $ snd (splitForAllTyCoVars t)
| isFunTy t =
let (args, ret) = splitFunTys t
in if isForAllTy ret
then getArgs ret
else Prelude.filter (not . isDictTy) $ map scaledThing args
| isPiTy t = getArgs $ snd (splitPiTys t)
| Just (Pair _ t) <- coercionKind <$> isCoercionTy_maybe t
= getArgs t
| otherwise = []
showForSnippet :: Outputable a => a -> T.Text
#if MIN_VERSION_ghc(9,2,0)
showForSnippet x = T.pack $ renderWithContext ctxt $ GHC.ppr x -- FIXme
where
ctxt = defaultSDocContext{sdocStyle = mkUserStyle neverQualify AllTheWay}
#else
showForSnippet x = printOutputable x
#endif
mkModCompl :: T.Text -> CompletionItem
mkModCompl label =
CompletionItem label (Just CiModule) Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing
mkModuleFunctionImport :: T.Text -> T.Text -> CompletionItem
mkModuleFunctionImport moduleName label =
CompletionItem label (Just CiFunction) Nothing (Just moduleName)
Nothing Nothing Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing
mkImportCompl :: T.Text -> T.Text -> CompletionItem
mkImportCompl enteredQual label =
CompletionItem m (Just CiModule) Nothing (Just label)
Nothing Nothing Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing
where
m = fromMaybe "" (T.stripPrefix enteredQual label)
mkExtCompl :: T.Text -> CompletionItem
mkExtCompl label =
CompletionItem label (Just CiKeyword) Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing Nothing
Nothing Nothing Nothing Nothing Nothing Nothing
fromIdentInfo :: Uri -> IdentInfo -> Maybe T.Text -> CompItem
fromIdentInfo doc IdentInfo{..} q = CI
{ compKind= occNameToComKind Nothing name
, insertText=rendered
, provenance = DefinedIn moduleNameText
, typeText=Nothing
, label=rendered
, isInfix=Nothing
, docs=emptySpanDoc
, isTypeCompl= not isDatacon && isUpper (T.head rendered)
, additionalTextEdits= Just $
ExtendImport
{ doc,
thingParent = parent,
importName = moduleNameText,
importQual = q,
newThing = rendered
}
}
cacheDataProducer :: Uri -> HscEnvEq -> Module -> GlobalRdrEnv-> GlobalRdrEnv -> [LImportDecl GhcPs] -> IO CachedCompletions
cacheDataProducer uri env curMod globalEnv inScopeEnv limports = do
let
packageState = hscEnv env
curModName = moduleName curMod
curModNameText = printOutputable curModName
importMap = Map.fromList [ (l, imp) | imp@(L (locA -> (RealSrcSpan l _)) _) <- limports ]
iDeclToModName :: ImportDecl GhcPs -> ModuleName
iDeclToModName = unLoc . ideclName
asNamespace :: ImportDecl GhcPs -> ModuleName
asNamespace imp = maybe (iDeclToModName imp) GHC.unLoc (ideclAs imp)
-- Full canonical names of imported modules
importDeclarations = map unLoc limports
-- The given namespaces for the imported modules (ie. full name, or alias if used)
allModNamesAsNS = map (showModName . asNamespace) importDeclarations
rdrElts = globalRdrEnvElts globalEnv
foldMapM :: (Foldable f, Monad m, Monoid b) => (a -> m b) -> f a -> m b
foldMapM f xs = foldr step return xs mempty where
step x r z = f x >>= \y -> r $! z `mappend` y
getCompls :: [GlobalRdrElt] -> IO ([CompItem],QualCompls)
getCompls = foldMapM getComplsForOne
getComplsForOne :: GlobalRdrElt -> IO ([CompItem],QualCompls)
getComplsForOne (GRE n par True _) =
(, mempty) <$> toCompItem par curMod curModNameText n Nothing
getComplsForOne (GRE n par False prov) =
flip foldMapM (map is_decl prov) $ \spec -> do
let originalImportDecl = do
-- we don't want to extend import if it's already in scope
guard . null $ lookupGRE_Name inScopeEnv n
-- or if it doesn't have a real location
loc <- realSpan $ is_dloc spec
Map.lookup loc importMap
compItem <- toCompItem par curMod (printOutputable $ is_mod spec) n originalImportDecl
let unqual
| is_qual spec = []
| otherwise = compItem
qual
| is_qual spec = Map.singleton asMod compItem
| otherwise = Map.fromList [(asMod,compItem),(origMod,compItem)]
asMod = showModName (is_as spec)
origMod = showModName (is_mod spec)
return (unqual,QualCompls qual)
toCompItem :: Parent -> Module -> T.Text -> Name -> Maybe (LImportDecl GhcPs) -> IO [CompItem]
toCompItem par m mn n imp' = do
docs <- getDocumentationTryGhc packageState curMod n
let (mbParent, originName) = case par of
NoParent -> (Nothing, nameOccName n)
ParentIs n' -> (Just . T.pack $ printName n', nameOccName n)
#if !MIN_VERSION_ghc(9,2,0)
FldParent n' lbl -> (Just . T.pack $ printName n', maybe (nameOccName n) mkVarOccFS lbl)
#endif
tys <- catchSrcErrors (hsc_dflags packageState) "completion" $ do
name' <- lookupName packageState m n
return ( name' >>= safeTyThingType
, guard (isJust mbParent) >> name' >>= safeTyThingForRecord
)
let (ty, record_ty) = fromRight (Nothing, Nothing) tys
let recordCompls = case record_ty of
Just (ctxStr, flds) | not (null flds) ->
[mkRecordSnippetCompItem uri mbParent ctxStr flds (ImportedFrom mn) docs imp']
_ -> []
return $ mkNameCompItem uri mbParent originName (ImportedFrom mn) ty Nothing docs imp'
: recordCompls
(unquals,quals) <- getCompls rdrElts
-- The list of all importable Modules from all packages
moduleNames <- maybe [] (map showModName) <$> envVisibleModuleNames env
return $ CC
{ allModNamesAsNS = allModNamesAsNS
, unqualCompls = unquals
, qualCompls = quals
, anyQualCompls = []
, importableModules = moduleNames
}
-- | Produces completions from the top level declarations of a module.
localCompletionsForParsedModule :: Uri -> ParsedModule -> CachedCompletions
localCompletionsForParsedModule uri pm@ParsedModule{pm_parsed_source = L _ HsModule{hsmodDecls}} =
CC { allModNamesAsNS = mempty
, unqualCompls = compls
, qualCompls = mempty
, anyQualCompls = []
, importableModules = mempty
}
where
typeSigIds = Set.fromList
[ id
| L _ (SigD _ (TypeSig _ ids _)) <- hsmodDecls
, L _ id <- ids
]
hasTypeSig = (`Set.member` typeSigIds) . unLoc
compls = concat
[ case decl of
SigD _ (TypeSig _ ids typ) ->
[mkComp id CiFunction (Just $ showForSnippet typ) | id <- ids]
ValD _ FunBind{fun_id} ->
[ mkComp fun_id CiFunction Nothing
| not (hasTypeSig fun_id)
]
ValD _ PatBind{pat_lhs} ->
[mkComp id CiVariable Nothing
| VarPat _ id <- listify (\(_ :: Pat GhcPs) -> True) pat_lhs]
TyClD _ ClassDecl{tcdLName, tcdSigs, tcdATs} ->
mkComp tcdLName CiInterface (Just $ showForSnippet tcdLName) :
[ mkComp id CiFunction (Just $ showForSnippet typ)
| L _ (ClassOpSig _ _ ids typ) <- tcdSigs
, id <- ids] ++
[ mkComp fdLName CiStruct (Just $ showForSnippet fdLName)
| L _ (FamilyDecl{fdLName}) <- tcdATs]
TyClD _ x ->
let generalCompls = [mkComp id cl (Just $ showForSnippet $ tyClDeclLName x)
| id <- listify (\(_ :: LIdP GhcPs) -> True) x
, let cl = occNameToComKind Nothing (rdrNameOcc $ unLoc id)]
-- here we only have to look at the outermost type
recordCompls = findRecordCompl uri pm (Local pos) x
in
-- the constructors and snippets will be duplicated here giving the user 2 choices.
generalCompls ++ recordCompls
ForD _ ForeignImport{fd_name,fd_sig_ty} ->
[mkComp fd_name CiVariable (Just $ showForSnippet fd_sig_ty)]
ForD _ ForeignExport{fd_name,fd_sig_ty} ->
[mkComp fd_name CiVariable (Just $ showForSnippet fd_sig_ty)]
_ -> []
| L (locA -> pos) decl <- hsmodDecls,
let mkComp = mkLocalComp pos
]
mkLocalComp pos n ctyp ty =
CI ctyp pn (Local pos) ensureTypeText pn Nothing doc (ctyp `elem` [CiStruct, CiInterface]) Nothing
where
-- when sorting completions, we use the presence of typeText
-- to tell local completions and global completions apart
-- instead of using the empty string here, we should probably introduce a new field...
ensureTypeText = Just $ fromMaybe "" ty
pn = showForSnippet n
doc = SpanDocText (getDocumentation [pm] $ reLoc n) (SpanDocUris Nothing Nothing)
findRecordCompl :: Uri -> ParsedModule -> Provenance -> TyClDecl GhcPs -> [CompItem]
findRecordCompl uri pmod mn DataDecl {tcdLName, tcdDataDefn} = result
where
result = [mkRecordSnippetCompItem uri (Just $ printOutputable $ unLoc tcdLName)
(printOutputable . unLoc $ con_name) field_labels mn doc Nothing
| ConDeclH98{..} <- unLoc <$> dd_cons tcdDataDefn
, Just con_details <- [getFlds con_args]
, let field_names = concatMap extract con_details
, let field_labels = printOutputable <$> field_names
, (not . List.null) field_labels
]
doc = SpanDocText (getDocumentation [pmod] $ reLoc tcdLName) (SpanDocUris Nothing Nothing)
getFlds conArg = case conArg of
RecCon rec -> Just $ unLoc <$> unLoc rec
PrefixCon{} -> Just []
_ -> Nothing
-- NOTE: 'cd_fld_names' is grouped so that the fields
-- sharing the same type declaration to fit in the same group; e.g.
--
-- @
-- data Foo = Foo {arg1, arg2 :: Int, arg3 :: Int, arg4 :: Bool}
-- @
--
-- is encoded as @[[arg1, arg2], [arg3], [arg4]]@
-- Hence, we must concat nested arguments into one to get all the fields.
#if MIN_VERSION_ghc(9,3,0)
extract ConDeclField{..}
= map (foLabel . unLoc) cd_fld_names
#else
extract ConDeclField{..}
= map (rdrNameFieldOcc . unLoc) cd_fld_names
#endif
-- XConDeclField
extract _ = []
findRecordCompl _ _ _ _ = []
toggleSnippets :: ClientCapabilities -> CompletionsConfig -> CompletionItem -> CompletionItem
toggleSnippets ClientCapabilities {_textDocument} CompletionsConfig{..} =
removeSnippetsWhen (not $ enableSnippets && supported)
where
supported =
Just True == (_textDocument >>= _completion >>= _completionItem >>= _snippetSupport)
toggleAutoExtend :: CompletionsConfig -> CompItem -> CompItem
toggleAutoExtend CompletionsConfig{enableAutoExtend=False} x = x {additionalTextEdits = Nothing}
toggleAutoExtend _ x = x
removeSnippetsWhen :: Bool -> CompletionItem -> CompletionItem
removeSnippetsWhen condition x =
if condition
then
x
{ _insertTextFormat = Just PlainText,
_insertText = Nothing
}
else x
-- | Returns the cached completions for the given module and position.
getCompletions
:: IdePlugins a
-> IdeOptions
-> CachedCompletions
-> Maybe (ParsedModule, PositionMapping)
-> Maybe (HieAstResult, PositionMapping)
-> (Bindings, PositionMapping)
-> PosPrefixInfo
-> ClientCapabilities
-> CompletionsConfig
-> HM.HashMap T.Text (HashSet.HashSet IdentInfo)
-> IO [Scored CompletionItem]
getCompletions plugins ideOpts CC {allModNamesAsNS, anyQualCompls, unqualCompls, qualCompls, importableModules}
maybe_parsed maybe_ast_res (localBindings, bmapping) prefixInfo caps config moduleExportsMap = do
let PosPrefixInfo { fullLine, prefixScope, prefixText } = prefixInfo
enteredQual = if T.null prefixScope then "" else prefixScope <> "."
fullPrefix = enteredQual <> prefixText
-- Boolean labels to tag suggestions as qualified (or not)
qual = not(T.null prefixScope)
notQual = False
{- correct the position by moving 'foo :: Int -> String -> '
^
to 'foo :: Int -> String -> '
^
-}
pos = cursorPos prefixInfo
maxC = maxCompletions config
filtModNameCompls :: [Scored CompletionItem]
filtModNameCompls =
(fmap.fmap) mkModCompl
$ Fuzzy.simpleFilter chunkSize maxC fullPrefix
$ (if T.null enteredQual then id else mapMaybe (T.stripPrefix enteredQual))
allModNamesAsNS
filtCompls = Fuzzy.filter chunkSize maxC prefixText ctxCompls (label . snd)
where
mcc = case maybe_parsed of
Nothing -> Nothing
Just (pm, pmapping) ->
let PositionMapping pDelta = pmapping
position' = fromDelta pDelta pos
lpos = lowerRange position'
hpos = upperRange position'
in getCContext lpos pm <|> getCContext hpos pm
-- We need the hieast to be "fresh". We can't get types from "stale" hie files, so hasfield won't work,
-- since it gets the record fields from the types.
-- Perhaps this could be fixed with a refactor to GHC's IfaceTyCon, to have it also contain record fields.
-- Requiring fresh hieast is fine for normal workflows, because it is generated while the user edits.
recordDotSyntaxCompls :: [(Bool, CompItem)]
recordDotSyntaxCompls = case maybe_ast_res of
Just (HAR {hieAst = hieast, hieKind = HieFresh},_) -> concat $ pointCommand hieast (completionPrefixPos prefixInfo) nodeCompletions
_ -> []
where
nodeCompletions :: HieAST Type -> [(Bool, CompItem)]
nodeCompletions node = concatMap g (nodeType $ nodeInfo node)
g :: Type -> [(Bool, CompItem)]
g (TyConApp theTyCon _) = map (dotFieldSelectorToCompl (printOutputable $ GHC.tyConName theTyCon)) $ getSels theTyCon
g _ = []
getSels :: GHC.TyCon -> [T.Text]
getSels tycon = let f fieldLabel = printOutputable fieldLabel
in map f $ tyConFieldLabels tycon
-- Completions can return more information that just the completion itself, but it will
-- require more than what GHC currently gives us in the HieAST, since it only gives the Type
-- of the fields, not where they are defined, etc. So for now the extra fields remain empty.
-- Also: additionalTextEdits is a todo, since we may want to import the record. It requires a way
-- to get the record's module, which isn't included in the type information used to get the fields.
dotFieldSelectorToCompl :: T.Text -> T.Text -> (Bool, CompItem)
dotFieldSelectorToCompl recname label = (True, CI
{ compKind = CiField
, insertText = label
, provenance = DefinedIn recname
, typeText = Nothing
, label = label
, isInfix = Nothing
, docs = emptySpanDoc
, isTypeCompl = False
, additionalTextEdits = Nothing
})
-- completions specific to the current context
ctxCompls' = case mcc of
Nothing -> compls
Just TypeContext -> filter ( isTypeCompl . snd) compls
Just ValueContext -> filter (not . isTypeCompl . snd) compls
Just _ -> filter (not . isTypeCompl . snd) compls
-- Add whether the text to insert has backticks
ctxCompls = (fmap.fmap) (\comp -> toggleAutoExtend config $ comp { isInfix = infixCompls }) ctxCompls'
infixCompls :: Maybe Backtick
infixCompls = isUsedAsInfix fullLine prefixScope prefixText pos
PositionMapping bDelta = bmapping
oldPos = fromDelta bDelta $ cursorPos prefixInfo
startLoc = lowerRange oldPos
endLoc = upperRange oldPos
localCompls = map (uncurry localBindsToCompItem) $ getFuzzyScope localBindings startLoc endLoc
localBindsToCompItem :: Name -> Maybe Type -> CompItem
localBindsToCompItem name typ = CI ctyp pn thisModName ty pn Nothing emptySpanDoc (not $ isValOcc occ) Nothing
where
occ = nameOccName name
ctyp = occNameToComKind Nothing occ
pn = showForSnippet name
ty = showForSnippet <$> typ
thisModName = Local $ nameSrcSpan name
-- When record-dot-syntax completions are available, we return them exclusively.
-- They are only available when we write i.e. `myrecord.` with OverloadedRecordDot enabled.
-- Anything that isn't a field is invalid, so those completion don't make sense.
compls
| T.null prefixScope = map (notQual,) localCompls ++ map (qual,) unqualCompls ++ map (\compl -> (notQual, compl Nothing)) anyQualCompls
| not $ null recordDotSyntaxCompls = recordDotSyntaxCompls
| otherwise = ((qual,) <$> Map.findWithDefault [] prefixScope (getQualCompls qualCompls))
++ map (\compl -> (notQual, compl (Just prefixScope))) anyQualCompls
filtListWith f list =
[ fmap f label
| label <- Fuzzy.simpleFilter chunkSize maxC fullPrefix list
, enteredQual `T.isPrefixOf` original label
]
filtImportCompls = filtListWith (mkImportCompl enteredQual) importableModules
filterModuleExports moduleName = filtListWith $ mkModuleFunctionImport moduleName
filtKeywordCompls
| T.null prefixScope = filtListWith mkExtCompl (optKeywords ideOpts)
| otherwise = []
if
-- TODO: handle multiline imports
| "import " `T.isPrefixOf` fullLine
&& (List.length (words (T.unpack fullLine)) >= 2)
&& "(" `isInfixOf` T.unpack fullLine
-> do
let moduleName = T.pack $ words (T.unpack fullLine) !! 1
funcs = HM.lookupDefault HashSet.empty moduleName moduleExportsMap
funs = map (show . name) $ HashSet.toList funcs
return $ filterModuleExports moduleName $ map T.pack funs
| "import " `T.isPrefixOf` fullLine
-> return filtImportCompls
-- we leave this condition here to avoid duplications and return empty list
-- since HLS implements these completions (#haskell-language-server/pull/662)
| "{-# " `T.isPrefixOf` fullLine
-> return []
| otherwise -> do
-- assumes that nubOrdBy is stable
let uniqueFiltCompls = nubOrdBy (uniqueCompl `on` snd . Fuzzy.original) filtCompls
let compls = (fmap.fmap.fmap) (mkCompl pId ideOpts) uniqueFiltCompls
pId = lookupCommandProvider plugins (CommandId extendImportCommandId)
return $
(fmap.fmap) snd $
sortBy (compare `on` lexicographicOrdering) $
mergeListsBy (flip compare `on` score)
[ (fmap.fmap) (notQual,) filtModNameCompls
, (fmap.fmap) (notQual,) filtKeywordCompls
, (fmap.fmap.fmap) (toggleSnippets caps config) compls
]
where
-- We use this ordering to alphabetically sort suggestions while respecting
-- all the previously applied ordering sources. These are:
-- 1. Qualified suggestions go first
-- 2. Fuzzy score ranks next
-- 3. In-scope completions rank next
-- 4. label alphabetical ordering next
-- 4. detail alphabetical ordering (proxy for module)
lexicographicOrdering Fuzzy.Scored{score, original} =
case original of
(isQual, CompletionItem{_label,_detail}) -> do
let isLocal = maybe False (":" `T.isPrefixOf`) _detail
(Down isQual, Down score, Down isLocal, _label, _detail)
uniqueCompl :: CompItem -> CompItem -> Ordering
uniqueCompl candidate unique =
case compare (label candidate, compKind candidate)
(label unique, compKind unique) of
EQ ->
-- preserve completions for duplicate record fields where the only difference is in the type
-- remove redundant completions with less type info than the previous
if (typeText candidate == typeText unique && isLocalCompletion unique)
-- filter global completions when we already have a local one
|| not(isLocalCompletion candidate) && isLocalCompletion unique
then EQ
else compare (importedFrom candidate, insertText candidate) (importedFrom unique, insertText unique)
other -> other
where
isLocalCompletion ci = isJust(typeText ci)
importedFrom :: CompItem -> T.Text
importedFrom (provenance -> ImportedFrom m) = m
importedFrom (provenance -> DefinedIn m) = m
importedFrom (provenance -> Local _) = "local"
-- ---------------------------------------------------------------------
-- helper functions for infix backticks
-- ---------------------------------------------------------------------
hasTrailingBacktick :: T.Text -> Position -> Bool
hasTrailingBacktick line Position { _character=(fromIntegral -> c) }
| T.length line > c = (line `T.index` c) == '`'
| otherwise = False
isUsedAsInfix :: T.Text -> T.Text -> T.Text -> Position -> Maybe Backtick
isUsedAsInfix line prefixMod prefixText pos
| hasClosingBacktick && hasOpeningBacktick = Just Surrounded
| hasOpeningBacktick = Just LeftSide
| otherwise = Nothing
where
hasOpeningBacktick = openingBacktick line prefixMod prefixText pos
hasClosingBacktick = hasTrailingBacktick line pos
openingBacktick :: T.Text -> T.Text -> T.Text -> Position -> Bool
openingBacktick line prefixModule prefixText Position { _character=(fromIntegral -> c) }
| backtickIndex < 0 || backtickIndex >= T.length line = False
| otherwise = (line `T.index` backtickIndex) == '`'
where
backtickIndex :: Int
backtickIndex =
let
prefixLength = T.length prefixText
moduleLength = if prefixModule == ""
then 0
else T.length prefixModule + 1 {- Because of "." -}
in
-- Points to the first letter of either the module or prefix text
c - (prefixLength + moduleLength) - 1
-- ---------------------------------------------------------------------
-- | Under certain circumstance GHC generates some extra stuff that we
-- don't want in the autocompleted symbols
{- When e.g. DuplicateRecordFields is enabled, compiler generates
names like "$sel:accessor:One" and "$sel:accessor:Two" to disambiguate record selectors
https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/DuplicateRecordFields#Implementation
-}
-- TODO: Turn this into an alex lexer that discards prefixes as if they were whitespace.
stripPrefix :: T.Text -> T.Text
stripPrefix name = T.takeWhile (/=':') $ fromMaybe name $
getFirst $ foldMap (First . (`T.stripPrefix` name)) prefixes
-- | Prefixes that can occur in a GHC OccName
prefixes :: [T.Text]
prefixes =
[
-- long ones
"$con2tag_"
, "$tag2con_"
, "$maxtag_"
-- four chars
, "$sel:"
, "$tc'"
-- three chars
, "$dm"
, "$co"
, "$tc"
, "$cp"
, "$fx"
-- two chars
, "$W"
, "$w"
, "$m"
, "$b"
, "$c"
, "$d"
, "$i"
, "$s"
, "$f"
, "$r"
, "C:"
, "N:"
, "D:"
, "$p"
, "$L"
, "$f"
, "$t"
, "$c"
, "$m"
]
safeTyThingForRecord :: TyThing -> Maybe (T.Text, [T.Text])
safeTyThingForRecord (AnId _) = Nothing
safeTyThingForRecord (AConLike dc) =
let ctxStr = printOutputable . occName . conLikeName $ dc
field_names = T.pack . unpackFS . flLabel <$> conLikeFieldLabels dc
in
Just (ctxStr, field_names)
safeTyThingForRecord _ = Nothing
mkRecordSnippetCompItem :: Uri -> Maybe T.Text -> T.Text -> [T.Text] -> Provenance -> SpanDoc -> Maybe (LImportDecl GhcPs) -> CompItem
mkRecordSnippetCompItem uri parent ctxStr compl importedFrom docs imp = r
where
r = CI {
compKind = CiSnippet
, insertText = buildSnippet
, provenance = importedFrom
, typeText = Nothing
, label = ctxStr
, isInfix = Nothing
, docs = docs
, isTypeCompl = False
, additionalTextEdits = imp <&> \x ->
ExtendImport
{ doc = uri,
thingParent = parent,
importName = showModName $ unLoc $ ideclName $ unLoc x,
importQual = getImportQual x,
newThing = ctxStr
}
}
placeholder_pairs = zip compl ([1..]::[Int])
snippet_parts = map (\(x, i) -> x <> "=${" <> T.pack (show i) <> ":_" <> x <> "}") placeholder_pairs
snippet = T.intercalate (T.pack ", ") snippet_parts
buildSnippet = ctxStr <> " {" <> snippet <> "}"
getImportQual :: LImportDecl GhcPs -> Maybe T.Text
getImportQual (L _ imp)
| isQualifiedImport imp = Just $ T.pack $ moduleNameString $ maybe (unLoc $ ideclName imp) unLoc (ideclAs imp)
| otherwise = Nothing
--------------------------------------------------------------------------------
-- This comes from the GHC.Utils.Misc module (not exported)
-- | Merge an unsorted list of sorted lists, for example:
--
-- > mergeListsBy compare [ [2,5,15], [1,10,100] ] = [1,2,5,10,15,100]
--
-- \( O(n \log{} k) \)
mergeListsBy :: forall a. (a -> a -> Ordering) -> [[a]] -> [a]
mergeListsBy cmp all_lists = merge_lists all_lists
where
-- Implements "Iterative 2-Way merge" described at
-- https://en.wikipedia.org/wiki/K-way_merge_algorithm
-- Merge two sorted lists into one in O(n).
merge2 :: [a] -> [a] -> [a]
merge2 [] ys = ys
merge2 xs [] = xs
merge2 (x:xs) (y:ys) =
case cmp x y of
Prelude.GT -> y : merge2 (x:xs) ys
_ -> x : merge2 xs (y:ys)
-- Merge the first list with the second, the third with the fourth, and so
-- on. The output has half as much lists as the input.
merge_neighbours :: [[a]] -> [[a]]
merge_neighbours [] = []
merge_neighbours [xs] = [xs]
merge_neighbours (xs : ys : lists) =
merge2 xs ys : merge_neighbours lists
-- Since 'merge_neighbours' halves the amount of lists in each iteration,
-- we perform O(log k) iteration. Each iteration is O(n). The total running
-- time is therefore O(n log k).
merge_lists :: [[a]] -> [a]
merge_lists lists =
case merge_neighbours lists of
[] -> []
[xs] -> xs
lists' -> merge_lists lists'
-- |From the given cursor position, gets the prefix module or record for autocompletion
getCompletionPrefix :: Position -> VFS.VirtualFile -> PosPrefixInfo
getCompletionPrefix pos@(Position l c) (VFS.VirtualFile _ _ ropetext) =
fromMaybe (PosPrefixInfo "" "" "" pos) $ do -- Maybe monad
let headMaybe = listToMaybe
lastMaybe = headMaybe . reverse
-- grab the entire line the cursor is at
curLine <- headMaybe $ T.lines $ Rope.toText
$ fst $ Rope.splitAtLine 1 $ snd $ Rope.splitAtLine (fromIntegral l) ropetext
let beforePos = T.take (fromIntegral c) curLine
-- the word getting typed, after previous space and before cursor
curWord <-
if | T.null beforePos -> Just ""
| T.last beforePos == ' ' -> Just "" -- don't count abc as the curword in 'abc '
| otherwise -> lastMaybe (T.words beforePos)
let parts = T.split (=='.')
$ T.takeWhileEnd (\x -> isAlphaNum x || x `elem` ("._'"::String)) curWord
case reverse parts of
[] -> Nothing
(x:xs) -> do
let modParts = reverse $ filter (not .T.null) xs
modName = T.intercalate "." modParts
return $ PosPrefixInfo { fullLine = curLine, prefixScope = modName, prefixText = x, cursorPos = pos }
completionPrefixPos :: PosPrefixInfo -> Position
completionPrefixPos PosPrefixInfo { cursorPos = Position ln co, prefixText = str} = Position ln (co - (fromInteger . toInteger . T.length $ str) - 1)