Skip to content

Commit a0ec0a0

Browse files
authored
Use pretty printing context when expanding records (#4917)
* Add reproducer * Utilize pretty print context when expanding records * Address review feedback * Add explicit-record test with unicode fields * Move `formatOutputable` to `Utils` and rename
1 parent b6f2b74 commit a0ec0a0

8 files changed

Lines changed: 135 additions & 51 deletions

File tree

ghcide/src/Development/IDE/GHC/Compat/Outputable.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ printSDocQualifiedUnsafe unqual doc =
100100
sty = mkUserStyle unqual AllTheWay
101101
doc' = pprWithUnitState emptyUnitState doc
102102

103-
104-
105103
formatErrorWithQual :: DynFlags -> MsgEnvelope DecoratedSDoc -> String
106104
formatErrorWithQual dflags e =
107105
showSDoc dflags (pprNoLocMsgEnvelope e)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module Development.IDE.GHC.Util(
2828
disableWarningsAsErrors,
2929
printOutputable,
3030
printOutputableOneLine,
31+
printOutputableQualified,
3132
getExtensions,
3233
getExtensionsSet,
3334
stripOccNamePrefix,
@@ -271,6 +272,10 @@ printOutputable = printOutputable' printWithoutUniques
271272
printOutputableOneLine :: Outputable a => a -> T.Text
272273
printOutputableOneLine = printOutputable' printWithoutUniquesOneLine
273274

275+
printOutputableQualified :: Outputable a => PrintUnqualified -> a -> T.Text
276+
printOutputableQualified ctx =
277+
printOutputable' (printSDocQualifiedUnsafe ctx . ppr)
278+
274279
printOutputable' :: Outputable a => (a -> String) -> a -> T.Text
275280
printOutputable' print =
276281
-- IfaceTyLit from GHC.Iface.Type implements Outputable with 'show'.

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

Lines changed: 68 additions & 49 deletions
Large diffs are not rendered by default.

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ test = testGroup "explicit-fields"
3838
, mkTestNoAction "Prefix" "Prefix" 10 11 10 28
3939
, mkTestNoAction "PartiallyAppliedCon" "PartiallyAppliedCon" 7 8 7 12
4040
, mkConversionTest "PolymorphicRecordConstruction" "PolymorphicRecordConstruction" 15 5 15 15
41+
, mkConversionTest "QualifiedPositionalConstruction" "QualifiedPositionalConstruction" 9 10 9 15
4142
, mkConversionTest "CursorAwarePositional" "CursorPositional" 15 26 15 34
4243
, mkExpansionTest "CursorAwareRecords" "CursorRecords" 9 40 9 40
44+
, mkConversionTest "UnicodeStrings" "UnicodeStrings" 10 12 10 17
4345
]
4446
, testGroup "inlay hints"
4547
[ mkInlayHintsTest "Construction" Nothing 16 $ \ih -> do
@@ -294,6 +296,24 @@ test = testGroup "explicit-fields"
294296
, _paddingLeft = Nothing
295297
}
296298
]
299+
, mkInlayHintsTest "UnicodeStrings" Nothing 10 $ \ih -> do
300+
let mkLabelPart' = mkLabelPartOffsetLengthSub1 "UnicodeStrings"
301+
name <- mkLabelPart' 5 4 "αβγa="
302+
count <- mkLabelPart' 6 4 "count="
303+
(@?=) ih
304+
[ defInlayHint { _position = Position 10 18
305+
, _label = InR [ name ]
306+
, _textEdits = Just [ mkLineTextEdit "MyRec { αβγa = \"αβγ\", count = 42 }" 10 12 26 ]
307+
, _tooltip = Just $ InL "Convert to traditional record syntax"
308+
, _paddingLeft = Nothing
309+
}
310+
, defInlayHint { _position = Position 10 24
311+
, _label = InR [ count ]
312+
, _textEdits = Just [ mkLineTextEdit "MyRec { αβγa = \"αβγ\", count = 42 }" 10 12 26 ]
313+
, _tooltip = Just $ InL "Convert to traditional record syntax"
314+
, _paddingLeft = Nothing
315+
}
316+
]
297317
, mkInlayHintsTest "CursorRecords" Nothing 9 $ \ih -> do
298318
let mkLabelPart' = mkLabelPartOffsetLength "CursorRecords"
299319
a0 <- mkLabelPart' 3 14 "a0"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
3+
module QualifiedPositionalConstruction where
4+
5+
import qualified Data.Foldable as Foldable
6+
7+
data Foo = Foo { bar :: [Int] }
8+
9+
foo :: Foo
10+
foo = Foo { bar = (Foldable.toList [1, 2, 3]) }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
3+
module QualifiedPositionalConstruction where
4+
5+
import qualified Data.Foldable as Foldable
6+
7+
data Foo = Foo { bar :: [Int] }
8+
9+
foo :: Foo
10+
foo = Foo (Foldable.toList [1, 2, 3])
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
3+
module UnicodeStrings where
4+
5+
data MyRec = MyRec
6+
{ αβγa :: String
7+
, count :: Int
8+
}
9+
10+
convertMe :: MyRec
11+
convertMe = MyRec { αβγa = "αβγ", count = 42 }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{-# LANGUAGE Haskell2010 #-}
2+
3+
module UnicodeStrings where
4+
5+
data MyRec = MyRec
6+
{ αβγa :: String
7+
, count :: Int
8+
}
9+
10+
convertMe :: MyRec
11+
convertMe = MyRec "αβγ" 42

0 commit comments

Comments
 (0)