Skip to content

Commit 1a34357

Browse files
authored
Invert the dependency between hls-plugin-api and ghcide (#701)
* Invert the dependency between ghcide and hls-plugin-api This PR includes changes both to ghcide and HLS to implement the reorg described in https://github.com/haskell/ghcide/issues/936#issuecomment-751437853 To summarise: - `hls-plugin-api` no longer depends on ghcide. - `ghcide` now depends on `hls-plugin-api` and exposes: - The ghcide HLS plugin - The `asGhcIdePlugin` adaptor The goals are: - to be able to break the `ghcide` HLS plugin down - to rewrite exe:ghcide on top of the HLS plugin model. The ghcide side is reviewed in haskell/ghcide#963 If this change is accepted there are two further considerations: - This would be a good moment to merge the 2 repos, so that there is no history loss. - `hls-plugin-api` will need to be released to Hackage prior to merging haskell/ghcide#963 * clean up * Fix the ghcide plugin to include the rules * clean up PartialHandlers definition The ghcide partial handlers for completions, code actions and hover are not really being used, since they get overriden by the `<> ps` append. This is due to the right-biased semantics of `PartialHandlers` * Move ghcide LspConfig into Ide.Plugin.Config * Use HLS plugins in ghcide For now there is only one, the main ghcide plugin. But this will allow us to break it down in more fine grained plugins with parallel semantics, both for execution and error handling * Fix hlints * Revert "Temporarily disable the upstream branch for benchmarks" This reverts commit 7bb3c6e. * Disable the Windows 8.6.4 test * Fix unrelated hlints Not sure why these are triggering now. Linting should be restricted to the Diff ...
1 parent 50632e7 commit 1a34357

File tree

41 files changed

+456
-550
lines changed

Some content is hidden

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

41 files changed

+456
-550
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020
- os: windows-latest
2121
ghc: "8.8.2" # fails due to error with Cabal
2222
include:
23+
- os: windows-latest
24+
ghc: "8.6.4" # times out after 300m
2325
- os: windows-latest
2426
ghc: "8.10.2.2" # only available for windows and choco
2527
# one ghc-lib build

exe/Plugins.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
module Plugins where
44

55
import Ide.Types (IdePlugins)
6-
import Ide.Plugin (pluginDescToIdePlugins)
6+
import Ide.PluginUtils (pluginDescToIdePlugins)
77

88
-- fixed plugins
99
import Ide.Plugin.Example as Example
1010
import Ide.Plugin.Example2 as Example2
11-
import Ide.Plugin.GhcIde as GhcIde
11+
import Development.IDE (IdeState)
12+
import Development.IDE.Plugin.HLS.GhcIde as GhcIde
1213

1314
-- haskell-language-server optional plugins
1415

@@ -73,7 +74,7 @@ import Ide.Plugin.Brittany as Brittany
7374
-- These can be freely added or removed to tailor the available
7475
-- features of the server.
7576

76-
idePlugins :: Bool -> IdePlugins
77+
idePlugins :: Bool -> IdePlugins IdeState
7778
idePlugins includeExamples = pluginDescToIdePlugins allPlugins
7879
where
7980
allPlugins = if includeExamples

ghcide/bench/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ versions:
5555
# - v0.4.0
5656
# - v0.5.0
5757
# - v0.6.0
58-
# - upstream: origin/master
58+
- upstream: origin/master
5959
- HEAD

ghcide/exe/Main.hs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import Development.IDE.Types.Diagnostics
2828
import Development.IDE.Types.Options
2929
import Development.IDE.Types.Logger
3030
import Development.IDE.Plugin
31-
import Development.IDE.Plugin.Completions as Completions
32-
import Development.IDE.Plugin.CodeAction as CodeAction
3331
import Development.IDE.Plugin.Test as Test
3432
import Development.IDE.Session (loadSession)
3533
import qualified Language.Haskell.LSP.Core as LSP
@@ -54,6 +52,10 @@ import Development.IDE (action)
5452
import Text.Printf
5553
import Development.IDE.Core.Tracing
5654
import Development.IDE.Types.Shake (Key(Key))
55+
import Development.IDE.Plugin.HLS (asGhcIdePlugin)
56+
import Development.IDE.Plugin.HLS.GhcIde as GhcIde
57+
import Ide.Plugin.Config
58+
import Ide.PluginUtils (allLspCmdIds', getProcessID, pluginDescToIdePlugins)
5759

5860
ghcideVersion :: IO String
5961
ghcideVersion = do
@@ -83,18 +85,23 @@ main = do
8385
whenJust argsCwd IO.setCurrentDirectory
8486

8587
dir <- IO.getCurrentDirectory
86-
command <- makeLspCommandId "typesignature.add"
8788

88-
let plugins = Completions.plugin <> CodeAction.plugin
89+
let hlsPlugins = pluginDescToIdePlugins [GhcIde.descriptor "ghcide"]
90+
91+
pid <- T.pack . show <$> getProcessID
92+
let hlsPlugin = asGhcIdePlugin hlsPlugins
93+
hlsCommands = allLspCmdIds' pid hlsPlugins
94+
95+
let plugins = hlsPlugin
8996
<> if argsTesting then Test.plugin else mempty
90-
onInitialConfiguration :: InitializeRequest -> Either T.Text LspConfig
97+
onInitialConfiguration :: InitializeRequest -> Either T.Text Config
9198
onInitialConfiguration x = case x ^. params . initializationOptions of
92-
Nothing -> Right defaultLspConfig
99+
Nothing -> Right def
93100
Just v -> case J.fromJSON v of
94101
J.Error err -> Left $ T.pack err
95102
J.Success a -> Right a
96103
onConfigurationChange = const $ Left "Updating Not supported"
97-
options = def { LSP.executeCommandCommands = Just [command]
104+
options = def { LSP.executeCommandCommands = Just hlsCommands
98105
, LSP.completionTriggerCharacters = Just "."
99106
}
100107

@@ -106,7 +113,7 @@ main = do
106113
t <- t
107114
hPutStrLn stderr $ "Started LSP server in " ++ showDuration t
108115
sessionLoader <- loadSession $ fromMaybe dir rootPath
109-
config <- fromMaybe defaultLspConfig <$> getConfig
116+
config <- fromMaybe def <$> getConfig
110117
let options = (defaultIdeOptions sessionLoader)
111118
{ optReportProgress = clientSupportsProgress caps
112119
, optShakeProfiling = argsShakeProfiling
@@ -159,7 +166,7 @@ main = do
159166
, optTesting = IdeTesting argsTesting
160167
, optThreads = argsThreads
161168
, optCheckParents = NeverCheck
162-
, optCheckProject = CheckProject False
169+
, optCheckProject = False
163170
}
164171
logLevel = if argsVerbose then minBound else Info
165172
ide <- initialise def mainRule (pure $ IdInt 0) (showEvent lock) dummyWithProg (const (const id)) (logger logLevel) debouncer options vfs

ghcide/ghcide.cabal

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ library
5656
haskell-lsp-types == 0.22.*,
5757
haskell-lsp == 0.22.*,
5858
hie-compat,
59+
hls-plugin-api,
60+
lens,
5961
mtl,
6062
network-uri,
6163
parallel,
@@ -127,7 +129,6 @@ library
127129
include
128130
exposed-modules:
129131
Development.IDE
130-
Development.IDE.Compat
131132
Development.IDE.Core.Debouncer
132133
Development.IDE.Core.FileStore
133134
Development.IDE.Core.IdeConfiguration
@@ -163,6 +164,8 @@ library
163164
Development.IDE.Plugin
164165
Development.IDE.Plugin.Completions
165166
Development.IDE.Plugin.CodeAction
167+
Development.IDE.Plugin.HLS
168+
Development.IDE.Plugin.HLS.GhcIde
166169
Development.IDE.Plugin.Test
167170

168171
-- Unfortunately, we cannot use loadSession with ghc-lib since hie-bios uses
@@ -190,6 +193,7 @@ library
190193
Development.IDE.Plugin.CodeAction.RuleTypes
191194
Development.IDE.Plugin.Completions.Logic
192195
Development.IDE.Plugin.Completions.Types
196+
Development.IDE.Plugin.HLS.Formatter
193197
Development.IDE.Types.Action
194198
ghc-options: -Wall -Wno-name-shadowing -Wincomplete-uni-patterns
195199

@@ -265,6 +269,7 @@ executable ghcide
265269
haskell-lsp-types,
266270
heapsize,
267271
hie-bios,
272+
hls-plugin-api,
268273
ghcide,
269274
lens,
270275
optparse-applicative,

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ loadSessionWithOptions SessionLoadingOptions{..} dir = do
136136
} <- getShakeExtras
137137

138138
IdeOptions{ optTesting = IdeTesting optTesting
139-
, optCheckProject = CheckProject checkProject
139+
, optCheckProject = checkProject
140140
, optCustomDynFlags
141141
, optExtensions
142142
} <- getIdeOptions

ghcide/src/Development/IDE.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Development.IDE
99
import Development.IDE.Core.RuleTypes as X
1010
import Development.IDE.Core.Rules as X
1111
(getAtPoint
12+
,getClientConfigAction
1213
,getDefinition
1314
,getParsedModule
1415
,getTypeDefinition

ghcide/src/Development/IDE/Compat.hs

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import Development.IDE.Core.RuleTypes
4040
import Development.IDE.Types.Options
4141
import qualified Data.Rope.UTF16 as Rope
4242
import Development.IDE.Import.DependencyInformation
43+
import Ide.Plugin.Config (CheckParents(..))
4344

4445
#ifdef mingw32_HOST_OS
4546
import qualified System.Directory as Dir

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ isWorkspaceFile file =
8888
workspaceFolders
8989

9090
getClientSettings :: Action (Maybe Value)
91-
getClientSettings = unhashed . clientSettings <$> getIdeConfiguration
91+
getClientSettings = unhashed . clientSettings <$> getIdeConfiguration

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ module Development.IDE.Core.Rules(
2727
highlightAtPoint,
2828
getDependencies,
2929
getParsedModule,
30+
getClientConfigAction,
3031
) where
3132

3233
import Fingerprint
3334

35+
import Data.Aeson (fromJSON, Result(Success), FromJSON)
3436
import Data.Binary hiding (get, put)
37+
import Data.Default
3538
import Data.Tuple.Extra
3639
import Control.Monad.Extra
3740
import Control.Monad.Trans.Class
@@ -890,6 +893,15 @@ getClientSettingsRule = defineEarlyCutOffNoFile $ \GetClientSettings -> do
890893
settings <- clientSettings <$> getIdeConfiguration
891894
return (BS.pack . show . hash $ settings, settings)
892895

896+
-- | Returns the client configurarion stored in the IdeState.
897+
-- You can use this function to access it from shake Rules
898+
getClientConfigAction :: (Default a, FromJSON a) => Action a
899+
getClientConfigAction = do
900+
mbVal <- unhashed <$> useNoFile_ GetClientSettings
901+
case fromJSON <$> mbVal of
902+
Just (Success c) -> return c
903+
_ -> return def
904+
893905
-- | For now we always use bytecode
894906
getLinkableType :: NormalizedFilePath -> Action (Maybe LinkableType)
895907
getLinkableType f = do

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import qualified Data.Text as Text
3333
import Development.IDE.Core.FileStore (setSomethingModified, setFileModified, typecheckParents)
3434
import Development.IDE.Core.FileExists (modifyFileExists, watchedGlobs)
3535
import Development.IDE.Core.OfInterest
36+
import Ide.Plugin.Config (CheckParents(CheckOnClose))
3637

3738

3839
whenUriFile :: Uri -> (NormalizedFilePath -> IO ()) -> IO ()

ghcide/src/Development/IDE/Plugin.hs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
2-
module Development.IDE.Plugin(Plugin(..), codeActionPlugin, codeActionPluginWithRules,makeLspCommandId,getPid) where
1+
module Development.IDE.Plugin
2+
( Plugin(..)
3+
, codeActionPlugin
4+
, codeActionPluginWithRules
5+
, makeLspCommandId
6+
) where
37

48
import Data.Default
59
import qualified Data.Text as T
610
import Development.Shake
711
import Development.IDE.LSP.Server
8-
9-
import Language.Haskell.LSP.Types
10-
import Development.IDE.Compat
1112
import Development.IDE.Core.Rules
13+
import Ide.PluginUtils
14+
import Language.Haskell.LSP.Types
1215
import qualified Language.Haskell.LSP.Core as LSP
1316
import Language.Haskell.LSP.Messages
1417

@@ -50,11 +53,5 @@ codeActionPluginWithRules rr f = Plugin rr $ PartialHandlers $ \WithMessage{..}
5053
-- on that.
5154
makeLspCommandId :: T.Text -> IO T.Text
5255
makeLspCommandId command = do
53-
pid <- getPid
54-
return $ pid <> ":ghcide:" <> command
55-
56-
-- | Get the operating system process id for the running server
57-
-- instance. This should be the same for the lifetime of the instance,
58-
-- and different from that of any other currently running instance.
59-
getPid :: IO T.Text
60-
getPid = T.pack . show <$> getProcessID
56+
pid <- getProcessID
57+
return $ T.pack (show pid) <> ":ghcide:" <> command

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import ConLike
5353
import GhcPlugins (
5454
flLabel,
5555
unpackFS)
56+
import Data.Either (fromRight)
5657

5758
-- From haskell-ide-engine/hie-plugin-api/Haskell/Ide/Engine/Context.hs
5859

@@ -337,14 +338,14 @@ cacheDataProducer packageState curMod rdrEnv limports deps = do
337338
name' <- lookupName packageState m n
338339
return $ name' >>= safeTyThingForRecord
339340

340-
let recordCompls = case either (const Nothing) id record_ty of
341+
let recordCompls = case fromRight Nothing record_ty of
341342
Just (ctxStr, flds) -> case flds of
342343
[] -> []
343344
_ -> [mkRecordSnippetCompItem ctxStr flds (ppr mn) docs imp']
344345
Nothing -> []
345346

346-
return $ [mkNameCompItem n mn (either (const Nothing) id ty) Nothing docs imp'] ++
347-
recordCompls
347+
return $ mkNameCompItem n mn (fromRight Nothing ty) Nothing docs imp'
348+
: recordCompls
348349

349350
(unquals,quals) <- getCompls rdrElts
350351

0 commit comments

Comments
 (0)