Skip to content

Commit 972d993

Browse files
authored
Merge branch 'master' into alex/4057-2
2 parents 0b63299 + 9f4d673 commit 972d993

File tree

53 files changed

+1984
-196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1984
-196
lines changed

.github/actions/setup-build/action.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ runs:
3131
sudo chown -R $USER /usr/local/.ghcup
3232
shell: bash
3333

34-
- uses: haskell-actions/[email protected].5
34+
- uses: haskell-actions/[email protected].6
3535
id: HaskEnvSetup
3636
with:
3737
ghc-version : ${{ inputs.ghc }}
@@ -116,3 +116,18 @@ runs:
116116
- name: "Remove freeze file"
117117
run: rm -f cabal.project.freeze
118118
shell: bash
119+
120+
# Make sure to clear all unneeded `ghcup`` caches.
121+
# At some point, we were running out of disk space, see issue
122+
# https://github.com/haskell/haskell-language-server/issues/4386 for details.
123+
#
124+
# Using "printf" debugging (`du -sh *` and `df -h /`) and binary searching,
125+
# we figured out that `ghcup` caches are taking up a sizable portion of the
126+
# disk space.
127+
# Thus, we remove anything we don't need, especially caches and temporary files.
128+
# For got measure, we also make sure no other tooling versions are
129+
# installed besides the ones we explicitly want.
130+
- name: "Remove ghcup caches"
131+
if: runner.os == 'Linux'
132+
run: ghcup gc --ghc-old --share-dir --hls-no-ghc --cache --tmpdirs --unset
133+
shell: bash

.github/workflows/bench.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ jobs:
127127
example: ['cabal', 'lsp-types']
128128

129129
steps:
130-
- uses: haskell-actions/[email protected].3
130+
- uses: haskell-actions/[email protected].6
131131
with:
132132
ghc-version : ${{ matrix.ghc }}
133133
cabal-version: ${{ matrix.cabal }}

cabal.project

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ packages:
77
./hls-plugin-api
88
./hls-test-utils
99

10-
index-state: 2024-06-29T00:00:00Z
10+
11+
index-state: 2024-08-22T00:00:00Z
1112

1213
tests: True
1314
test-show-details: direct

exe/Wrapper.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module Main where
99

1010
import Control.Monad.Extra
1111
import Data.Default
12-
import Data.Either.Extra (eitherToMaybe)
1312
import Data.Foldable
1413
import Data.List
1514
import Data.List.Extra (trimEnd)
@@ -76,8 +75,11 @@ main = do
7675
putStrLn $ showProgramVersionOfInterest programsOfInterest
7776
putStrLn "Tool versions in your project"
7877
cradle <- findProjectCradle' recorder False
79-
ghcVersion <- runExceptT $ getRuntimeGhcVersion' cradle
80-
putStrLn $ showProgramVersion "ghc" $ mkVersion =<< eitherToMaybe ghcVersion
78+
runExceptT (getRuntimeGhcVersion' cradle) >>= \case
79+
Left err ->
80+
T.hPutStrLn stderr (prettyError err NoShorten)
81+
Right ghcVersion ->
82+
putStrLn $ showProgramVersion "ghc" $ mkVersion ghcVersion
8183

8284
VersionMode PrintVersion ->
8385
putStrLn hlsVer

ghcide/src/Development/IDE/Core/Actions.hs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,56 +66,59 @@ getAtPoint file pos = runMaybeT $ do
6666
!pos' <- MaybeT (return $ fromCurrentPosition mapping pos)
6767
MaybeT $ liftIO $ fmap (first (toCurrentRange mapping =<<)) <$> AtPoint.atPoint opts hf dkMap env pos'
6868

69-
-- | For each Location, determine if we have the PositionMapping
70-
-- for the correct file. If not, get the correct position mapping
71-
-- and then apply the position mapping to the location.
72-
toCurrentLocations
69+
-- | Converts locations in the source code to their current positions,
70+
-- taking into account changes that may have occurred due to edits.
71+
toCurrentLocation
7372
:: PositionMapping
7473
-> NormalizedFilePath
75-
-> [Location]
76-
-> IdeAction [Location]
77-
toCurrentLocations mapping file = mapMaybeM go
74+
-> Location
75+
-> IdeAction (Maybe Location)
76+
toCurrentLocation mapping file (Location uri range) =
77+
-- The Location we are going to might be in a different
78+
-- file than the one we are calling gotoDefinition from.
79+
-- So we check that the location file matches the file
80+
-- we are in.
81+
if nUri == normalizedFilePathToUri file
82+
-- The Location matches the file, so use the PositionMapping
83+
-- we have.
84+
then pure $ Location uri <$> toCurrentRange mapping range
85+
-- The Location does not match the file, so get the correct
86+
-- PositionMapping and use that instead.
87+
else do
88+
otherLocationMapping <- fmap (fmap snd) $ runMaybeT $ do
89+
otherLocationFile <- MaybeT $ pure $ uriToNormalizedFilePath nUri
90+
useWithStaleFastMT GetHieAst otherLocationFile
91+
pure $ Location uri <$> (flip toCurrentRange range =<< otherLocationMapping)
7892
where
79-
go :: Location -> IdeAction (Maybe Location)
80-
go (Location uri range) =
81-
-- The Location we are going to might be in a different
82-
-- file than the one we are calling gotoDefinition from.
83-
-- So we check that the location file matches the file
84-
-- we are in.
85-
if nUri == normalizedFilePathToUri file
86-
-- The Location matches the file, so use the PositionMapping
87-
-- we have.
88-
then pure $ Location uri <$> toCurrentRange mapping range
89-
-- The Location does not match the file, so get the correct
90-
-- PositionMapping and use that instead.
91-
else do
92-
otherLocationMapping <- fmap (fmap snd) $ runMaybeT $ do
93-
otherLocationFile <- MaybeT $ pure $ uriToNormalizedFilePath nUri
94-
useWithStaleFastMT GetHieAst otherLocationFile
95-
pure $ Location uri <$> (flip toCurrentRange range =<< otherLocationMapping)
96-
where
97-
nUri :: NormalizedUri
98-
nUri = toNormalizedUri uri
93+
nUri :: NormalizedUri
94+
nUri = toNormalizedUri uri
9995

10096
-- | Goto Definition.
101-
getDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [Location])
97+
getDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [(Location, Identifier)])
10298
getDefinition file pos = runMaybeT $ do
10399
ide@ShakeExtras{ withHieDb, hiedbWriter } <- ask
104100
opts <- liftIO $ getIdeOptionsIO ide
105101
(HAR _ hf _ _ _, mapping) <- useWithStaleFastMT GetHieAst file
106102
(ImportMap imports, _) <- useWithStaleFastMT GetImportMap file
107103
!pos' <- MaybeT (pure $ fromCurrentPosition mapping pos)
108-
locations <- AtPoint.gotoDefinition withHieDb (lookupMod hiedbWriter) opts imports hf pos'
109-
MaybeT $ Just <$> toCurrentLocations mapping file locations
104+
locationsWithIdentifier <- AtPoint.gotoDefinition withHieDb (lookupMod hiedbWriter) opts imports hf pos'
105+
mapMaybeM (\(location, identifier) -> do
106+
fixedLocation <- MaybeT $ toCurrentLocation mapping file location
107+
pure $ Just (fixedLocation, identifier)
108+
) locationsWithIdentifier
110109

111-
getTypeDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [Location])
110+
111+
getTypeDefinition :: NormalizedFilePath -> Position -> IdeAction (Maybe [(Location, Identifier)])
112112
getTypeDefinition file pos = runMaybeT $ do
113113
ide@ShakeExtras{ withHieDb, hiedbWriter } <- ask
114114
opts <- liftIO $ getIdeOptionsIO ide
115115
(hf, mapping) <- useWithStaleFastMT GetHieAst file
116116
!pos' <- MaybeT (return $ fromCurrentPosition mapping pos)
117-
locations <- AtPoint.gotoTypeDefinition withHieDb (lookupMod hiedbWriter) opts hf pos'
118-
MaybeT $ Just <$> toCurrentLocations mapping file locations
117+
locationsWithIdentifier <- AtPoint.gotoTypeDefinition withHieDb (lookupMod hiedbWriter) opts hf pos'
118+
mapMaybeM (\(location, identifier) -> do
119+
fixedLocation <- MaybeT $ toCurrentLocation mapping file location
120+
pure $ Just (fixedLocation, identifier)
121+
) locationsWithIdentifier
119122

120123
highlightAtPoint :: NormalizedFilePath -> Position -> IdeAction (Maybe [DocumentHighlight])
121124
highlightAtPoint file pos = runMaybeT $ do

ghcide/src/Development/IDE/GHC/Compat/Core.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,8 @@ instance HasSrcSpan (EpAnn a) where
633633
#if MIN_VERSION_ghc(9,9,0)
634634
instance HasSrcSpan (SrcLoc.GenLocated (EpAnn ann) a) where
635635
getLoc (L l _) = getLoc l
636+
instance HasSrcSpan (SrcLoc.GenLocated (GHC.EpaLocation) a) where
637+
getLoc = GHC.getHasLoc
636638
#else
637639
instance HasSrcSpan (SrcSpanAnn' ann) where
638640
getLoc = GHC.locA

ghcide/src/Development/IDE/GHC/Orphans.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ instance NFData (HsExpr (GhcPass Renamed)) where
226226
instance NFData (Pat (GhcPass Renamed)) where
227227
rnf = rwhnf
228228

229+
instance NFData (HsExpr (GhcPass Typechecked)) where
230+
rnf = rwhnf
231+
232+
instance NFData (Pat (GhcPass Typechecked)) where
233+
rnf = rwhnf
234+
229235
instance NFData Extension where
230236
rnf = rwhnf
231237

ghcide/src/Development/IDE/LSP/HoverDefinition.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ gotoDefinition :: Recorder (WithPriority Log) -> IdeState -> TextDocumentPos
4747
hover :: Recorder (WithPriority Log) -> IdeState -> TextDocumentPositionParams -> ExceptT PluginError (HandlerM c) (Hover |? Null)
4848
gotoTypeDefinition :: Recorder (WithPriority Log) -> IdeState -> TextDocumentPositionParams -> ExceptT PluginError (HandlerM c) (MessageResult Method_TextDocumentTypeDefinition)
4949
documentHighlight :: Recorder (WithPriority Log) -> IdeState -> TextDocumentPositionParams -> ExceptT PluginError (HandlerM c) ([DocumentHighlight] |? Null)
50-
gotoDefinition = request "Definition" getDefinition (InR $ InR Null) (InL . Definition. InR)
51-
gotoTypeDefinition = request "TypeDefinition" getTypeDefinition (InR $ InR Null) (InL . Definition. InR)
50+
gotoDefinition = request "Definition" getDefinition (InR $ InR Null) (InL . Definition. InR . map fst)
51+
gotoTypeDefinition = request "TypeDefinition" getTypeDefinition (InR $ InR Null) (InL . Definition. InR . map fst)
5252
hover = request "Hover" getAtPoint (InR Null) foundHover
5353
documentHighlight = request "DocumentHighlight" highlightAtPoint (InR Null) InL
5454

ghcide/src/Development/IDE/Spans/AtPoint.hs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,15 @@ documentHighlight hf rf pos = pure highlights
179179
then DocumentHighlightKind_Write
180180
else DocumentHighlightKind_Read
181181

182+
-- | Locate the type definition of the name at a given position.
182183
gotoTypeDefinition
183184
:: MonadIO m
184185
=> WithHieDb
185186
-> LookupModule m
186187
-> IdeOptions
187188
-> HieAstResult
188189
-> Position
189-
-> MaybeT m [Location]
190+
-> MaybeT m [(Location, Identifier)]
190191
gotoTypeDefinition withHieDb lookupModule ideOpts srcSpans pos
191192
= lift $ typeLocationsAtPoint withHieDb lookupModule ideOpts pos srcSpans
192193

@@ -199,7 +200,7 @@ gotoDefinition
199200
-> M.Map ModuleName NormalizedFilePath
200201
-> HieASTs a
201202
-> Position
202-
-> MaybeT m [Location]
203+
-> MaybeT m [(Location, Identifier)]
203204
gotoDefinition withHieDb getHieFile ideOpts imports srcSpans pos
204205
= lift $ locationsAtPoint withHieDb getHieFile ideOpts imports pos srcSpans
205206

@@ -306,6 +307,7 @@ atPoint IdeOptions{} (HAR _ hf _ _ (kind :: HieKind hietype)) (DKMap dm km) env
306307
UnhelpfulLoc {} | isInternalName name || isSystemName name -> Nothing
307308
_ -> Just $ "*Defined " <> printOutputable (pprNameDefnLoc name) <> "*"
308309

310+
-- | Find 'Location's of type definition at a specific point and return them along with their 'Identifier's.
309311
typeLocationsAtPoint
310312
:: forall m
311313
. MonadIO m
@@ -314,7 +316,7 @@ typeLocationsAtPoint
314316
-> IdeOptions
315317
-> Position
316318
-> HieAstResult
317-
-> m [Location]
319+
-> m [(Location, Identifier)]
318320
typeLocationsAtPoint withHieDb lookupModule _ideOptions pos (HAR _ ast _ _ hieKind) =
319321
case hieKind of
320322
HieFromDisk hf ->
@@ -332,12 +334,12 @@ typeLocationsAtPoint withHieDb lookupModule _ideOptions pos (HAR _ ast _ _ hieKi
332334
HQualTy a b -> getTypes' [a,b]
333335
HCastTy a -> getTypes' [a]
334336
_ -> []
335-
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation withHieDb lookupModule) (getTypes' ts)
337+
in fmap nubOrd $ concatMapM (\n -> fmap (maybe [] (fmap (,Right n))) (nameToLocation withHieDb lookupModule n)) (getTypes' ts)
336338
HieFresh ->
337339
let ts = concat $ pointCommand ast pos getts
338340
getts x = nodeType ni ++ (mapMaybe identType $ M.elems $ nodeIdentifiers ni)
339341
where ni = nodeInfo x
340-
in fmap nubOrd $ concatMapM (fmap (fromMaybe []) . nameToLocation withHieDb lookupModule) (getTypes ts)
342+
in fmap nubOrd $ concatMapM (\n -> fmap (maybe [] (fmap (,Right n))) (nameToLocation withHieDb lookupModule n)) (getTypes ts)
341343

342344
namesInType :: Type -> [Name]
343345
namesInType (TyVarTy n) = [varName n]
@@ -352,6 +354,7 @@ namesInType _ = []
352354
getTypes :: [Type] -> [Name]
353355
getTypes ts = concatMap namesInType ts
354356

357+
-- | Find 'Location's of definition at a specific point and return them along with their 'Identifier's.
355358
locationsAtPoint
356359
:: forall m a
357360
. MonadIO m
@@ -361,13 +364,16 @@ locationsAtPoint
361364
-> M.Map ModuleName NormalizedFilePath
362365
-> Position
363366
-> HieASTs a
364-
-> m [Location]
367+
-> m [(Location, Identifier)]
365368
locationsAtPoint withHieDb lookupModule _ideOptions imports pos ast =
366369
let ns = concat $ pointCommand ast pos (M.keys . getNodeIds)
367370
zeroPos = Position 0 0
368371
zeroRange = Range zeroPos zeroPos
369-
modToLocation m = fmap (\fs -> pure $ Location (fromNormalizedUri $ filePathToUri' fs) zeroRange) $ M.lookup m imports
370-
in fmap (nubOrd . concat) $ mapMaybeM (either (pure . modToLocation) $ nameToLocation withHieDb lookupModule) ns
372+
modToLocation m = fmap (\fs -> pure (Location (fromNormalizedUri $ filePathToUri' fs) zeroRange)) $ M.lookup m imports
373+
in fmap (nubOrd . concat) $ mapMaybeM
374+
(either (\m -> pure ((fmap $ fmap (,Left m)) (modToLocation m)))
375+
(\n -> fmap (fmap $ fmap (,Right n)) (nameToLocation withHieDb lookupModule n)))
376+
ns
371377

372378
-- | Given a 'Name' attempt to find the location where it is defined.
373379
nameToLocation :: MonadIO m => WithHieDb -> LookupModule m -> Name -> m (Maybe [Location])

haskell-language-server.cabal

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,10 @@ library hls-cabal-plugin
248248
Ide.Plugin.Cabal.Completion.Completions
249249
Ide.Plugin.Cabal.Completion.Data
250250
Ide.Plugin.Cabal.Completion.Types
251+
Ide.Plugin.Cabal.Definition
251252
Ide.Plugin.Cabal.FieldSuggest
252253
Ide.Plugin.Cabal.LicenseSuggest
254+
Ide.Plugin.Cabal.CabalAdd
253255
Ide.Plugin.Cabal.Orphans
254256
Ide.Plugin.Cabal.Outline
255257
Ide.Plugin.Cabal.Parse
@@ -277,6 +279,12 @@ library hls-cabal-plugin
277279
, transformers
278280
, unordered-containers >=0.2.10.0
279281
, containers
282+
, cabal-add
283+
, process
284+
, aeson
285+
, Cabal
286+
, pretty
287+
280288
hs-source-dirs: plugins/hls-cabal-plugin/src
281289

282290
test-suite hls-cabal-plugin-tests
@@ -287,10 +295,12 @@ test-suite hls-cabal-plugin-tests
287295
hs-source-dirs: plugins/hls-cabal-plugin/test
288296
main-is: Main.hs
289297
other-modules:
298+
CabalAdd
290299
Completer
291300
Context
292-
Utils
301+
Definition
293302
Outline
303+
Utils
294304
build-depends:
295305
, base
296306
, bytestring
@@ -303,6 +313,7 @@ test-suite hls-cabal-plugin-tests
303313
, lens
304314
, lsp-types
305315
, text
316+
, hls-plugin-api
306317

307318
-----------------------------
308319
-- class plugin
@@ -1361,6 +1372,7 @@ test-suite hls-explicit-record-fields-plugin-tests
13611372
, base
13621373
, filepath
13631374
, text
1375+
, ghcide
13641376
, haskell-language-server:hls-explicit-record-fields-plugin
13651377
, hls-test-utils == 2.9.0.1
13661378

0 commit comments

Comments
 (0)