on develop help sign in
fix offsetMap computation to include cursor character
commit15e4329217bccb05b1fbb79df06c8cb0be079797 authorpdlla <chippermonky@gmail.com> dateFri 2 Jul 2021, 10:52 (5 years ago) parent53abc34
2 files changed+10−2
src/Data/Text/Zipper.hs+5−2 test/Data/Text/ZipperSpec.hs+5
src/Data/Text/Zipper.hs+5−2
@@ -12,7 +12,7 @@ module Data.Text.Zipper where
1212import Prelude
1313
1414import Control.Exception (assert)
15−import Control.Monad.State (evalState, forM, get, put, join)
15+import Control.Monad.State (evalState, forM, get, put)
1616import Data.Char (isSpace)
1717import Data.Map (Map)
1818import Data.Maybe (fromMaybe)
@@ -418,10 +418,13 @@ displayLinesWithAlignment alignment width tag cursorTag (TextZipper lb b a la) =
418418 linesBefore = map (wrapWithOffsetAndAlignment alignment width 0) $ reverse lb
419419 linesAfter :: [[WrappedLine]] -- The wrapped lines after the cursor line
420420 linesAfter = map (wrapWithOffsetAndAlignment alignment width 0) la
421+
422+ -- simulate trailing cursor character when computing OffsetMap
423+ afterWithCursor = if T.null a then " " else a
421424 offsets :: OffsetMapWithAlignment
422425 offsets = offsetMapWithAlignmentInternal $ mconcat
423426 [ linesBefore
424− , [wrapWithOffsetAndAlignment alignment width 0 $ b <> a]
427+ , [wrapWithOffsetAndAlignment alignment width 0 $ b <> afterWithCursor]
425428 , linesAfter
426429 ]
427430 flattenLines = concatMap (fmap _wrappedLines_text)
test/Data/Text/ZipperSpec.hs+5
@@ -88,6 +88,11 @@ spec =
8888 _displayLines_cursorPos dl4 `shouldBe` (5,0)
8989 _displayLines_cursorPos dl5 `shouldBe` (4,0)
9090 _displayLines_cursorPos dl6 `shouldBe` (4,0)
91+ it "displayLines - offsetMap" $ do
92+ let
93+ dl0 = displayLinesWithAlignment TextAlignment_Left 5 () () (end $ fromText "aoeku")
94+ _displayLines_cursorPos dl0 `shouldBe` (0,1)
95+ Map.size (_displayLines_offsetMap dl0) `shouldBe` 2 -- cursor character is on second line
9196 it "displayLinesWithAlignment - spans" $ do
9297 let
9398 someText = top $ fromText "0123456789abcdefgh"