]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/DocumentAccessor.cxx
merged 2.2 branch
[wxWidgets.git] / src / stc / scintilla / src / DocumentAccessor.cxx
1 // SciTE - Scintilla based Text Editor
2 // Accessor.cxx - rapid easy access to contents of a Scintilla
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
5
6 #include <stdlib.h>
7 #include <ctype.h>
8 #include <stdio.h>
9
10 #include "Platform.h"
11
12 #include "PropSet.h"
13 #include "SVector.h"
14 #include "Accessor.h"
15 #include "DocumentAccessor.h"
16 #include "CellBuffer.h"
17 #include "Scintilla.h"
18 #include "Document.h"
19
20 DocumentAccessor::~DocumentAccessor() {
21 }
22
23 bool DocumentAccessor::InternalIsLeadByte(char ch) {
24 #if PLAT_GTK
25 // TODO: support DBCS under GTK+
26 return false;
27 #elif PLAT_WIN
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 #elif PLAT_WX
35 return false;
36 #endif
37 }
38
39 void DocumentAccessor::Fill(int position) {
40 if (lenDoc == -1)
41 lenDoc = pdoc->Length();
42 startPos = position - slopSize;
43 if (startPos + bufferSize > lenDoc)
44 startPos = lenDoc - bufferSize;
45 if (startPos < 0)
46 startPos = 0;
47 endPos = startPos + bufferSize;
48 if (endPos > lenDoc)
49 endPos = lenDoc;
50
51 pdoc->GetCharRange(buf, startPos, endPos-startPos);
52 buf[endPos-startPos] = '\0';
53 }
54
55 char DocumentAccessor::StyleAt(int position) {
56 return pdoc->StyleAt(position);
57 }
58
59 int DocumentAccessor::GetLine(int position) {
60 return pdoc->LineFromPosition(position);
61 }
62
63 int DocumentAccessor::LineStart(int line) {
64 return pdoc->LineStart(line);
65 }
66
67 int DocumentAccessor::LevelAt(int line) {
68 return pdoc->GetLevel(line);
69 }
70
71 int DocumentAccessor::Length() {
72 if (lenDoc == -1)
73 lenDoc = pdoc->Length();
74 return lenDoc;
75 }
76
77 int DocumentAccessor::GetLineState(int line) {
78 return pdoc->GetLineState(line);
79 }
80
81 int DocumentAccessor::SetLineState(int line, int state) {
82 return pdoc->SetLineState(line, state);
83 }
84
85 void DocumentAccessor::StartAt(unsigned int start, char chMask) {
86 pdoc->StartStyling(start, chMask);
87 }
88
89 void DocumentAccessor::StartSegment(unsigned int pos) {
90 startSeg = pos;
91 }
92
93 void DocumentAccessor::ColourTo(unsigned int pos, int chAttr) {
94 // Only perform styling if non empty range
95 if (pos != startSeg - 1) {
96 if (pos < startSeg) {
97 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg, pos);
98 }
99
100 if (validLen + (pos - startSeg + 1) >= bufferSize)
101 Flush();
102 if (validLen + (pos - startSeg + 1) >= bufferSize) {
103 // Too big for buffer so send directly
104 pdoc->SetStyleFor(pos - startSeg + 1, static_cast<char>(chAttr));
105 } else {
106 if (chAttr != chWhile)
107 chFlags = 0;
108 chAttr |= chFlags;
109 for (unsigned int i = startSeg; i <= pos; i++) {
110 styleBuf[validLen++] = static_cast<char>(chAttr);
111 }
112 }
113 }
114 startSeg = pos+1;
115 }
116
117 void DocumentAccessor::SetLevel(int line, int level) {
118 pdoc->SetLevel(line, level);
119 }
120
121 void DocumentAccessor::Flush() {
122 startPos = extremePosition;
123 lenDoc = -1;
124 if (validLen > 0) {
125 pdoc->SetStyles(validLen, styleBuf);
126 validLen = 0;
127 }
128 }
129
130 int DocumentAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
131 int end = Length();
132 int spaceFlags = 0;
133
134 // Determines the indentation level of the current line and also checks for consistent
135 // indentation compared to the previous line.
136 // Indentation is judged consistent when the indentation whitespace of each line lines
137 // the same or the indentation of one line is a prefix of the other.
138
139 int pos = LineStart(line);
140 char ch = (*this)[pos];
141 int indent = 0;
142 bool inPrevPrefix = line > 0;
143 int posPrev = inPrevPrefix ? LineStart(line-1) : 0;
144 while ((ch == ' ' || ch == '\t') && (pos < end)) {
145 if (inPrevPrefix) {
146 char chPrev = (*this)[posPrev++];
147 if (chPrev == ' ' || chPrev == '\t') {
148 if (chPrev != ch)
149 spaceFlags |= wsInconsistent;
150 } else {
151 inPrevPrefix = false;
152 }
153 }
154 if (ch == ' ') {
155 spaceFlags |= wsSpace;
156 indent++;
157 } else { // Tab
158 spaceFlags |= wsTab;
159 if (spaceFlags & wsSpace)
160 spaceFlags |= wsSpaceTab;
161 indent = (indent / 8 + 1) * 8;
162 }
163 ch = (*this)[++pos];
164 }
165
166 *flags = spaceFlags;
167 indent += SC_FOLDLEVELBASE;
168 // if completely empty line or the start of a comment...
169 if (isspace(ch) || (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) )
170 return indent | SC_FOLDLEVELWHITEFLAG;
171 else
172 return indent;
173 }
174