@@ -19,6 +19,7 @@ import Control.Monad.IO.Class (MonadIO (liftIO))
1919import Control.Monad.Trans.Class (lift )
2020import Control.Monad.Trans.Maybe
2121import Data.Aeson (ToJSON (toJSON ))
22+ import Data.Function (on )
2223import Data.Generics (GenericQ , everything ,
2324 everythingBut , extQ , mkQ )
2425import qualified Data.IntMap.Strict as IntMap
@@ -27,7 +28,6 @@ import Data.List (find, intersperse,
2728import qualified Data.Map as Map
2829import Data.Maybe (fromMaybe , isJust ,
2930 mapMaybe , maybeToList )
30- import Data.Ord (Down (.. ))
3131import Data.Text (Text )
3232import qualified Data.Text as T
3333import 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
307310recordDepth 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
311314pragmaEdit :: [Extension ] -> NextPragmaInfo -> Maybe TextEdit
312315pragmaEdit exts pragma = if NamedFieldPuns `elem` exts
0 commit comments