]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/DocumentAccessor.cxx
c187f2a442b781cb80721729fa9960150b0731a7
[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 #if PLAT_WIN
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 IsDBCSLeadByteEx(codePage, ch);
34 }
35 #else
36 // PLAT_GTK or PLAT_WX
37 // TODO: support DBCS under GTK+ and WX
38 bool DocumentAccessor::InternalIsLeadByte(char) {
39 return false;
40 }
41 #endif
42
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;
54
55 pdoc->GetCharRange(buf, startPos, endPos-startPos);
56 buf[endPos-startPos] = '\0';
57 }
58
59 char DocumentAccessor::StyleAt(int position) {
60 return pdoc->StyleAt(position);
61 }
62
63 int DocumentAccessor::GetLine(int position) {
64 return pdoc->LineFromPosition(position);
65 }
66
67 int DocumentAccessor::LineStart(int line) {
68 return pdoc->LineStart(line);
69 }
70
71 int DocumentAccessor::LevelAt(int line) {
72 return pdoc->GetLevel(line);
73 }
74
75 int DocumentAccessor::Length() {
76 if (lenDoc == -1)
77 lenDoc = pdoc->Length();
78 return lenDoc;
79 }
80
81 int DocumentAccessor::GetLineState(int line) {
82 return pdoc->GetLineState(line);
83 }
84
85 int DocumentAccessor::SetLineState(int line, int state) {
86 return pdoc->SetLineState(line, state);
87 }
88
89 void DocumentAccessor::StartAt(unsigned int start, char chMask) {
90 pdoc->StartStyling(start, chMask);
91 }
92
93 void DocumentAccessor::StartSegment(unsigned int pos) {
94 startSeg = pos;
95 }
96
97 void DocumentAccessor::ColourTo(unsigned int pos, int chAttr) {
98 // Only perform styling if non empty range
99 if (pos != startSeg - 1) {
100 if (pos < startSeg) {
101 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg, pos);
102 }
103
104 if (validLen + (pos - startSeg + 1) >= bufferSize)
105 Flush();
106 if (validLen + (pos - startSeg + 1) >= bufferSize) {
107 // Too big for buffer so send directly
108 pdoc->SetStyleFor(pos - startSeg + 1, static_cast<char>(chAttr));
109 } else {
110 if (chAttr != chWhile)
111 chFlags = 0;
112 chAttr |= chFlags;
113 for (unsigned int i = startSeg; i <= pos; i++) {
114 styleBuf[validLen++] = static_cast<char>(chAttr);
115 }
116 }
117 }
118 startSeg = pos+1;
119 }
120
121 void DocumentAccessor::SetLevel(int line, int level) {
122 pdoc->SetLevel(line, level);
123 }
124
125 void DocumentAccessor::Flush() {
126 startPos = extremePosition;
127 lenDoc = -1;
128 if (validLen > 0) {
129 pdoc->SetStyles(validLen, styleBuf);
130 validLen = 0;
131 }
132 }
133
134 int DocumentAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
135 int end = Length();
136 int spaceFlags = 0;
137
138 // Determines the indentation level of the current line and also checks for consistent
139 // indentation compared to the previous line.
140 // Indentation is judged consistent when the indentation whitespace of each line lines
141 // the same or the indentation of one line is a prefix of the other.
142
143 int pos = LineStart(line);
144 char ch = (*this)[pos];
145 int indent = 0;
146 bool inPrevPrefix = line > 0;
147 int posPrev = inPrevPrefix ? LineStart(line-1) : 0;
148 while ((ch == ' ' || ch == '\t') && (pos < end)) {
149 if (inPrevPrefix) {
150 char chPrev = (*this)[posPrev++];
151 if (chPrev == ' ' || chPrev == '\t') {
152 if (chPrev != ch)
153 spaceFlags |= wsInconsistent;
154 } else {
155 inPrevPrefix = false;
156 }
157 }
158 if (ch == ' ') {
159 spaceFlags |= wsSpace;
160 indent++;
161 } else { // Tab
162 spaceFlags |= wsTab;
163 if (spaceFlags & wsSpace)
164 spaceFlags |= wsSpaceTab;
165 indent = (indent / 8 + 1) * 8;
166 }
167 ch = (*this)[++pos];
168 }
169
170 *flags = spaceFlags;
171 indent += SC_FOLDLEVELBASE;
172 // if completely empty line or the start of a comment...
173 if ((ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') ||
174 (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) )
175 return indent | SC_FOLDLEVELWHITEFLAG;
176 else
177 return indent;
178 }
179