This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathMainHie.hs
217 lines (181 loc) · 7.69 KB
/
MainHie.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
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Main where
import qualified Control.Exception as E
import Control.Monad
#if __GLASGOW_HASKELL__ < 808
import Data.Monoid ((<>))
#endif
import Data.Version (showVersion)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Yaml as Yaml
import HIE.Bios.Types
import Haskell.Ide.Engine.Cradle (findLocalCradle, cradleDisplay, getProjectGhcLibDir)
import Haskell.Ide.Engine.MonadFunctions
import Haskell.Ide.Engine.MonadTypes
import Haskell.Ide.Engine.Options
import Haskell.Ide.Engine.Scheduler
import Haskell.Ide.Engine.Server
import Haskell.Ide.Engine.Version
import qualified Language.Haskell.LSP.Core as Core
import Options.Applicative.Simple
import qualified Paths_haskell_ide_engine as Meta
import System.Directory
import System.Environment
import System.FilePath
import System.Info
import System.IO
import qualified System.Log.Logger as L
-- ---------------------------------------------------------------------
import RunTest
-- ---------------------------------------------------------------------
-- plugins
import Haskell.Ide.Engine.Plugin.ApplyRefact
import Haskell.Ide.Engine.Plugin.Brittany
import Haskell.Ide.Engine.Plugin.Example2
import Haskell.Ide.Engine.Plugin.Floskell
import Haskell.Ide.Engine.Plugin.Generic
import Haskell.Ide.Engine.Plugin.GhcMod
-- import Haskell.Ide.Engine.Plugin.HaRe
import Haskell.Ide.Engine.Plugin.Haddock
import Haskell.Ide.Engine.Plugin.HfaAlign
import Haskell.Ide.Engine.Plugin.HsImport
import Haskell.Ide.Engine.Plugin.Liquid
import Haskell.Ide.Engine.Plugin.Ormolu
import Haskell.Ide.Engine.Plugin.Package
import Haskell.Ide.Engine.Plugin.Pragmas
import Haskell.Ide.Engine.Plugin.Stylish (stylishDescriptor)
-- ---------------------------------------------------------------------
-- | This will be read from a configuration, eventually
plugins :: Bool -> IdePlugins
plugins includeExamples = pluginDescToIdePlugins allPlugins
where
allPlugins = if includeExamples
then basePlugins ++ examplePlugins
else basePlugins
basePlugins =
[ applyRefactDescriptor "applyrefact"
, brittanyDescriptor "brittany"
, haddockDescriptor "haddock"
-- , hareDescriptor "hare"
, hsimportDescriptor "hsimport"
, liquidDescriptor "liquid"
, packageDescriptor "package"
, pragmasDescriptor "pragmas"
, floskellDescriptor "floskell"
, genericDescriptor "generic"
, ghcmodDescriptor "ghcmod"
, ormoluDescriptor "ormolu"
, stylishDescriptor "stylish"
]
examplePlugins =
[example2Descriptor "eg2"
,hfaAlignDescriptor "hfaa"
]
-- ---------------------------------------------------------------------
main :: IO ()
main = do
let
numericVersion :: Parser (a -> a)
numericVersion =
infoOption
(showVersion Meta.version)
(long "numeric-version" <>
help "Show only version number")
compiler :: Parser (a -> a)
compiler =
infoOption
hieGhcDisplayVersion
(long "compiler" <>
help "Show only compiler and version supported")
-- Parse the options and run
(global, ()) <-
simpleOptions
hieVersion
"haskell-ide-engine - Provide a common engine to power any Haskell IDE"
""
(numericVersion <*> compiler <*> globalOptsParser)
empty
run global
-- ---------------------------------------------------------------------
run :: GlobalOpts -> IO ()
run opts = do
hSetBuffering stderr LineBuffering
let mLogFileName = optLogFile opts
logLevel = if optDebugOn opts
then L.DEBUG
else L.INFO
Core.setupLogger mLogFileName ["hie", "hie-bios"] logLevel
origDir <- getCurrentDirectory
maybe (pure ()) setCurrentDirectory $ projectRoot opts
progName <- getProgName
args <- getArgs
let plugins' = plugins (optExamplePlugin opts)
if optLsp opts
then do
-- Start up in LSP mode
logm $ "Run entered for HIE(" ++ progName ++ ") " ++ hieVersion
logm $ "Operating as a LSP server on stdio"
logm $ "Current directory:" ++ origDir
logm $ "Operating system:" ++ os
logm $ "args:" ++ show args
let initOpts = defaultCradleOpts { cradleOptsVerbosity = verbosity }
verbosity = if optBiosVerbose opts then Verbose else Silent
when (optBiosVerbose opts) $
logm "Enabling verbose mode for hie-bios. This option currently doesn't do anything."
when (optExamplePlugin opts) $
logm "Enabling Example2 plugin, will insert constant diagnostics etc."
-- launch the dispatcher.
scheduler <- newScheduler plugins' initOpts
server scheduler origDir plugins' (optCaptureFile opts)
else do
-- Provide debug info
cliOut $ "Running HIE(" ++ progName ++ ")"
cliOut $ " " ++ hieVersion
cliOut $ "To run as a LSP server on stdio, provide the '--lsp' argument"
cliOut $ "Current directory:" ++ origDir
-- args <- getArgs
cliOut $ "\nargs:" ++ show args
cliOut $ "\nLooking for project config cradle...\n"
ecradle <- getCradleInfo origDir
case ecradle of
Left e -> cliOut $ "Could not get cradle:" ++ show e
Right cradle -> do
projGhc <- getProjectGhcVersion cradle
mlibdir <- getProjectGhcLibDir cradle
cliOut "\n\n###################################################\n"
cliOut $ "Cradle: " ++ cradleDisplay cradle
cliOut $ "Project Ghc version: " ++ projGhc
cliOut $ "Libdir: " ++ show mlibdir
cliOut "Searching for Haskell source files..."
targets <- case optFiles opts of
[] -> findAllSourceFiles origDir
xs -> concat <$> mapM findAllSourceFiles xs
cliOut $ "Found " ++ show (length targets) ++ " Haskell source files.\n"
cliOut "###################################################"
cliOut "\nFound the following files:\n"
mapM_ cliOut targets
cliOut ""
unless (optDryRun opts) $ do
cliOut "\nLoad them all now. This may take a very long time.\n"
loadDiagnostics <- runServer mlibdir plugins' targets
cliOut ""
cliOut "###################################################"
cliOut "###################################################"
cliOut "\nDumping diagnostics:\n\n"
mapM_ (cliOut' . uncurry prettyPrintDiags) loadDiagnostics
cliOut "\n\nNote: loading of 'Setup.hs' is not supported."
-- ---------------------------------------------------------------------
getCradleInfo :: FilePath -> IO (Either Yaml.ParseException Cradle)
getCradleInfo currentDir = do
let dummyCradleFile = currentDir </> "File.hs"
cradleRes <- E.try (findLocalCradle dummyCradleFile)
return cradleRes
-- ---------------------------------------------------------------------
cliOut :: String -> IO ()
cliOut = putStrLn
cliOut' :: T.Text -> IO ()
cliOut' = T.putStrLn
-- ---------------------------------------------------------------------