Skip to content

Commit e233ad5

Browse files
committed
Use URIs instead of filepaths
1 parent 12587b3 commit e233ad5

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

plugins/default/src/Ide/Plugin/Retrie.hs

+25-15
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import Control.Monad.IO.Class (MonadIO (liftIO))
2525
import Control.Monad.Trans.Class (MonadTrans (lift))
2626
import Control.Monad.Trans.Except (ExceptT (..), runExceptT,
2727
throwE)
28-
import Data.Aeson (ToJSON (toJSON), Value (Null))
29-
import Data.Aeson.Types (FromJSON)
28+
import Data.Aeson (genericParseJSON, FromJSON(..), ToJSON (..), Value (Null))
3029
import Data.Bifunctor (Bifunctor (first), second)
3130
import Data.Coerce
3231
import Data.Either (partitionEithers)
@@ -88,6 +87,7 @@ import StringBuffer (stringToStringBuffer)
8887
import System.Directory (makeAbsolute)
8988
import Control.Monad.Trans.Maybe
9089
import Development.IDE.Core.PositionMapping
90+
import qualified Data.Aeson as Aeson
9191

9292
descriptor :: PluginId -> PluginDescriptor
9393
descriptor plId =
@@ -107,32 +107,41 @@ retrieCommand =
107107
data RunRetrieParams = RunRetrieParams
108108
{ description :: T.Text,
109109
rewrites :: [RewriteSpec],
110-
originatingFile :: String,
110+
originatingFile :: NormalizedUriJSON,
111111
restrictToOriginatingFile :: Bool
112112
}
113113
deriving (Eq, Show, Generic, FromJSON, ToJSON)
114114

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+
115124
runRetrieCmd ::
116125
LspFuncs a ->
117126
IdeState ->
118127
RunRetrieParams ->
119128
IO (Either ResponseError Value, Maybe (ServerMethod, ApplyWorkspaceEditParams))
120-
runRetrieCmd lsp state RunRetrieParams{..} =
129+
runRetrieCmd lsp state RunRetrieParams{originatingFile = NormalizedUriJSON nuri, ..} =
121130
withIndefiniteProgress lsp description Cancellable $ do
122131
res <- runMaybeT $ do
123-
let nfp = toNormalizedFilePath' originatingFile
132+
nfp <- MaybeT $ return $ uriToNormalizedFilePath nuri
124133
(session, _) <- MaybeT $
125134
runAction "Retrie.GhcSessionDeps" state $
126135
useWithStale GhcSessionDeps $
127-
toNormalizedFilePath originatingFile
136+
nfp
128137
(ms, binds, _, _, _) <- MaybeT $ runAction "Retrie.getBinds" state $ getBinds nfp
129138
let importRewrites = concatMap (extractImports ms binds) rewrites
130139
(errors, edits) <- lift $
131140
callRetrie
132141
state
133142
(hscEnv session)
134143
(map Right rewrites <> map Left importRewrites)
135-
(toNormalizedFilePath originatingFile)
144+
nfp
136145
restrictToOriginatingFile
137146
unless (null errors) $
138147
lift $ sendFunc lsp $
@@ -172,20 +181,21 @@ extractImports _ _ _ = []
172181
provider :: CodeActionProvider
173182
provider _a state plId (TextDocumentIdentifier uri) range ca = response $ do
174183
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
177187

178188
(ModSummary{ms_mod}, topLevelBinds, posMapping, hs_ruleds, hs_tyclds)
179189
<- handleMaybeM "typecheck" $ runAction "retrie" state $ getBinds nfp
180190

181191
pos <- handleMaybe "pos" $ _start <$> fromCurrentRange posMapping range
182192
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
185195
++ [ r
186196
| TyClGroup {group_tyclds} <- hs_tyclds,
187197
L l g <- group_tyclds,
188-
r <- suggestTypeRewrites fp ms_mod g,
198+
r <- suggestTypeRewrites nuriJson ms_mod g,
189199
pos `isInsideSrcSpan` l
190200

191201
]
@@ -224,7 +234,7 @@ getBinds nfp = runMaybeT $ do
224234
return (tmrModSummary tm, topLevelBinds, posMapping, hs_ruleds, hs_tyclds)
225235

226236
suggestBindRewrites ::
227-
String ->
237+
NormalizedUriJSON ->
228238
Position ->
229239
GHC.Module ->
230240
HsBindLR GhcRn GhcRn ->
@@ -251,7 +261,7 @@ describeRestriction restrictToOriginatingFile =
251261

252262
suggestTypeRewrites ::
253263
(Outputable (IdP pass)) =>
254-
String ->
264+
NormalizedUriJSON ->
255265
GHC.Module ->
256266
TyClDecl pass ->
257267
[(T.Text, CodeActionKind, RunRetrieParams)]
@@ -270,7 +280,7 @@ suggestTypeRewrites originatingFile ms_mod (SynDecl {tcdLName = L _ rdrName}) =
270280
suggestTypeRewrites _ _ _ = []
271281

272282
suggestRuleRewrites ::
273-
FilePath ->
283+
NormalizedUriJSON ->
274284
Position ->
275285
GHC.Module ->
276286
LRuleDecls pass ->

0 commit comments

Comments
 (0)