This repository was archived by the owner on Oct 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 206
Make casesplit a HaRe action #917
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
module Haskell.Ide.Engine.Plugin.HaRe where | ||
|
||
import Control.Lens.Operators | ||
import Control.Lens.Setter ((%~)) | ||
import Control.Lens.Traversal (traverseOf) | ||
import Control.Monad.State | ||
import Control.Monad.Trans.Control | ||
import Data.Aeson | ||
|
@@ -22,12 +24,14 @@ import qualified Data.Text.IO as T | |
import Exception | ||
import GHC.Generics (Generic) | ||
import qualified GhcMod.Error as GM | ||
import qualified GhcMod.Exe.CaseSplit as GM | ||
import qualified GhcMod.Monad as GM | ||
import qualified GhcMod.Utils as GM | ||
import Haskell.Ide.Engine.ArtifactMap | ||
import Haskell.Ide.Engine.MonadFunctions | ||
import Haskell.Ide.Engine.MonadTypes | ||
import Haskell.Ide.Engine.PluginUtils | ||
import Haskell.Ide.Engine.Plugin.GhcMod (runGhcModCommand) | ||
import qualified Haskell.Ide.Engine.Plugin.HieExtras as Hie | ||
import Language.Haskell.GHC.ExactPrint.Print | ||
import qualified Language.Haskell.LSP.Core as Core | ||
|
@@ -64,6 +68,9 @@ hareDescriptor plId = PluginDescriptor | |
deleteDefCmd | ||
, PluginCommand "genapplicative" "Generalise a monadic function to use applicative" | ||
genApplicativeCommand | ||
|
||
, PluginCommand "casesplit" "Generate a pattern match for a binding under (LINE,COL)" | ||
splitCaseCmd | ||
] | ||
, pluginCodeActionProvider = Just codeActionProvider | ||
, pluginDiagnosticProvider = Nothing | ||
|
@@ -213,6 +220,64 @@ genApplicativeCommand' uri pos = | |
|
||
-- --------------------------------------------------------------------- | ||
|
||
splitCaseCmd :: CommandFunc HarePoint WorkspaceEdit | ||
splitCaseCmd = CmdSync $ \_ (HP uri pos) -> splitCaseCmd' uri pos | ||
|
||
splitCaseCmd' :: Uri -> Position -> IdeGhcM (IdeResult WorkspaceEdit) | ||
splitCaseCmd' uri newPos = | ||
pluginGetFile "splitCaseCmd: " uri $ \path -> do | ||
origText <- GM.withMappedFile path $ liftIO . T.readFile | ||
ifCachedModule path (IdeResultOk mempty) $ \tm info -> runGhcModCommand $ | ||
case newPosToOld info newPos of | ||
Just oldPos -> do | ||
let (line, column) = unPos oldPos | ||
splitResult' <- GM.splits' path tm line column | ||
case splitResult' of | ||
Just splitResult -> do | ||
wEdit <- liftToGhc $ splitResultToWorkspaceEdit origText splitResult | ||
return $ oldToNewPositions info wEdit | ||
Nothing -> return mempty | ||
Nothing -> return mempty | ||
where | ||
|
||
-- | Transform all ranges in a WorkspaceEdit from old to new positions. | ||
oldToNewPositions :: CachedInfo -> WorkspaceEdit -> WorkspaceEdit | ||
oldToNewPositions info wsEdit = | ||
wsEdit | ||
& J.documentChanges %~ (>>= traverseOf (traverse . J.edits . traverse . J.range) (oldRangeToNew info)) | ||
& J.changes %~ (>>= traverseOf (traverse . traverse . J.range) (oldRangeToNew info)) | ||
|
||
-- | Given the range and text to replace, construct a 'WorkspaceEdit' | ||
-- by diffing the change against the current text. | ||
splitResultToWorkspaceEdit :: T.Text -> GM.SplitResult -> IdeM WorkspaceEdit | ||
splitResultToWorkspaceEdit originalText (GM.SplitResult replaceFromLine replaceFromCol replaceToLine replaceToCol replaceWith) = | ||
diffText (uri, originalText) newText IncludeDeletions | ||
where | ||
before = takeUntil (toPos (replaceFromLine, replaceFromCol)) originalText | ||
after = dropUntil (toPos (replaceToLine, replaceToCol)) originalText | ||
newText = before <> replaceWith <> after | ||
|
||
-- | Take the first part of text until the given position. | ||
-- Returns all characters before the position. | ||
takeUntil :: Position -> T.Text -> T.Text | ||
takeUntil (Position l c) txt = | ||
T.unlines takeLines <> takeCharacters | ||
where | ||
textLines = T.lines txt | ||
takeLines = take l textLines | ||
takeCharacters = T.take c (textLines !! c) | ||
|
||
-- | Drop the first part of text until the given position. | ||
-- Returns all characters after and including the position. | ||
dropUntil :: Position -> T.Text -> T.Text | ||
dropUntil (Position l c) txt = dropCharacters | ||
where | ||
textLines = T.lines txt | ||
dropLines = drop l textLines | ||
dropCharacters = T.drop c (T.unlines dropLines) | ||
|
||
-- --------------------------------------------------------------------- | ||
|
||
getRefactorResult :: [ApplyRefacResult] -> [(FilePath,T.Text)] | ||
getRefactorResult = map getNewFile . filter fileModified | ||
where fileModified ((_,m),_) = m == RefacModified | ||
|
@@ -294,20 +359,26 @@ hoist f a = | |
codeActionProvider :: CodeActionProvider | ||
codeActionProvider pId docId _ _ (J.Range pos _) _ = | ||
pluginGetFile "HaRe codeActionProvider: " (docId ^. J.uri) $ \file -> | ||
ifCachedInfo file (IdeResultOk mempty) $ \info -> do | ||
let symbols = getArtifactsAtPos pos (defMap info) | ||
debugm $ show $ map (Hie.showName . snd) symbols | ||
if not (null symbols) | ||
then | ||
let name = Hie.showName $ snd $ head symbols | ||
in IdeResultOk <$> sequence [ | ||
ifCachedInfo file (IdeResultOk mempty) $ \info -> | ||
case getArtifactsAtPos pos (defMap info) of | ||
[h] -> do | ||
let name = Hie.showName $ snd h | ||
debugm $ show name | ||
IdeResultOk <$> sequence [ | ||
mkLiftOneAction name | ||
, mkLiftTopAction name | ||
, mkDemoteAction name | ||
, mkDeleteAction name | ||
, mkDuplicateAction name | ||
] | ||
else return (IdeResultOk []) | ||
_ -> case getArtifactsAtPos pos (locMap info) of | ||
[h] -> do | ||
let name = Hie.showName $ snd h | ||
debugm $ show name | ||
IdeResultOk <$> sequence [ | ||
mkCaseSplitAction name | ||
] | ||
_ -> return $ IdeResultOk [] | ||
|
||
where | ||
mkLiftOneAction name = do | ||
|
@@ -339,3 +410,9 @@ codeActionProvider pId docId _ _ (J.Range pos _) _ = | |
title = "Duplicate definition of " <> name | ||
dupCmd <- mkLspCommand pId "dupdef" title (Just args) | ||
return $ J.CodeAction title (Just J.CodeActionRefactor) mempty Nothing (Just dupCmd) | ||
|
||
mkCaseSplitAction name = do | ||
let args = [J.toJSON $ HP (docId ^. J.uri) pos] | ||
title = "Case split on " <> name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @alanz This generates the command. It works on neovim. |
||
splCmd <- mkLspCommand pId "casesplit" title (Just args) | ||
return $ J.CodeAction title (Just J.CodeActionRefactorRewrite) mempty Nothing (Just splCmd) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The command generation stuff LGTM