Skip to content

Commit

Permalink
Merge pull request #155 from rzk-lang/hotfix/format-paren-space
Browse files Browse the repository at this point in the history
Fix extra space after open parens in formatter
  • Loading branch information
fizruk authored Dec 11, 2023
2 parents cbbefea + f87c67f commit 127da4e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions rzk/src/Rzk/Format.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module Rzk.Format (
) where

import Control.Monad ((<$!>))
import Data.List (elemIndex, foldl', sort)
import Data.List (elemIndex, foldl', isInfixOf, sort,
stripPrefix)

import Language.Rzk.Syntax (tryExtractMarkdownCodeBlocks)
import Language.Rzk.Syntax.Layout (resolveLayout)
Expand Down Expand Up @@ -49,6 +50,12 @@ data FormatState = FormatState
, lambdaArrow :: Bool -- ^ After a lambda '\', in the parameters (to leave its -> on the same line)
}

-- Inspired by https://hackage.haskell.org/package/extra-1.7.14/docs/src/Data.List.Extra.html#stripInfix
stripInfix :: Eq a => [a] -> [a] -> [a]
stripInfix needle haystack | Just rest <- stripPrefix needle haystack = stripInfix needle rest
stripInfix _ [] = []
stripInfix needle (x:xs) = x : stripInfix needle xs

-- TODO: replace all tabs with 1 space before processing
formatTextEdits :: String -> [FormattingEdit]
formatTextEdits contents = go initialState toks
Expand Down Expand Up @@ -120,9 +127,8 @@ formatTextEdits contents = go initialState toks
where
spaceCol = col + 1
lineContent = contentLines line
-- | This is similar to (\\) but removes all occurrences (not just the first one)
setDifference xs excludes = filter (not . (`elem` excludes)) xs
precededBySingleCharOnly = null $ foldl' setDifference (take (col - 1) lineContent) punctuations
contentTillParen = take (col - 1) lineContent
precededBySingleCharOnly = not (":=" `isInfixOf` contentTillParen) && null (foldl' (flip stripInfix) contentTillParen punctuations)
punctuations = concat
[ map fst unicodeTokens -- ASCII sequences will be converted soon
, map snd unicodeTokens
Expand Down

0 comments on commit 127da4e

Please sign in to comment.