Skip to content

Commit

Permalink
Replace setDifference with stripInfix
Browse files Browse the repository at this point in the history
  • Loading branch information
aabounegm committed Dec 10, 2023
1 parent cbbefea commit f87c67f
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 f87c67f

Please sign in to comment.