Skip to content
This repository was archived by the owner on Jan 2, 2021. It is now read-only.

Commit a531f9c

Browse files
committed
Merge remote-tracking branch 'upstream/master' into opentelemetry
2 parents 0c41056 + 28f33cc commit a531f9c

File tree

10 files changed

+150
-136
lines changed

10 files changed

+150
-136
lines changed

.azure/windows-stack.yml

-39
This file was deleted.

azure-pipelines.yml

-18
This file was deleted.

cabal.project

-5
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,3 @@ allow-newer:
1414
monoid-extras:base,
1515
statestack:base,
1616
svg-builder:base
17-
18-
-- To ensure the build get the version with the fix for
19-
-- https://github.com/Avi-D-coder/implicit-hie/issues/17
20-
constraints: implicit-hie >= 0.1.2.3
21-
constraints: implicit-hie-cradle >= 0.3.0.0

ghcide.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ library
9393
ghc-paths,
9494
cryptohash-sha1 >=0.11.100 && <0.12,
9595
hie-bios >= 0.7.1 && < 0.8.0,
96-
implicit-hie-cradle >= 0.3.0.0 && < 0.4,
96+
implicit-hie-cradle >= 0.3.0.2 && < 0.4,
9797
base16-bytestring >=0.1.1 && <0.2
9898
if os(windows)
9999
build-depends:

src/Development/IDE/Plugin/Completions.hs

+2-3
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ produceCompletions = do
7272
(Just (ms,imps), Just sess) -> do
7373
let env = hscEnv sess
7474
-- We do this to be able to provide completions of items that are not restricted to the explicit list
75-
let imps' = map dropListFromImportDecl imps
76-
res <- liftIO $ tcRnImportDecls env imps'
75+
res <- liftIO $ tcRnImportDecls env (dropListFromImportDecl <$> imps)
7776
case res of
7877
(_, Just rdrEnv) -> do
79-
cdata <- liftIO $ cacheDataProducer env (ms_mod ms) rdrEnv imps' parsedDeps
78+
cdata <- liftIO $ cacheDataProducer env (ms_mod ms) rdrEnv imps parsedDeps
8079
return ([], Just cdata)
8180
(_diag, _) ->
8281
return ([], Nothing)

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

+74-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{-# LANGUAGE CPP #-}
22

33
#include "ghc-api-version.h"
4+
#if MIN_GHC_API_VERSION (8,8,4)
5+
{-# LANGUAGE GADTs#-}
6+
#endif
47
-- Mostly taken from "haskell-ide-engine"
58
module Development.IDE.Plugin.Completions.Logic (
69
CachedCompletions
@@ -11,7 +14,7 @@ module Development.IDE.Plugin.Completions.Logic (
1114
) where
1215

1316
import Control.Applicative
14-
import Data.Char (isUpper)
17+
import Data.Char (isAlphaNum, isUpper)
1518
import Data.Generics
1619
import Data.List.Extra as List hiding (stripPrefix)
1720
import qualified Data.Map as Map
@@ -144,21 +147,44 @@ occNameToComKind ty oc
144147
showModName :: ModuleName -> T.Text
145148
showModName = T.pack . moduleNameString
146149

150+
-- mkCompl :: IdeOptions -> CompItem -> CompletionItem
151+
-- mkCompl IdeOptions{..} CI{compKind,insertText, importedFrom,typeText,label,docs} =
152+
-- CompletionItem label kind (List []) ((colon <>) <$> typeText)
153+
-- (Just $ CompletionDocMarkup $ MarkupContent MkMarkdown $ T.intercalate sectionSeparator docs')
154+
-- Nothing Nothing Nothing Nothing (Just insertText) (Just Snippet)
155+
-- Nothing Nothing Nothing Nothing Nothing
156+
147157
mkCompl :: IdeOptions -> CompItem -> CompletionItem
148-
mkCompl IdeOptions{..} CI{compKind,insertText, importedFrom,typeText,label,docs} =
149-
CompletionItem label kind (List []) ((colon <>) <$> typeText)
150-
(Just $ CompletionDocMarkup $ MarkupContent MkMarkdown $ T.intercalate sectionSeparator docs')
151-
Nothing Nothing Nothing Nothing (Just insertText) (Just Snippet)
152-
Nothing Nothing Nothing Nothing Nothing
158+
mkCompl IdeOptions{..} CI{compKind,insertText, importedFrom,typeText,label,docs, additionalTextEdits} =
159+
CompletionItem {_label = label,
160+
_kind = kind,
161+
_tags = List [],
162+
_detail = (colon <>) <$> typeText,
163+
_documentation = documentation,
164+
_deprecated = Nothing,
165+
_preselect = Nothing,
166+
_sortText = Nothing,
167+
_filterText = Nothing,
168+
_insertText = Just insertText,
169+
_insertTextFormat = Just Snippet,
170+
_textEdit = Nothing,
171+
_additionalTextEdits = List <$> additionalTextEdits,
172+
_commitCharacters = Nothing,
173+
_command = Nothing,
174+
_xdata = Nothing}
175+
153176
where kind = Just compKind
154177
docs' = imported : spanDocToMarkdown docs
155178
imported = case importedFrom of
156179
Left pos -> "*Defined at '" <> ppr pos <> "'*\n'"
157180
Right mod -> "*Defined in '" <> mod <> "'*\n"
158181
colon = if optNewColonConvention then ": " else ":: "
182+
documentation = Just $ CompletionDocMarkup $
183+
MarkupContent MkMarkdown $
184+
T.intercalate sectionSeparator docs'
159185

160-
mkNameCompItem :: Name -> ModuleName -> Maybe Type -> Maybe Backtick -> SpanDoc -> CompItem
161-
mkNameCompItem origName origMod thingType isInfix docs = CI{..}
186+
mkNameCompItem :: Name -> ModuleName -> Maybe Type -> Maybe Backtick -> SpanDoc -> Maybe (LImportDecl GhcPs) -> CompItem
187+
mkNameCompItem origName origMod thingType isInfix docs !imp = CI{..}
162188
where
163189
compKind = occNameToComKind typeText $ occName origName
164190
importedFrom = Right $ showModName origMod
@@ -174,7 +200,7 @@ mkNameCompItem origName origMod thingType isInfix docs = CI{..}
174200
typeText
175201
| Just t <- thingType = Just . stripForall $ T.pack (showGhc t)
176202
| otherwise = Nothing
177-
203+
additionalTextEdits = imp >>= extendImportList (showGhc origName)
178204

179205
stripForall :: T.Text -> T.Text
180206
stripForall t
@@ -236,11 +262,37 @@ mkPragmaCompl label insertText =
236262
Nothing Nothing Nothing Nothing Nothing (Just insertText) (Just Snippet)
237263
Nothing Nothing Nothing Nothing Nothing
238264

265+
extendImportList :: String -> LImportDecl GhcPs -> Maybe [TextEdit]
266+
extendImportList name lDecl = let
267+
f (Just range) ImportDecl {ideclHiding} = case ideclHiding of
268+
Just (False, x)
269+
| Set.notMember name (Set.fromList [show y| y <- unLoc x])
270+
-> let
271+
start_pos = _end range
272+
new_start_pos = start_pos {_character = _character start_pos - 1}
273+
-- use to same start_pos to handle situation where we do not have latest edits due to caching of Rules
274+
new_range = Range new_start_pos new_start_pos
275+
-- we cannot wrap mapM_ inside (mapM_) but we need to wrap (<$)
276+
alpha = all isAlphaNum $ filter (\c -> c /= '_') name
277+
result = if alpha then name ++ ", "
278+
else "(" ++ name ++ "), "
279+
in Just [TextEdit new_range (T.pack result)]
280+
| otherwise -> Nothing
281+
_ -> Nothing -- hiding import list and no list
282+
f _ _ = Nothing
283+
src_span = srcSpanToRange . getLoc $ lDecl
284+
in f src_span . unLoc $ lDecl
285+
286+
239287
cacheDataProducer :: HscEnv -> Module -> GlobalRdrEnv -> [LImportDecl GhcPs] -> [ParsedModule] -> IO CachedCompletions
240288
cacheDataProducer packageState curMod rdrEnv limports deps = do
241289
let dflags = hsc_dflags packageState
242290
curModName = moduleName curMod
243291

292+
importMap = Map.fromList [
293+
(getLoc imp, imp)
294+
| imp <- limports ]
295+
244296
iDeclToModName :: ImportDecl name -> ModuleName
245297
iDeclToModName = unLoc . ideclName
246298

@@ -266,10 +318,11 @@ cacheDataProducer packageState curMod rdrEnv limports deps = do
266318

267319
getComplsForOne :: GlobalRdrElt -> IO ([CompItem],QualCompls)
268320
getComplsForOne (GRE n _ True _) =
269-
(, mempty) <$> toCompItem curMod curModName n
321+
(, mempty) <$> toCompItem curMod curModName n Nothing
270322
getComplsForOne (GRE n _ False prov) =
271323
flip foldMapM (map is_decl prov) $ \spec -> do
272-
compItem <- toCompItem curMod (is_mod spec) n
324+
let originalImportDecl = Map.lookup (is_dloc spec) importMap
325+
compItem <- toCompItem curMod (is_mod spec) n originalImportDecl
273326
let unqual
274327
| is_qual spec = []
275328
| otherwise = compItem
@@ -280,8 +333,8 @@ cacheDataProducer packageState curMod rdrEnv limports deps = do
280333
origMod = showModName (is_mod spec)
281334
return (unqual,QualCompls qual)
282335

283-
toCompItem :: Module -> ModuleName -> Name -> IO [CompItem]
284-
toCompItem m mn n = do
336+
toCompItem :: Module -> ModuleName -> Name -> Maybe (LImportDecl GhcPs) -> IO [CompItem]
337+
toCompItem m mn n imp' = do
285338
docs <- getDocumentationTryGhc packageState curMod deps n
286339
ty <- catchSrcErrors (hsc_dflags packageState) "completion" $ do
287340
name' <- lookupName packageState m n
@@ -294,10 +347,10 @@ cacheDataProducer packageState curMod rdrEnv limports deps = do
294347
let recordCompls = case either (const Nothing) id record_ty of
295348
Just (ctxStr, flds) -> case flds of
296349
[] -> []
297-
_ -> [mkRecordSnippetCompItem ctxStr flds (ppr mn) docs]
350+
_ -> [mkRecordSnippetCompItem ctxStr flds (ppr mn) docs imp']
298351
Nothing -> []
299352

300-
return $ [mkNameCompItem n mn (either (const Nothing) id ty) Nothing docs] ++
353+
return $ [mkNameCompItem n mn (either (const Nothing) id ty) Nothing docs imp'] ++
301354
recordCompls
302355

303356
(unquals,quals) <- getCompls rdrElts
@@ -360,19 +413,17 @@ localCompletionsForParsedModule pm@ParsedModule{pm_parsed_source = L _ HsModule{
360413
]
361414

362415
mkComp n ctyp ty =
363-
CI ctyp pn (Right thisModName) ty pn Nothing doc (ctyp `elem` [CiStruct, CiClass])
416+
CI ctyp pn (Right thisModName) ty pn Nothing doc (ctyp `elem` [CiStruct, CiClass]) Nothing
364417
where
365418
pn = ppr n
366419
doc = SpanDocText (getDocumentation [pm] n) (SpanDocUris Nothing Nothing)
367420

368421
thisModName = ppr hsmodName
369422

370-
--recordCompls = localRecordSnippetProducer pm thisModName
371-
372423
findRecordCompl :: ParsedModule -> T.Text -> TyClDecl GhcPs -> [CompItem]
373424
findRecordCompl pmod mn DataDecl {tcdLName, tcdDataDefn} = result
374425
where
375-
result = [mkRecordSnippetCompItem (T.pack . showGhc . unLoc $ con_name) field_labels mn doc
426+
result = [mkRecordSnippetCompItem (T.pack . showGhc . unLoc $ con_name) field_labels mn doc Nothing
376427
| ConDeclH98{..} <- unLoc <$> dd_cons tcdDataDefn
377428
, Just con_details <- [getFlds con_args]
378429
, let field_names = mapMaybe extract con_details
@@ -468,7 +519,7 @@ getCompletions ideOpts CC { allModNamesAsNS, unqualCompls, qualCompls, importabl
468519
endLoc = upperRange oldPos
469520
localCompls = map (uncurry localBindsToCompItem) $ getFuzzyScope localBindings startLoc endLoc
470521
localBindsToCompItem :: Name -> Maybe Type -> CompItem
471-
localBindsToCompItem name typ = CI ctyp pn thisModName ty pn Nothing emptySpanDoc (not $ isValOcc occ)
522+
localBindsToCompItem name typ = CI ctyp pn thisModName ty pn Nothing emptySpanDoc (not $ isValOcc occ) Nothing
472523
where
473524
occ = nameOccName name
474525
ctyp = occNameToComKind Nothing occ
@@ -665,8 +716,8 @@ safeTyThingForRecord (AConLike dc) =
665716
Just (ctxStr, field_names)
666717
safeTyThingForRecord _ = Nothing
667718

668-
mkRecordSnippetCompItem :: T.Text -> [T.Text] -> T.Text -> SpanDoc -> CompItem
669-
mkRecordSnippetCompItem ctxStr compl mn docs = r
719+
mkRecordSnippetCompItem :: T.Text -> [T.Text] -> T.Text -> SpanDoc -> Maybe (LImportDecl GhcPs) -> CompItem
720+
mkRecordSnippetCompItem ctxStr compl mn docs imp = r
670721
where
671722
r = CI {
672723
compKind = CiSnippet
@@ -677,6 +728,7 @@ mkRecordSnippetCompItem ctxStr compl mn docs = r
677728
, isInfix = Nothing
678729
, docs = docs
679730
, isTypeCompl = False
731+
, additionalTextEdits = imp >>= extendImportList (T.unpack ctxStr)
680732
}
681733

682734
placeholder_pairs = zip compl ([1..]::[Int])

src/Development/IDE/Plugin/Completions/Types.hs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import qualified Data.Text as T
88
import SrcLoc
99

1010
import Development.IDE.Spans.Common
11-
import Language.Haskell.LSP.Types (CompletionItemKind)
11+
import Language.Haskell.LSP.Types (TextEdit, CompletionItemKind)
1212

1313
-- From haskell-ide-engine/src/Haskell/Ide/Engine/LSP/Completions.hs
1414

@@ -25,6 +25,7 @@ data CompItem = CI
2525
-- in the context of an infix notation.
2626
, docs :: SpanDoc -- ^ Available documentation.
2727
, isTypeCompl :: Bool
28+
, additionalTextEdits :: Maybe [TextEdit]
2829
}
2930
deriving (Eq, Show)
3031

stack-windows.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ extra-deps:
3030
- dual-tree-0.2.2.1
3131
- force-layout-0.4.0.6
3232
- statestack-0.3
33-
- implicit-hie-0.1.2.3
34-
- implicit-hie-cradle-0.3.0.0
33+
- implicit-hie-0.1.2.5
34+
- implicit-hie-cradle-0.3.0.2
3535

3636
nix:
3737
packages: [zlib]

stack.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ extra-deps:
3030
- dual-tree-0.2.2.1
3131
- force-layout-0.4.0.6
3232
- statestack-0.3
33-
- implicit-hie-0.1.2.3
34-
- implicit-hie-cradle-0.3.0.0
33+
- implicit-hie-0.1.2.5
34+
- implicit-hie-cradle-0.3.0.2
3535

3636
nix:
3737
packages: [zlib]

0 commit comments

Comments
 (0)