packages feed

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

module Rasa.Ext.Cursors.Actions  ( delete  , insertText  , findNext  , findPrev  , findNextFrom  , findPrevFrom  , moveRangesByN  , moveRangesByC  ) whereimport qualified Yi.Rope as Yimport qualified Data.Text as Timport Control.Lensimport Control.Lens.Textimport Rasa.Extimport Rasa.Ext.Cursors.Base-- | Moves all Ranges that are on the same END row as the given range by the coord's row and column-- This is used to adjust cursors when things have been inserted/deleted before them in the row.moveSameLineRangesBy :: Range -> Coord -> BufAction ()moveSameLineRangesBy (Range _ (Coord endRow endCol)) amt = do  let moveInLine r@(Range (Coord startRow startCol) _) = return $        if endRow == startRow && startCol > endCol           then moveRange amt r           else r  ranges <~ rangeDo moveInLine-- | Delete the text of all ranges in a bufferdelete :: BufAction ()delete = rangeDo_ $ \r -> do  deleteRange r  moveSameLineRangesBy r (negate $ sizeOfR r)-- | Insert text at the beginning of all ranges in the buffer.insertText :: Y.YiString -> BufAction ()insertText txt = rangeDo_ $ \r@(Range s _) -> do  insertAt s txt  moveSameLineRangesBy r (Coord 0 (Y.length txt))-- | Move all ranges to the location of the next occurence of the given text.findNext :: Y.YiString -> BufAction ()findNext pat = do  res <- rangeDo $ \(Range _ e) -> do    off <- findNextFrom pat e    let end = moveCursorByN 1 off    return $ Range off end  ranges .= res-- | Get the 'Coord' of the next occurence of the given text after the given 'Coord'findNextFrom :: Y.YiString -> Coord -> BufAction CoordfindNextFrom pat c = do  distance <- use (text . afterC c . asText . tillNext (Y.toText pat) . from asText . to sizeOf)  return (distance + c)-- | Move all ranges to the location of the previous occurence of the given text.findPrev :: Y.YiString -> BufAction ()findPrev pat = do  res <- rangeDo $ \(Range s _) -> do    off <- findPrevFrom pat s    let end = moveCursorByN 1 off    return $ Range off end  ranges .= res-- | Get the 'Coord' of the previous occurence of the given text before the given 'Coord'findPrevFrom :: Y.YiString -> Coord -> BufAction CoordfindPrevFrom pat c = do  txt <- use text  let Offset o = c^.from (asCoord txt)  distance <- use (text . asText . before o . tillPrev (Y.toText pat) . to T.length .to negate)  return ((Offset $ distance + o)^.asCoord txt)-- | Move all ranges by the given number of columnsmoveRangesByN :: Int -> BufAction ()moveRangesByN n = overRanges $ return . moveRangeByN n-- | Move all ranges by the given number of rows and columnsmoveRangesByC :: Coord -> BufAction ()moveRangesByC c = overRanges $ return . moveRange c