JUCE  v5.1.1-3-g1a0b28c73
JUCE API
CodeDocument Class Reference

A class for storing and manipulating a source code file. More...

#include <juce_CodeDocument.h>

Collaboration diagram for CodeDocument:

Classes

class  Iterator
 Iterates the text in a CodeDocument. More...
 
class  Listener
 An object that receives callbacks from the CodeDocument when its text changes. More...
 
class  Position
 A position in a code document. More...
 

Public Member Functions

 CodeDocument ()
 Creates a new, empty document. More...
 
 ~CodeDocument ()
 Destructor. More...
 
void addListener (Listener *listener) noexcept
 Registers a listener object to receive callbacks when the document changes. More...
 
void applyChanges (const String &newContent)
 Analyses the changes between the current content and some new text, and applies those changes. More...
 
void clearUndoHistory ()
 Clears the undo history. More...
 
void deleteSection (const Position &startPosition, const Position &endPosition)
 Deletes a section of the text. More...
 
void deleteSection (int startIndex, int endIndex)
 Deletes a section of the text. More...
 
void findLineContaining (const Position &pos, Position &start, Position &end) const noexcept
 Finds the line that contains the given position. More...
 
void findTokenContaining (const Position &pos, Position &start, Position &end) const noexcept
 Finds the token that contains the given position. More...
 
Position findWordBreakAfter (const Position &position) const noexcept
 Searches for a word-break. More...
 
Position findWordBreakBefore (const Position &position) const noexcept
 Searches for a word-break. More...
 
String getAllContent () const
 Returns the full text of the document. More...
 
String getLine (int lineIndex) const noexcept
 Returns a line from the document. More...
 
int getMaximumLineLength () noexcept
 Returns the number of characters in the longest line of the document. More...
 
String getNewLineCharacters () const noexcept
 Returns the preferred new-line characters for the document. More...
 
int getNumCharacters () const noexcept
 Returns the number of characters in the document. More...
 
int getNumLines () const noexcept
 Returns the number of lines in the document. More...
 
String getTextBetween (const Position &start, const Position &end) const
 Returns a section of the document's text. More...
 
UndoManagergetUndoManager () noexcept
 Returns the document's UndoManager. More...
 
bool hasChangedSinceSavePoint () const noexcept
 Returns true if the state of the document differs from the state it was in when setSavePoint() was last called. More...
 
void insertText (const Position &position, const String &text)
 Inserts some text into the document at a given position. More...
 
void insertText (int insertIndex, const String &text)
 Inserts some text into the document at a given position. More...
 
bool loadFromStream (InputStream &stream)
 Replaces the editor's contents with the contents of a stream. More...
 
void newTransaction ()
 Begins a new undo transaction. More...
 
void redo ()
 Redo the last operation. More...
 
void removeListener (Listener *listener) noexcept
 Deregisters a listener. More...
 
void replaceAllContent (const String &newContent)
 Clears the document and replaces it with some new text. More...
 
void replaceSection (int startIndex, int endIndex, const String &newText)
 Replaces a section of the text with a new string. More...
 
void setNewLineCharacters (const String &newLineCharacters) noexcept
 Sets the new-line characters that the document should use. More...
 
void setSavePoint () noexcept
 Makes a note that the document's current state matches the one that is saved. More...
 
void undo ()
 Undo the last operation. More...
 
bool writeToStream (OutputStream &stream)
 Writes the editor's current contents to a stream. More...
 

Private Member Functions

void checkLastLineStatus ()
 
void insert (const String &text, int insertPos, bool undoable)
 
void remove (int startPos, int endPos, bool undoable)
 

Private Attributes

int currentActionIndex
 
int indexOfSavedState
 
OwnedArray< CodeDocumentLine > lines
 
ListenerList< Listenerlisteners
 
int maximumLineLength
 
String newLineChars
 
Array< Position * > positionsToMaintain
 
UndoManager undoManager
 

Friends

class CodeDocumentDeleteAction
 
class CodeDocumentInsertAction
 
class Iterator
 
class Position
 

Detailed Description

A class for storing and manipulating a source code file.

When using a CodeEditorComponent, it takes one of these as its source object.

The CodeDocument stores its content as an array of lines, which makes it quick to insert and delete.

See also
CodeEditorComponent

Constructor & Destructor Documentation

◆ CodeDocument()

CodeDocument::CodeDocument ( )

Creates a new, empty document.

◆ ~CodeDocument()

CodeDocument::~CodeDocument ( )

Destructor.

Member Function Documentation

◆ addListener()

void CodeDocument::addListener ( Listener listener)
noexcept

Registers a listener object to receive callbacks when the document changes.

If the listener is already registered, this method has no effect.

See also
removeListener

◆ applyChanges()

void CodeDocument::applyChanges ( const String newContent)

Analyses the changes between the current content and some new text, and applies those changes.

◆ checkLastLineStatus()

void CodeDocument::checkLastLineStatus ( )
private

◆ clearUndoHistory()

void CodeDocument::clearUndoHistory ( )

Clears the undo history.

See also
UndoManager::clearUndoHistory

◆ deleteSection() [1/2]

void CodeDocument::deleteSection ( const Position startPosition,
const Position endPosition 
)

Deletes a section of the text.

This operation is undoable.

◆ deleteSection() [2/2]

void CodeDocument::deleteSection ( int  startIndex,
int  endIndex 
)

Deletes a section of the text.

This operation is undoable.

◆ findLineContaining()

void CodeDocument::findLineContaining ( const Position pos,
Position start,
Position end 
) const
noexcept

Finds the line that contains the given position.

◆ findTokenContaining()

void CodeDocument::findTokenContaining ( const Position pos,
Position start,
Position end 
) const
noexcept

Finds the token that contains the given position.

◆ findWordBreakAfter()

Position CodeDocument::findWordBreakAfter ( const Position position) const
noexcept

Searches for a word-break.

◆ findWordBreakBefore()

Position CodeDocument::findWordBreakBefore ( const Position position) const
noexcept

Searches for a word-break.

◆ getAllContent()

String CodeDocument::getAllContent ( ) const

Returns the full text of the document.

◆ getLine()

String CodeDocument::getLine ( int  lineIndex) const
noexcept

Returns a line from the document.

◆ getMaximumLineLength()

int CodeDocument::getMaximumLineLength ( )
noexcept

Returns the number of characters in the longest line of the document.

◆ getNewLineCharacters()

String CodeDocument::getNewLineCharacters ( ) const
inlinenoexcept

Returns the preferred new-line characters for the document.

This will be either "\n", "\r\n", or (rarely) "\r".

See also
setNewLineCharacters

References StandardApplicationCommandIDs::redo, and StandardApplicationCommandIDs::undo.

◆ getNumCharacters()

int CodeDocument::getNumCharacters ( ) const
noexcept

Returns the number of characters in the document.

◆ getNumLines()

int CodeDocument::getNumLines ( ) const
inlinenoexcept

Returns the number of lines in the document.

◆ getTextBetween()

String CodeDocument::getTextBetween ( const Position start,
const Position end 
) const

Returns a section of the document's text.

◆ getUndoManager()

UndoManager& CodeDocument::getUndoManager ( )
inlinenoexcept

Returns the document's UndoManager.

◆ hasChangedSinceSavePoint()

bool CodeDocument::hasChangedSinceSavePoint ( ) const
noexcept

Returns true if the state of the document differs from the state it was in when setSavePoint() was last called.

See also
setSavePoint

◆ insert()

void CodeDocument::insert ( const String text,
int  insertPos,
bool  undoable 
)
private

◆ insertText() [1/2]

void CodeDocument::insertText ( const Position position,
const String text 
)

Inserts some text into the document at a given position.

This operation is undoable.

◆ insertText() [2/2]

void CodeDocument::insertText ( int  insertIndex,
const String text 
)

Inserts some text into the document at a given position.

This operation is undoable.

◆ loadFromStream()

bool CodeDocument::loadFromStream ( InputStream stream)

Replaces the editor's contents with the contents of a stream.

This will also reset the undo history and save point marker.

◆ newTransaction()

void CodeDocument::newTransaction ( )

Begins a new undo transaction.

The document itself will not call this internally, so relies on whatever is using the document to periodically call this to break up the undo sequence into sensible chunks.

See also
UndoManager::beginNewTransaction

◆ redo()

void CodeDocument::redo ( )

Redo the last operation.

See also
UndoManager::redo

◆ remove()

void CodeDocument::remove ( int  startPos,
int  endPos,
bool  undoable 
)
private

◆ removeListener()

void CodeDocument::removeListener ( Listener listener)
noexcept

Deregisters a listener.

See also
addListener

◆ replaceAllContent()

void CodeDocument::replaceAllContent ( const String newContent)

Clears the document and replaces it with some new text.

This operation is undoable - if you're trying to completely reset the document, you might want to also call clearUndoHistory() and setSavePoint() after using this method.

◆ replaceSection()

void CodeDocument::replaceSection ( int  startIndex,
int  endIndex,
const String newText 
)

Replaces a section of the text with a new string.

This operation is undoable.

◆ setNewLineCharacters()

void CodeDocument::setNewLineCharacters ( const String newLineCharacters)
noexcept

Sets the new-line characters that the document should use.

The string must be either "\n", "\r\n", or (rarely) "\r".

See also
getNewLineCharacters

◆ setSavePoint()

void CodeDocument::setSavePoint ( )
noexcept

Makes a note that the document's current state matches the one that is saved.

After this has been called, hasChangedSinceSavePoint() will return false until the document has been altered, and then it'll start returning true. If the document is altered, but then undone until it gets back to this state, hasChangedSinceSavePoint() will again return false.

See also
hasChangedSinceSavePoint

◆ undo()

void CodeDocument::undo ( )

Undo the last operation.

See also
UndoManager::undo

◆ writeToStream()

bool CodeDocument::writeToStream ( OutputStream stream)

Writes the editor's current contents to a stream.

Friends And Related Function Documentation

◆ CodeDocumentDeleteAction

friend class CodeDocumentDeleteAction
friend

◆ CodeDocumentInsertAction

friend class CodeDocumentInsertAction
friend

◆ Iterator

friend class Iterator
friend

◆ Position

friend class Position
friend

Member Data Documentation

◆ currentActionIndex

int CodeDocument::currentActionIndex
private

◆ indexOfSavedState

int CodeDocument::indexOfSavedState
private

◆ lines

OwnedArray<CodeDocumentLine> CodeDocument::lines
private

◆ listeners

ListenerList<Listener> CodeDocument::listeners
private

◆ maximumLineLength

int CodeDocument::maximumLineLength
private

◆ newLineChars

String CodeDocument::newLineChars
private

◆ positionsToMaintain

Array<Position*> CodeDocument::positionsToMaintain
private

◆ undoManager

UndoManager CodeDocument::undoManager
private

The documentation for this class was generated from the following file: