Skip to content

Commit

Permalink
Merge branch 'master' into list-args
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil authored Nov 26, 2024
2 parents 7867adc + 923d379 commit 79f65fc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/**/*.nix linguist-vendored
15 changes: 11 additions & 4 deletions main/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -51,6 +52,7 @@ data Nixfmt = Nixfmt
verify :: Bool,
ast :: Bool,
ir :: Bool
filename :: Maybe FilePath
}
deriving (Show, Data, Typeable)

Expand Down Expand Up @@ -82,6 +84,11 @@ options =
False
&= help
"Pretty print the internal intermediate representation, 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"
Expand Down Expand Up @@ -137,8 +144,8 @@ checkTarget format Target{tDoRead, tPath} = do
| formatted == contents -> Right ()
| otherwise -> Left $ tPath ++ ": not formatted"

stdioTarget :: Target
stdioTarget = Target TextIO.getContents "<stdin>" (const TextIO.putStr)
stdioTarget :: Maybe FilePath -> Target
stdioTarget filename = Target TextIO.getContents (fromMaybe "<stdin>" filename) (const TextIO.putStr)

fileTarget :: FilePath -> Target
fileTarget path = Target (readFileUtf8 path) path atomicWriteFile
Expand All @@ -153,8 +160,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

Expand Down
3 changes: 2 additions & 1 deletion src/Nixfmt/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import Control.Monad.State.Strict (StateT)
import Data.Bifunctor (first)
import Data.Foldable (toList)
import Data.Function (on)
import Data.Int (Int64)
import Data.List.NonEmpty as NonEmpty
import Data.Maybe (maybeToList)
import Data.Text (Text, pack)
Expand Down Expand Up @@ -487,7 +488,7 @@ instance (LanguageElement a) => LanguageElement (NonEmpty a) where
mapAllTokens f = NonEmpty.map (mapAllTokens f)

data Token
= Integer Int
= Integer Int64
| Float Text
| Identifier Text
| EnvPath Text
Expand Down

0 comments on commit 79f65fc

Please sign in to comment.