fix-whitespace 0.0.7 → 0.0.8
raw patch · 8 files changed
+113/−58 lines, 8 filesdep ~basedep ~text
Dependency ranges changed: base, text
Files
- CHANGELOG.md +8/−3
- FixWhitespace.hs +71/−31
- README.md +6/−3
- fix-whitespace.cabal +21/−13
- stack-8.10.7.yaml +1/−1
- stack-9.0.1.yaml +0/−7
- stack-9.0.2.yaml +3/−0
- stack-9.2.2.yaml +3/−0
CHANGELOG.md view
@@ -2,9 +2,14 @@ Version history. +## 0.0.8 released 2022-05-29++- New option `--version` displaying program version.+- Tested with GHC 8.0.2 - 9.4.1 alpha.+ ## 0.0.7 released 2021-09-07 -- Supported GHC 8.10.7.+- Support GHC 8.10.7. ## 0.0.6 released 2021-07-29 @@ -14,5 +19,5 @@ ## 0.0.5 released 2021-03-11 -- initial release.-- tested with GHC 8.0.2 - 9.0.1.+- Initial release.+- Tested with GHC 8.0.2 - 9.0.1.
FixWhitespace.hs view
@@ -1,13 +1,14 @@--- Liang-Ting Chen 2019-10-13:--- this program is partially re-written so that the configuration part is--- controlled by a configuration file `fix-whitespace.yaml" in the base--- directory instead.+-- | Program to enforce a whitespace policy. +module Main where+ import Control.Monad+import Control.Exception (IOException, handle) import Data.Char as Char import Data.List.Extra (nubOrd) import Data.Text (Text)+import Data.Version (showVersion) import qualified Data.Text as Text import qualified Data.Text.IO as Text -- Strict IO. @@ -21,7 +22,13 @@ import System.Console.GetOpt import ParseConfig+import qualified Paths_fix_whitespace as PFW (version) +-- | Default configuration file.++defaultConfigFile :: String+defaultConfigFile = "fix-whitespace.yaml"+ -- Modes. data Mode = Fix -- ^ Fix whitespace issues.@@ -35,6 +42,8 @@ -- ^ Display the location of a file being checked or not. , optHelp :: Bool -- ^ Display the help information.+ , optVersion :: Bool+ -- ^ Display the program's version. , optMode :: Mode , optConfig :: FilePath -- ^ The location to the configuration file.@@ -44,8 +53,9 @@ defaultOptions = Options { optVerbose = False , optHelp = False+ , optVersion = False , optMode = Fix- , optConfig = "fix-whitespace.yaml"+ , optConfig = defaultConfigFile } options :: [OptDescr (Options -> Options)]@@ -53,12 +63,15 @@ [ Option ['h'] ["help"] (NoArg (\opts -> opts { optHelp = True })) "Show this help information."+ , Option [] ["version"]+ (NoArg (\opts -> opts { optVersion = True }))+ "Show the program's version." , Option ['v'] ["verbose"] (NoArg (\opts -> opts { optVerbose = True })) "Show files as they are being checked." , Option [] ["config"] (ReqArg (\loc opts -> opts { optConfig = loc }) "CONFIG")- "Override the project configuration fix-whitespace.yaml."+ (concat ["Override the project configuration ", defaultConfigFile, "."]) , Option [] ["check"] (NoArg (\opts -> opts { optMode = Check })) (unlines@@ -68,26 +81,28 @@ ]) ] -compilerOpts :: String -> IO (Options, [String])-compilerOpts progName = do+programOpts :: String -> IO (Options, [String])+programOpts progName = do argv <- getArgs case getOpt Permute options argv of (o, n, [] ) -> return (foldl (flip id) defaultOptions o, n)- (_, _, errs) -> ioError (userError (concat errs ++ "\n" ++ shortUsageHeader progName))+ (_, _, errs) -> ioError $ userError $ concat errs ++ "\n" ++ shortUsageHeader progName -shortUsageHeader, usageHeader, usage :: String -> String +shortUsageHeader :: String -> String shortUsageHeader progName = "Usage: " ++ progName ++ " [-h|--help] [-v|--verbose] [--check] [--config CONFIG] [FILES]" +usageHeader :: String -> String usageHeader progName = unlines [ shortUsageHeader progName , "" , "The program does the following" , ""- , "* Removes trailing whitespace."- , "* Removes trailing lines containing nothing but whitespace."- , "* Ensures that the file ends in a newline character."+ , " * Removes trailing whitespace."+ , " * Removes trailing lines containing nothing but whitespace."+ , " * Ensures that the file ends in a newline character."+ , " * Convert tabs to spaces, assuming a tab-size of 8." , "" , "for files specified in [FILES] or" , ""@@ -95,31 +110,31 @@ , "" , "under the current directory." , ""- , "Background: Agda was reported to fail to compile on Windows"- , "because a file did not end with a newline character (Agda"- , "uses -Werror)."- , "" , "Available options:" ] +usage :: String -> String usage progName = usageInfo (usageHeader progName) options main :: IO () main = do progName <- getProgName- (opts, nonOpts) <- compilerOpts progName+ (opts, nonOpts) <- programOpts progName -- check if the user asks for help when (optHelp opts) $ putStr (usage progName) >> exitSuccess + -- check if the user asks for the program's version+ when (optVersion opts) $ putStrLn (showVersion PFW.version) >> exitSuccess+ -- check if the configuration file exists configExist <- doesFileExist $ optConfig opts unless (configExist || not (null nonOpts)) $ do- hPutStr stderr (unlines- [ "fix-whitespace.yaml is not found and there are no files specified as arguments."+ hPutStr stderr $ unlines+ [ unwords [defaultConfigFile, "is not found and there are no files specified as arguments."] , "" , shortUsageHeader progName- ])+ ] exitFailure let mode = optMode opts@@ -170,18 +185,15 @@ when (or changes && mode == Check) exitFailure fix :: Mode -> Verbose -> FilePath -> IO Bool-fix mode verbose f = do+fix mode verbose f =+ checkFile f >>= \case - new <- withFile f ReadMode $ \h -> do- hSetEncoding h utf8- s <- Text.hGetContents h- let s' = transform s- return $ if s' == s then Nothing else Just s'- case new of- Nothing -> do- when verbose (putStrLn $ "[ Checked ] " ++ f)+ CheckOK -> do+ when verbose $+ putStrLn $ "[ Checked ] " ++ f return False- Just s -> do++ CheckViolation s -> do hPutStrLn stderr $ "[ Violation " ++ (if mode == Fix then "fixed" else "detected") ++@@ -191,6 +203,34 @@ hSetEncoding h utf8 Text.hPutStr h s return True++ CheckIOError _e -> do+ hPutStrLn stderr $+ "[ Read error ] " ++ f+ return False++-- | Result of checking a file against the whitespace policy.++data CheckResult+ = CheckOK+ -- ^ The file satifies the policy.+ | CheckViolation Text+ -- ^ The file violates the policy, a fix is returned.+ | CheckIOError IOException+ -- ^ An I/O error occurred while accessing the file.+ -- (E.g., the file is not UTF8 encoded.)++-- | Check a file against the whitespace policy,+-- returning a fix if violations occurred.++checkFile :: FilePath -> IO CheckResult+checkFile f =+ handle (\ (e :: IOException) -> return $ CheckIOError e) $+ withFile f ReadMode $ \ h -> do+ hSetEncoding h utf8+ s <- Text.hGetContents h+ let s' = transform s+ return $ if s' == s then CheckOK else CheckViolation s' -- | Transforms the contents of a file.
README.md view
@@ -2,16 +2,15 @@ ======================================= [](http://hackage.haskell.org/package/fix-whitespace)-[](https://matrix.hackage.haskell.org/package/fix-whitespace) [](https://stackage.org/nightly/package/fix-whitespace) [](https://www.stackage.org/package/fix-whitespace) [](https://github.com/agda/fix-whitespace/actions)-+[](https://github.com/agda/fix-whitespace/actions/workflows/haskell-ci.yml) This tool can keep your project and repository clean of trailing whitespace and missing terminal newline. -Usage: `fix-whitespace [-h|--help] [-v|--verbose] [--check] [--config CONFIG] [FILES]`+Usage: `fix-whitespace [-h|--help] [-v|--verbose] [--version] [--check] [--config CONFIG] [FILES]` The program does the following to files specified in `FILES` or in the configuration file `fix-whitespace.yaml` under the current directory@@ -30,6 +29,10 @@ * `-v --verbose` Show files as they are being checked.++* `--version`++ Show program's version. * `--config=CONFIG`
fix-whitespace.cabal view
@@ -1,6 +1,6 @@ name: fix-whitespace-version: 0.0.7-cabal-version: >= 1.10+version: 0.0.8+cabal-version: 1.24 build-type: Simple description: Removes trailing whitespace, lines containing only whitespace and ensure that every file ends in a newline character. license: OtherLicense@@ -8,16 +8,19 @@ author: fix-whitespace was originally written by Nils Anders Danielsson as part of Agda 2 with contributions from Ulf Norell, Andrés Sicard-Ramírez, Andreas Abel, Philipp Hausmann, Jesper Cockx, Vlad Semenov, and Liang-Ting Chen. homepage: https://github.com/agda/fix-whitespace bug-reports: https://github.com/agda/fix-whitespace/issues-maintainer: Liang-Ting Chen <liang.ting.chen.tw@gmail.com>+maintainer: Liang-Ting Chen <liang.ting.chen.tw@gmail.com>, Andreas Abel Category: Text Synopsis: Fixes whitespace issues.-tested-with: GHC == 8.0.2- GHC == 8.2.2- GHC == 8.4.4- GHC == 8.6.5- GHC == 8.8.4- GHC == 8.10.7- GHC == 9.0.1+tested-with:+ GHC == 9.4.1+ GHC == 9.2.3+ GHC == 9.0.2+ GHC == 8.10.7+ GHC == 8.8.4+ GHC == 8.6.5+ GHC == 8.4.4+ GHC == 8.2.2+ GHC == 8.0.2 extra-source-files: CHANGELOG.md@@ -29,7 +32,8 @@ stack-8.6.5.yaml stack-8.8.4.yaml stack-8.10.7.yaml- stack-9.0.1.yaml+ stack-9.0.2.yaml+ stack-9.2.2.yaml source-repository head type: git@@ -39,14 +43,18 @@ hs-source-dirs: . main-is: FixWhitespace.hs other-modules: ParseConfig+ Paths_fix_whitespace default-language: Haskell2010+ default-extensions:+ LambdaCase+ ScopedTypeVariables - build-depends: base >= 4.9.0.0 && < 4.16+ build-depends: base >= 4.9.0.0 && < 5 , directory >= 1.2.6.2 && < 1.4 , extra >= 1.1 && < 2.0 , filepath >= 1.4.1.0 && < 1.5 , filepattern >= 0.1.2 && < 0.1.3- , text >= 1.2.3.0 && < 1.3+ , text >= 1.2.3.0 && < 1.3 || == 2.0.* , yaml >= 0.8.4 && < 0.12 -- ASR (2018-10-16).
stack-8.10.7.yaml view
@@ -1,3 +1,3 @@-resolver: lts-18.9+resolver: lts-18.28 compiler: ghc-8.10.7 compiler-check: match-exact
− stack-9.0.1.yaml
@@ -1,7 +0,0 @@-resolver: nightly-2021-07-29-compiler: ghc-9.0.1-compiler-check: match-exact--flags:- transformers-compat:- five-three: true
+ stack-9.0.2.yaml view
@@ -0,0 +1,3 @@+resolver: lts-19.8+compiler: ghc-9.0.2+compiler-check: match-exact
+ stack-9.2.2.yaml view
@@ -0,0 +1,3 @@+resolver: nightly-2022-05-28+compiler: ghc-9.2.2+compiler-check: match-exact