1 // Scintilla source code edit control
2 /** @file Document.h
3  ** Text document that handles notifications, DBCS, styling, words and end of line.
4  **/
5 // Copyright 1998-2011 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7 
8 #ifndef DOCUMENT_H
9 #define DOCUMENT_H
10 
11 namespace Scintilla {
12 
13 class DocWatcher;
14 class DocModification;
15 class Document;
16 class LineMarkers;
17 class LineLevels;
18 class LineState;
19 class LineAnnotation;
20 
21 enum class EncodingFamily { eightBit, unicode, dbcs };
22 
23 /**
24  * The range class represents a range of text in a document.
25  * The two values are not sorted as one end may be more significant than the other
26  * as is the case for the selection where the end position is the position of the caret.
27  * If either position is invalidPosition then the range is invalid and most operations will fail.
28  */
29 class Range {
30 public:
31 	Sci::Position start;
32 	Sci::Position end;
33 
34 	explicit Range(Sci::Position pos=0) noexcept :
start(pos)35 		start(pos), end(pos) {
36 	}
Range(Sci::Position start_,Sci::Position end_)37 	Range(Sci::Position start_, Sci::Position end_) noexcept :
38 		start(start_), end(end_) {
39 	}
40 
41 	bool operator==(const Range &other) const noexcept {
42 		return (start == other.start) && (end == other.end);
43 	}
44 
Valid()45 	bool Valid() const noexcept {
46 		return (start != Sci::invalidPosition) && (end != Sci::invalidPosition);
47 	}
48 
First()49 	Sci::Position First() const noexcept {
50 		return (start <= end) ? start : end;
51 	}
52 
Last()53 	Sci::Position Last() const noexcept {
54 		return (start > end) ? start : end;
55 	}
56 
57 	// Is the position within the range?
Contains(Sci::Position pos)58 	bool Contains(Sci::Position pos) const noexcept {
59 		if (start < end) {
60 			return (pos >= start && pos <= end);
61 		} else {
62 			return (pos <= start && pos >= end);
63 		}
64 	}
65 
66 	// Is the character after pos within the range?
ContainsCharacter(Sci::Position pos)67 	bool ContainsCharacter(Sci::Position pos) const noexcept {
68 		if (start < end) {
69 			return (pos >= start && pos < end);
70 		} else {
71 			return (pos < start && pos >= end);
72 		}
73 	}
74 
Contains(Range other)75 	bool Contains(Range other) const noexcept {
76 		return Contains(other.start) && Contains(other.end);
77 	}
78 
Overlaps(Range other)79 	bool Overlaps(Range other) const noexcept {
80 		return
81 		Contains(other.start) ||
82 		Contains(other.end) ||
83 		other.Contains(start) ||
84 		other.Contains(end);
85 	}
86 };
87 
88 /**
89  * Interface class for regular expression searching
90  */
91 class RegexSearchBase {
92 public:
~RegexSearchBase()93 	virtual ~RegexSearchBase() {}
94 
95 	virtual Sci::Position FindText(Document *doc, Sci::Position minPos, Sci::Position maxPos, const char *s,
96                         bool caseSensitive, bool word, bool wordStart, int flags, Sci::Position *length) = 0;
97 
98 	///@return String with the substitutions, must remain valid until the next call or destruction
99 	virtual const char *SubstituteByPosition(Document *doc, const char *text, Sci::Position *length) = 0;
100 };
101 
102 /// Factory function for RegexSearchBase
103 extern RegexSearchBase *CreateRegexSearch(CharClassify *charClassTable);
104 
105 struct StyledText {
106 	size_t length;
107 	const char *text;
108 	bool multipleStyles;
109 	size_t style;
110 	const unsigned char *styles;
StyledTextStyledText111 	StyledText(size_t length_, const char *text_, bool multipleStyles_, int style_, const unsigned char *styles_) noexcept :
112 		length(length_), text(text_), multipleStyles(multipleStyles_), style(style_), styles(styles_) {
113 	}
114 	// Return number of bytes from start to before '\n' or end of text.
115 	// Return 1 when start is outside text
LineLengthStyledText116 	size_t LineLength(size_t start) const noexcept {
117 		size_t cur = start;
118 		while ((cur < length) && (text[cur] != '\n'))
119 			cur++;
120 		return cur-start;
121 	}
StyleAtStyledText122 	size_t StyleAt(size_t i) const noexcept {
123 		return multipleStyles ? styles[i] : style;
124 	}
125 };
126 
127 class HighlightDelimiter {
128 public:
HighlightDelimiter()129 	HighlightDelimiter() noexcept : isEnabled(false) {
130 		Clear();
131 	}
132 
Clear()133 	void Clear() noexcept {
134 		beginFoldBlock = -1;
135 		endFoldBlock = -1;
136 		firstChangeableLineBefore = -1;
137 		firstChangeableLineAfter = -1;
138 	}
139 
NeedsDrawing(Sci::Line line)140 	bool NeedsDrawing(Sci::Line line) const noexcept {
141 		return isEnabled && (line <= firstChangeableLineBefore || line >= firstChangeableLineAfter);
142 	}
143 
IsFoldBlockHighlighted(Sci::Line line)144 	bool IsFoldBlockHighlighted(Sci::Line line) const noexcept {
145 		return isEnabled && beginFoldBlock != -1 && beginFoldBlock <= line && line <= endFoldBlock;
146 	}
147 
IsHeadOfFoldBlock(Sci::Line line)148 	bool IsHeadOfFoldBlock(Sci::Line line) const noexcept {
149 		return beginFoldBlock == line && line < endFoldBlock;
150 	}
151 
IsBodyOfFoldBlock(Sci::Line line)152 	bool IsBodyOfFoldBlock(Sci::Line line) const noexcept {
153 		return beginFoldBlock != -1 && beginFoldBlock < line && line < endFoldBlock;
154 	}
155 
IsTailOfFoldBlock(Sci::Line line)156 	bool IsTailOfFoldBlock(Sci::Line line) const noexcept {
157 		return beginFoldBlock != -1 && beginFoldBlock < line && line == endFoldBlock;
158 	}
159 
160 	Sci::Line beginFoldBlock;	// Begin of current fold block
161 	Sci::Line endFoldBlock;	// End of current fold block
162 	Sci::Line firstChangeableLineBefore;	// First line that triggers repaint before starting line that determined current fold block
163 	Sci::Line firstChangeableLineAfter;	// First line that triggers repaint after starting line that determined current fold block
164 	bool isEnabled;
165 };
166 
LevelNumber(int level)167 constexpr int LevelNumber(int level) noexcept {
168 	return level & SC_FOLDLEVELNUMBERMASK;
169 }
170 
171 class LexInterface {
172 protected:
173 	Document *pdoc;
174 	ILexer5 *instance;
175 	bool performingStyle;	///< Prevent reentrance
176 public:
LexInterface(Document * pdoc_)177 	explicit LexInterface(Document *pdoc_) noexcept : pdoc(pdoc_), instance(nullptr), performingStyle(false) {
178 	}
~LexInterface()179 	virtual ~LexInterface() {
180 	}
181 	void Colourise(Sci::Position start, Sci::Position end);
182 	virtual int LineEndTypesSupported();
UseContainerLexing()183 	bool UseContainerLexing() const noexcept {
184 		return instance == nullptr;
185 	}
186 };
187 
188 struct RegexError : public std::runtime_error {
RegexErrorRegexError189 	RegexError() : std::runtime_error("regex failure") {}
190 };
191 
192 /**
193  * The ActionDuration class stores the average time taken for some action such as styling or
194  * wrapping a line. It is used to decide how many repetitions of that action can be performed
195  * on idle to maximize efficiency without affecting application responsiveness.
196  * The duration changes if the time for the action changes. For example, if a simple lexer is
197  * changed to a complex lexer. Changes are damped and clamped to avoid short periods of easy
198  * or difficult processing moving the value too far leading to inefficiency or poor user
199  * experience.
200  */
201 
202 class ActionDuration {
203 	double duration;
204 	const double minDuration;
205 	const double maxDuration;
206 public:
207 	ActionDuration(double duration_, double minDuration_, double maxDuration_) noexcept;
208 	void AddSample(size_t numberActions, double durationOfActions) noexcept;
209 	double Duration() const noexcept;
210 };
211 
212 /**
213  */
214 class Document : PerLine, public IDocument, public ILoader {
215 
216 public:
217 	/** Used to pair watcher pointer with user data. */
218 	struct WatcherWithUserData {
219 		DocWatcher *watcher;
220 		void *userData;
221 		WatcherWithUserData(DocWatcher *watcher_=nullptr, void *userData_=nullptr) noexcept :
watcherWatcherWithUserData222 			watcher(watcher_), userData(userData_) {
223 		}
224 		bool operator==(const WatcherWithUserData &other) const noexcept {
225 			return (watcher == other.watcher) && (userData == other.userData);
226 		}
227 	};
228 
229 private:
230 	int refCount;
231 	CellBuffer cb;
232 	CharClassify charClass;
233 	CharacterCategoryMap charMap;
234 	std::unique_ptr<CaseFolder> pcf;
235 	Sci::Position endStyled;
236 	int styleClock;
237 	int enteredModification;
238 	int enteredStyling;
239 	int enteredReadOnlyCount;
240 
241 	bool insertionSet;
242 	std::string insertion;
243 
244 	std::vector<WatcherWithUserData> watchers;
245 
246 	// ldSize is not real data - it is for dimensions and loops
247 	enum lineData { ldMarkers, ldLevels, ldState, ldMargin, ldAnnotation, ldEOLAnnotation, ldSize };
248 	std::unique_ptr<PerLine> perLineData[ldSize];
249 	LineMarkers *Markers() const noexcept;
250 	LineLevels *Levels() const noexcept;
251 	LineState *States() const noexcept;
252 	LineAnnotation *Margins() const noexcept;
253 	LineAnnotation *Annotations() const noexcept;
254 	LineAnnotation *EOLAnnotations() const noexcept;
255 
256 	bool matchesValid;
257 	std::unique_ptr<RegexSearchBase> regex;
258 	std::unique_ptr<LexInterface> pli;
259 
260 public:
261 
262 	struct CharacterExtracted {
263 		unsigned int character;
264 		unsigned int widthBytes;
CharacterExtractedCharacterExtracted265 		CharacterExtracted(unsigned int character_, unsigned int widthBytes_) noexcept :
266 			character(character_), widthBytes(widthBytes_) {
267 		}
268 		// For DBCS characters turn 2 bytes into an int
DBCSCharacterExtracted269 		static CharacterExtracted DBCS(unsigned char lead, unsigned char trail) noexcept {
270 			return CharacterExtracted((lead << 8) | trail, 2);
271 		}
272 	};
273 
274 	int eolMode;
275 	/// Can also be SC_CP_UTF8 to enable UTF-8 mode
276 	int dbcsCodePage;
277 	int lineEndBitSet;
278 	int tabInChars;
279 	int indentInChars;
280 	int actualIndentInChars;
281 	bool useTabs;
282 	bool tabIndents;
283 	bool backspaceUnindents;
284 	ActionDuration durationStyleOneLine;
285 
286 	std::unique_ptr<IDecorationList> decorations;
287 
288 	Document(int options);
289 	// Deleted so Document objects can not be copied.
290 	Document(const Document &) = delete;
291 	Document(Document &&) = delete;
292 	void operator=(const Document &) = delete;
293 	Document &operator=(Document &&) = delete;
294 	~Document() override;
295 
296 	int AddRef();
297 	int SCI_METHOD Release() override;
298 
299 	// From PerLine
300 	void Init() override;
301 	void InsertLine(Sci::Line line) override;
302 	void InsertLines(Sci::Line line, Sci::Line lines) override;
303 	void RemoveLine(Sci::Line line) override;
304 
305 	int LineEndTypesSupported() const;
306 	bool SetDBCSCodePage(int dbcsCodePage_);
GetLineEndTypesAllowed()307 	int GetLineEndTypesAllowed() const noexcept { return cb.GetLineEndTypes(); }
308 	bool SetLineEndTypesAllowed(int lineEndBitSet_);
GetLineEndTypesActive()309 	int GetLineEndTypesActive() const noexcept { return cb.GetLineEndTypes(); }
310 
Version()311 	int SCI_METHOD Version() const override {
312 		return dvRelease4;
313 	}
314 
315 	void SCI_METHOD SetErrorStatus(int status) override;
316 
317 	Sci_Position SCI_METHOD LineFromPosition(Sci_Position pos) const override;
318 	Sci::Line SciLineFromPosition(Sci::Position pos) const noexcept;	// Avoids casting LineFromPosition
319 	Sci::Position ClampPositionIntoDocument(Sci::Position pos) const noexcept;
ContainsLineEnd(const char * s,Sci::Position length)320 	bool ContainsLineEnd(const char *s, Sci::Position length) const noexcept { return cb.ContainsLineEnd(s, length); }
321 	bool IsCrLf(Sci::Position pos) const noexcept;
322 	int LenChar(Sci::Position pos) const noexcept;
323 	bool InGoodUTF8(Sci::Position pos, Sci::Position &start, Sci::Position &end) const noexcept;
324 	Sci::Position MovePositionOutsideChar(Sci::Position pos, Sci::Position moveDir, bool checkLineEnd=true) const noexcept;
325 	Sci::Position NextPosition(Sci::Position pos, int moveDir) const noexcept;
326 	bool NextCharacter(Sci::Position &pos, int moveDir) const noexcept;	// Returns true if pos changed
327 	Document::CharacterExtracted CharacterAfter(Sci::Position position) const noexcept;
328 	Document::CharacterExtracted CharacterBefore(Sci::Position position) const noexcept;
329 	Sci_Position SCI_METHOD GetRelativePosition(Sci_Position positionStart, Sci_Position characterOffset) const override;
330 	Sci::Position GetRelativePositionUTF16(Sci::Position positionStart, Sci::Position characterOffset) const noexcept;
331 	int SCI_METHOD GetCharacterAndWidth(Sci_Position position, Sci_Position *pWidth) const override;
332 	int SCI_METHOD CodePage() const override;
333 	bool SCI_METHOD IsDBCSLeadByte(char ch) const override;
334 	bool IsDBCSLeadByteNoExcept(char ch) const noexcept;
335 	bool IsDBCSLeadByteInvalid(char ch) const noexcept;
336 	bool IsDBCSTrailByteInvalid(char ch) const noexcept;
337 	int DBCSDrawBytes(std::string_view text) const noexcept;
338 	int SafeSegment(const char *text, int length, int lengthSegment) const noexcept;
339 	EncodingFamily CodePageFamily() const noexcept;
340 
341 	// Gateways to modifying document
342 	void ModifiedAt(Sci::Position pos) noexcept;
343 	void CheckReadOnly();
344 	bool DeleteChars(Sci::Position pos, Sci::Position len);
345 	Sci::Position InsertString(Sci::Position position, const char *s, Sci::Position insertLength);
346 	void ChangeInsertion(const char *s, Sci::Position length);
347 	int SCI_METHOD AddData(const char *data, Sci_Position length) override;
348 	void * SCI_METHOD ConvertToDocument() override;
349 	Sci::Position Undo();
350 	Sci::Position Redo();
CanUndo()351 	bool CanUndo() const noexcept { return cb.CanUndo(); }
CanRedo()352 	bool CanRedo() const noexcept { return cb.CanRedo(); }
DeleteUndoHistory()353 	void DeleteUndoHistory() { cb.DeleteUndoHistory(); }
SetUndoCollection(bool collectUndo)354 	bool SetUndoCollection(bool collectUndo) {
355 		return cb.SetUndoCollection(collectUndo);
356 	}
IsCollectingUndo()357 	bool IsCollectingUndo() const noexcept { return cb.IsCollectingUndo(); }
BeginUndoAction()358 	void BeginUndoAction() { cb.BeginUndoAction(); }
EndUndoAction()359 	void EndUndoAction() { cb.EndUndoAction(); }
AddUndoAction(Sci::Position token,bool mayCoalesce)360 	void AddUndoAction(Sci::Position token, bool mayCoalesce) { cb.AddUndoAction(token, mayCoalesce); }
361 	void SetSavePoint();
IsSavePoint()362 	bool IsSavePoint() const noexcept { return cb.IsSavePoint(); }
363 
TentativeStart()364 	void TentativeStart() { cb.TentativeStart(); }
TentativeCommit()365 	void TentativeCommit() { cb.TentativeCommit(); }
366 	void TentativeUndo();
TentativeActive()367 	bool TentativeActive() const noexcept { return cb.TentativeActive(); }
368 
BufferPointer()369 	const char * SCI_METHOD BufferPointer() override { return cb.BufferPointer(); }
RangePointer(Sci::Position position,Sci::Position rangeLength)370 	const char *RangePointer(Sci::Position position, Sci::Position rangeLength) noexcept { return cb.RangePointer(position, rangeLength); }
GapPosition()371 	Sci::Position GapPosition() const noexcept { return cb.GapPosition(); }
372 
373 	int SCI_METHOD GetLineIndentation(Sci_Position line) override;
374 	Sci::Position SetLineIndentation(Sci::Line line, Sci::Position indent);
375 	Sci::Position GetLineIndentPosition(Sci::Line line) const;
376 	Sci::Position GetColumn(Sci::Position pos);
377 	Sci::Position CountCharacters(Sci::Position startPos, Sci::Position endPos) const noexcept;
378 	Sci::Position CountUTF16(Sci::Position startPos, Sci::Position endPos) const noexcept;
379 	Sci::Position FindColumn(Sci::Line line, Sci::Position column);
380 	void Indent(bool forwards, Sci::Line lineBottom, Sci::Line lineTop);
381 	static std::string TransformLineEnds(const char *s, size_t len, int eolModeWanted);
382 	void ConvertLineEnds(int eolModeSet);
SetReadOnly(bool set)383 	void SetReadOnly(bool set) { cb.SetReadOnly(set); }
IsReadOnly()384 	bool IsReadOnly() const noexcept { return cb.IsReadOnly(); }
IsLarge()385 	bool IsLarge() const noexcept { return cb.IsLarge(); }
386 	int Options() const noexcept;
387 
388 	void DelChar(Sci::Position pos);
389 	void DelCharBack(Sci::Position pos);
390 
CharAt(Sci::Position position)391 	char CharAt(Sci::Position position) const noexcept { return cb.CharAt(position); }
GetCharRange(char * buffer,Sci_Position position,Sci_Position lengthRetrieve)392 	void SCI_METHOD GetCharRange(char *buffer, Sci_Position position, Sci_Position lengthRetrieve) const override {
393 		cb.GetCharRange(buffer, position, lengthRetrieve);
394 	}
StyleAt(Sci_Position position)395 	char SCI_METHOD StyleAt(Sci_Position position) const override { return cb.StyleAt(position); }
StyleIndexAt(Sci_Position position)396 	int StyleIndexAt(Sci_Position position) const noexcept { return static_cast<unsigned char>(cb.StyleAt(position)); }
GetStyleRange(unsigned char * buffer,Sci::Position position,Sci::Position lengthRetrieve)397 	void GetStyleRange(unsigned char *buffer, Sci::Position position, Sci::Position lengthRetrieve) const {
398 		cb.GetStyleRange(buffer, position, lengthRetrieve);
399 	}
400 	int GetMark(Sci::Line line) const noexcept;
401 	Sci::Line MarkerNext(Sci::Line lineStart, int mask) const noexcept;
402 	int AddMark(Sci::Line line, int markerNum);
403 	void AddMarkSet(Sci::Line line, int valueSet);
404 	void DeleteMark(Sci::Line line, int markerNum);
405 	void DeleteMarkFromHandle(int markerHandle);
406 	void DeleteAllMarks(int markerNum);
407 	Sci::Line LineFromHandle(int markerHandle) const noexcept;
408 	int MarkerNumberFromLine(Sci::Line line, int which) const noexcept;
409 	int MarkerHandleFromLine(Sci::Line line, int which) const noexcept;
410 	Sci_Position SCI_METHOD LineStart(Sci_Position line) const override;
411 	bool IsLineStartPosition(Sci::Position position) const;
412 	Sci_Position SCI_METHOD LineEnd(Sci_Position line) const override;
413 	Sci::Position LineEndPosition(Sci::Position position) const;
414 	bool IsLineEndPosition(Sci::Position position) const;
415 	bool IsPositionInLineEnd(Sci::Position position) const;
416 	Sci::Position VCHomePosition(Sci::Position position) const;
417 	Sci::Position IndexLineStart(Sci::Line line, int lineCharacterIndex) const noexcept;
418 	Sci::Line LineFromPositionIndex(Sci::Position pos, int lineCharacterIndex) const noexcept;
419 
420 	int SCI_METHOD SetLevel(Sci_Position line, int level) override;
421 	int SCI_METHOD GetLevel(Sci_Position line) const override;
422 	void ClearLevels();
423 	Sci::Line GetLastChild(Sci::Line lineParent, int level=-1, Sci::Line lastLine=-1);
424 	Sci::Line GetFoldParent(Sci::Line line) const;
425 	void GetHighlightDelimiters(HighlightDelimiter &highlightDelimiter, Sci::Line line, Sci::Line lastLine);
426 
427 	Sci::Position ExtendWordSelect(Sci::Position pos, int delta, bool onlyWordCharacters=false) const;
428 	Sci::Position NextWordStart(Sci::Position pos, int delta) const;
429 	Sci::Position NextWordEnd(Sci::Position pos, int delta) const;
Length()430 	Sci_Position SCI_METHOD Length() const override { return cb.Length(); }
LengthNoExcept()431 	Sci::Position LengthNoExcept() const noexcept { return cb.Length(); }
Allocate(Sci::Position newSize)432 	void Allocate(Sci::Position newSize) { cb.Allocate(newSize); }
433 
434 	CharacterExtracted ExtractCharacter(Sci::Position position) const noexcept;
435 
436 	bool IsWordStartAt(Sci::Position pos) const;
437 	bool IsWordEndAt(Sci::Position pos) const;
438 	bool IsWordAt(Sci::Position start, Sci::Position end) const;
439 
440 	bool MatchesWordOptions(bool word, bool wordStart, Sci::Position pos, Sci::Position length) const;
441 	bool HasCaseFolder() const noexcept;
442 	void SetCaseFolder(CaseFolder *pcf_) noexcept;
443 	Sci::Position FindText(Sci::Position minPos, Sci::Position maxPos, const char *search, int flags, Sci::Position *length);
444 	const char *SubstituteByPosition(const char *text, Sci::Position *length);
445 	int LineCharacterIndex() const noexcept;
446 	void AllocateLineCharacterIndex(int lineCharacterIndex);
447 	void ReleaseLineCharacterIndex(int lineCharacterIndex);
448 	Sci::Line LinesTotal() const noexcept;
449 
450 	void SetDefaultCharClasses(bool includeWordClass);
451 	void SetCharClasses(const unsigned char *chars, CharClassify::cc newCharClass);
452 	int GetCharsOfClass(CharClassify::cc characterClass, unsigned char *buffer) const;
453 	void SetCharacterCategoryOptimization(int countCharacters);
454 	int CharacterCategoryOptimization() const noexcept;
455 	void SCI_METHOD StartStyling(Sci_Position position) override;
456 	bool SCI_METHOD SetStyleFor(Sci_Position length, char style) override;
457 	bool SCI_METHOD SetStyles(Sci_Position length, const char *styles) override;
GetEndStyled()458 	Sci::Position GetEndStyled() const noexcept { return endStyled; }
459 	void EnsureStyledTo(Sci::Position pos);
460 	void StyleToAdjustingLineDuration(Sci::Position pos);
461 	void LexerChanged();
GetStyleClock()462 	int GetStyleClock() const noexcept { return styleClock; }
463 	void IncrementStyleClock() noexcept;
464 	void SCI_METHOD DecorationSetCurrentIndicator(int indicator) override;
465 	void SCI_METHOD DecorationFillRange(Sci_Position position, int value, Sci_Position fillLength) override;
466 	LexInterface *GetLexInterface() const noexcept;
467 	void SetLexInterface(std::unique_ptr<LexInterface> pLexInterface) noexcept;
468 
469 	int SCI_METHOD SetLineState(Sci_Position line, int state) override;
470 	int SCI_METHOD GetLineState(Sci_Position line) const override;
471 	Sci::Line GetMaxLineState() const noexcept;
472 	void SCI_METHOD ChangeLexerState(Sci_Position start, Sci_Position end) override;
473 
474 	StyledText MarginStyledText(Sci::Line line) const noexcept;
475 	void MarginSetStyle(Sci::Line line, int style);
476 	void MarginSetStyles(Sci::Line line, const unsigned char *styles);
477 	void MarginSetText(Sci::Line line, const char *text);
478 	void MarginClearAll();
479 
480 	StyledText AnnotationStyledText(Sci::Line line) const noexcept;
481 	void AnnotationSetText(Sci::Line line, const char *text);
482 	void AnnotationSetStyle(Sci::Line line, int style);
483 	void AnnotationSetStyles(Sci::Line line, const unsigned char *styles);
484 	int AnnotationLines(Sci::Line line) const noexcept;
485 	void AnnotationClearAll();
486 
487 	StyledText EOLAnnotationStyledText(Sci::Line line) const noexcept;
488 	void EOLAnnotationSetStyle(Sci::Line line, int style);
489 	void EOLAnnotationSetText(Sci::Line line, const char *text);
490 	void EOLAnnotationClearAll();
491 
492 	bool AddWatcher(DocWatcher *watcher, void *userData);
493 	bool RemoveWatcher(DocWatcher *watcher, void *userData);
494 
495 	CharClassify::cc WordCharacterClass(unsigned int ch) const;
496 	bool IsWordPartSeparator(unsigned int ch) const;
497 	Sci::Position WordPartLeft(Sci::Position pos) const;
498 	Sci::Position WordPartRight(Sci::Position pos) const;
499 	Sci::Position ExtendStyleRange(Sci::Position pos, int delta, bool singleLine) noexcept;
500 	bool IsWhiteLine(Sci::Line line) const;
501 	Sci::Position ParaUp(Sci::Position pos) const;
502 	Sci::Position ParaDown(Sci::Position pos) const;
IndentSize()503 	int IndentSize() const noexcept { return actualIndentInChars; }
504 	Sci::Position BraceMatch(Sci::Position position, Sci::Position maxReStyle, Sci::Position startPos, bool useStartPos) noexcept;
505 
506 private:
507 	void NotifyModifyAttempt();
508 	void NotifySavePoint(bool atSavePoint);
509 	void NotifyModified(DocModification mh);
510 };
511 
512 class UndoGroup {
513 	Document *pdoc;
514 	bool groupNeeded;
515 public:
516 	UndoGroup(Document *pdoc_, bool groupNeeded_=true) :
pdoc(pdoc_)517 		pdoc(pdoc_), groupNeeded(groupNeeded_) {
518 		if (groupNeeded) {
519 			pdoc->BeginUndoAction();
520 		}
521 	}
522 	// Deleted so UndoGroup objects can not be copied.
523 	UndoGroup(const UndoGroup &) = delete;
524 	UndoGroup(UndoGroup &&) = delete;
525 	void operator=(const UndoGroup &) = delete;
526 	UndoGroup &operator=(UndoGroup &&) = delete;
~UndoGroup()527 	~UndoGroup() {
528 		if (groupNeeded) {
529 			pdoc->EndUndoAction();
530 		}
531 	}
Needed()532 	bool Needed() const noexcept {
533 		return groupNeeded;
534 	}
535 };
536 
537 
538 /**
539  * To optimise processing of document modifications by DocWatchers, a hint is passed indicating the
540  * scope of the change.
541  * If the DocWatcher is a document view then this can be used to optimise screen updating.
542  */
543 class DocModification {
544 public:
545 	int modificationType;
546 	Sci::Position position;
547 	Sci::Position length;
548 	Sci::Line linesAdded;	/**< Negative if lines deleted. */
549 	const char *text;	/**< Only valid for changes to text, not for changes to style. */
550 	Sci::Line line;
551 	int foldLevelNow;
552 	int foldLevelPrev;
553 	Sci::Line annotationLinesAdded;
554 	Sci::Position token;
555 
556 	DocModification(int modificationType_, Sci::Position position_=0, Sci::Position length_=0,
557 		Sci::Line linesAdded_=0, const char *text_=nullptr, Sci::Line line_=0) noexcept :
modificationType(modificationType_)558 		modificationType(modificationType_),
559 		position(position_),
560 		length(length_),
561 		linesAdded(linesAdded_),
562 		text(text_),
563 		line(line_),
564 		foldLevelNow(0),
565 		foldLevelPrev(0),
566 		annotationLinesAdded(0),
567 		token(0) {}
568 
569 	DocModification(int modificationType_, const Action &act, Sci::Line linesAdded_=0) noexcept :
modificationType(modificationType_)570 		modificationType(modificationType_),
571 		position(act.position),
572 		length(act.lenData),
573 		linesAdded(linesAdded_),
574 		text(act.data.get()),
575 		line(0),
576 		foldLevelNow(0),
577 		foldLevelPrev(0),
578 		annotationLinesAdded(0),
579 		token(0) {}
580 };
581 
582 /**
583  * A class that wants to receive notifications from a Document must be derived from DocWatcher
584  * and implement the notification methods. It can then be added to the watcher list with AddWatcher.
585  */
586 class DocWatcher {
587 public:
~DocWatcher()588 	virtual ~DocWatcher() {}
589 
590 	virtual void NotifyModifyAttempt(Document *doc, void *userData) = 0;
591 	virtual void NotifySavePoint(Document *doc, void *userData, bool atSavePoint) = 0;
592 	virtual void NotifyModified(Document *doc, DocModification mh, void *userData) = 0;
593 	virtual void NotifyDeleted(Document *doc, void *userData) noexcept = 0;
594 	virtual void NotifyStyleNeeded(Document *doc, void *userData, Sci::Position endPos) = 0;
595 	virtual void NotifyLexerChanged(Document *doc, void *userData) = 0;
596 	virtual void NotifyErrorOccurred(Document *doc, void *userData, int status) = 0;
597 };
598 
599 }
600 
601 #endif
602