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