Skip to content

Commit ad17433

Browse files
committed
Harmonise CLI flags between ghcide/haskell-ide(-wrapper)
And add --example to enable the Example plugin in haskell-ide Closes haskell#24
1 parent 638aa1b commit ad17433

File tree

7 files changed

+135
-163
lines changed

7 files changed

+135
-163
lines changed

cabal.project

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ tests: true
66

77
package ide
88
test-show-details: direct
9+
package ghcide
10+
test-show-details: direct
911

1012
write-ghc-environment-files: never

exe/Arguments.hs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,73 @@
11
-- Copyright (c) 2019 The DAML Authors. All rights reserved.
22
-- SPDX-License-Identifier: Apache-2.0
3+
{-# LANGUAGE CPP #-} -- To get precise GHC version
4+
{-# LANGUAGE RecordWildCards #-}
5+
{-# LANGUAGE TemplateHaskell #-}
6+
{-# LANGUAGE TupleSections #-}
7+
{-# LANGUAGE ViewPatterns #-}
8+
{-# OPTIONS_GHC -Wno-dodgy-imports #-} -- GHC no longer exports def in GHC 8.6 and above
39

4-
module Arguments(Arguments(..), getArguments) where
10+
module Arguments
11+
( Arguments(..)
12+
, getArguments
13+
, ghcideVersion
14+
, getLibdir
15+
) where
516

17+
import Data.Maybe
18+
import Data.Version
19+
import Development.GitRev
20+
import qualified GHC.Paths
621
import Options.Applicative
22+
import Paths_ide
23+
import System.Environment
724

25+
-- ---------------------------------------------------------------------
826

927
data Arguments = Arguments
1028
{argLSP :: Bool
1129
,argsCwd :: Maybe FilePath
1230
,argFiles :: [FilePath]
1331
,argsVersion :: Bool
1432
,argsShakeProfiling :: Maybe FilePath
33+
,argsExamplePlugin :: Bool
1534
}
1635

17-
getArguments :: IO Arguments
18-
getArguments = execParser opts
36+
getArguments :: String -> IO Arguments
37+
getArguments exeName = execParser opts
1938
where
20-
opts = info (arguments <**> helper)
39+
opts = info (arguments exeName <**> helper)
2140
( fullDesc
2241
<> progDesc "Used as a test bed to check your IDE will work"
23-
<> header "ghcide - the core of a Haskell IDE")
42+
<> header (exeName ++ " - GHC Haskell LSP server"))
2443

25-
arguments :: Parser Arguments
26-
arguments = Arguments
44+
arguments :: String -> Parser Arguments
45+
arguments exeName = Arguments
2746
<$> switch (long "lsp" <> help "Start talking to an LSP server")
28-
<*> optional (strOption $ long "cwd" <> metavar "DIR" <> help "Change to this directory")
47+
<*> optional (strOption $ long "cwd" <> metavar "DIR"
48+
<> help "Change to this directory")
2949
<*> many (argument str (metavar "FILES/DIRS..."))
30-
<*> switch (long "version" <> help "Show ghcide and GHC versions")
31-
<*> optional (strOption $ long "shake-profiling" <> metavar "DIR" <> help "Dump profiling reports to this directory")
50+
<*> switch (long "version"
51+
<> help ("Show " ++ exeName ++ " and GHC versions"))
52+
<*> optional (strOption $ long "shake-profiling" <> metavar "DIR"
53+
<> help "Dump profiling reports to this directory")
54+
<*> switch (long "example"
55+
<> help "Include the Example Plugin. For Plugin devs only")
3256

57+
-- ---------------------------------------------------------------------
58+
-- Set the GHC libdir to the nix libdir if it's present.
59+
getLibdir :: IO FilePath
60+
getLibdir = fromMaybe GHC.Paths.libdir <$> lookupEnv "NIX_GHC_LIBDIR"
61+
62+
ghcideVersion :: IO String
63+
ghcideVersion = do
64+
path <- getExecutablePath
65+
let gitHashSection = case $(gitHash) of
66+
x | x == "UNKNOWN" -> ""
67+
x -> " (GIT hash: " <> x <> ")"
68+
return $ "ghcide version: " <> showVersion version
69+
<> " (GHC: " <> VERSION_ghc
70+
<> ") (PATH: " <> path <> ")"
71+
<> gitHashSection
72+
73+
-- ---------------------------------------------------------------------

exe/Main.hs

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
-- Copyright (c) 2019 The DAML Authors. All rights reserved.
22
-- SPDX-License-Identifier: Apache-2.0
33
{-# OPTIONS_GHC -Wno-dodgy-imports #-} -- GHC no longer exports def in GHC 8.6 and above
4-
{-# LANGUAGE CPP #-} -- To get precise GHC version
5-
{-# LANGUAGE TemplateHaskell #-}
64
{-# LANGUAGE RecordWildCards #-}
75
{-# LANGUAGE ViewPatterns #-}
86
{-# LANGUAGE TupleSections #-}
@@ -37,51 +35,37 @@ import qualified Data.Text.IO as T
3735
import Language.Haskell.LSP.Messages
3836
import Language.Haskell.LSP.Types (LspId(IdInt))
3937
import Linker
40-
import Data.Version
4138
import Development.IDE.LSP.LanguageServer
4239
import System.Directory.Extra as IO
43-
import System.Environment
4440
import System.IO
4541
import System.Exit
46-
import Paths_ide
47-
import Development.GitRev
4842
import Development.Shake (Action, action)
4943
import qualified Data.Set as Set
5044
import qualified Data.Map.Strict as Map
5145

5246
import GHC hiding (def)
53-
import qualified GHC.Paths
5447

5548
import HIE.Bios
5649

5750
-- ---------------------------------------------------------------------
5851

59-
import Ide.Plugin.Example as Example
52+
import Development.IDE.Plugin.CodeAction as CodeAction
6053
import Development.IDE.Plugin.Completions as Completions
61-
import Development.IDE.Plugin.CodeAction as CodeAction
54+
import Ide.Plugin.Example as Example
6255

6356
-- ---------------------------------------------------------------------
6457

65-
-- Set the GHC libdir to the nix libdir if it's present.
66-
getLibdir :: IO FilePath
67-
getLibdir = fromMaybe GHC.Paths.libdir <$> lookupEnv "NIX_GHC_LIBDIR"
68-
69-
ghcideVersion :: IO String
70-
ghcideVersion = do
71-
path <- getExecutablePath
72-
let gitHashSection = case $(gitHash) of
73-
x | x == "UNKNOWN" -> ""
74-
x -> " (GIT hash: " <> x <> ")"
75-
return $ "ghcide version: " <> showVersion version
76-
<> " (GHC: " <> VERSION_ghc
77-
<> ") (PATH: " <> path <> ")"
78-
<> gitHashSection
58+
idePlugins :: Bool -> Plugin
59+
idePlugins includeExample
60+
= Completions.plugin <>
61+
CodeAction.plugin <>
62+
if includeExample then Example.plugin else mempty
7963

8064
main :: IO ()
8165
main = do
8266
-- WARNING: If you write to stdout before runLanguageServer
8367
-- then the language server will not work
84-
Arguments{..} <- getArguments
68+
Arguments{..} <- getArguments "haskell-ide"
8569

8670
if argsVersion then ghcideVersion >>= putStrLn >> exitSuccess
8771
else hPutStrLn stderr {- see WARNING above -} =<< ghcideVersion
@@ -95,8 +79,7 @@ main = do
9579

9680
dir <- getCurrentDirectory
9781

98-
let plugins = Completions.plugin <> CodeAction.plugin
99-
<> Example.plugin
82+
let plugins = idePlugins argsExamplePlugin
10083

10184
if argLSP then do
10285
t <- offsetTime

exe/Wrapper.hs

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,58 @@
11
{-# LANGUAGE CPP #-}
2+
{-# LANGUAGE RecordWildCards #-}
23
-- | This module is based on the hie-wrapper.sh script in
34
-- https://github.com/alanz/vscode-hie-server
45
module Main where
56

67
#if __GLASGOW_HASKELL__ < 804
78
import Data.Semigroup
89
#endif
10+
11+
import Arguments
12+
import Control.Concurrent.Extra
13+
import Control.Monad.Extra
914
import Data.Foldable
1015
import Data.List
11-
import Data.Version (showVersion)
12-
import HIE.Bios
16+
import Data.List.Extra
17+
import qualified Data.Text as T
18+
import qualified Data.Text.IO as T
19+
import Development.IDE.Types.Logger
20+
import HIE.Bios
1321
import Ide.Cradle (findLocalCradle, logm)
14-
import Ide.Options
1522
import Ide.Version
16-
import qualified Language.Haskell.LSP.Core as Core
17-
import Options.Applicative.Simple
18-
import qualified Paths_ide as Meta
1923
import System.Directory
2024
import System.Environment
25+
import System.Exit
2126
import System.FilePath
27+
import System.IO
2228
import System.Info
23-
import qualified System.Log.Logger as L
2429
import System.Process
2530

2631
-- ---------------------------------------------------------------------
2732

2833
main :: IO ()
2934
main = do
30-
let
31-
numericVersion :: Parser (a -> a)
32-
numericVersion =
33-
infoOption
34-
(showVersion Meta.version)
35-
(long "numeric-version" <>
36-
help "Show only version number")
37-
compiler :: Parser (a -> a)
38-
compiler =
39-
infoOption
40-
hieGhcDisplayVersion
41-
(long "compiler" <>
42-
help "Show only compiler and version supported")
43-
-- Parse the options and run
44-
(global, ()) <-
45-
simpleOptions
46-
hieVersion
47-
"haskell-ide-wrapper - Launch the appropriate haskell-ide for a given project"
48-
""
49-
(numericVersion <*> compiler <*> globalOptsParser)
50-
empty
51-
52-
run global
35+
-- WARNING: If you write to stdout before runLanguageServer
36+
-- then the language server will not work
37+
Arguments{..} <- getArguments "haskell-ide"
5338

54-
-- ---------------------------------------------------------------------
39+
if argsVersion then ghcideVersion >>= putStrLn >> exitSuccess
40+
else hPutStrLn stderr {- see WARNING above -} =<< ghcideVersion
5541

56-
run :: GlobalOpts -> IO ()
57-
run opts = do
58-
let mLogFileName = optLogFile opts
42+
-- lock to avoid overlapping output on stdout
43+
-- lock <- newLock
44+
-- let logger p = Logger $ \pri msg -> when (pri >= p) $ withLock lock $
45+
-- T.putStrLn $ T.pack ("[" ++ upper (show pri) ++ "] ") <> msg
5946

60-
logLevel = if optDebugOn opts
61-
then L.DEBUG
62-
else L.INFO
47+
whenJust argsCwd setCurrentDirectory
6348

64-
Core.setupLogger mLogFileName ["hie"] logLevel
49+
-- let mLogFileName = optLogFile opts
6550

66-
maybe (pure ()) setCurrentDirectory $ projectRoot opts
51+
-- logLevel = if optDebugOn opts
52+
-- then L.DEBUG
53+
-- else L.INFO
6754

55+
-- Core.setupLogger mLogFileName ["hie"] logLevel
6856

6957
progName <- getProgName
7058
logm $ "run entered for haskell-ide-wrapper(" ++ progName ++ ") " ++ hieVersion

hie.yaml.cbl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ cradle:
1616

1717
- path: "./src"
1818
component: "lib:ide"
19+
20+
- path: "./ghcide/src"
21+
component: "ghcide:lib:ghcide"
22+
23+
- path: "./ghcide/exe"
24+
component: "ghcide:exe:ghcide"

ide.cabal

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ library
3030
exposed-modules:
3131
Ide.Cradle
3232
Ide.Plugin.Example
33-
Ide.Options
3433
Ide.Version
3534
other-modules:
3635
Paths_ide
@@ -90,10 +89,13 @@ library
9089

9190
executable haskell-ide
9291
main-is: Main.hs
93-
other-modules:
94-
Paths_ide
9592
hs-source-dirs:
9693
exe
94+
other-modules:
95+
Arguments
96+
Paths_ide
97+
autogen-modules:
98+
Paths_ide
9799
ghc-options:
98100
-threaded
99101
-Wall
@@ -118,7 +120,6 @@ executable haskell-ide
118120
, ghc
119121
, ghc-paths
120122
, ghcide
121-
, ghcide
122123
, gitrev
123124
, haskell-lsp
124125
, hie-bios >= 0.3.2 && < 0.4
@@ -127,31 +128,48 @@ executable haskell-ide
127128
, optparse-applicative
128129
, shake >= 0.17.5
129130
, text
130-
other-modules:
131-
Arguments
132-
Paths_ide
133131
default-language: Haskell2010
134132

135133
executable haskell-ide-wrapper
136-
hs-source-dirs: exe
137-
main-is: Wrapper.hs
138-
other-modules: Paths_ide
139-
autogen-modules: Paths_ide
140-
build-depends: base
141-
, directory
142-
, filepath
143-
, haskell-lsp
144-
, hie-bios
145-
, hslogger
146-
, optparse-simple
147-
, process
148-
, ide
149-
ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall -Wredundant-constraints
150-
-with-rtsopts=-T
134+
main-is: Wrapper.hs
135+
hs-source-dirs:
136+
exe
137+
other-modules:
138+
Arguments
139+
Paths_ide
140+
autogen-modules:
141+
Paths_ide
142+
ghc-options:
143+
-threaded
144+
-Wall
145+
-Wno-name-shadowing
146+
-Wredundant-constraints
147+
-- allow user RTS overrides
148+
-rtsopts
149+
-- disable idle GC
150+
-- disable parallel GC
151+
-- increase nursery size
152+
"-with-rtsopts=-I0 -qg -A128M"
151153
if flag(pedantic)
152154
ghc-options: -Werror
155+
build-depends:
156+
base
157+
, directory
158+
, extra
159+
, filepath
160+
, gitrev
161+
, ghc
162+
, ghcide
163+
, ghc-paths
164+
, haskell-lsp
165+
, hie-bios
166+
, ide
167+
, optparse-applicative
168+
, process
169+
, text
153170
default-language: Haskell2010
154171

172+
155173
test-suite test
156174
type: exitcode-stdio-1.0
157175
main-is: Spec.hs

0 commit comments

Comments
 (0)