nano-ui
Safe HaskellNone
LanguageGHC2024

NanoUI.Widgets.TextBuffer

Description

A text document as a finger tree of lines with a cursor. Every change is a TextEdit: replace the text at a position with other text. An edit touches only the lines it spans, so edits, cursor moves and line lookups cost O(log lines) plus the size of the lines involved, however long the document is.

Synopsis

Types

data TextBuffer Source #

Lines (never empty, and without their newlines) and the cursor.

Constructors

TextBuffer 

Fields

Instances

Instances details
Eq TextBuffer Source # 
Instance details

Defined in NanoUI.Widgets.TextBuffer

Show TextBuffer Source # 
Instance details

Defined in NanoUI.Widgets.TextBuffer

data Cursor Source #

Zero-indexed logical (row, column) position in the buffer. Fields are row then column, so the derived Ord is document order.

Constructors

Cursor 

Fields

Instances

Instances details
Eq Cursor Source # 
Instance details

Defined in NanoUI.Widgets.TextBuffer

Methods

(==) :: Cursor -> Cursor -> Bool #

(/=) :: Cursor -> Cursor -> Bool #

Ord Cursor Source # 
Instance details

Defined in NanoUI.Widgets.TextBuffer

Show Cursor Source # 
Instance details

Defined in NanoUI.Widgets.TextBuffer

data TextEdit Source #

Replace editRemoved at editAt with editInserted. Both texts may span lines. An edit carries what it removes, so it can be inverted without looking at the document.

Constructors

TextEdit 

Instances

Instances details
Eq TextEdit Source # 
Instance details

Defined in NanoUI.Widgets.TextBuffer

Show TextEdit Source # 
Instance details

Defined in NanoUI.Widgets.TextBuffer

Construction & Conversion

empty :: TextBuffer Source #

A TextBuffer containing a single blank line.

fromText :: Text -> TextBuffer Source #

Construct a TextBuffer from raw Text. Cursor is always (0, 0).

toText :: TextBuffer -> Text Source #

All lines joined with newlines, copied once into a new text.

lineAt :: Int -> TextBuffer -> Text Source #

The text of a row, or empty outside the document.

Cursor & Metrics

withCursor :: Cursor -> TextBuffer -> TextBuffer Source #

Move to a position, clamped into the document, without changing text.

clampCursor :: TextBuffer -> Cursor -> Cursor Source #

The nearest position inside the document.

changedLines :: TextBuffer -> (Int, Int) Source #

How many lines at the start and at the end are the ones there at the last markLinesSeen; the lines between may have changed. Whatever was derived per line from the marked document can be kept for those lines and rederived for the rest.

markLinesSeen :: TextBuffer -> TextBuffer Source #

Record that every line has been seen, for changedLines.

Navigation

moveWordLeft :: TextBuffer -> TextBuffer Source #

Back over spaces (line breaks count), then over the word before them.

moveWordRight :: TextBuffer -> TextBuffer Source #

Forward over spaces (line breaks count), then over the word after them.

Selection

textRange :: Cursor -> Cursor -> TextBuffer -> Text Source #

The text between two positions in document order, reading only the lines between them.

Edits

applyEdit :: TextEdit -> TextBuffer -> TextBuffer Source #

Apply an edit and leave the cursor after the inserted text. The removed text decides how far the edit reaches, so an edit recorded against this document (or undone from one) is applied without reading the text it removes.

invertEdit :: TextEdit -> TextEdit Source #

The edit that takes the document back.

replaceEdit :: Text -> Cursor -> Cursor -> TextBuffer -> TextEdit Source #

The edit replacing the text between two positions.

insertableText :: Text -> Text Source #

Text as it can enter the document: printable characters, tabs and line breaks, with Windows line ends folded.