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 all 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 |
---|---|---|
|
@@ -64,6 +64,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)" | ||
Hie.splitCaseCmd | ||
] | ||
, pluginCodeActionProvider = Just codeActionProvider | ||
, pluginDiagnosticProvider = Nothing | ||
|
@@ -73,29 +76,16 @@ hareDescriptor plId = PluginDescriptor | |
|
||
-- --------------------------------------------------------------------- | ||
|
||
customOptions :: Int -> J.Options | ||
customOptions n = J.defaultOptions { J.fieldLabelModifier = J.camelTo2 '_' . drop n} | ||
|
||
data HarePoint = | ||
HP { hpFile :: Uri | ||
, hpPos :: Position | ||
} deriving (Eq,Generic,Show) | ||
|
||
instance FromJSON HarePoint where | ||
parseJSON = genericParseJSON $ customOptions 2 | ||
instance ToJSON HarePoint where | ||
toJSON = genericToJSON $ customOptions 2 | ||
|
||
data HarePointWithText = | ||
HPT { hptFile :: Uri | ||
, hptPos :: Position | ||
, hptText :: T.Text | ||
} deriving (Eq,Generic,Show) | ||
|
||
instance FromJSON HarePointWithText where | ||
parseJSON = genericParseJSON $ customOptions 3 | ||
parseJSON = genericParseJSON $ Hie.customOptions 3 | ||
instance ToJSON HarePointWithText where | ||
toJSON = genericToJSON $ customOptions 3 | ||
toJSON = genericToJSON $ Hie.customOptions 3 | ||
|
||
data HareRange = | ||
HR { hrFile :: Uri | ||
|
@@ -104,14 +94,14 @@ data HareRange = | |
} deriving (Eq,Generic,Show) | ||
|
||
instance FromJSON HareRange where | ||
parseJSON = genericParseJSON $ customOptions 2 | ||
parseJSON = genericParseJSON $ Hie.customOptions 2 | ||
instance ToJSON HareRange where | ||
toJSON = genericToJSON $ customOptions 2 | ||
toJSON = genericToJSON $ Hie.customOptions 2 | ||
|
||
-- --------------------------------------------------------------------- | ||
|
||
demoteCmd :: CommandFunc HarePoint WorkspaceEdit | ||
demoteCmd = CmdSync $ \_ (HP uri pos) -> | ||
demoteCmd :: CommandFunc Hie.HarePoint WorkspaceEdit | ||
demoteCmd = CmdSync $ \_ (Hie.HP uri pos) -> | ||
demoteCmd' uri pos | ||
|
||
demoteCmd' :: Uri -> Position -> IdeGhcM (IdeResult WorkspaceEdit) | ||
|
@@ -149,8 +139,8 @@ iftocaseCmd' uri (Range startPos endPos) = | |
|
||
-- --------------------------------------------------------------------- | ||
|
||
liftonelevelCmd :: CommandFunc HarePoint WorkspaceEdit | ||
liftonelevelCmd = CmdSync $ \_ (HP uri pos) -> | ||
liftonelevelCmd :: CommandFunc Hie.HarePoint WorkspaceEdit | ||
liftonelevelCmd = CmdSync $ \_ (Hie.HP uri pos) -> | ||
liftonelevelCmd' uri pos | ||
|
||
liftonelevelCmd' :: Uri -> Position -> IdeGhcM (IdeResult WorkspaceEdit) | ||
|
@@ -162,8 +152,8 @@ liftonelevelCmd' uri pos = | |
|
||
-- --------------------------------------------------------------------- | ||
|
||
lifttotoplevelCmd :: CommandFunc HarePoint WorkspaceEdit | ||
lifttotoplevelCmd = CmdSync $ \_ (HP uri pos) -> | ||
lifttotoplevelCmd :: CommandFunc Hie.HarePoint WorkspaceEdit | ||
lifttotoplevelCmd = CmdSync $ \_ (Hie.HP uri pos) -> | ||
lifttotoplevelCmd' uri pos | ||
|
||
lifttotoplevelCmd' :: Uri -> Position -> IdeGhcM (IdeResult WorkspaceEdit) | ||
|
@@ -188,8 +178,8 @@ renameCmd' uri pos name = | |
|
||
-- --------------------------------------------------------------------- | ||
|
||
deleteDefCmd :: CommandFunc HarePoint WorkspaceEdit | ||
deleteDefCmd = CmdSync $ \_ (HP uri pos) -> | ||
deleteDefCmd :: CommandFunc Hie.HarePoint WorkspaceEdit | ||
deleteDefCmd = CmdSync $ \_ (Hie.HP uri pos) -> | ||
deleteDefCmd' uri pos | ||
|
||
deleteDefCmd' :: Uri -> Position -> IdeGhcM (IdeResult WorkspaceEdit) | ||
|
@@ -201,8 +191,8 @@ deleteDefCmd' uri pos = | |
|
||
-- --------------------------------------------------------------------- | ||
|
||
genApplicativeCommand :: CommandFunc HarePoint WorkspaceEdit | ||
genApplicativeCommand = CmdSync $ \_ (HP uri pos) -> | ||
genApplicativeCommand :: CommandFunc Hie.HarePoint WorkspaceEdit | ||
genApplicativeCommand = CmdSync $ \_ (Hie.HP uri pos) -> | ||
genApplicativeCommand' uri pos | ||
|
||
genApplicativeCommand' :: Uri -> Position -> IdeGhcM (IdeResult WorkspaceEdit) | ||
|
@@ -294,42 +284,48 @@ 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 | ||
let args = [J.toJSON $ HP (docId ^. J.uri) pos] | ||
let args = [J.toJSON $ Hie.HP (docId ^. J.uri) pos] | ||
title = "Lift " <> name <> " one level" | ||
liftCmd <- mkLspCommand pId "liftonelevel" title (Just args) | ||
return $ J.CodeAction title (Just J.CodeActionRefactorExtract) mempty Nothing (Just liftCmd) | ||
|
||
mkLiftTopAction name = do | ||
let args = [J.toJSON $ HP (docId ^. J.uri) pos] | ||
let args = [J.toJSON $ Hie.HP (docId ^. J.uri) pos] | ||
title = "Lift " <> name <> " to top level" | ||
liftCmd <- mkLspCommand pId "lifttotoplevel" title (Just args) | ||
return $ J.CodeAction title (Just J.CodeActionRefactorExtract) mempty Nothing (Just liftCmd) | ||
|
||
mkDemoteAction name = do | ||
let args = [J.toJSON $ HP (docId ^. J.uri) pos] | ||
let args = [J.toJSON $ Hie.HP (docId ^. J.uri) pos] | ||
title = "Demote " <> name <> " one level" | ||
demCmd <- mkLspCommand pId "demote" title (Just args) | ||
return $ J.CodeAction title (Just J.CodeActionRefactorInline) mempty Nothing (Just demCmd) | ||
|
||
mkDeleteAction name = do | ||
let args = [J.toJSON $ HP (docId ^. J.uri) pos] | ||
let args = [J.toJSON $ Hie.HP (docId ^. J.uri) pos] | ||
title = "Delete definition of " <> name | ||
delCmd <- mkLspCommand pId "deletedef" title (Just args) | ||
return $ J.CodeAction title (Just J.CodeActionRefactor) mempty Nothing (Just delCmd) | ||
|
@@ -339,3 +335,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 $ Hie.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) |
Oops, something went wrong.
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