]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/DocumentAccessor.cxx
c695c5f510e510131f0b496cc9eae95efddb7b7b
[wxWidgets.git] / src / stc / scintilla / src / DocumentAccessor.cxx
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.
7
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include <stdio.h>
12
13 #include "Platform.h"
14
15 #include "PropSet.h"
16 #include "SVector.h"
17 #include "Accessor.h"
18 #include "DocumentAccessor.h"
19 #include "CellBuffer.h"
20 #include "Scintilla.h"
21 #include "CharClassify.h"
22 #include "Document.h"
23
24 DocumentAccessor::~DocumentAccessor() {
25 }
26
27 bool DocumentAccessor::InternalIsLeadByte(char ch) {
28 if (SC_CP_UTF8 == codePage)
29 // For lexing, all characters >= 0x80 are treated the
30 // same so none is considered a lead byte.
31 return false;
32 else
33 return Platform::IsDBCSLeadByte(codePage, ch);
34 }
35
36 void DocumentAccessor::Fill(int position) {
37 if (lenDoc == -1)
38 lenDoc = pdoc->Length();
39 startPos = position - slopSize;
40 if (startPos + bufferSize > lenDoc)
41 startPos = lenDoc - bufferSize;
42 if (startPos < 0)
43 startPos = 0;
44 endPos = startPos + bufferSize;
45 if (endPos > lenDoc)
46 endPos = lenDoc;
47
48 pdoc->GetCharRange(buf, startPos, endPos-startPos);
49 buf[endPos-startPos] = '\0';
50 }
51
52 bool DocumentAccessor::Match(int pos, const char *s) {
53 for (int i=0; *s; i++) {
54 if (*s != SafeGetCharAt(pos+i))
55 return false;
56 s++;
57 }
58 return true;
59 }
60
61 char DocumentAccessor::StyleAt(int position) {
62 // Mask off all bits which aren't in the 'mask'.
63 return static_cast<char>(pdoc->StyleAt(position) & mask);
64 }
65
66 int DocumentAccessor::GetLine(int position) {
67 return pdoc->LineFromPosition(position);
68 }
69
70 int DocumentAccessor::LineStart(int line) {
71 return pdoc->LineStart(line);
72 }
73
74 int DocumentAccessor::LevelAt(int line) {
75 return pdoc->GetLevel(line);
76 }
77
78 int DocumentAccessor::Length() {
79 if (lenDoc == -1)
80 lenDoc = pdoc->Length();
81 return lenDoc;
82 }
83
84 int DocumentAccessor::GetLineState(int line) {
85 return pdoc->GetLineState(line);
86 }
87
88 int DocumentAccessor::SetLineState(int line, int state) {
89 return pdoc->SetLineState(line, state);
90 }
91
92 void DocumentAccessor::StartAt(unsigned int start, char chMask) {
93 // Store the mask specified for use with StyleAt.
94 mask = chMask;
95 pdoc->StartStyling(start, chMask);
96 startPosStyling = start;
97 }
98
99 void DocumentAccessor::StartSegment(unsigned int pos) {
100 startSeg = pos;
101 }
102
103 void DocumentAccessor::ColourTo(unsigned int pos, int chAttr) {
104 // Only perform styling if non empty range
105 if (pos != startSeg - 1) {
106 if (pos < startSeg) {
107 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg, pos);
108 }
109
110 if (validLen + (pos - startSeg + 1) >= bufferSize)
111 Flush();
112 if (validLen + (pos - startSeg + 1) >= bufferSize) {
113 // Too big for buffer so send directly
114 pdoc->SetStyleFor(pos - startSeg + 1, static_cast<char>(chAttr));
115 } else {
116 if (chAttr != chWhile)
117 chFlags = 0;
118 chAttr |= chFlags;
119 for (unsigned int i = startSeg; i <= pos; i++) {
120 PLATFORM_ASSERT((startPosStyling + validLen) < Length());
121 styleBuf[validLen++] = static_cast<char>(chAttr);
122 }
123 }
124 }
125 startSeg = pos+1;
126 }
127
128 void DocumentAccessor::SetLevel(int line, int level) {
129 pdoc->SetLevel(line, level);
130 }
131
132 void DocumentAccessor::Flush() {
133 startPos = extremePosition;
134 lenDoc = -1;
135 if (validLen > 0) {
136 pdoc->SetStyles(validLen, styleBuf);
137 startPosStyling += validLen;
138 validLen = 0;
139 }
140 }
141
142 int DocumentAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
143 int end = Length();
144 int spaceFlags = 0;
145
146 // Determines the indentation level of the current line and also checks for consistent
147 // indentation compared to the previous line.
148 // Indentation is judged consistent when the indentation whitespace of each line lines
149 // the same or the indentation of one line is a prefix of the other.
150
151 int pos = LineStart(line);
152 char ch = (*this)[pos];
153 int indent = 0;
154 bool inPrevPrefix = line > 0;
155 int posPrev = inPrevPrefix ? LineStart(line-1) : 0;
156 while ((ch == ' ' || ch == '\t') && (pos < end)) {
157 if (inPrevPrefix) {
158 char chPrev = (*this)[posPrev++];
159 if (chPrev == ' ' || chPrev == '\t') {
160 if (chPrev != ch)
161 spaceFlags |= wsInconsistent;
162 } else {
163 inPrevPrefix = false;
164 }
165 }
166 if (ch == ' ') {
167 spaceFlags |= wsSpace;
168 indent++;
169 } else { // Tab
170 spaceFlags |= wsTab;
171 if (spaceFlags & wsSpace)
172 spaceFlags |= wsSpaceTab;
173 indent = (indent / 8 + 1) * 8;
174 }
175 ch = (*this)[++pos];
176 }
177
178 *flags = spaceFlags;
179 indent += SC_FOLDLEVELBASE;
180 // if completely empty line or the start of a comment...
181 if ((ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') ||
182 (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) )
183 return indent | SC_FOLDLEVELWHITEFLAG;
184 else
185 return indent;
186 }
187