From 562a5610357ea4c9d8dfb58f1b4257b72f61ce1d Mon Sep 17 00:00:00 2001 From: Greg Hurrell Date: Sat, 4 Jun 2016 23:12:13 -0700 Subject: [PATCH] Suppress unwanted trailing whitespace --- docvim.cabal | 2 ++ lib/Docvim/Printer/Vim.hs | 15 +++++++++++---- .../fixtures/vim/integration-ferret-plugin.golden | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/docvim.cabal b/docvim.cabal index 9dc976d..bc69e16 100644 --- a/docvim.cabal +++ b/docvim.cabal @@ -82,6 +82,7 @@ library , optparse-applicative , parsec , pretty-show + , split , transformers default-language: Haskell2010 exposed-modules: Docvim.AST @@ -119,6 +120,7 @@ test-suite tasty , parsec , pretty-show , process + , split , tasty , tasty-golden , tasty-hunit diff --git a/lib/Docvim/Printer/Vim.hs b/lib/Docvim/Printer/Vim.hs index fda1d13..2f66df0 100644 --- a/lib/Docvim/Printer/Vim.hs +++ b/lib/Docvim/Printer/Vim.hs @@ -10,6 +10,7 @@ import Control.Monad.Reader import Control.Monad.State import Data.Char (isSpace, toUpper) import Data.List (intercalate, isSuffixOf, span, sort) +import Data.List.Split (splitOn) import Data.Tuple (swap) import Docvim.AST import Docvim.Parse (parseUnit, rstrip) @@ -37,7 +38,7 @@ textwidth :: Int textwidth = 78 vimHelp :: Node -> String -vimHelp n = rstrip output ++ "\n" +vimHelp n = suppressTrailingWhitespace output ++ "\n" where metadata = Metadata (getSymbols n) (getPluginName n) context = Context defaultLineBreak "" operations = evalState (runReaderT (node n) metadata) context @@ -47,16 +48,22 @@ vimHelp n = rstrip output ++ "\n" reduce acc (Slurp atom) = if isSuffixOf atom acc then take (length acc - length atom) acc else acc + suppressTrailingWhitespace str = rstrip $ intercalate "\n" (map rstrip (splitOn "\n" str)) -- Helper function that appends and updates `partialLine` context. append :: String -> Env append string = do context <- get -- TODO obviously tidy this up - -- TODO: always suppress trailing whitespace (some of it is making it into the - -- output) let (ops, line) = if length (partialLine context) + length leading >= textwidth - then ([Delete (length $ snd $ split $ partialLine context), Slurp " ", Append (lineBreak context), Append (snd $ split $ partialLine context), Append $ string], lineBreak context ++ (snd $ split $ partialLine context) ++ string) + then ( [ Delete (length $ snd $ split $ partialLine context) + , Slurp " " + , Append (lineBreak context) + , Append (snd $ split $ partialLine context) + , Append $ string + ] + , lineBreak context ++ (snd $ split $ partialLine context) ++ string + ) else ([Append string], partialLine context ++ string) put (Context (lineBreak context) (end line)) return ops diff --git a/tests/fixtures/vim/integration-ferret-plugin.golden b/tests/fixtures/vim/integration-ferret-plugin.golden index cc5a20d..e2a258d 100644 --- a/tests/fixtures/vim/integration-ferret-plugin.golden +++ b/tests/fixtures/vim/integration-ferret-plugin.golden @@ -200,7 +200,7 @@ Note that including quotes will not do what you intend. > " Search for '"foo' in the 'bar"' directory: :Ack "foo bar" - + " Search for "'foo' in the "bar'" directory: :Ack 'foo bar' < -- 2.40.1