Skip to content

Commit 57dccc2

Browse files
committed
Extracted textInRange function to Util module
1 parent 71a6e9c commit 57dccc2

File tree

1 file changed

+8
-25
lines changed
  • plugins/hls-refactor-plugin/src/Development/IDE/Plugin

1 file changed

+8
-25
lines changed

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

+8-25
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ import Development.IDE.GHC.Error
5252
import Development.IDE.GHC.ExactPrint
5353
import qualified Development.IDE.GHC.ExactPrint as E
5454
import Development.IDE.GHC.Util (printOutputable,
55-
printRdrName)
55+
printRdrName,
56+
textInRange)
5657
import Development.IDE.Plugin.CodeAction.Args
5758
import Development.IDE.Plugin.CodeAction.ExactPrint
5859
import Development.IDE.Plugin.CodeAction.PositionIndexed
@@ -330,8 +331,8 @@ findSigOfBinds range = go
330331
findSigOfBind range (unLoc lHsBindLR)
331332
go _ = Nothing
332333

333-
findInstanceHead :: (Outputable (HsType p), p ~ GhcPass p0) => DynFlags -> Range -> [LHsDecl p] -> Maybe (LHsType p)
334-
findInstanceHead df diagnosticLocation decls =
334+
findInstanceHead :: (p ~ GhcPass p0) => Range -> [LHsDecl p] -> Maybe (LHsType p)
335+
findInstanceHead diagnosticLocation decls =
335336
listToMaybe
336337
[ hsib_body
337338
| L (locA -> instanceLocation) (InstD _ (ClsInstD _ ClsInstDecl {cid_poly_ty = (unLoc -> HsSig {sig_body = hsib_body})})) <- decls
@@ -1215,7 +1216,7 @@ suggestConstraint df (makeDeltaAst -> parsedModule) diag@Diagnostic {..}
12151216
| Just missingConstraint <- findMissingConstraint _message
12161217
= let codeAction = if _message =~ ("the type signature for:" :: String)
12171218
then suggestFunctionConstraint df parsedModule
1218-
else suggestInstanceConstraint df parsedModule
1219+
else suggestInstanceConstraint parsedModule
12191220
in codeAction diag missingConstraint
12201221
| otherwise = []
12211222
where
@@ -1237,9 +1238,9 @@ suggestConstraint df (makeDeltaAst -> parsedModule) diag@Diagnostic {..}
12371238
in getCorrectGroup <$> match
12381239

12391240
-- | Suggests a constraint for an instance declaration for which a constraint is missing.
1240-
suggestInstanceConstraint :: DynFlags -> ParsedSource -> Diagnostic -> T.Text -> [(T.Text, Rewrite)]
1241+
suggestInstanceConstraint :: ParsedSource -> Diagnostic -> T.Text -> [(T.Text, Rewrite)]
12411242

1242-
suggestInstanceConstraint df (L _ HsModule {hsmodDecls}) Diagnostic {..} missingConstraint
1243+
suggestInstanceConstraint (L _ HsModule {hsmodDecls}) Diagnostic {..} missingConstraint
12431244
| Just instHead <- instanceHead
12441245
= [(actionTitle missingConstraint , appendConstraint (T.unpack missingConstraint) instHead)]
12451246
| otherwise = []
@@ -1251,7 +1252,7 @@ suggestInstanceConstraint df (L _ HsModule {hsmodDecls}) Diagnostic {..} missing
12511252
-- • In the expression: x == y
12521253
-- In an equation for ‘==’: (Wrap x) == (Wrap y) = x == y
12531254
-- In the instance declaration for ‘Eq (Wrap a)’
1254-
| Just instHead <- findInstanceHead df _range hsmodDecls
1255+
| Just instHead <- findInstanceHead _range hsmodDecls
12551256
= Just instHead
12561257
-- Suggests a constraint for an instance declaration with one or more existing constraints.
12571258
-- • Could not deduce (Eq b) arising from a use of ‘==’
@@ -1884,24 +1885,6 @@ splitTextAtPosition (Position (fromIntegral -> row) (fromIntegral -> col)) x
18841885
= (T.intercalate "\n" $ preRow ++ [preCol], T.intercalate "\n" $ postCol : postRow)
18851886
| otherwise = (x, T.empty)
18861887

1887-
-- | Returns [start .. end[
1888-
textInRange :: Range -> T.Text -> T.Text
1889-
textInRange (Range (Position (fromIntegral -> startRow) (fromIntegral -> startCol)) (Position (fromIntegral -> endRow) (fromIntegral -> endCol))) text =
1890-
case compare startRow endRow of
1891-
LT ->
1892-
let (linesInRangeBeforeEndLine, endLineAndFurtherLines) = splitAt (endRow - startRow) linesBeginningWithStartLine
1893-
(textInRangeInFirstLine, linesBetween) = case linesInRangeBeforeEndLine of
1894-
[] -> ("", [])
1895-
firstLine:linesInBetween -> (T.drop startCol firstLine, linesInBetween)
1896-
maybeTextInRangeInEndLine = T.take endCol <$> listToMaybe endLineAndFurtherLines
1897-
in T.intercalate "\n" (textInRangeInFirstLine : linesBetween ++ maybeToList maybeTextInRangeInEndLine)
1898-
EQ ->
1899-
let line = fromMaybe "" (listToMaybe linesBeginningWithStartLine)
1900-
in T.take (endCol - startCol) (T.drop startCol line)
1901-
GT -> ""
1902-
where
1903-
linesBeginningWithStartLine = drop startRow (T.splitOn "\n" text)
1904-
19051888
-- | Returns the ranges for a binding in an import declaration
19061889
rangesForBindingImport :: ImportDecl GhcPs -> String -> [Range]
19071890
#if MIN_VERSION_ghc(9,5,0)

0 commit comments

Comments
 (0)