packages feed

zwirn-0.2.3.1: app/zwirnmill/Editor/Util.hs

module Editor.Util where

import Data.Maybe (isJust)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Zipper as Z
import Editor.Core
import Lens.Micro ((%~), (.~), (?~), (^.))
import Lens.Micro.Extras (view)

withZipper :: (Z.TextZipper Text -> Z.TextZipper Text) -> EditorState -> EditorState
withZipper f = esZipper %~ f

hasSelection :: EditorState -> Bool
hasSelection = isJust . view esSelection

getContent :: EditorState -> Text
getContent es = T.intercalate "\n" $ Z.getText $ es ^. esZipper

getLines :: EditorState -> [Text]
getLines es = Z.getText $ es ^. esZipper

getLongestLine :: EditorState -> Int
getLongestLine es = maximum $ map T.length $ getLines es

lineNumberWidth :: EditorState -> Int
lineNumberWidth es = length (show (lineCount es)) + 1

lineCount :: EditorState -> Int
lineCount = length . Z.getText . view esZipper

currentLine :: EditorState -> Int
currentLine = fst . Z.cursorPosition . view esZipper

currentLineText :: EditorState -> Text
currentLineText = Z.currentLine . view esZipper

currentCursor :: EditorState -> (Int, Int)
currentCursor = Z.cursorPosition . view esZipper

clearSelection :: EditorState -> EditorState
clearSelection = esSelection .~ Nothing

clearPopup :: EditorState -> EditorState
clearPopup = esPopup .~ Nothing

clearFlashBlock :: EditorState -> EditorState
clearFlashBlock = esFlashBlock .~ Nothing

clearMessage :: EditorState -> EditorState
clearMessage = esMessage .~ Nothing

setMessage :: String -> EditorState -> EditorState
setMessage msg = esMessage ?~ msg

inSpan :: (Ord a1, Ord a2) => (a2, a1) -> (a2, a1) -> (a2, a1) -> Bool
inSpan (a, b) (srs, scs) (sre, sce)
  | a == srs && a == sre = scs <= b && b <= sce
  | a == srs = b >= scs
  | a == sre = b <= sce
  | otherwise = a > srs && a < sre