From d11e3fcfa5a048a33854a7c77d5a0d7bb8804a68 Mon Sep 17 00:00:00 2001 From: Ali Abrar Date: Mon, 24 Jul 2023 11:36:19 -0400 Subject: [PATCH] Add option to convert pre/post qualified --- app/Main.hs | 37 ++++++++++++++++++++++++++++--------- src/SortImports.hs | 22 +++++++++++++--------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 58d046b..89f0cdb 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -6,23 +6,29 @@ import Options.Applicative import SortImports (sortImports) import System.Environment (getArgs) import System.IO (hReady, stdin) +import SortImports.Types (Qualified(..)) versionNumber :: String versionNumber = "v1.2.0" -version :: Parser Options +version :: Parser Mode version = flag' Version $ mconcat [ long "version" , short 'v' , help "Version number" ] -data Options +data Mode = Version | FileInput FilePath | StdInput -fileInput :: Parser Options +data Options = Options + { _options_mode :: Mode + , _options_convert :: Maybe Qualified + } + +fileInput :: Parser Mode fileInput = FileInput <$> strOption (mconcat [ long "file" , short 'f' @@ -30,27 +36,40 @@ fileInput = FileInput <$> strOption (mconcat , help "Input file" ]) -stdInput :: Parser Options +stdInput :: Parser Mode stdInput = flag' StdInput $ mconcat [ long "stdin" , help "Read input from stdin" ] +convertToQualifiedPost :: Parser Bool +convertToQualifiedPost = switch (long "convert-to-qualified-post" <> help "Whether to convert all qualified imports to use ImportQualifiedPost") + +convertToQualifiedPre :: Parser Bool +convertToQualifiedPre = switch (long "convert-to-qualified-pre" <> help "Whether to convert all ImportQualifiedPost imports to use older \"import qualified\" syntax") + options :: Parser Options -options = version <|> fileInput <|> stdInput +options = Options + <$> (version <|> fileInput <|> stdInput) + <*> (post <|> pre) + where + post = ((\x -> if x then Just Qualified_Post else Nothing) <$> convertToQualifiedPost) + pre = ((\x -> if x then Just Qualified_Pre else Nothing) <$> convertToQualifiedPre) run :: Options -> IO () -run = \case +run opts = case _options_mode opts of Version -> putStrLn versionNumber - FileInput path -> readFile path >>= putStr . sortImports - StdInput -> interact sortImports + FileInput path -> readFile path >>= putStr . (sortImports convert) + StdInput -> interact (sortImports convert) + where + convert = _options_convert opts main :: IO () main = do noArgs <- null <$> getArgs anyStdin <- hReady stdin if noArgs && anyStdin - then run StdInput + then run (Options StdInput Nothing) else execParser opts >>= run where opts = info (options <**> helper) $ mconcat [ briefDesc diff --git a/src/SortImports.hs b/src/SortImports.hs index b039e11..73d3232 100644 --- a/src/SortImports.hs +++ b/src/SortImports.hs @@ -93,8 +93,8 @@ exposure = do hiding <- isJust <$> optional (rword "hiding") (if hiding then Hidden else Exposed) <$> exports -module' :: Parser Module -module' = do +module' :: Maybe Qualified -> Parser Module +module' cfgQualified = do void $ rword "import" _qualified' <- isJust <$> optional (rword "qualified") _name <- moduleName @@ -102,15 +102,19 @@ module' = do _alias <- optional alias _exports <- optional exposure space - let _qualified = if _qualified' then Just Qualified_Pre else if _qualified'' then Just Qualified_Post else Nothing + let _qualified = case cfgQualified of + Nothing -> if _qualified' + then Just Qualified_Pre + else if _qualified'' then Just Qualified_Post else Nothing + Just q -> if (_qualified' || _qualified'') then Just q else Nothing pure Module {..} sortExports :: Module -> Module sortExports m = m { _exports = fmap sort <$> _exports m } -lineType :: String -> LineType -lineType x = - case parse module' "" x of +lineType :: Maybe Qualified -> String -> LineType +lineType mq x = + case parse (module' mq) "" x of Left _ -> CodeLine x Right m -> ModuleLine m @@ -127,12 +131,12 @@ sortIfModules xs@(ModuleLine _:_) = sort . fmap f $ xs where f (ModuleLine x) = ModuleLine $ sortExports x f x = x -sortImports :: String -> String -sortImports +sortImports :: Maybe Qualified -> String -> String +sortImports mq = unlines . fmap (\case CodeLine x -> x ModuleLine x -> view x) . concatMap sortIfModules . groupLines - . map lineType + . map (lineType mq) . lines