Skip to content

Commit 6d58935

Browse files
committed
Fix misplaced inlay hints by applying PositionMapping
1 parent cd42bcf commit 6d58935

File tree

2 files changed

+32
-25
lines changed

2 files changed

+32
-25
lines changed

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

Lines changed: 12 additions & 10 deletions
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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ 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 (PositionMapping,
47+
toCurrentPosition,
48+
toCurrentRange)
4749
import Development.IDE.Core.RuleTypes (TcModuleResult (..),
4850
TypeCheck (..))
4951
import qualified Development.IDE.Core.Shake as Shake
@@ -204,19 +206,19 @@ inlayHintDotdotProvider _ state pId InlayHintParams {_textDocument = TextDocumen
204206
| record <- records
205207
, pos <- maybeToList $ fmap _start $ recordInfoToDotDotRange record ]
206208
defnLocsList <- lift $ sequence locations
207-
pure $ InL $ mapMaybe (mkInlayHint crr pragma) defnLocsList
209+
pure $ InL $ mapMaybe (mkInlayHint crr pragma pm) defnLocsList
208210
where
209-
mkInlayHint :: CollectRecordsResult -> NextPragmaInfo -> (Maybe [(Location, Identifier)], RecordInfo) -> Maybe InlayHint
210-
mkInlayHint CRR {enabledExtensions, nameMap} pragma (defnLocs, record) =
211+
mkInlayHint :: CollectRecordsResult -> NextPragmaInfo -> PositionMapping -> (Maybe [(Location, Identifier)], RecordInfo) -> Maybe InlayHint
212+
mkInlayHint CRR {enabledExtensions, nameMap} pragma pm (defnLocs, record) =
211213
let range = recordInfoToDotDotRange record
212214
textEdits = maybeToList (renderRecordInfoAsTextEdit nameMap record)
213215
<> maybeToList (pragmaEdit enabledExtensions pragma)
214216
names = renderRecordInfoAsDotdotLabelName record
215217
in do
216-
end <- fmap _end range
218+
currentEnd <- range >>= toCurrentPosition pm . _end
217219
names' <- names
218220
defnLocs' <- defnLocs
219-
let excludeDotDot (Location _ (Range _ end')) = end' /= end
221+
let excludeDotDot (Location _ (Range _ end)) = end /= currentEnd
220222
-- find location from dotdot definitions that name equal to label name
221223
findLocation name locations =
222224
let -- filter locations not within dotdot range
@@ -227,7 +229,7 @@ inlayHintDotdotProvider _ state pId InlayHintParams {_textDocument = TextDocumen
227229
valueWithLoc = [ (T.pack $ printName name, findLocation name defnLocs') | name <- names' ]
228230
-- use `, ` to separate labels with definition location
229231
label = intersperse (mkInlayHintLabelPart (", ", Nothing)) $ fmap mkInlayHintLabelPart valueWithLoc
230-
pure $ InlayHint { _position = end -- at the end of dotdot
232+
pure $ InlayHint { _position = currentEnd -- at the end of dotdot
231233
, _label = InR label
232234
, _kind = Nothing -- neither a type nor a parameter
233235
, _textEdits = Just textEdits -- same as CodeAction
@@ -248,20 +250,22 @@ inlayHintPosRecProvider _ state _pId InlayHintParams {_textDocument = TextDocume
248250
| Just range <- [toCurrentRange pm visibleRange]
249251
, uid <- RangeMap.elementsInRange range crCodeActions
250252
, Just record <- [IntMap.lookup uid crCodeActionResolve] ]
251-
pure $ InL (concatMap (mkInlayHints nameMap) records)
253+
pure $ InL (concatMap (mkInlayHints nameMap pm) records)
252254
where
253-
mkInlayHints :: UniqFM Name [Name] -> RecordInfo -> [InlayHint]
254-
mkInlayHints nameMap record@(RecordInfoApp _ (RecordAppExpr _ fla)) =
255+
mkInlayHints :: UniqFM Name [Name] -> PositionMapping -> RecordInfo -> [InlayHint]
256+
mkInlayHints nameMap pm record@(RecordInfoApp _ (RecordAppExpr _ fla)) =
255257
let textEdits = renderRecordInfoAsTextEdit nameMap record
256-
in mapMaybe (mkInlayHint textEdits) fla
257-
mkInlayHints _ _ = []
258-
mkInlayHint :: Maybe TextEdit -> (Located FieldLabel, HsExpr GhcTc) -> Maybe InlayHint
259-
mkInlayHint te (label, _) =
258+
in mapMaybe (mkInlayHint textEdits pm) fla
259+
mkInlayHints _ _ _ = []
260+
261+
mkInlayHint :: Maybe TextEdit -> PositionMapping -> (Located FieldLabel, HsExpr GhcTc) -> Maybe InlayHint
262+
mkInlayHint te pm (label, _) =
260263
let (name, loc) = ((flSelector . unLoc) &&& (srcSpanToLocation . getLoc)) label
261264
fieldDefLoc = srcSpanToLocation (nameSrcSpan name)
262265
in do
263266
(Location _ recRange) <- loc
264-
pure InlayHint { _position = _start recRange
267+
currentStart <- toCurrentPosition pm (_start recRange)
268+
pure InlayHint { _position = currentStart
265269
, _label = InR $ pure (mkInlayHintLabelPart name fieldDefLoc)
266270
, _kind = Nothing -- neither a type nor a parameter
267271
, _textEdits = Just (maybeToList te) -- same as CodeAction
@@ -270,6 +274,7 @@ inlayHintPosRecProvider _ state _pId InlayHintParams {_textDocument = TextDocume
270274
, _paddingRight = Nothing
271275
, _data_ = Nothing
272276
}
277+
273278
mkInlayHintLabelPart name loc = InlayHintLabelPart (printOutputable (pprNameUnqualified name) <> "=") Nothing loc Nothing
274279

275280
mkTitle :: [Extension] -> Text

0 commit comments

Comments
 (0)