Skip to content

Commit 481d152

Browse files
committed
Fix misplaced inlay hints by applying PositionMapping
1 parent cd42bcf commit 481d152

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

plugins/hls-explicit-imports-plugin/src/Ide/Plugin/ExplicitImports.hs

+12-10
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,18 @@ inlayHintProvider _ state _ InlayHintParams {_textDocument = TextDocumentIdentif
218218
-- |^-_paddingLeft
219219
-- ^-_position
220220
generateInlayHints :: Range -> ImportEdit -> PositionMapping -> Maybe InlayHint
221-
generateInlayHints (Range _ end) ie pm = mkLabel ie <&> \label ->
222-
InlayHint { _position = end
223-
, _label = InL label
224-
, _kind = Nothing -- neither a type nor a parameter
225-
, _textEdits = fmap singleton $ toTEdit pm ie
226-
, _tooltip = Just $ InL "Make this import explicit" -- simple enough, no need to resolve
227-
, _paddingLeft = Just True -- show an extra space before the inlay hint
228-
, _paddingRight = Nothing
229-
, _data_ = Nothing
230-
}
221+
generateInlayHints (Range _ end) ie pm = do
222+
label <- mkLabel ie
223+
currentEnd <- toCurrentPosition pm end
224+
return InlayHint { _position = currentEnd
225+
, _label = InL label
226+
, _kind = Nothing -- neither a type nor a parameter
227+
, _textEdits = fmap singleton $ toTEdit pm ie
228+
, _tooltip = Just $ InL "Make this import explicit" -- simple enough, no need to resolve
229+
, _paddingLeft = Just True -- show an extra space before the inlay hint
230+
, _paddingRight = Nothing
231+
, _data_ = Nothing
232+
}
231233
mkLabel :: ImportEdit -> Maybe T.Text
232234
mkLabel (ImportEdit{ieResType, ieText}) =
233235
let title ExplicitImport = Just $ abbreviateImportTitleWithoutModule ieText

plugins/hls-explicit-record-fields-plugin/src/Ide/Plugin/ExplicitFields.hs

+24-23
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ import Development.IDE (IdeState,
4343
srcSpanToLocation,
4444
srcSpanToRange, viaShow)
4545
import Development.IDE.Core.PluginUtils
46-
import Development.IDE.Core.PositionMapping (toCurrentRange)
46+
import Development.IDE.Core.PositionMapping (toCurrentPosition,
47+
toCurrentRange)
4748
import Development.IDE.Core.RuleTypes (TcModuleResult (..),
4849
TypeCheck (..))
4950
import qualified Development.IDE.Core.Shake as Shake
@@ -204,19 +205,19 @@ inlayHintDotdotProvider _ state pId InlayHintParams {_textDocument = TextDocumen
204205
| record <- records
205206
, pos <- maybeToList $ fmap _start $ recordInfoToDotDotRange record ]
206207
defnLocsList <- lift $ sequence locations
207-
pure $ InL $ mapMaybe (mkInlayHint crr pragma) defnLocsList
208+
pure $ InL $ mapMaybe (mkInlayHint crr pragma pm) defnLocsList
208209
where
209-
mkInlayHint :: CollectRecordsResult -> NextPragmaInfo -> (Maybe [(Location, Identifier)], RecordInfo) -> Maybe InlayHint
210-
mkInlayHint CRR {enabledExtensions, nameMap} pragma (defnLocs, record) =
210+
mkInlayHint :: CollectRecordsResult -> NextPragmaInfo -> PositionMapping -> (Maybe [(Location, Identifier)], RecordInfo) -> Maybe InlayHint
211+
mkInlayHint CRR {enabledExtensions, nameMap} pragma pm (defnLocs, record) =
211212
let range = recordInfoToDotDotRange record
212213
textEdits = maybeToList (renderRecordInfoAsTextEdit nameMap record)
213214
<> maybeToList (pragmaEdit enabledExtensions pragma)
214215
names = renderRecordInfoAsDotdotLabelName record
215216
in do
216-
end <- fmap _end range
217+
currentEnd <- range >>= toCurrentPosition pm . _end
217218
names' <- names
218219
defnLocs' <- defnLocs
219-
let excludeDotDot (Location _ (Range _ end')) = end' /= end
220+
let excludeDotDot (Location _ (Range _ end)) = end /= currentEnd
220221
-- find location from dotdot definitions that name equal to label name
221222
findLocation name locations =
222223
let -- filter locations not within dotdot range
@@ -227,7 +228,7 @@ inlayHintDotdotProvider _ state pId InlayHintParams {_textDocument = TextDocumen
227228
valueWithLoc = [ (T.pack $ printName name, findLocation name defnLocs') | name <- names' ]
228229
-- use `, ` to separate labels with definition location
229230
label = intersperse (mkInlayHintLabelPart (", ", Nothing)) $ fmap mkInlayHintLabelPart valueWithLoc
230-
pure $ InlayHint { _position = end -- at the end of dotdot
231+
pure $ InlayHint { _position = currentEnd -- at the end of dotdot
231232
, _label = InR label
232233
, _kind = Nothing -- neither a type nor a parameter
233234
, _textEdits = Just textEdits -- same as CodeAction
@@ -319,11 +320,11 @@ collectNamesRule = defineNoDiagnostics mempty $ \CollectNames nfp -> runMaybeT $
319320
-- | Collects all 'Name's of a given source file, to be used
320321
-- in the variable usage analysis.
321322
getNames :: TcModuleResult -> UniqFM Name [Name]
322-
#if __GLASGOW_HASKELL__ < 910
323+
323324
getNames (tmrRenamed -> (group,_,_,_)) = collectNames group
324-
#else
325-
getNames (tmrRenamed -> (group,_,_,_,_)) = collectNames group
326-
#endif
325+
326+
327+
327328

328329
data CollectRecords = CollectRecords
329330
deriving (Eq, Show, Generic)
@@ -506,11 +507,11 @@ showRecordPatFlds (ConPat _ _ args) = do
506507
where
507508
processRecCon (RecCon flds) = Just $ processRecordFlds flds
508509
processRecCon _ = Nothing
509-
#if __GLASGOW_HASKELL__ < 911
510+
510511
getOccName (FieldOcc x _) = Just $ getName x
511-
#else
512-
getOccName (FieldOcc _ x) = Just $ getName (unLoc x)
513-
#endif
512+
513+
514+
514515
getOccName _ = Nothing
515516
getFieldName = getOccName . unLoc . hfbLHS . unLoc
516517
showRecordPatFlds _ = Nothing
@@ -561,11 +562,11 @@ getRecCons :: LHsExpr GhcTc -> ([RecordInfo], Bool)
561562
-- because there is a possibility that there were be more than one result per
562563
-- branch
563564

564-
#if __GLASGOW_HASKELL__ >= 910
565-
getRecCons (unLoc -> XExpr (ExpandedThingTc a _)) = (collectRecords a, False)
566-
#else
565+
566+
567+
567568
getRecCons (unLoc -> XExpr (ExpansionExpr (HsExpanded _ a))) = (collectRecords a, True)
568-
#endif
569+
569570
getRecCons e@(unLoc -> RecordCon _ _ flds)
570571
| isJust (rec_dotdot flds) = (mkRecInfo e, False)
571572
where
@@ -593,11 +594,11 @@ getRecCons expr@(unLoc -> app@(HsApp _ _ _)) =
593594

594595
getExprFields :: HsExpr GhcTc -> [FieldLabel]
595596
getExprFields (XExpr (ConLikeTc (conLikeFieldLabels -> fls) _ _)) = fls
596-
#if __GLASGOW_HASKELL__ >= 911
597-
getExprFields (XExpr (WrapExpr _ expr)) = getExprFields expr
598-
#else
597+
598+
599+
599600
getExprFields (XExpr (WrapExpr (HsWrap _ expr))) = getExprFields expr
600-
#endif
601+
601602
getExprFields _ = []
602603
getRecCons _ = ([], False)
603604

0 commit comments

Comments
 (0)