From 4200d2ed8a8d77d166060b9ddafa00fbe2cabcdc Mon Sep 17 00:00:00 2001 From: piegames Date: Mon, 2 Dec 2024 22:12:26 +0100 Subject: [PATCH] Fix string parsing of tab indentation As much as everyone would wish for, tabs are not whitespace according to Nix whitespace stripping. Fixes #116, but my overall confidence level in Nixfmt's correctness in parsing the mess which is the Nix syntax remains rather low. --- src/Nixfmt/Parser.hs | 1 + src/Nixfmt/Util.hs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Nixfmt/Parser.hs b/src/Nixfmt/Parser.hs index ddfb558d..a4c0c6f8 100644 --- a/src/Nixfmt/Parser.hs +++ b/src/Nixfmt/Parser.hs @@ -292,6 +292,7 @@ stripIndentation parts = case commonIndentation $ mapMaybe lineHead parts of Nothing -> map (const []) parts Just indentation -> map (stripParts indentation) parts +-- Merge adjacent string parts which resulted as parsing artifacts normalizeLine :: [StringPart] -> [StringPart] normalizeLine [] = [] normalizeLine (TextPart "" : xs) = normalizeLine xs diff --git a/src/Nixfmt/Util.hs b/src/Nixfmt/Util.hs index 8bc59866..c2ae1828 100644 --- a/src/Nixfmt/Util.hs +++ b/src/Nixfmt/Util.hs @@ -12,7 +12,7 @@ module Nixfmt.Util ( ) where -import Data.Char (isAlpha, isDigit, isSpace) +import Data.Char (isAlpha, isDigit) import Data.Text as Text ( Text, all, @@ -73,7 +73,7 @@ commonPrefix a b = -- prefix of zero texts is infinite, represented as Nothing. commonIndentation :: [Text] -> Maybe Text commonIndentation [] = Nothing -commonIndentation [x] = Just $ Text.takeWhile isSpace x +commonIndentation [x] = Just $ Text.takeWhile (== ' ') x commonIndentation (x : y : xs) = commonIndentation (commonPrefix x y : xs) isSpaces :: Text -> Bool