Skip to content

Commit

Permalink
Allow inplace refactoring of multiple files
Browse files Browse the repository at this point in the history
Allow multiple files to be refactored at a time, applying ideas
to each relevant file.

Closes #1539.
  • Loading branch information
alex-mckenna committed Feb 14, 2025
1 parent 4620d86 commit c7478e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/HLint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -209,18 +209,21 @@ getIdeas cmd@CmdMain{..} settings = do
-- #746: run refactor even if no hint, which ensures consistent output
-- whether there are hints or not.
handleRefactoring :: [Idea] -> [String] -> Cmd -> IO ()
handleRefactoring ideas files cmd@CmdMain{..} =
case cmdFiles of
[file] -> do
-- Ensure that we can find the executable
path <- checkRefactor (if cmdWithRefactor == "" then Nothing else Just cmdWithRefactor)
-- writeFile "hlint.refact"
let hints = show $ map (show &&& ideaRefactoring) ideas
withTempFile $ \f -> do
writeFile f hints
let ParseFlags{enabledExtensions, disabledExtensions} = cmdParseFlags cmd
exitWith =<< runRefactoring path file f enabledExtensions disabledExtensions cmdRefactorOptions
_ -> errorIO "Refactor flag can only be used with an individual file"
handleRefactoring ideas files cmd@CmdMain{..} = do
-- Ensure that we can find the executable
path <- checkRefactor (if cmdWithRefactor == "" then Nothing else Just cmdWithRefactor)
forM_ cmdFiles $ \file -> do
-- writeFile "hlint.refact"
let fileIdeas = filter (\i -> file == ideaFile i) ideas
let hints = show $ map (show &&& ideaRefactoring) fileIdeas
withTempFile $ \f -> do
writeFile f hints
let ParseFlags{enabledExtensions, disabledExtensions} = cmdParseFlags cmd
exitCode <- runRefactoring path file f enabledExtensions disabledExtensions cmdRefactorOptions

case exitCode of
ExitSuccess -> return ()
ExitFailure _ -> exitWith exitCode

handleReporting :: [Idea] -> Cmd -> IO ()
handleReporting showideas cmd@CmdMain{..} = do
Expand Down
8 changes: 8 additions & 0 deletions src/Idea.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Idea(
rawIdea, idea, suggest, suggestRemove, ideaRemove, warn, ignore,
rawIdeaN, suggestN, ignoreNoSuggestion,
showIdeasJson, showIdeaANSI,
ideaFile,
Note(..), showNotes,
Severity(..),
) where
Expand All @@ -16,6 +17,7 @@ import HsColour
import Refact.Types hiding (SrcSpan)
import Refact.Types qualified as R
import Prelude
import GHC.Data.FastString
import GHC.Types.SrcLoc
import GHC.Utils.Outputable
import GHC.Util
Expand All @@ -36,6 +38,12 @@ data Idea = Idea
}
deriving Eq

ideaFile :: Idea -> String
ideaFile idea =
case srcSpanFileName_maybe (ideaSpan idea) of
Just file -> unpackFS file
Nothing -> error "SrcSpan has no associated file"

-- I don't use aeson here for 2 reasons:
-- 1) Aeson doesn't escape unicode characters, and I want to (allows me to ignore encoding)
-- 2) I want to control the format so it's slightly human readable as well
Expand Down

0 comments on commit c7478e3

Please sign in to comment.