profiler: bgo#633018 crash in Anjuta IDE: misuse of xmlCleanupParser
[anjuta-extras.git] / plugins / scintilla / scintilla / DocumentAccessor.cxx
bloba25979dc2c9300b9025b627edae181d15aecf322
1 // Scintilla source code edit control
2 /** @file DocumentAccessor.cxx
3 ** Rapid easy access to contents of a Scintilla.
4 **/
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
13 #include "Platform.h"
15 #include "PropSet.h"
16 #include "Accessor.h"
17 #include "DocumentAccessor.h"
18 #include "SplitVector.h"
19 #include "Partitioning.h"
20 #include "RunStyles.h"
21 #include "CellBuffer.h"
22 #include "Scintilla.h"
23 #include "CharClassify.h"
24 #include "Decoration.h"
25 #include "Document.h"
27 #ifdef SCI_NAMESPACE
28 using namespace Scintilla;
29 #endif
31 DocumentAccessor::~DocumentAccessor() {
34 bool DocumentAccessor::InternalIsLeadByte(char ch) {
35 if (SC_CP_UTF8 == codePage)
36 // For lexing, all characters >= 0x80 are treated the
37 // same so none is considered a lead byte.
38 return false;
39 else
40 return Platform::IsDBCSLeadByte(codePage, ch);
43 void DocumentAccessor::Fill(int position) {
44 if (lenDoc == -1)
45 lenDoc = pdoc->Length();
46 startPos = position - slopSize;
47 if (startPos + bufferSize > lenDoc)
48 startPos = lenDoc - bufferSize;
49 if (startPos < 0)
50 startPos = 0;
51 endPos = startPos + bufferSize;
52 if (endPos > lenDoc)
53 endPos = lenDoc;
55 pdoc->GetCharRange(buf, startPos, endPos-startPos);
56 buf[endPos-startPos] = '\0';
59 bool DocumentAccessor::Match(int pos, const char *s) {
60 for (int i=0; *s; i++) {
61 if (*s != SafeGetCharAt(pos+i))
62 return false;
63 s++;
65 return true;
68 char DocumentAccessor::StyleAt(int position) {
69 // Mask off all bits which aren't in the 'mask'.
70 return static_cast<char>(pdoc->StyleAt(position) & mask);
73 int DocumentAccessor::GetLine(int position) {
74 return pdoc->LineFromPosition(position);
77 int DocumentAccessor::LineStart(int line) {
78 return pdoc->LineStart(line);
81 int DocumentAccessor::LevelAt(int line) {
82 return pdoc->GetLevel(line);
85 int DocumentAccessor::Length() {
86 if (lenDoc == -1)
87 lenDoc = pdoc->Length();
88 return lenDoc;
91 int DocumentAccessor::GetLineState(int line) {
92 return pdoc->GetLineState(line);
95 int DocumentAccessor::SetLineState(int line, int state) {
96 return pdoc->SetLineState(line, state);
99 void DocumentAccessor::StartAt(unsigned int start, char chMask) {
100 // Store the mask specified for use with StyleAt.
101 mask = chMask;
102 pdoc->StartStyling(start, chMask);
103 startPosStyling = start;
106 void DocumentAccessor::StartSegment(unsigned int pos) {
107 startSeg = pos;
110 void DocumentAccessor::ColourTo(unsigned int pos, int chAttr) {
111 // Only perform styling if non empty range
112 if (pos != startSeg - 1) {
113 PLATFORM_ASSERT(pos >= startSeg);
114 if (pos < startSeg) {
115 return;
118 if (validLen + (pos - startSeg + 1) >= bufferSize)
119 Flush();
120 if (validLen + (pos - startSeg + 1) >= bufferSize) {
121 // Too big for buffer so send directly
122 pdoc->SetStyleFor(pos - startSeg + 1, static_cast<char>(chAttr));
123 } else {
124 if (chAttr != chWhile)
125 chFlags = 0;
126 chAttr |= chFlags;
127 for (unsigned int i = startSeg; i <= pos; i++) {
128 PLATFORM_ASSERT((startPosStyling + validLen) < Length());
129 styleBuf[validLen++] = static_cast<char>(chAttr);
133 startSeg = pos+1;
136 void DocumentAccessor::SetLevel(int line, int level) {
137 pdoc->SetLevel(line, level);
140 void DocumentAccessor::Flush() {
141 startPos = extremePosition;
142 lenDoc = -1;
143 if (validLen > 0) {
144 pdoc->SetStyles(validLen, styleBuf);
145 startPosStyling += validLen;
146 validLen = 0;
150 int DocumentAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
151 int end = Length();
152 int spaceFlags = 0;
154 // Determines the indentation level of the current line and also checks for consistent
155 // indentation compared to the previous line.
156 // Indentation is judged consistent when the indentation whitespace of each line lines
157 // the same or the indentation of one line is a prefix of the other.
159 int pos = LineStart(line);
160 char ch = (*this)[pos];
161 int indent = 0;
162 bool inPrevPrefix = line > 0;
163 int posPrev = inPrevPrefix ? LineStart(line-1) : 0;
164 while ((ch == ' ' || ch == '\t') && (pos < end)) {
165 if (inPrevPrefix) {
166 char chPrev = (*this)[posPrev++];
167 if (chPrev == ' ' || chPrev == '\t') {
168 if (chPrev != ch)
169 spaceFlags |= wsInconsistent;
170 } else {
171 inPrevPrefix = false;
174 if (ch == ' ') {
175 spaceFlags |= wsSpace;
176 indent++;
177 } else { // Tab
178 spaceFlags |= wsTab;
179 if (spaceFlags & wsSpace)
180 spaceFlags |= wsSpaceTab;
181 indent = (indent / 8 + 1) * 8;
183 ch = (*this)[++pos];
186 *flags = spaceFlags;
187 indent += SC_FOLDLEVELBASE;
188 // if completely empty line or the start of a comment...
189 if ((ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') ||
190 (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) )
191 return indent | SC_FOLDLEVELWHITEFLAG;
192 else
193 return indent;
196 void DocumentAccessor::IndicatorFill(int start, int end, int indicator, int value) {
197 pdoc->decorations.SetCurrentIndicator(indicator);
198 pdoc->DecorationFillRange(start, value, end - start);