forked from bitc/hdevtools
- Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCommandLoop.hs
More file actions
Latest commit
276 lines (253 loc) · 10.3 KB
/
Copy pathCommandLoop.hs
File metadata and controls
276 lines (253 loc) · 10.3 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
{-# LANGUAGE CPP #-}
moduleCommandLoop
( newCommandLoopState
, Config(..)
, CabalConfig(..)
, newConfig
, startCommandLoop
) where
importControl.Monad (when)
importData.IORef
importData.List (find, intercalate)
#if __GLASGOW_HASKELL__ < 709
importControl.Applicative ((<$>))
importData.Traversable (traverse)
#endif
importMonadUtils (MonadIO, liftIO)
importSystem.Directory (setCurrentDirectory)
importSystem.Exit (ExitCode(ExitFailure, ExitSuccess))
importSystem.FilePath (takeDirectory)
importqualifiedErrUtils
importqualifiedException (ExceptionMonad)
importqualifiedGHC
importqualifiedGHC.Paths
importqualifiedOutputable
importSystem.Posix.Types (EpochTime)
importSystem.Posix.Files (getFileStatus, modificationTime)
importTypes (ClientDirective(..), Command(..), CommandExtra(..))
importInfo (getIdentifierInfo, getType)
importFindSymbol (findSymbol)
importCabal (getPackageGhcOpts)
importStack
typeClientSend=ClientDirective->IO()
dataState=State
{stateWarningsEnabled::Bool
}
newCommandLoopState::IO (IORefState)
newCommandLoopState =do
newIORef $State
{ stateWarningsEnabled =True
}
dataCabalConfig=CabalConfig
{cabalConfigPath::FilePath
, cabalConfigOpts:: [String]
, cabalConfigLastUpdatedAt::EpochTime
}
derivingEq
mkCabalConfig::FilePath-> [String] ->IOCabalConfig
mkCabalConfig path opts =do
fileStatus <- getFileStatus path
return$CabalConfig { cabalConfigPath = path
, cabalConfigOpts = opts
, cabalConfigLastUpdatedAt = modificationTime fileStatus
}
dataConfig=Config
{configGhcOpts:: [String]
, configCabal::MaybeCabalConfig
, configStack::MaybeStackConfig
}
derivingEq
newConfig::CommandExtra->IOConfig
newConfig cmdExtra =do
mbCabalConfig <-traverse (\path -> mkCabalConfig path (ceCabalOptions cmdExtra)) $ ceCabalConfig cmdExtra
mbStackConfig <- getStackConfig cmdExtra
return$Config { configGhcOpts ="-O0": ceGhcOptions cmdExtra
, configCabal = mbCabalConfig
, configStack = mbStackConfig
}
typeCommandObj= (Command, Config)
withWarnings:: (MonadIOm, Exception.ExceptionMonadm) =>IORefState->Bool->ma->ma
withWarnings state warningsValue action =do
beforeState <- liftIO $ getWarnings
liftIO $ setWarnings warningsValue
action `GHC.gfinally`
(liftIO $ setWarnings beforeState)
where
getWarnings::IOBool
getWarnings = readIORef state >>=return. stateWarningsEnabled
setWarnings::Bool->IO()
setWarnings val = modifyIORef state $\s -> s { stateWarningsEnabled = val }
startCommandLoop::IORefState->ClientSend->IO (MaybeCommandObj) ->Config->MaybeCommand->IO()
startCommandLoop state clientSend getNextCommand initialConfig mbInitialCommand =do
continue <-GHC.runGhc (JustGHC.Paths.libdir) $do
configResult <- configSession state clientSend initialConfig
case configResult of
Left e ->do
liftIO $mapM_ clientSend
[ ClientStderr e
, ClientExit (ExitFailure1)
]
processNextCommand True
Right _ ->do
doMaybe mbInitialCommand $\cmd -> sendErrors (runCommand state clientSend cmd)
processNextCommand False
case continue of
Nothing->
-- Exit
return()
Just (cmd, config) -> startCommandLoop state clientSend getNextCommand config (Just cmd)
where
processNextCommand::Bool->GHC.Ghc (MaybeCommandObj)
processNextCommand forceReconfig =do
mbNextCmd <- liftIO getNextCommand
case mbNextCmd of
Nothing->
-- Exit
returnNothing
Just (cmd, config) ->
if forceReconfig || (config /= initialConfig)
thenreturn (Just (cmd, config))
else sendErrors (runCommand state clientSend cmd) >> processNextCommand False
sendErrors::GHC.Ghc()->GHC.Ghc()
sendErrors action =GHC.gcatch action $\e ->do
liftIO $mapM_ clientSend
[ ClientStderr$GHC.showGhcException e ""
, ClientExit (ExitFailure1)
]
return()
doMaybe::Monadm=>Maybea-> (a->m()) ->m()
doMaybe Nothing _ =return()
doMaybe (Just x) f = f x
configSession::IORefState->ClientSend->Config->GHC.Ghc (EitherString())
configSession state clientSend config =do
eCabalGhcOpts <-case configCabal config of
Nothing->
return$Right[]
Just cabalConfig ->do
liftIO $ setCurrentDirectory . takeDirectory $ cabalConfigPath cabalConfig
liftIO $ getPackageGhcOpts (cabalConfigPath cabalConfig) (configStack config) (cabalConfigOpts cabalConfig)
case eCabalGhcOpts of
Left e ->return$Left e
Right cabalGhcOpts ->do
let allGhcOpts = cabalGhcOpts ++ configGhcOpts config
GHC.gcatch (Right<$> updateDynFlags allGhcOpts)
(fmapLeft. handleGhcError)
where
updateDynFlags:: [String] ->GHC.Ghc()
updateDynFlags ghcOpts =do
initialDynFlags <-GHC.getSessionDynFlags
let updatedDynFlags = initialDynFlags
{ GHC.log_action = logAction state clientSend
, GHC.ghcLink =GHC.NoLink
, GHC.hscTarget =GHC.HscInterpreted
}
(finalDynFlags, _, _) <-GHC.parseDynamicFlags updatedDynFlags (mapGHC.noLoc ghcOpts)
_ <-GHC.setSessionDynFlags finalDynFlags
return()
handleGhcError::GHC.GhcException->GHC.GhcString
handleGhcError e =return$GHC.showGhcException e ""
runCommand::IORefState->ClientSend->Command->GHC.Ghc()
runCommand _ clientSend (CmdCheck file) =do
let noPhase =Nothing
target <-GHC.guessTarget file noPhase
GHC.setTargets [target]
let handler err =GHC.printException err >>returnGHC.Failed
flag <-GHC.handleSourceError handler (GHC.load GHC.LoadAllTargets)
liftIO $case flag of
GHC.Succeeded-> clientSend (ClientExitExitSuccess)
GHC.Failed-> clientSend (ClientExit (ExitFailure1))
runCommand _ clientSend (CmdModuleFile moduleName) =do
moduleGraph <-GHC.getModuleGraph
case find (moduleSummaryMatchesModuleName moduleName) moduleGraph of
Nothing->
liftIO $mapM_ clientSend
[ ClientStderr"Module not found"
, ClientExit (ExitFailure1)
]
Just modSummary ->
caseGHC.ml_hs_file (GHC.ms_location modSummary) of
Nothing->
liftIO $mapM_ clientSend
[ ClientStderr"Module does not have a source file"
, ClientExit (ExitFailure1)
]
Just file ->
liftIO $mapM_ clientSend
[ ClientStdout file
, ClientExitExitSuccess
]
where
moduleSummaryMatchesModuleName modName modSummary =
modName == (GHC.moduleNameString .GHC.moduleName .GHC.ms_mod) modSummary
runCommand state clientSend (CmdInfo file identifier) =do
result <- withWarnings state False$
getIdentifierInfo file identifier
case result of
Left err ->
liftIO $mapM_ clientSend
[ ClientStderr err
, ClientExit (ExitFailure1)
]
Right info -> liftIO $mapM_ clientSend
[ ClientStdout info
, ClientExitExitSuccess
]
runCommand state clientSend (CmdType file (line, col)) =do
result <- withWarnings state False$
getType file (line, col)
case result of
Left err ->
liftIO $mapM_ clientSend
[ ClientStderr err
, ClientExit (ExitFailure1)
]
Right types -> liftIO $do
mapM_ (clientSend .ClientStdout. formatType) types
clientSend (ClientExitExitSuccess)
where
formatType:: ((Int, Int, Int, Int), String) ->String
formatType ((startLine, startCol, endLine, endCol), t) =
concat
[ show startLine , ""
, show startCol , ""
, show endLine , ""
, show endCol , ""
, "\"", t, "\""
]
runCommand state clientSend (CmdFindSymbol symbol files) =do
result <- withWarnings state False$ findSymbol symbol files
case result of
[]-> liftIO $mapM_ clientSend
[ ClientStderr$"Couldn't find modules containing '"++ symbol ++"'"
, ClientExit (ExitFailure1)
]
modules -> liftIO $mapM_ clientSend
[ ClientStdout (formatModules modules)
, ClientExitExitSuccess
]
where
formatModules = intercalate "\n"
#if __GLASGOW_HASKELL__ >= 706
logAction::IORefState->ClientSend->GHC.DynFlags->GHC.Severity->GHC.SrcSpan->Outputable.PprStyle->ErrUtils.MsgDoc->IO()
logAction state clientSend dflags severity srcspan style msg =
let out =Outputable.renderWithStyle dflags fullMsg style
_ = severity
in logActionSend state clientSend severity out
where fullMsg =ErrUtils.mkLocMessage severity srcspan msg
#else
logAction::IORefState->ClientSend->GHC.Severity->GHC.SrcSpan->Outputable.PprStyle->ErrUtils.Message->IO()
logAction state clientSend severity srcspan style msg =
let out =Outputable.renderWithStyle fullMsg style
_ = severity
in logActionSend state clientSend severity out
where fullMsg =ErrUtils.mkLocMessage srcspan msg
#endif
logActionSend::IORefState->ClientSend->GHC.Severity->String->IO()
logActionSend state clientSend severity out =do
currentState <- readIORef state
when (not (isWarning severity) || stateWarningsEnabled currentState) $
clientSend (ClientStdout out)
where
isWarning::GHC.Severity->Bool
isWarning GHC.SevWarning=True
isWarning _ =False