Skip to content

Commit 57e9201

Browse files
kritzcreekpaf31
authored andcommitted
Cache last rebuild (purescript#2083)
* Cache the last rebuilt module inside psc-ide state It's cached with its exports opened up, so that we can source completions from private members when we are editing a module. * Adds documentation to the functions inside ...Ide.State and breaks the functions into a pure and a stateful part to minimize time spent in STM * Cleans up json instances for Command to use Applicative style where possible * Don't add a default import for Prim rebuildModule inside ...Make already does that now * only parse a single module when rebuilding Now that multiple modules per file are forbidden, we can simplify the parsing code in Rebuild a bit. * remove the cacheSuccess flag * fix docs
1 parent ca98825 commit 57e9201

File tree

13 files changed

+361
-188
lines changed

13 files changed

+361
-188
lines changed

psc-ide-server/PROTOCOL.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ The `type` command looks up the type for a given identifier.
3838

3939
**Params:**
4040
- `search :: String`: The identifier to look for. Only matches on equality.
41-
- `filters :: [Filter]`: These filters will be applied before looking for the
41+
- `filters :: (optional) [Filter]`: These filters will be applied before looking for the
4242
identifier. These filters get combined with *AND*, so a candidate must match *ALL*
4343
of them to be eligible.
44+
- `currentModule :: (optional) String`: see *Complete* command
4445
```json
4546
{
4647
"command": "type",
4748
"params": {
4849
"search": "filterM",
49-
"filters": [Filter]
50+
"filters": [{..}],
51+
"currentModule": "Main"
5052
}
5153
}
5254
```
@@ -58,20 +60,25 @@ The possible types are returned in the same format as completions
5860
The `complete` command looks up possible completions/corrections.
5961

6062
**Params**:
61-
- `filters :: [Filter]`: The same as for the `type` command. A candidate must match
62-
all filters.
63-
- `matcher :: (optional) Matcher`: The strategy used for matching candidates after filtering.
64-
Results are scored internally and will be returned in the descending order where
65-
the nth element is better then the n+1-th.
66-
67-
If no matcher is given every candidate, that passes the filters, is returned in no
68-
particular order.
63+
- `filters :: [Filter]`: The same as for the `type` command. A candidate must
64+
match all filters.
65+
- `matcher :: (optional) Matcher`: The strategy used for matching candidates
66+
after filtering. Results are scored internally and will be returned in the
67+
descending order where the nth element is better then the n+1-th.
68+
- `currentModule :: (optional) String`: The current modules name. If it matches
69+
with the rebuild cache non-exported modules will also be completed. You can
70+
fill the rebuild cache by using the "Rebuild" command.
71+
72+
If no matcher is given every candidate, that passes the filters, is returned
73+
in no particular order.
74+
6975
```json
7076
{
7177
"command": "complete",
7278
"params": {
73-
"filters": [Filter],
74-
"matcher": (optional) Matcher
79+
"filters": [{..}, {..}],
80+
"matcher": {..}
81+
"currentModule": "Main"
7582
}
7683
}
7784
```
@@ -244,10 +251,11 @@ Example:
244251

245252
The `rebuild` command provides a fast rebuild for a single module. It doesn't
246253
recompile the entire project though. All the modules dependencies need to be
247-
loaded.
254+
loaded. A successful rebuild will be stored to allow for completions of private
255+
identifiers.
248256

249257
Arguments:
250-
- `file :: String` the path to the module to rebuild
258+
- `file :: String` the path to the module to rebuild
251259

252260
```json
253261
{

src/Language/PureScript/Ide.hs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import Data.Maybe (catMaybes, mapMaybe)
3636
import Data.Monoid
3737
import Data.Text (Text)
3838
import qualified Data.Text as T
39+
import qualified Language.PureScript as P
3940
import qualified Language.PureScript.Ide.CaseSplit as CS
4041
import Language.PureScript.Ide.Command
4142
import Language.PureScript.Ide.Completion
@@ -60,10 +61,10 @@ handleCommand :: (PscIde m, MonadLogger m, MonadError PscIdeError m) =>
6061
handleCommand (Load [] []) = loadAllModules
6162
handleCommand (Load modules deps) =
6263
loadModulesAndDeps modules deps
63-
handleCommand (Type search filters) =
64-
findType search filters
65-
handleCommand (Complete filters matcher) =
66-
findCompletions filters matcher
64+
handleCommand (Type search filters currentModule) =
65+
findType search filters currentModule
66+
handleCommand (Complete filters matcher currentModule) =
67+
findCompletions filters matcher currentModule
6768
handleCommand (Pursuit query Package) =
6869
findPursuitPackages query
6970
handleCommand (Pursuit query Identifier) =
@@ -94,14 +95,16 @@ handleCommand Reset = resetPscIdeState *> pure (TextResult "State has been reset
9495
handleCommand Quit = liftIO exitSuccess
9596

9697
findCompletions :: (PscIde m, MonadLogger m) =>
97-
[Filter] -> Matcher -> m Success
98-
findCompletions filters matcher =
99-
CompletionResult . mapMaybe completionFromMatch . getCompletions filters matcher <$> getAllModulesWithReexports
98+
[Filter] -> Matcher -> Maybe P.ModuleName -> m Success
99+
findCompletions filters matcher currentModule = do
100+
modules <- getAllModulesWithReexportsAndCache currentModule
101+
pure . CompletionResult . mapMaybe completionFromMatch . getCompletions filters matcher $ modules
100102

101103
findType :: (PscIde m, MonadLogger m) =>
102-
DeclIdent -> [Filter] -> m Success
103-
findType search filters =
104-
CompletionResult . mapMaybe completionFromMatch . getExactMatches search filters <$> getAllModulesWithReexports
104+
DeclIdent -> [Filter] -> Maybe P.ModuleName -> m Success
105+
findType search filters currentModule = do
106+
modules <- getAllModulesWithReexportsAndCache currentModule
107+
pure . CompletionResult . mapMaybe completionFromMatch . getExactMatches search filters $ modules
105108

106109
findPursuitCompletions :: (MonadIO m, MonadLogger m) =>
107110
PursuitQuery -> m Success
@@ -113,14 +116,14 @@ findPursuitPackages :: (MonadIO m, MonadLogger m) =>
113116
findPursuitPackages (PursuitQuery q) =
114117
PursuitResult <$> liftIO (findPackagesForModuleIdent q)
115118

116-
loadExtern ::(PscIde m, MonadLogger m, MonadError PscIdeError m) =>
119+
loadExtern :: (PscIde m, MonadLogger m, MonadError PscIdeError m) =>
117120
FilePath -> m ()
118121
loadExtern fp = do
119122
m <- readExternFile fp
120123
insertModule m
121124

122125
printModules :: (PscIde m) => m Success
123-
printModules = printModules' <$> getPscIdeState
126+
printModules = printModules' . pscIdeStateModules <$> getPscIdeState
124127

125128
printModules' :: M.Map ModuleIdent [ExternDecl] -> Success
126129
printModules' = ModuleList . M.keys

src/Language/PureScript/Ide/Command.hs

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ import Prelude.Compat
2121

2222
import Control.Monad
2323
import Data.Aeson
24-
import Data.Maybe
2524
import Data.Text (Text)
26-
import Language.PureScript (ModuleName,
27-
moduleNameFromString)
25+
import qualified Language.PureScript as P
2826
import Language.PureScript.Ide.CaseSplit
2927
import Language.PureScript.Ide.Filter
3028
import Language.PureScript.Ide.Matcher
@@ -36,12 +34,14 @@ data Command
3634
, loadDependencies :: [ModuleIdent]
3735
}
3836
| Type
39-
{ typeSearch :: DeclIdent
40-
, typeFilters :: [Filter]
37+
{ typeSearch :: DeclIdent
38+
, typeFilters :: [Filter]
39+
, typeCurrentModule :: Maybe P.ModuleName
4140
}
4241
| Complete
43-
{ completeFilters :: [Filter]
44-
, completeMatcher :: Matcher
42+
{ completeFilters :: [Filter]
43+
, completeMatcher :: Matcher
44+
, completeCurrentModule :: Maybe P.ModuleName
4545
}
4646
| Pursuit
4747
{ pursuitQuery :: PursuitQuery
@@ -67,20 +67,18 @@ data Command
6767
| Quit
6868

6969
data ImportCommand
70-
= AddImplicitImport ModuleName
70+
= AddImplicitImport P.ModuleName
7171
| AddImportForIdentifier DeclIdent
7272
deriving (Show, Eq)
7373

7474
instance FromJSON ImportCommand where
7575
parseJSON = withObject "ImportCommand" $ \o -> do
7676
(command :: String) <- o .: "importCommand"
7777
case command of
78-
"addImplicitImport" -> do
79-
mn <- o .: "module"
80-
pure (AddImplicitImport (moduleNameFromString mn))
81-
"addImport" -> do
82-
ident <- o .: "identifier"
83-
pure (AddImportForIdentifier ident)
78+
"addImplicitImport" ->
79+
AddImplicitImport <$> (P.moduleNameFromString <$> o .: "module")
80+
"addImport" ->
81+
AddImportForIdentifier <$> o .: "identifier"
8482
_ -> mzero
8583

8684
data ListType = LoadedModules | Imports FilePath | AvailableModules
@@ -89,69 +87,69 @@ instance FromJSON ListType where
8987
parseJSON = withObject "ListType" $ \o -> do
9088
(listType' :: String) <- o .: "type"
9189
case listType' of
92-
"import" -> do
93-
fp <- o .: "file"
94-
return (Imports fp)
95-
"loadedModules" -> return LoadedModules
96-
"availableModules" -> return AvailableModules
90+
"import" -> Imports <$> o .: "file"
91+
"loadedModules" -> pure LoadedModules
92+
"availableModules" -> pure AvailableModules
9793
_ -> mzero
9894

9995
instance FromJSON Command where
10096
parseJSON = withObject "command" $ \o -> do
10197
(command :: String) <- o .: "command"
10298
case command of
103-
"list" -> do
104-
listType' <- o .:? "params"
105-
return $ List (fromMaybe LoadedModules listType')
106-
"cwd" -> return Cwd
107-
"quit" -> return Quit
99+
"list" -> List <$> o .:? "params" .!= LoadedModules
100+
"cwd" -> pure Cwd
101+
"quit" -> pure Quit
108102
"reset" -> pure Reset
109-
"load" ->
110-
maybe (pure (Load [] [])) (\params -> do
111-
mods <- params .:? "modules"
112-
deps <- params .:? "dependencies"
113-
pure $ Load (fromMaybe [] mods) (fromMaybe [] deps)) =<< o .:? "params"
103+
"load" -> do
104+
params' <- o .:? "params"
105+
case params' of
106+
Nothing -> pure (Load [] [])
107+
Just params ->
108+
Load
109+
<$> params .:? "modules" .!= []
110+
<*> params .:? "dependencies" .!= []
114111
"type" -> do
115112
params <- o .: "params"
116-
search <- params .: "search"
117-
filters <- params .: "filters"
118-
return $ Type search filters
113+
Type
114+
<$> params .: "search"
115+
<*> params .: "filters"
116+
<*> (fmap P.moduleNameFromString <$> params .:? "currentModule")
119117
"complete" -> do
120118
params <- o .: "params"
121-
filters <- params .:? "filters"
122-
matcher <- params .:? "matcher"
123-
return $ Complete (fromMaybe [] filters) (fromMaybe mempty matcher)
119+
Complete
120+
<$> params .:? "filters" .!= []
121+
<*> params .:? "matcher" .!= mempty
122+
<*> (fmap P.moduleNameFromString <$> params .:? "currentModule")
124123
"pursuit" -> do
125124
params <- o .: "params"
126-
query <- params .: "query"
127-
queryType <- params .: "type"
128-
return $ Pursuit query queryType
125+
Pursuit
126+
<$> params .: "query"
127+
<*> params .: "type"
129128
"caseSplit" -> do
130129
params <- o .: "params"
131-
line <- params .: "line"
132-
begin <- params .: "begin"
133-
end <- params .: "end"
134-
annotations <- params .: "annotations"
135-
type' <- params .: "type"
136-
return $ CaseSplit line begin end (if annotations
137-
then explicitAnnotations
138-
else noAnnotations) type'
130+
CaseSplit
131+
<$> params .: "line"
132+
<*> params .: "begin"
133+
<*> params .: "end"
134+
<*> (mkAnnotations <$> params .: "annotations")
135+
<*> params .: "type"
139136
"addClause" -> do
140137
params <- o .: "params"
141-
line <- params .: "line"
142-
annotations <- params .: "annotations"
143-
return $ AddClause line (if annotations
144-
then explicitAnnotations
145-
else noAnnotations)
138+
AddClause
139+
<$> params .: "line"
140+
<*> (mkAnnotations <$> params .: "annotations")
146141
"import" -> do
147142
params <- o .: "params"
148-
fp <- params .: "file"
149-
out <- params .:? "outfile"
150-
filters <- params .:? "filters"
151-
importCommand <- params .: "importCommand"
152-
pure $ Import fp out (fromMaybe [] filters) importCommand
143+
Import
144+
<$> params .: "file"
145+
<*> params .:? "outfile"
146+
<*> params .:? "filters" .!= []
147+
<*> params .: "importCommand"
153148
"rebuild" -> do
154149
params <- o .: "params"
155-
filePath <- params .: "file"
156-
return $ Rebuild filePath
150+
Rebuild
151+
<$> params .: "file"
157152
_ -> mzero
153+
where
154+
mkAnnotations True = explicitAnnotations
155+
mkAnnotations False = noAnnotations

0 commit comments

Comments
 (0)