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