Skip to content

Commit 7ec7b65

Browse files
committed
Implement Reviews
1 parent d5baf09 commit 7ec7b65

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Control.Monad.IO.Class (MonadIO (liftIO))
1919
import Control.Monad.Trans.Class (lift)
2020
import Control.Monad.Trans.Maybe
2121
import Data.Aeson (ToJSON (toJSON))
22+
import Data.Function (on)
2223
import Data.Generics (GenericQ, everything,
2324
everythingBut, extQ, mkQ)
2425
import qualified Data.IntMap.Strict as IntMap
@@ -27,7 +28,6 @@ import Data.List (find, intersperse,
2728
import qualified Data.Map as Map
2829
import Data.Maybe (fromMaybe, isJust,
2930
mapMaybe, maybeToList)
30-
import Data.Ord (Down (..))
3131
import Data.Text (Text)
3232
import qualified Data.Text as T
3333
import Data.Unique (hashUnique, newUnique)
@@ -165,12 +165,13 @@ codeActionProvider ideState _ (CodeActionParams _ _ docId range _) = do
165165
-- converted to the record syntax through the code action
166166
, isConvertible record
167167
]
168-
sortedRecords = sortOn (Down . recordDepth recordsWithUid . snd) recordsWithUid
168+
recordsOnly = map snd recordsWithUid
169+
sortedRecords = sortOn (recordDepth recordsOnly . snd) recordsWithUid
169170
pure $ InL $ case sortedRecords of
170-
(top : _) -> [mkCodeAction enabledExtensions top]
171+
(top : _) -> [mkCodeAction enabledExtensions (fst top)]
171172
[] -> []
172173
where
173-
mkCodeAction exts (uid, _record) = InR CodeAction
174+
mkCodeAction exts uid = InR CodeAction
174175
{ _title = mkTitle exts -- TODO: `Expand positional record` without NamedFieldPuns if RecordInfoApp
175176
, _kind = Just CodeActionKind_RefactorRewrite
176177
, _diagnostics = Nothing
@@ -303,10 +304,12 @@ mkTitle exts = "Expand record wildcard"
303304
then mempty
304305
else " (needs extension: NamedFieldPuns)"
305306

306-
recordDepth :: [(Int, RecordInfo)] -> RecordInfo -> Int
307+
-- Calculate the nesting depth of a record by counting how many other records
308+
-- contain it. Used to prioritize more deeply nested records in code actions.
309+
recordDepth :: [RecordInfo] -> RecordInfo -> Int
307310
recordDepth allRecords record =
308-
let r = recordInfoToRange record
309-
in length [ ()| (_, other) <- allRecords, let r' = recordInfoToRange other, subRange r' r]
311+
let isSubrangeOf = subRange `on` recordInfoToRange
312+
in length $ filter (`isSubrangeOf` record) allRecords
310313

311314
pragmaEdit :: [Extension] -> NextPragmaInfo -> Maybe TextEdit
312315
pragmaEdit exts pragma = if NamedFieldPuns `elem` exts

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ test = testGroup "explicit-fields"
3838
, mkTestNoAction "Prefix" "Prefix" 10 11 10 28
3939
, mkTestNoAction "PartiallyAppliedCon" "PartiallyAppliedCon" 7 8 7 12
4040
, mkTest "PolymorphicRecordConstruction" "PolymorphicRecordConstruction" 15 5 15 15
41+
, mkTest "CursorAwarePositional" "CursorPositional" 15 26 15 34
42+
, mkTest "CursorAwareRecords" "CursorRecords" 9 40 9 40
4143
]
4244
, testGroup "inlay hints"
4345
[ mkInlayHintsTest "Construction" Nothing 16 $ \ih -> do
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 { foo = 'c', bar = 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 {..}, a1, a11}, ..}, ..} =
11+
a0 + a1 + a2 + a3 + a11

0 commit comments

Comments
 (0)