Skip to content

Commit c0bcf57

Browse files
committed
Nested Positional Constructor + Tests
1 parent aa8819a commit c0bcf57

File tree

4 files changed

+121
-4
lines changed

4 files changed

+121
-4
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,12 @@ inlayHintPosRecProvider _ state _pId InlayHintParams {_textDocument = TextDocume
269269
pure $ InL (concatMap (mkInlayHints nameMap pm) records)
270270
where
271271
mkInlayHints :: UniqFM Name [Name] -> PositionMapping -> RecordInfo -> [InlayHint]
272-
mkInlayHints nameMap pm record@(RecordInfoApp _ (RecordAppExpr _ _ fla)) =
273-
let textEdits = renderRecordInfoAsTextEdit nameMap record
274-
in mapMaybe (mkInlayHint textEdits pm) fla
272+
mkInlayHints nameMap pm record@(RecordInfoApp _ (RecordAppExpr sat _ fla)) =
273+
-- Only create inlay hints for fully saturated constructors
274+
case sat of
275+
Saturated -> let textEdits = renderRecordInfoAsTextEdit nameMap record
276+
in mapMaybe (mkInlayHint textEdits pm) fla
277+
Unsaturated -> []
275278
mkInlayHints _ _ _ = []
276279

277280
mkInlayHint :: Maybe TextEdit -> PositionMapping -> (Located FieldLabel, HsExpr GhcTc) -> Maybe InlayHint
@@ -612,7 +615,11 @@ getRecCons e@(unLoc -> RecordCon _ _ flds)
612615
getRecCons expr@(unLoc -> app@(HsApp _ _ _)) =
613616
let fieldss = maybeToList $ getFields app []
614617
recInfo = concatMap mkRecInfo fieldss
615-
in (recInfo, not (null recInfo))
618+
-- Search control for positional constructors.
619+
-- True stops further (nested) searching; False allows recursive search.
620+
-- Currently hardcoded to False to enable nested positional searches.
621+
-- Use `in (recInfo, not (null recInfo))` to disable nested searching.
622+
in (recInfo, False)
616623
where
617624
mkRecInfo :: RecordAppExpr -> [RecordInfo]
618625
mkRecInfo appExpr =

plugins/hls-explicit-record-fields-plugin/test/Main.hs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,89 @@ test = testGroup "explicit-fields"
292292
, _paddingLeft = Nothing
293293
}
294294
]
295+
, mkInlayHintsTest "CursorRecords" Nothing 9 $ \ih -> do
296+
let mkLabelPart' = mkLabelPartOffsetLength "CursorRecords"
297+
a0 <- mkLabelPart' 3 14 "a0"
298+
a1 <- mkLabelPart' 4 14 "a1"
299+
a11 <- mkLabelPart' 4 25 "a11"
300+
a2 <- mkLabelPart' 5 14 "a2"
301+
a3 <- mkLabelPart' 6 14 "a3"
302+
(@?=) ih
303+
[ defInlayHint
304+
{ _position = Position 9 52
305+
, _label = InR [ a3 ]
306+
, _textEdits = Just [ mkLineTextEdit "L1 {l2 = L2 {l3 = L3 {l4 = L4 {..}, ..}, ..}, a3}" 9 5 53 ]
307+
, _tooltip = Just $ InL "Expand record wildcard"
308+
, _paddingLeft = Just True
309+
, _paddingRight = Nothing
310+
, _data_ = Nothing
311+
}
312+
, defInlayHint
313+
{ _position = Position 9 47
314+
, _label = InR [ a2 ]
315+
, _textEdits = Just [ mkLineTextEdit "L2 {l3 = L3 {l4 = L4 {..}, ..}, a2}" 9 14 48 ]
316+
, _tooltip = Just $ InL "Expand record wildcard"
317+
, _paddingLeft = Just True
318+
, _paddingRight = Nothing
319+
, _data_ = Nothing
320+
}
321+
, defInlayHint
322+
{ _position = Position 9 42
323+
, _label = InR [ a1 , InlayHintLabelPart ", " Nothing Nothing Nothing , a11 ]
324+
, _textEdits = Just [ mkLineTextEdit "L3 {l4 = L4 {..}, a1, a11}" 9 23 43 ]
325+
, _tooltip = Just $ InL "Expand record wildcard"
326+
, _paddingLeft = Just True
327+
, _paddingRight = Nothing
328+
, _data_ = Nothing
329+
}
330+
, defInlayHint
331+
{ _position = Position 9 37
332+
, _label = InR [ a0 ]
333+
, _textEdits = Just [ mkLineTextEdit "L4 {a0}" 9 31 38 ]
334+
, _tooltip = Just $ InL "Expand record wildcard"
335+
, _paddingLeft = Just True
336+
, _paddingRight = Nothing
337+
, _data_ = Nothing
338+
}
339+
]
340+
, mkInlayHintsTest "CursorPositional" Nothing 15 $ \ih -> do
341+
let mkLabelPart' = mkLabelPartOffsetLengthSub1 "CursorPositional"
342+
middle <- mkLabelPart' 2 2 "middle="
343+
inner <- mkLabelPart' 6 2 "inner="
344+
foo <- mkLabelPart' 10 2 "foo="
345+
bar <- mkLabelPart' 11 4 "bar="
346+
(@?=) ih
347+
[ defInlayHint
348+
{ _position = Position 15 14
349+
, _label = InR [ middle ]
350+
, _textEdits = Just [ mkLineTextEdit "RecOuter { middle = (RecMiddle (RecInner 'c' 42)) }" 15 5 43 ]
351+
, _tooltip = Just $ InL "Expand positional record"
352+
, _paddingLeft = Nothing
353+
}
354+
, defInlayHint
355+
{ _position = Position 15 25
356+
, _label = InR [ inner ]
357+
, _textEdits = Just [ mkLineTextEdit "RecMiddle { inner = (RecInner 'c' 42) }" 15 15 42 ]
358+
, _tooltip = Just $ InL "Expand positional record"
359+
, _paddingLeft = Nothing
360+
}
361+
, defInlayHint
362+
{ _position = Position 15 35
363+
, _label = InR [ foo ]
364+
, _textEdits =
365+
Just [ mkLineTextEdit "RecInner { foo = 'c', bar = 42 }" 15 26 41 ]
366+
, _tooltip = Just $ InL "Expand positional record"
367+
, _paddingLeft = Nothing
368+
}
369+
, defInlayHint
370+
{ _position = Position 15 39
371+
, _label = InR [ bar ]
372+
, _textEdits =
373+
Just [ mkLineTextEdit "RecInner { foo = 'c', bar = 42 }" 15 26 41 ]
374+
, _tooltip = Just $ InL "Expand positional record"
375+
, _paddingLeft = Nothing
376+
}
377+
]
295378
]
296379
]
297380

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module CursorPositional where
2+
data RecOuter = RecOuter{
3+
middle :: RecMiddle
4+
}
5+
6+
data RecMiddle = RecMiddle {
7+
inner :: RecInner
8+
}
9+
10+
data RecInner = RecInner{
11+
foo :: Char
12+
, bar :: Int
13+
}
14+
15+
ex :: RecOuter
16+
ex = RecOuter (RecMiddle (RecInner 'c' 42))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE RecordWildCards #-}
2+
module CursorRecords where
3+
4+
data L4 = L4{ a0 :: Int}
5+
data L3 = L3{ a1 :: Int, a11 :: Int, l4 :: L4}
6+
data L2 = L2{ a2 :: Int, l3 :: L3}
7+
data L1 = L1{ a3 :: Int, l2 :: L2}
8+
9+
test :: L1 -> Int
10+
test L1 {l2 = L2{ l3 = L3{l4 = L4 {..}, ..}, ..}, ..} =
11+
a0 + a1 + a2 + a3 + a11

0 commit comments

Comments
 (0)