Skip to content

Commit b67871e

Browse files
committed
Search infix hole directly in file text
1 parent ac748c4 commit b67871e

File tree

2 files changed

+27
-4
lines changed
  • ghcide/src/Development/IDE/GHC
  • plugins/hls-refactor-plugin/src/Development/IDE/Plugin/Plugins

2 files changed

+27
-4
lines changed

ghcide/src/Development/IDE/GHC/Util.hs

+20-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module Development.IDE.GHC.Util(
2727
dontWriteHieFiles,
2828
disableWarningsAsErrors,
2929
printOutputable,
30-
getExtensions
30+
getExtensions,
31+
textInRange
3132
) where
3233

3334
import Control.Concurrent
@@ -272,3 +273,21 @@ printOutputable =
272273

273274
getExtensions :: ParsedModule -> [Extension]
274275
getExtensions = toList . extensionFlags . ms_hspp_opts . pm_mod_summary
276+
277+
-- | Returns [start .. end[
278+
textInRange :: Range -> T.Text -> T.Text
279+
textInRange (Range (Position (fromIntegral -> startRow) (fromIntegral -> startCol)) (Position (fromIntegral -> endRow) (fromIntegral -> endCol))) text =
280+
case compare startRow endRow of
281+
LT ->
282+
let (linesInRangeBeforeEndLine, endLineAndFurtherLines) = splitAt (endRow - startRow) linesBeginningWithStartLine
283+
(textInRangeInFirstLine, linesBetween) = case linesInRangeBeforeEndLine of
284+
[] -> ("", [])
285+
firstLine:linesInBetween -> (T.drop startCol firstLine, linesInBetween)
286+
maybeTextInRangeInEndLine = T.take endCol <$> listToMaybe endLineAndFurtherLines
287+
in T.intercalate "\n" (textInRangeInFirstLine : linesBetween ++ maybeToList maybeTextInRangeInEndLine)
288+
EQ ->
289+
let line = fromMaybe "" (listToMaybe linesBeginningWithStartLine)
290+
in T.take (endCol - startCol) (T.drop startCol line)
291+
GT -> ""
292+
where
293+
linesBeginningWithStartLine = drop startRow (T.splitOn "\n" text)

plugins/hls-refactor-plugin/src/Development/IDE/Plugin/Plugins/FillHole.hs

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@ module Development.IDE.Plugin.Plugins.FillHole
55
import Control.Monad (guard)
66
import Data.Char
77
import qualified Data.Text as T
8+
import Development.IDE.GHC.Util (textInRange)
89
import Development.IDE.Plugin.Plugins.Diagnostic
910
import Language.LSP.Protocol.Types (Diagnostic (..),
1011
TextEdit (TextEdit))
1112
import Text.Regex.TDFA (MatchResult (..),
1213
(=~))
1314

14-
suggestFillHole :: Diagnostic -> [(T.Text, TextEdit)]
15-
suggestFillHole Diagnostic{_range=_range,..}
15+
suggestFillHole :: Maybe T.Text -> Diagnostic -> [(T.Text, TextEdit)]
16+
suggestFillHole contents Diagnostic{_range=_range,..}
1617
| Just holeName <- extractHoleName _message
1718
, (holeFits, refFits) <- processHoleSuggestions (T.lines _message) =
18-
let isInfixHole = _message =~ addBackticks holeName :: Bool in
19+
let isInfixHole = textInDiagnosticRange =~ addBackticks holeName :: Bool in
1920
map (proposeHoleFit holeName False isInfixHole) holeFits
2021
++ map (proposeHoleFit holeName True isInfixHole) refFits
2122
| otherwise = []
2223
where
2324
extractHoleName = fmap (headOrThrow "impossible") . flip matchRegexUnifySpaces "Found hole: ([^ ]*)"
25+
textInDiagnosticRange = case contents of
26+
Nothing -> ""
27+
Just text -> textInRange _range text
2428
addBackticks text = "`" <> text <> "`"
2529
addParens text = "(" <> text <> ")"
2630
proposeHoleFit holeName parenthise isInfixHole name =

0 commit comments

Comments
 (0)