The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

418 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_CODEDOCUMENT_H_INCLUDED
  18. #define JUCE_CODEDOCUMENT_H_INCLUDED
  19. class CodeDocumentLine;
  20. //==============================================================================
  21. /**
  22. A class for storing and manipulating a source code file.
  23. When using a CodeEditorComponent, it takes one of these as its source object.
  24. The CodeDocument stores its content as an array of lines, which makes it
  25. quick to insert and delete.
  26. @see CodeEditorComponent
  27. */
  28. class JUCE_API CodeDocument
  29. {
  30. public:
  31. /** Creates a new, empty document. */
  32. CodeDocument();
  33. /** Destructor. */
  34. ~CodeDocument();
  35. //==============================================================================
  36. /** A position in a code document.
  37. Using this class you can find a position in a code document and quickly get its
  38. character position, line, and index. By calling setPositionMaintained (true), the
  39. position is automatically updated when text is inserted or deleted in the document,
  40. so that it maintains its original place in the text.
  41. */
  42. class JUCE_API Position
  43. {
  44. public:
  45. /** Creates an uninitialised position.
  46. Don't attempt to call any methods on this until you've given it an owner document
  47. to refer to!
  48. */
  49. Position() noexcept;
  50. /** Creates a position based on a line and index in a document.
  51. Note that this index is NOT the column number, it's the number of characters from the
  52. start of the line. The "column" number isn't quite the same, because if the line
  53. contains any tab characters, the relationship of the index to its visual column depends on
  54. the number of spaces per tab being used!
  55. Lines are numbered from zero, and if the line or index are beyond the bounds of the document,
  56. they will be adjusted to keep them within its limits.
  57. */
  58. Position (const CodeDocument& ownerDocument,
  59. int line, int indexInLine) noexcept;
  60. /** Creates a position based on a character index in a document.
  61. This position is placed at the specified number of characters from the start of the
  62. document. The line and column are auto-calculated.
  63. If the position is beyond the range of the document, it'll be adjusted to keep it
  64. inside.
  65. */
  66. Position (const CodeDocument& ownerDocument,
  67. int charactersFromStartOfDocument) noexcept;
  68. /** Creates a copy of another position.
  69. This will copy the position, but the new object will not be set to maintain its position,
  70. even if the source object was set to do so.
  71. */
  72. Position (const Position&) noexcept;
  73. /** Destructor. */
  74. ~Position();
  75. Position& operator= (const Position&);
  76. bool operator== (const Position&) const noexcept;
  77. bool operator!= (const Position&) const noexcept;
  78. /** Points this object at a new position within the document.
  79. If the position is beyond the range of the document, it'll be adjusted to keep it
  80. inside.
  81. @see getPosition, setLineAndIndex
  82. */
  83. void setPosition (int charactersFromStartOfDocument);
  84. /** Returns the position as the number of characters from the start of the document.
  85. @see setPosition, getLineNumber, getIndexInLine
  86. */
  87. int getPosition() const noexcept { return characterPos; }
  88. /** Moves the position to a new line and index within the line.
  89. Note that the index is NOT the column at which the position appears in an editor.
  90. If the line contains any tab characters, the relationship of the index to its
  91. visual position depends on the number of spaces per tab being used!
  92. Lines are numbered from zero, and if the line or index are beyond the bounds of the document,
  93. they will be adjusted to keep them within its limits.
  94. */
  95. void setLineAndIndex (int newLineNumber, int newIndexInLine);
  96. /** Returns the line number of this position.
  97. The first line in the document is numbered zero, not one!
  98. */
  99. int getLineNumber() const noexcept { return line; }
  100. /** Returns the number of characters from the start of the line.
  101. Note that this value is NOT the column at which the position appears in an editor.
  102. If the line contains any tab characters, the relationship of the index to its
  103. visual position depends on the number of spaces per tab being used!
  104. */
  105. int getIndexInLine() const noexcept { return indexInLine; }
  106. /** Allows the position to be automatically updated when the document changes.
  107. If this is set to true, the position will register with its document so that
  108. when the document has text inserted or deleted, this position will be automatically
  109. moved to keep it at the same position in the text.
  110. */
  111. void setPositionMaintained (bool isMaintained);
  112. //==============================================================================
  113. /** Moves the position forwards or backwards by the specified number of characters.
  114. @see movedBy
  115. */
  116. void moveBy (int characterDelta);
  117. /** Returns a position which is the same as this one, moved by the specified number of
  118. characters.
  119. @see moveBy
  120. */
  121. Position movedBy (int characterDelta) const;
  122. /** Returns a position which is the same as this one, moved up or down by the specified
  123. number of lines.
  124. @see movedBy
  125. */
  126. Position movedByLines (int deltaLines) const;
  127. /** Returns the character in the document at this position.
  128. @see getLineText
  129. */
  130. juce_wchar getCharacter() const;
  131. /** Returns the line from the document that this position is within.
  132. @see getCharacter, getLineNumber
  133. */
  134. String getLineText() const;
  135. private:
  136. CodeDocument* owner;
  137. int characterPos, line, indexInLine;
  138. bool positionMaintained;
  139. };
  140. //==============================================================================
  141. /** Returns the full text of the document. */
  142. String getAllContent() const;
  143. /** Returns a section of the document's text. */
  144. String getTextBetween (const Position& start, const Position& end) const;
  145. /** Returns a line from the document. */
  146. String getLine (int lineIndex) const noexcept;
  147. /** Returns the number of characters in the document. */
  148. int getNumCharacters() const noexcept;
  149. /** Returns the number of lines in the document. */
  150. int getNumLines() const noexcept { return lines.size(); }
  151. /** Returns the number of characters in the longest line of the document. */
  152. int getMaximumLineLength() noexcept;
  153. /** Deletes a section of the text.
  154. This operation is undoable.
  155. */
  156. void deleteSection (const Position& startPosition, const Position& endPosition);
  157. /** Deletes a section of the text.
  158. This operation is undoable.
  159. */
  160. void deleteSection (int startIndex, int endIndex);
  161. /** Inserts some text into the document at a given position.
  162. This operation is undoable.
  163. */
  164. void insertText (const Position& position, const String& text);
  165. /** Inserts some text into the document at a given position.
  166. This operation is undoable.
  167. */
  168. void insertText (int insertIndex, const String& text);
  169. /** Replaces a section of the text with a new string.
  170. This operation is undoable.
  171. */
  172. void replaceSection (int startIndex, int endIndex, const String& newText);
  173. /** Clears the document and replaces it with some new text.
  174. This operation is undoable - if you're trying to completely reset the document, you
  175. might want to also call clearUndoHistory() and setSavePoint() after using this method.
  176. */
  177. void replaceAllContent (const String& newContent);
  178. /** Analyses the changes between the current content and some new text, and applies
  179. those changes.
  180. */
  181. void applyChanges (const String& newContent);
  182. /** Replaces the editor's contents with the contents of a stream.
  183. This will also reset the undo history and save point marker.
  184. */
  185. bool loadFromStream (InputStream& stream);
  186. /** Writes the editor's current contents to a stream. */
  187. bool writeToStream (OutputStream& stream);
  188. //==============================================================================
  189. /** Returns the preferred new-line characters for the document.
  190. This will be either "\n", "\r\n", or (rarely) "\r".
  191. @see setNewLineCharacters
  192. */
  193. String getNewLineCharacters() const noexcept { return newLineChars; }
  194. /** Sets the new-line characters that the document should use.
  195. The string must be either "\n", "\r\n", or (rarely) "\r".
  196. @see getNewLineCharacters
  197. */
  198. void setNewLineCharacters (const String& newLineCharacters) noexcept;
  199. //==============================================================================
  200. /** Begins a new undo transaction.
  201. The document itself will not call this internally, so relies on whatever is using the
  202. document to periodically call this to break up the undo sequence into sensible chunks.
  203. @see UndoManager::beginNewTransaction
  204. */
  205. void newTransaction();
  206. /** Undo the last operation.
  207. @see UndoManager::undo
  208. */
  209. void undo();
  210. /** Redo the last operation.
  211. @see UndoManager::redo
  212. */
  213. void redo();
  214. /** Clears the undo history.
  215. @see UndoManager::clearUndoHistory
  216. */
  217. void clearUndoHistory();
  218. /** Returns the document's UndoManager */
  219. UndoManager& getUndoManager() noexcept { return undoManager; }
  220. //==============================================================================
  221. /** Makes a note that the document's current state matches the one that is saved.
  222. After this has been called, hasChangedSinceSavePoint() will return false until
  223. the document has been altered, and then it'll start returning true. If the document is
  224. altered, but then undone until it gets back to this state, hasChangedSinceSavePoint()
  225. will again return false.
  226. @see hasChangedSinceSavePoint
  227. */
  228. void setSavePoint() noexcept;
  229. /** Returns true if the state of the document differs from the state it was in when
  230. setSavePoint() was last called.
  231. @see setSavePoint
  232. */
  233. bool hasChangedSinceSavePoint() const noexcept;
  234. //==============================================================================
  235. /** Searches for a word-break. */
  236. Position findWordBreakAfter (const Position& position) const noexcept;
  237. /** Searches for a word-break. */
  238. Position findWordBreakBefore (const Position& position) const noexcept;
  239. /** Finds the token that contains the given position. */
  240. void findTokenContaining (const Position& pos, Position& start, Position& end) const noexcept;
  241. /** Finds the line that contains the given position. */
  242. void findLineContaining (const Position& pos, Position& start, Position& end) const noexcept;
  243. //==============================================================================
  244. /** An object that receives callbacks from the CodeDocument when its text changes.
  245. @see CodeDocument::addListener, CodeDocument::removeListener
  246. */
  247. class JUCE_API Listener
  248. {
  249. public:
  250. Listener() {}
  251. virtual ~Listener() {}
  252. /** Called by a CodeDocument when text is added. */
  253. virtual void codeDocumentTextInserted (const String& newText, int insertIndex) = 0;
  254. /** Called by a CodeDocument when text is deleted. */
  255. virtual void codeDocumentTextDeleted (int startIndex, int endIndex) = 0;
  256. };
  257. /** Registers a listener object to receive callbacks when the document changes.
  258. If the listener is already registered, this method has no effect.
  259. @see removeListener
  260. */
  261. void addListener (Listener* listener) noexcept;
  262. /** Deregisters a listener.
  263. @see addListener
  264. */
  265. void removeListener (Listener* listener) noexcept;
  266. //==============================================================================
  267. /** Iterates the text in a CodeDocument.
  268. This class lets you read characters from a CodeDocument. It's designed to be used
  269. by a CodeTokeniser object.
  270. @see CodeDocument
  271. */
  272. class JUCE_API Iterator
  273. {
  274. public:
  275. Iterator (const CodeDocument& document) noexcept;
  276. Iterator (const Iterator&) noexcept;
  277. Iterator& operator= (const Iterator&) noexcept;
  278. ~Iterator() noexcept;
  279. /** Reads the next character and returns it.
  280. @see peekNextChar
  281. */
  282. juce_wchar nextChar() noexcept;
  283. /** Reads the next character without advancing the current position. */
  284. juce_wchar peekNextChar() const noexcept;
  285. /** Advances the position by one character. */
  286. void skip() noexcept;
  287. /** Returns the position as the number of characters from the start of the document. */
  288. int getPosition() const noexcept { return position; }
  289. /** Skips over any whitespace characters until the next character is non-whitespace. */
  290. void skipWhitespace() noexcept;
  291. /** Skips forward until the next character will be the first character on the next line */
  292. void skipToEndOfLine() noexcept;
  293. /** Returns the line number of the next character. */
  294. int getLine() const noexcept { return line; }
  295. /** Returns true if the iterator has reached the end of the document. */
  296. bool isEOF() const noexcept;
  297. private:
  298. const CodeDocument* document;
  299. mutable String::CharPointerType charPointer;
  300. int line, position;
  301. };
  302. private:
  303. //==============================================================================
  304. friend class CodeDocumentInsertAction;
  305. friend class CodeDocumentDeleteAction;
  306. friend class Iterator;
  307. friend class Position;
  308. OwnedArray <CodeDocumentLine> lines;
  309. Array <Position*> positionsToMaintain;
  310. UndoManager undoManager;
  311. int currentActionIndex, indexOfSavedState;
  312. int maximumLineLength;
  313. ListenerList <Listener> listeners;
  314. String newLineChars;
  315. void insert (const String& text, int insertPos, bool undoable);
  316. void remove (int startPos, int endPos, bool undoable);
  317. void checkLastLineStatus();
  318. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CodeDocument)
  319. };
  320. #endif // JUCE_CODEDOCUMENT_H_INCLUDED