From f34a1b4be998af91934aa9f15933c1727f5c1f54 Mon Sep 17 00:00:00 2001 From: Leah Amelia Chen Date: Thu, 21 Nov 2024 18:36:12 +0100 Subject: [PATCH] Add `--filename` flag for overriding the filename when using stdinTarget When `--filename path` is specified, any diagnostics will use the `path` instead of ``. Closes #263 --- main/Main.hs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/main/Main.hs b/main/Main.hs index 99c592c..9bd80b0 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -12,6 +12,7 @@ import Data.ByteString.Char8 (unpack) import Data.Either (lefts) import Data.FileEmbed import Data.List (isSuffixOf) +import Data.Maybe (fromMaybe) import Data.Text (Text) import qualified Data.Text.IO as TextIO (getContents, hPutStr, putStr) import Data.Version (showVersion) @@ -49,7 +50,8 @@ data Nixfmt = Nixfmt quiet :: Bool, strict :: Bool, verify :: Bool, - ast :: Bool + ast :: Bool, + filename :: Maybe FilePath } deriving (Show, Data, Typeable) @@ -76,7 +78,13 @@ options = ast = False &= help - "Pretty print the internal AST, only for debugging" + "Pretty print the internal AST, only for debugging", + + filename = + Nothing + &= help + "The filename to display when the file input is given through stdin.\n\ + \Useful for tools like editors and autoformatters that wish to use Nixfmt without providing it direct file access, while still providing context to where the file is." } &= summary ("nixfmt " ++ versionFromFile) &= help "Format Nix source code" @@ -132,8 +140,8 @@ checkTarget format Target{tDoRead, tPath} = do | formatted == contents -> Right () | otherwise -> Left $ tPath ++ ": not formatted" -stdioTarget :: Target -stdioTarget = Target TextIO.getContents "" (const TextIO.putStr) +stdioTarget :: Maybe FilePath -> Target +stdioTarget filename = Target TextIO.getContents (fromMaybe "" filename) (const TextIO.putStr) fileTarget :: FilePath -> Target fileTarget path = Target (readFileUtf8 path) path atomicWriteFile @@ -148,8 +156,8 @@ checkFileTarget :: FilePath -> Target checkFileTarget path = Target (readFileUtf8 path) path (const $ const $ pure ()) toTargets :: Nixfmt -> IO [Target] -toTargets Nixfmt{files = []} = pure [stdioTarget] -toTargets Nixfmt{files = ["-"]} = pure [stdioTarget] +toTargets Nixfmt{files = [], filename} = pure [stdioTarget filename] +toTargets Nixfmt{files = ["-"], filename} = pure [stdioTarget filename] toTargets Nixfmt{check = False, files = paths} = map fileTarget <$> collectAllNixFiles paths toTargets Nixfmt{check = True, files = paths} = map checkFileTarget <$> collectAllNixFiles paths