Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extra space after open parens in formatter #155

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading