@@ -25,8 +25,7 @@ import Control.Monad.IO.Class (MonadIO (liftIO))
25
25
import Control.Monad.Trans.Class (MonadTrans (lift ))
26
26
import Control.Monad.Trans.Except (ExceptT (.. ), runExceptT ,
27
27
throwE )
28
- import Data.Aeson (ToJSON (toJSON ), Value (Null ))
29
- import Data.Aeson.Types (FromJSON )
28
+ import Data.Aeson (genericParseJSON , FromJSON (.. ), ToJSON (.. ), Value (Null ))
30
29
import Data.Bifunctor (Bifunctor (first ), second )
31
30
import Data.Coerce
32
31
import Data.Either (partitionEithers )
@@ -88,6 +87,7 @@ import StringBuffer (stringToStringBuffer)
88
87
import System.Directory (makeAbsolute )
89
88
import Control.Monad.Trans.Maybe
90
89
import Development.IDE.Core.PositionMapping
90
+ import qualified Data.Aeson as Aeson
91
91
92
92
descriptor :: PluginId -> PluginDescriptor
93
93
descriptor plId =
@@ -107,32 +107,41 @@ retrieCommand =
107
107
data RunRetrieParams = RunRetrieParams
108
108
{ description :: T. Text ,
109
109
rewrites :: [RewriteSpec ],
110
- originatingFile :: String ,
110
+ originatingFile :: NormalizedUriJSON ,
111
111
restrictToOriginatingFile :: Bool
112
112
}
113
113
deriving (Eq , Show , Generic , FromJSON , ToJSON )
114
114
115
+ newtype NormalizedUriJSON = NormalizedUriJSON NormalizedUri
116
+ deriving (Eq , Show )
117
+
118
+ instance FromJSON NormalizedUriJSON where
119
+ parseJSON = fmap NormalizedUriJSON . genericParseJSON Aeson. defaultOptions
120
+
121
+ instance ToJSON NormalizedUriJSON where
122
+ toJSON (NormalizedUriJSON x) = Aeson. genericToJSON Aeson. defaultOptions x
123
+
115
124
runRetrieCmd ::
116
125
LspFuncs a ->
117
126
IdeState ->
118
127
RunRetrieParams ->
119
128
IO (Either ResponseError Value , Maybe (ServerMethod , ApplyWorkspaceEditParams ))
120
- runRetrieCmd lsp state RunRetrieParams {.. } =
129
+ runRetrieCmd lsp state RunRetrieParams {originatingFile = NormalizedUriJSON nuri, .. } =
121
130
withIndefiniteProgress lsp description Cancellable $ do
122
131
res <- runMaybeT $ do
123
- let nfp = toNormalizedFilePath' originatingFile
132
+ nfp <- MaybeT $ return $ uriToNormalizedFilePath nuri
124
133
(session, _) <- MaybeT $
125
134
runAction " Retrie.GhcSessionDeps" state $
126
135
useWithStale GhcSessionDeps $
127
- toNormalizedFilePath originatingFile
136
+ nfp
128
137
(ms, binds, _, _, _) <- MaybeT $ runAction " Retrie.getBinds" state $ getBinds nfp
129
138
let importRewrites = concatMap (extractImports ms binds) rewrites
130
139
(errors, edits) <- lift $
131
140
callRetrie
132
141
state
133
142
(hscEnv session)
134
143
(map Right rewrites <> map Left importRewrites)
135
- (toNormalizedFilePath originatingFile)
144
+ nfp
136
145
restrictToOriginatingFile
137
146
unless (null errors) $
138
147
lift $ sendFunc lsp $
@@ -172,20 +181,21 @@ extractImports _ _ _ = []
172
181
provider :: CodeActionProvider
173
182
provider _a state plId (TextDocumentIdentifier uri) range ca = response $ do
174
183
let (J. CodeActionContext _diags _monly) = ca
175
- fp <- handleMaybe " uri" $ uriToFilePath' uri
176
- let nfp = toNormalizedFilePath' fp
184
+ nuri = toNormalizedUri uri
185
+ nuriJson = NormalizedUriJSON nuri
186
+ nfp <- handleMaybe " uri" $ uriToNormalizedFilePath nuri
177
187
178
188
(ModSummary {ms_mod}, topLevelBinds, posMapping, hs_ruleds, hs_tyclds)
179
189
<- handleMaybeM " typecheck" $ runAction " retrie" state $ getBinds nfp
180
190
181
191
pos <- handleMaybe " pos" $ _start <$> fromCurrentRange posMapping range
182
192
let rewrites =
183
- concatMap (suggestBindRewrites fp pos ms_mod) topLevelBinds
184
- ++ concatMap (suggestRuleRewrites fp pos ms_mod) hs_ruleds
193
+ concatMap (suggestBindRewrites nuriJson pos ms_mod) topLevelBinds
194
+ ++ concatMap (suggestRuleRewrites nuriJson pos ms_mod) hs_ruleds
185
195
++ [ r
186
196
| TyClGroup {group_tyclds} <- hs_tyclds,
187
197
L l g <- group_tyclds,
188
- r <- suggestTypeRewrites fp ms_mod g,
198
+ r <- suggestTypeRewrites nuriJson ms_mod g,
189
199
pos `isInsideSrcSpan` l
190
200
191
201
]
@@ -224,7 +234,7 @@ getBinds nfp = runMaybeT $ do
224
234
return (tmrModSummary tm, topLevelBinds, posMapping, hs_ruleds, hs_tyclds)
225
235
226
236
suggestBindRewrites ::
227
- String ->
237
+ NormalizedUriJSON ->
228
238
Position ->
229
239
GHC. Module ->
230
240
HsBindLR GhcRn GhcRn ->
@@ -251,7 +261,7 @@ describeRestriction restrictToOriginatingFile =
251
261
252
262
suggestTypeRewrites ::
253
263
(Outputable (IdP pass )) =>
254
- String ->
264
+ NormalizedUriJSON ->
255
265
GHC. Module ->
256
266
TyClDecl pass ->
257
267
[(T. Text , CodeActionKind , RunRetrieParams )]
@@ -270,7 +280,7 @@ suggestTypeRewrites originatingFile ms_mod (SynDecl {tcdLName = L _ rdrName}) =
270
280
suggestTypeRewrites _ _ _ = []
271
281
272
282
suggestRuleRewrites ::
273
- FilePath ->
283
+ NormalizedUriJSON ->
274
284
Position ->
275
285
GHC. Module ->
276
286
LRuleDecls pass ->
0 commit comments