forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIde.hs
More file actions
217 lines (196 loc) · 8 KB
/
Ide.hs
File metadata and controls
217 lines (196 loc) · 8 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
-----------------------------------------------------------------------------
--
-- Module : Language.PureScript.Ide
-- Description : Interface for the psc-ide-server
-- Copyright : Christoph Hegemann 2016
-- License : MIT (http://opensource.org/licenses/MIT)
--
-- Maintainer : Christoph Hegemann <christoph.hegemann1337@gmail.com>
-- Stability : experimental
--
-- |
-- Interface for the psc-ide-server
-----------------------------------------------------------------------------
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE TemplateHaskell #-}
module Language.PureScript.Ide
( handleCommand
) where
import Protolude
import "monad-logger" Control.Monad.Logger
import qualified Language.PureScript as P
import qualified Language.PureScript.Ide.CaseSplit as CS
import Language.PureScript.Ide.Command
import Language.PureScript.Ide.Completion
import Language.PureScript.Ide.Error
import Language.PureScript.Ide.Externs
import Language.PureScript.Ide.Filter
import Language.PureScript.Ide.Imports hiding (Import)
import Language.PureScript.Ide.Matcher
import Language.PureScript.Ide.Pursuit
import Language.PureScript.Ide.Rebuild
import Language.PureScript.Ide.SourceFile
import Language.PureScript.Ide.State
import Language.PureScript.Ide.Types
import Language.PureScript.Ide.Util
import System.Directory (getCurrentDirectory, getDirectoryContents, doesDirectoryExist, doesFileExist)
import System.FilePath ((</>))
import System.FilePath.Glob (glob)
-- | Accepts a Commmand and runs it against psc-ide's State. This is the main
-- entry point for the server.
handleCommand :: (Ide m, MonadLogger m, MonadError IdeError m) =>
Command -> m Success
handleCommand c = case c of
Load [] ->
findAvailableExterns >>= loadModulesAsync
Load modules ->
loadModulesAsync modules
LoadSync [] ->
findAvailableExterns >>= loadModulesSync
LoadSync modules ->
loadModulesSync modules
Type search filters currentModule ->
findType search filters currentModule
Complete filters matcher currentModule ->
findCompletions filters matcher currentModule
Pursuit query Package ->
findPursuitPackages query
Pursuit query Identifier ->
findPursuitCompletions query
List LoadedModules ->
printModules
List AvailableModules ->
listAvailableModules
List (Imports fp) ->
ImportList <$> getImportsForFile fp
CaseSplit l b e wca t ->
caseSplit l b e wca t
AddClause l wca ->
MultilineTextResult <$> CS.addClause l wca
Import fp outfp _ (AddImplicitImport mn) -> do
rs <- addImplicitImport fp mn
answerRequest outfp rs
Import fp outfp filters (AddImportForIdentifier ident) -> do
rs <- addImportForIdentifier fp ident filters
case rs of
Right rs' -> answerRequest outfp rs'
Left question ->
pure (CompletionResult (map (completionFromMatch . map withEmptyAnn) question))
Rebuild file ->
rebuildFileAsync file
RebuildSync file ->
rebuildFileSync file
Cwd ->
TextResult . toS <$> liftIO getCurrentDirectory
Reset ->
resetIdeState $> TextResult "State has been reset."
Quit ->
liftIO exitSuccess
findCompletions :: Ide m =>
[Filter] -> Matcher IdeDeclarationAnn -> Maybe P.ModuleName -> m Success
findCompletions filters matcher currentModule = do
modules <- getAllModules currentModule
pure . CompletionResult . map completionFromMatch . getCompletions filters matcher $ modules
findType :: Ide m =>
Text -> [Filter] -> Maybe P.ModuleName -> m Success
findType search filters currentModule = do
modules <- getAllModules currentModule
pure . CompletionResult . map completionFromMatch . getExactMatches search filters $ modules
findPursuitCompletions :: MonadIO m =>
PursuitQuery -> m Success
findPursuitCompletions (PursuitQuery q) =
PursuitResult <$> liftIO (searchPursuitForDeclarations q)
findPursuitPackages :: MonadIO m =>
PursuitQuery -> m Success
findPursuitPackages (PursuitQuery q) =
PursuitResult <$> liftIO (findPackagesForModuleIdent q)
printModules :: Ide m => m Success
printModules = ModuleList . map P.runModuleName <$> getLoadedModulenames
outputDirectory :: Ide m => m FilePath
outputDirectory = do
outputPath <- confOutputPath . ideConfiguration <$> ask
cwd <- liftIO getCurrentDirectory
pure (cwd </> outputPath)
listAvailableModules :: Ide m => m Success
listAvailableModules = do
oDir <- outputDirectory
liftIO $ do
contents <- getDirectoryContents oDir
let cleaned = filter (`notElem` [".", ".."]) contents
return (ModuleList (map toS cleaned))
caseSplit :: (Ide m, MonadError IdeError m) =>
Text -> Int -> Int -> CS.WildcardAnnotations -> Text -> m Success
caseSplit l b e csa t = do
patterns <- CS.makePattern l b e csa <$> CS.caseSplit t
pure (MultilineTextResult patterns)
-- | Finds all the externs.json files inside the output folder and returns the
-- corresponding Modulenames
findAvailableExterns :: (Ide m, MonadError IdeError m) => m [P.ModuleName]
findAvailableExterns = do
oDir <- outputDirectory
unlessM (liftIO (doesDirectoryExist oDir))
(throwError (GeneralError "Couldn't locate your output directory."))
liftIO $ do
directories <- getDirectoryContents oDir
moduleNames <- filterM (containsExterns oDir) directories
pure (P.moduleNameFromString . toS <$> moduleNames)
where
-- Takes the output directory and a filepath like "Monad.Control.Eff" and
-- looks up, whether that folder contains an externs.json
containsExterns :: FilePath -> FilePath -> IO Bool
containsExterns oDir d
| d `elem` [".", ".."] = pure False
| otherwise = do
let file = oDir </> d </> "externs.json"
doesFileExist file
-- | Finds all matches for the globs specified at the commandline
findAllSourceFiles :: Ide m => m [FilePath]
findAllSourceFiles = do
globs <- confGlobs . ideConfiguration <$> ask
liftIO (concatMapM glob globs)
-- | Looks up the ExternsFiles for the given Modulenames and loads them into the
-- server state. Then proceeds to parse all the specified sourcefiles and
-- inserts their ASTs into the state. Finally kicks off an async worker, which
-- populates Stage 2 and 3 of the state.
loadModulesAsync
:: (Ide m, MonadError IdeError m, MonadLogger m)
=> [P.ModuleName]
-> m Success
loadModulesAsync moduleNames = do
tr <- loadModules moduleNames
-- Finally we kick off the worker with @async@ and return the number of
-- successfully parsed modules.
env <- ask
let ll = confLogLevel (ideConfiguration env)
-- populateStage2 and 3 return Unit for now, so it's fine to discard this
-- result. We might want to block on this in a benchmarking situation.
_ <- liftIO (async (runLogger ll (runReaderT (populateStage2 *> populateStage3) env)))
pure tr
loadModulesSync
:: (Ide m, MonadError IdeError m, MonadLogger m)
=> [P.ModuleName]
-> m Success
loadModulesSync moduleNames = do
tr <- loadModules moduleNames
populateStage2 *> populateStage3
pure tr
loadModules
:: (Ide m, MonadError IdeError m, MonadLogger m)
=> [P.ModuleName]
-> m Success
loadModules moduleNames = do
-- We resolve all the modulenames to externs files and load these into memory.
oDir <- outputDirectory
let efPaths =
map (\mn -> oDir </> toS (P.runModuleName mn) </> "externs.json") moduleNames
efiles <- traverse readExternFile efPaths
traverse_ insertExterns efiles
-- We parse all source files, log eventual parse failures and insert the
-- successful parses into the state.
(failures, allModules) <-
partitionEithers <$> (traverse parseModule =<< findAllSourceFiles)
unless (null failures) $
$(logWarn) ("Failed to parse: " <> show failures)
traverse_ insertModule allModules
pure (TextResult ("Loaded " <> show (length efiles) <> " modules and "
<> show (length allModules) <> " source files."))