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