Skip to content

Commit 54bc8a8

Browse files
committed
Huge performance gains
Use splitAtLine from Rope to quickly find cursor position, don't split the entire string up on newlines, only the row we need.
1 parent 27929e9 commit 54bc8a8

File tree

2 files changed

+5
-16
lines changed

2 files changed

+5
-16
lines changed

rasa-ext-cursors/src/Rasa/Ext/Cursors/Internal/Actions.hs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ findPrevFrom pat c = do
7070
let distance = txt ^. beforeC c . asText . tillPrev (Y.toText pat) . from asText . to sizeOf
7171
return (c - distance)
7272

73-
-- let Offset o = c^.from (asCoord txt)
74-
-- distance = txt ^. asText . before o . tillPrev (Y.toText pat) . to T.length .to negate
75-
-- return ((Offset $ distance + o)^.asCoord txt)
76-
7773
-- | Move all ranges by the given number of columns
7874
moveRangesByN :: Int -> BufAction ()
7975
moveRangesByN = overRanges . fmap . moveRangeByN

rasa/src/Rasa/Internal/Range.hs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ module Rasa.Internal.Range
3838
import Rasa.Internal.Text
3939
import Control.Lens
4040

41-
import Data.Maybe
4241
import Data.Monoid
4342
import Data.List
4443
import Data.Bifunctor
@@ -176,7 +175,8 @@ clampCoord txt (Coord row col) =
176175
Coord (clamp 0 maxRow row) (clamp 0 maxColumn col)
177176
where
178177
maxRow = Y.countNewLines txt
179-
maxColumn = fromMaybe col (txt ^? asLines . ix row . to Y.length)
178+
selectedRow = fst . Y.splitAtLine 1 . snd . Y.splitAtLine row $ txt
179+
maxColumn = Y.length selectedRow
180180

181181
-- | This will restrict a given 'Range' to a valid one which lies within the given text.
182182
clampRange :: Y.YiString -> CrdRange -> CrdRange
@@ -237,22 +237,15 @@ sizeOf txt = Coord (Y.countNewLines txt) (Y.length (txt ^. asLines . _last))
237237
-- | A lens over text before a given 'Coord'
238238
beforeC :: Coord -> Lens' Y.YiString Y.YiString
239239
beforeC c@(Coord row col) = lens getter setter
240-
where getter txt =
241-
txt ^.. asLines . taking (row + 1) traverse
242-
& _last %~ Y.take col
243-
& Y.concat
244-
240+
where getter txt = let (before, after) = Y.splitAtLine row $ txt
241+
in before <> Y.take col after
245242
setter old new = let suffix = old ^. afterC c
246243
in new <> suffix
247244

248245
-- | A lens over text after a given 'Coord'
249246
afterC :: Coord -> Lens' Y.YiString Y.YiString
250247
afterC c@(Coord row col) = lens getter setter
251-
where getter txt =
252-
txt ^.. asLines . dropping row traverse
253-
& _head %~ Y.drop col
254-
& Y.concat
255-
248+
where getter txt = Y.drop col . snd . Y.splitAtLine row $ txt
256249
setter old new = let prefix = old ^. beforeC c
257250
in prefix <> new
258251

0 commit comments

Comments
 (0)