Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

Commit dd9cbdd

Browse files
authored
Fix makeRefactorResult
1 parent db18602 commit dd9cbdd

File tree

1 file changed

+24
-19
lines changed
  • src/Haskell/Ide/Engine/Plugin

1 file changed

+24
-19
lines changed

src/Haskell/Ide/Engine/Plugin/HaRe.hs

+24-19
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import Data.Foldable
1818
import Data.Monoid
1919
#endif
2020
import qualified Data.Text as T
21-
import qualified Data.Text.IO as T
2221
import Exception
2322
import GHC.Generics (Generic)
2423
import Haskell.Ide.Engine.ArtifactMap
@@ -206,27 +205,34 @@ getRefactorResult = map getNewFile . filter fileModified
206205
where fileModified ((_,m),_) = m == RefacModified
207206
getNewFile ((file,_),(ann, parsed)) = (file, T.pack $ exactPrint parsed ann)
208207

209-
makeRefactorResult :: [(FilePath,T.Text)] -> IdeGhcM WorkspaceEdit
208+
makeRefactorResult :: [(FilePath,T.Text)] -> IdeGhcM (IdeResult WorkspaceEdit)
210209
makeRefactorResult changedFiles = do
211210
let
212-
diffOne :: (FilePath, T.Text) -> IdeGhcM WorkspaceEdit
211+
diffOne :: (FilePath, T.Text) -> IdeGhcM (IdeResult WorkspaceEdit)
213212
diffOne (fp, newText) = do
214213
uri <- canonicalizeUri $ filePathToUri fp
215214
mvf <- getVirtualFile uri
216-
let resultFail = return $ IdeResultFail
217-
(IdeError PluginError
218-
(T.pack "makeRefactorResult: no access to the persisted file.")
219-
Null
220-
)
221-
origText <- case mvf of
222-
Nothing -> withMappedFile fp resultFail $ liftIO . T.readFile
223-
Just vf -> pure (Rope.toText $ _text vf)
224-
-- TODO: remove this logging once we are sure we have a working solution
225-
logm $ "makeRefactorResult:groupedDiff = " ++ show (getGroupedDiff (lines $ T.unpack origText) (lines $ T.unpack newText))
226-
logm $ "makeRefactorResult:diffops = " ++ show (diffToLineRanges $ getGroupedDiff (lines $ T.unpack origText) (lines $ T.unpack newText))
227-
liftToGhc $ diffText (filePathToUri fp, origText) newText IncludeDeletions
228-
diffs <- mapM diffOne changedFiles
229-
return $ Core.reverseSortEdit $ fold diffs
215+
216+
case mvf of
217+
Nothing ->
218+
-- if there is no virtual file, dont try to persist it!
219+
return $ IdeResultFail
220+
(IdeError PluginError
221+
(T.pack "makeRefactorResult: no access to the persisted file.")
222+
Null
223+
)
224+
Just vf -> do
225+
let origText = Rope.toText $ _text vf
226+
-- TODO: remove this logging once we are sure we have a working solution
227+
logm $ "makeRefactorResult:groupedDiff = " ++ show (getGroupedDiff (lines $ T.unpack origText) (lines $ T.unpack newText))
228+
logm $ "makeRefactorResult:diffops = " ++ show (diffToLineRanges $ getGroupedDiff (lines $ T.unpack origText) (lines $ T.unpack newText))
229+
liftToGhc $ IdeResultOk <$> diffText (filePathToUri fp, origText) newText IncludeDeletions
230+
231+
diffResults <- mapM diffOne changedFiles
232+
let diffs = sequenceA diffResults
233+
case diffs of
234+
IdeResultOk diffs' -> return $ IdeResultOk $ Core.reverseSortEdit $ fold diffs'
235+
IdeResultFail err -> return $ IdeResultFail err
230236

231237
-- ---------------------------------------------------------------------
232238

@@ -242,8 +248,7 @@ runHareCommand name cmd = do
242248
Null))
243249
Right res -> do
244250
let changes = getRefactorResult res
245-
refactRes <- makeRefactorResult changes
246-
pure (IdeResultOk refactRes)
251+
makeRefactorResult changes
247252

248253
-- ---------------------------------------------------------------------
249254

0 commit comments

Comments
 (0)