Skip to content

Commit c3e8842

Browse files
committed
Updated splitAtLineCol to use Maybe
1 parent 288797c commit c3e8842

2 files changed

Lines changed: 10 additions & 16 deletions

File tree

src/Data/Rope.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,20 @@ splitAtPositionR8 (Rope8.Position initPL initPC) rope = do
123123
x : _ -> x
124124
[] -> (mempty, rope) -- should be unreachable, as one of the tried positions should split the string neatly
125125

126-
-- TODO: return a maybe
127-
splitAtLineCol :: LineCol -> Rope -> (Rope, Rope)
128-
splitAtLineCol (LineCol (Pos line) (Pos col)) (Rope rope) = (Rope before, Rope after)
126+
splitAtLineCol :: LineCol -> Rope -> Maybe (Rope, Rope)
127+
splitAtLineCol lineCol r
128+
| isValidLineColEnd r lineCol = Just (Rope before, Rope after)
129+
| otherwise = Nothing
129130
where
131+
(LineCol (Pos line) (Pos col)) = lineCol
130132
(before, after) =
131133
splitAtPositionR8
132134
( Rope8.Position
133135
{ posLine = (fromIntegral line)
134136
, posColumn = (fromIntegral col)
135137
}
136138
)
137-
rope
139+
r.rope
138140

139141
indexRange :: Rope -> Range -> Maybe Rope
140142
indexRange (Rope r) (Range (Pos start) (Pos end))

test/Data/RopeSpec.hs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,28 @@ spec = do
2424
describe "line col split at" do
2525
let check name t lineCol (before, after) = it name do
2626
let r = Rope.fromText t
27-
let (before', after') = Rope.splitAtLineCol lineCol r
28-
before' `shouldBe` before
29-
after' `shouldBe` after
27+
Rope.splitAtLineCol lineCol r `shouldBe` Just (before, after)
3028
pure @IO ()
3129

3230
check "index past the line" "abcd\n1234" (LineCol (Pos 0) (Pos 4)) ("abcd", "\n1234")
3331

3432
it "index on newline" do
3533
let t = "abcd\n1234"
3634
let r = Rope.fromText t
37-
let (before, after) = Rope.splitAtLineCol (LineCol (Pos 0) (Pos 5)) r
38-
before `shouldBe` "abcd\n"
39-
after `shouldBe` "1234"
35+
Rope.splitAtLineCol (LineCol (Pos 0) (Pos 5)) r `shouldBe` Just ("abcd\n", "1234")
4036
pure @IO ()
4137

4238
-- TODO: prevent the rope from doing this
4339
it "index past newline" do
4440
let t = "abcd\n1234"
4541
let r = Rope.fromText t
46-
let (before, after) = Rope.splitAtLineCol (LineCol (Pos 0) (Pos 6)) r
47-
before `shouldBe` "abcd\n1"
48-
after `shouldBe` "234"
42+
Rope.splitAtLineCol (LineCol (Pos 0) (Pos 6)) r `shouldBe` Nothing
4943
pure @IO ()
5044

5145
it "index past newline" do
5246
let t = "abcd\n1234\n"
5347
let r = Rope.fromText t
54-
let (before, after) = Rope.splitAtLineCol (LineCol (Pos 2) (Pos 0)) r
55-
before `shouldBe` "abcd\n1234\n"
56-
after `shouldBe` ""
48+
Rope.splitAtLineCol (LineCol (Pos 2) (Pos 0)) r `shouldBe` Just ("abcd\n1234\n", "")
5749
pure @IO ()
5850

5951
it "convert line col at the end" do

0 commit comments

Comments
 (0)