-
-
Notifications
You must be signed in to change notification settings - Fork 389
Cleanup Development.IDE.CodeAction #3360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
santiweight
merged 6 commits into
haskell:master
from
santiweight:cleanup-plugin-codeaction
Nov 23, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8035c7
refact: extract ImportUtils
df7139c
refact: extract FillTypeWildcard
975ef7a
refact: Extract FillHole
d8a8cda
remove partial hlint warnings
24329ad
Merge branch 'master' into cleanup-plugin-codeaction
santiweight ab08cee
fix import
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
104 changes: 104 additions & 0 deletions
104
plugins/hls-refactor-plugin/src/Development/IDE/Plugin/Plugins/FillHole.hs
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
module Development.IDE.Plugin.Plugins.FillHole | ||
( suggestFillHole | ||
) where | ||
|
||
import Control.Monad (guard) | ||
import Data.Char | ||
import qualified Data.Text as T | ||
import Development.IDE.Plugin.Plugins.Diagnostic | ||
import Language.LSP.Types (Diagnostic (..), | ||
TextEdit (TextEdit)) | ||
import Text.Regex.TDFA (MatchResult (..), | ||
(=~)) | ||
|
||
suggestFillHole :: Diagnostic -> [(T.Text, TextEdit)] | ||
suggestFillHole Diagnostic{_range=_range,..} | ||
| Just holeName <- extractHoleName _message | ||
, (holeFits, refFits) <- processHoleSuggestions (T.lines _message) = | ||
let isInfixHole = _message =~ addBackticks holeName :: Bool in | ||
map (proposeHoleFit holeName False isInfixHole) holeFits | ||
++ map (proposeHoleFit holeName True isInfixHole) refFits | ||
| otherwise = [] | ||
where | ||
extractHoleName = fmap (headOrThrow "impossible") . flip matchRegexUnifySpaces "Found hole: ([^ ]*)" | ||
addBackticks text = "`" <> text <> "`" | ||
addParens text = "(" <> text <> ")" | ||
proposeHoleFit holeName parenthise isInfixHole name = | ||
case T.uncons name of | ||
Nothing -> error "impossible: empty name provided by ghc" | ||
Just (firstChr, _) -> | ||
let isInfixOperator = firstChr == '(' | ||
name' = getOperatorNotation isInfixHole isInfixOperator name in | ||
( "replace " <> holeName <> " with " <> name | ||
, TextEdit _range (if parenthise then addParens name' else name') | ||
) | ||
getOperatorNotation True False name = addBackticks name | ||
getOperatorNotation True True name = T.drop 1 (T.dropEnd 1 name) | ||
getOperatorNotation _isInfixHole _isInfixOperator name = name | ||
headOrThrow msg = \case | ||
[] -> error msg | ||
(x:_) -> x | ||
|
||
processHoleSuggestions :: [T.Text] -> ([T.Text], [T.Text]) | ||
processHoleSuggestions mm = (holeSuggestions, refSuggestions) | ||
{- | ||
• Found hole: _ :: LSP.Handlers | ||
|
||
Valid hole fits include def | ||
Valid refinement hole fits include | ||
fromMaybe (_ :: LSP.Handlers) (_ :: Maybe LSP.Handlers) | ||
fromJust (_ :: Maybe LSP.Handlers) | ||
haskell-lsp-types-0.22.0.0:Language.LSP.Types.Window.$sel:_value:ProgressParams (_ :: ProgressParams | ||
LSP.Handlers) | ||
T.foldl (_ :: LSP.Handlers -> Char -> LSP.Handlers) | ||
(_ :: LSP.Handlers) | ||
(_ :: T.Text) | ||
T.foldl' (_ :: LSP.Handlers -> Char -> LSP.Handlers) | ||
(_ :: LSP.Handlers) | ||
(_ :: T.Text) | ||
-} | ||
where | ||
t = id @T.Text | ||
holeSuggestions = do | ||
-- get the text indented under Valid hole fits | ||
validHolesSection <- | ||
getIndentedGroupsBy (=~ t " *Valid (hole fits|substitutions) include") mm | ||
-- the Valid hole fits line can contain a hole fit | ||
holeFitLine <- | ||
mapHead | ||
(mrAfter . (=~ t " *Valid (hole fits|substitutions) include")) | ||
validHolesSection | ||
let holeFit = T.strip $ T.takeWhile (/= ':') holeFitLine | ||
guard (not $ T.null holeFit) | ||
return holeFit | ||
refSuggestions = do -- @[] | ||
-- get the text indented under Valid refinement hole fits | ||
refinementSection <- | ||
getIndentedGroupsBy (=~ t " *Valid refinement hole fits include") mm | ||
case refinementSection of | ||
[] -> error "GHC provided invalid hole fit options" | ||
(_:refinementSection) -> do | ||
-- get the text for each hole fit | ||
holeFitLines <- getIndentedGroups refinementSection | ||
let holeFit = T.strip $ T.unwords holeFitLines | ||
guard $ not $ holeFit =~ t "Some refinement hole fits suppressed" | ||
return holeFit | ||
|
||
mapHead f (a:aa) = f a : aa | ||
mapHead _ [] = [] | ||
|
||
-- > getIndentedGroups [" H1", " l1", " l2", " H2", " l3"] = [[" H1,", " l1", " l2"], [" H2", " l3"]] | ||
getIndentedGroups :: [T.Text] -> [[T.Text]] | ||
getIndentedGroups [] = [] | ||
getIndentedGroups ll@(l:_) = getIndentedGroupsBy ((== indentation l) . indentation) ll | ||
-- | | ||
-- > getIndentedGroupsBy (" H" `isPrefixOf`) [" H1", " l1", " l2", " H2", " l3"] = [[" H1", " l1", " l2"], [" H2", " l3"]] | ||
getIndentedGroupsBy :: (T.Text -> Bool) -> [T.Text] -> [[T.Text]] | ||
getIndentedGroupsBy pred inp = case dropWhile (not.pred) inp of | ||
(l:ll) -> case span (\l' -> indentation l < indentation l') ll of | ||
(indented, rest) -> (l:indented) : getIndentedGroupsBy pred rest | ||
_ -> [] | ||
|
||
indentation :: T.Text -> Int | ||
indentation = T.length . T.takeWhile isSpace | ||
|
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.
To be clear, these are an improvement in that they give clearer errors, but they will still crash HLS unrecoverably. We really, really want to avoid
error
and friends also. But fixing them properly would require more refactoring.