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

Refactor getLine to return TextLines/Rope #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 7 additions & 7 deletions src/Data/Text/Lines/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,17 @@ splitAtLine k = splitAtPosition (Position k 0)
-- ["fя𐀀","☺bar","",""]
--
-- @since 0.3
getLine :: Word -> TextLines -> Text
getLine :: Word -> TextLines -> TextLines
getLine line (TextLines t@(Text arr off len) nls)
| intToWord (U.length nls) < line = mempty
| otherwise =
let lineIdx = wordToInt line
in case (nls U.!? (lineIdx - 1), nls U.!? lineIdx) of
(Nothing, Nothing) -> t
(Nothing, Just endNl) -> Text arr off (endNl - off) -- branch triggered by `getLine 0 "a\n"`
(Just startNl, Nothing) -> Text arr (startNl + 1) (len + off - startNl - 1)
(Just startNl, Just endNl) -> Text arr (startNl + 1) (endNl - startNl - 1)

tNew = case (nls U.!? (lineIdx - 1), nls U.!? lineIdx) of
(Nothing, Nothing) -> t
(Nothing, Just endNl) -> Text arr off (endNl - off) -- branch triggered by `getLine 0 "a\n"`
(Just startNl, Nothing) -> Text arr (startNl + 1) (len + off - startNl - 1)
(Just startNl, Just endNl) -> Text arr (startNl + 1) (endNl - startNl - 1)
in TextLines tNew mempty
-------------------------------------------------------------------------------
-- Unicode code points

Expand Down
6 changes: 3 additions & 3 deletions src/Data/Text/Mixed/Rope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -567,17 +567,17 @@ utf16SplitAtPosition (Utf16.Position l c) rp = do
Just (beforeLine <> beforeColumn, afterColumn)

-- | Get a line by its 0-based index.
-- Returns @""@ if the index is out of bounds.
-- Returns empty Rope if the index is out of bounds.
-- The result doesn't contain @\\n@ characters.
--
-- >>> :set -XOverloadedStrings
-- >>> map (\l -> getLine l "foo\nbar\n😊😊\n\n") [0..3]
-- ["foo","bar","😊😊",""]
--
-- @since 0.3
getLine :: Word -> Rope -> Text
getLine :: Word -> Rope -> Rope
getLine lineIdx rp =
case T.unsnoc firstLine of
fromText $ case T.unsnoc firstLine of
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to return Rope, can we avoid firstLineRope and stick to firstLineRope? Otherwise we are doing a double conversion.

Just (firstLineInit, '\n') -> firstLineInit
_ -> firstLine
where
Expand Down
6 changes: 3 additions & 3 deletions src/Data/Text/Rope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,17 @@ splitAtPosition (Position l c) rp = (beforeLine <> beforeColumn, afterColumn)
(beforeColumn, afterColumn) = splitAt c afterLine

-- | Get a line by its 0-based index.
-- Returns @""@ if the index is out of bounds.
-- Returns empty Rope if the index is out of bounds.
-- The result doesn't contain @\\n@ characters.
--
-- >>> :set -XOverloadedStrings
-- >>> map (\l -> getLine l "foo\nbar\n😊😊\n\n") [0..3]
-- ["foo","bar","😊😊",""]
--
-- @since 0.3
getLine :: Word -> Rope -> Text
getLine :: Word -> Rope -> Rope
getLine lineIdx rp =
case T.unsnoc firstLine of
fromText $ case T.unsnoc firstLine of
Just (firstLineInit, '\n') -> firstLineInit
_ -> firstLine
where
Expand Down
6 changes: 3 additions & 3 deletions src/Data/Text/Utf16/Rope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,17 @@ splitAtPosition (Position l c) rp = do
Just (beforeLine <> beforeColumn, afterColumn)

-- | Get a line by its 0-based index.
-- Returns @""@ if the index is out of bounds.
-- Returns empty Rope if the index is out of bounds.
-- The result doesn't contain @\\n@ characters.
--
-- >>> :set -XOverloadedStrings
-- >>> map (\l -> getLine l "foo\nbar\n😊😊\n\n") [0..3]
-- ["foo","bar","😊😊",""]
--
-- @since 0.3
getLine :: Word -> Rope -> Text
getLine :: Word -> Rope -> Rope
getLine lineIdx rp =
case T.unsnoc firstLine of
fromText $ case T.unsnoc firstLine of
Just (firstLineInit, '\n') -> firstLineInit
_ -> firstLine
where
Expand Down
6 changes: 3 additions & 3 deletions src/Data/Text/Utf8/Rope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,17 @@ splitAtPosition (Position l c) rp = do
Just (beforeLine <> beforeColumn, afterColumn)

-- | Get a line by its 0-based index.
-- Returns @""@ if the index is out of bounds.
-- Returns empty Rope if the index is out of bounds.
-- The result doesn't contain @\\n@ characters.
--
-- >>> :set -XOverloadedStrings
-- >>> map (\l -> getLine l "foo\nbar\n😊😊\n\n") [0..3]
-- ["foo","bar","😊😊",""]
--
-- @since 0.3
getLine :: Word -> Rope -> Text
getLine :: Word -> Rope -> Rope
getLine lineIdx rp =
case T.unsnoc firstLine of
fromText $ case T.unsnoc firstLine of
Just (firstLineInit, '\n') -> firstLineInit
_ -> firstLine
where
Expand Down
2 changes: 1 addition & 1 deletion test/CharLines.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ testSuite = testGroup "Char Lines"

, testProperty "forall i in bounds: getLine i x == lines x !! i" $
\x -> let lns = lines x in
conjoin $ zipWith (\idx ln -> getLine idx x === ln) [0..] lns
conjoin $ zipWith (\idx ln -> toText (getLine idx x) === ln) [0..] lns
, testProperty "forall i out of bounds: getLine i x == mempty" $
\x (Positive offset) ->
let maxIdx = L.genericLength (lines x) - 1
Expand Down
2 changes: 1 addition & 1 deletion test/CharRope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ testSuite = testGroup "Char Rope"

, testProperty "forall i in bounds: getLine i x == lines x !! i" $
\x -> let lns = Rope.lines x in
conjoin $ L.zipWith (\idx ln -> Rope.getLine idx x === ln) [0..] lns
conjoin $ L.zipWith (\idx ln -> Rope.toText (Rope.getLine idx x) === ln) [0..] lns
, testProperty "forall i out of bounds: getLine i x == mempty" $
\x (Positive offset) ->
let maxIdx = L.genericLength (Rope.lines x) - 1
Expand Down
2 changes: 1 addition & 1 deletion test/MixedRope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ testSuite = testGroup "Utf16 Mixed"

, testProperty "forall i in bounds: getLine i x == lines x !! i" $
\x -> let lns = Mixed.lines x in
conjoin $ L.zipWith (\idx ln -> Mixed.getLine idx x === ln) [0..] lns
conjoin $ L.zipWith (\idx ln -> Mixed.toText (Mixed.getLine idx x) === ln) [0..] lns
, testProperty "forall i out of bounds: getLine i x == mempty" $
\x (Positive offset) ->
let maxIdx = L.genericLength (Mixed.lines x) - 1
Expand Down
2 changes: 1 addition & 1 deletion test/Utf16Rope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ testSuite = testGroup "Utf16 Rope"

, testProperty "forall i in bounds: getLine i x == lines x !! i" $
\x -> let lns = Rope.lines x in
conjoin $ L.zipWith (\idx ln -> Rope.getLine idx x === ln) [0..] lns
conjoin $ L.zipWith (\idx ln -> Rope.toText (Rope.getLine idx x) === ln) [0..] lns
, testProperty "forall i out of bounds: getLine i x == mempty" $
\x (Positive offset) ->
let maxIdx = L.genericLength (Rope.lines x) - 1
Expand Down
2 changes: 1 addition & 1 deletion test/Utf8Rope.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ testSuite = testGroup "Utf8 Rope"

, testProperty "forall i in bounds: getLine i x == lines x !! i" $
\x -> let lns = Rope.lines x in
conjoin $ L.zipWith (\idx ln -> Rope.getLine idx x === ln) [0..] lns
conjoin $ L.zipWith (\idx ln -> Rope.toText (Rope.getLine idx x) === ln) [0..] lns
, testProperty "forall i out of bounds: getLine i x == mempty" $
\x (Positive offset) ->
let maxIdx = L.genericLength (Rope.lines x) - 1
Expand Down
Loading