Commit | Line | Data |
---|---|---|
65ec6247 RD |
1 | // Scintilla source code edit control |
2 | /** @file WindowAccessor.cxx | |
3 | ** Rapid easy access to contents of a Scintilla. | |
4 | **/ | |
5 | // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org> | |
f6bcfd97 BP |
6 | // The License.txt file describes the conditions under which this software may be distributed. |
7 | ||
8 | #include <stdlib.h> | |
d134f170 | 9 | #include <string.h> |
88a8b04e | 10 | #include <ctype.h> |
f6bcfd97 BP |
11 | #include <stdio.h> |
12 | ||
13 | #include "Platform.h" | |
14 | ||
15 | #include "PropSet.h" | |
16 | #include "Accessor.h" | |
17 | #include "WindowAccessor.h" | |
18 | #include "Scintilla.h" | |
19 | ||
20 | WindowAccessor::~WindowAccessor() { | |
21 | } | |
22 | ||
23 | bool WindowAccessor::InternalIsLeadByte(char ch) { | |
f6bcfd97 BP |
24 | if (SC_CP_UTF8 == codePage) |
25 | // For lexing, all characters >= 0x80 are treated the | |
26 | // same so none is considered a lead byte. | |
88a8b04e | 27 | return false; |
f6bcfd97 | 28 | else |
1a2fb4cd | 29 | return Platform::IsDBCSLeadByte(codePage, ch); |
d134f170 | 30 | } |
f6bcfd97 BP |
31 | |
32 | void WindowAccessor::Fill(int position) { | |
33 | if (lenDoc == -1) | |
d134f170 | 34 | lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0); |
f6bcfd97 BP |
35 | startPos = position - slopSize; |
36 | if (startPos + bufferSize > lenDoc) | |
37 | startPos = lenDoc - bufferSize; | |
38 | if (startPos < 0) | |
39 | startPos = 0; | |
40 | endPos = startPos + bufferSize; | |
41 | if (endPos > lenDoc) | |
42 | endPos = lenDoc; | |
43 | ||
d134f170 | 44 | TextRange tr = {{startPos, endPos}, buf}; |
a834585d RD |
45 | Platform::SendScintillaPointer(id, SCI_GETTEXTRANGE, 0, &tr); |
46 | } | |
47 | ||
48 | bool WindowAccessor::Match(int pos, const char *s) { | |
49 | for (int i=0; *s; i++) { | |
50 | if (*s != SafeGetCharAt(pos+i)) | |
51 | return false; | |
52 | s++; | |
53 | } | |
54 | return true; | |
f6bcfd97 BP |
55 | } |
56 | ||
57 | char WindowAccessor::StyleAt(int position) { | |
58 | return static_cast<char>(Platform::SendScintilla( | |
59 | id, SCI_GETSTYLEAT, position, 0)); | |
60 | } | |
61 | ||
62 | int WindowAccessor::GetLine(int position) { | |
d134f170 | 63 | return Platform::SendScintilla(id, SCI_LINEFROMPOSITION, position, 0); |
f6bcfd97 BP |
64 | } |
65 | ||
66 | int WindowAccessor::LineStart(int line) { | |
d134f170 | 67 | return Platform::SendScintilla(id, SCI_POSITIONFROMLINE, line, 0); |
f6bcfd97 BP |
68 | } |
69 | ||
70 | int WindowAccessor::LevelAt(int line) { | |
71 | return Platform::SendScintilla(id, SCI_GETFOLDLEVEL, line, 0); | |
72 | } | |
73 | ||
88a8b04e RD |
74 | int WindowAccessor::Length() { |
75 | if (lenDoc == -1) | |
d134f170 | 76 | lenDoc = Platform::SendScintilla(id, SCI_GETTEXTLENGTH, 0, 0); |
88a8b04e | 77 | return lenDoc; |
f6bcfd97 BP |
78 | } |
79 | ||
80 | int WindowAccessor::GetLineState(int line) { | |
81 | return Platform::SendScintilla(id, SCI_GETLINESTATE, line); | |
82 | } | |
83 | ||
84 | int WindowAccessor::SetLineState(int line, int state) { | |
85 | return Platform::SendScintilla(id, SCI_SETLINESTATE, line, state); | |
86 | } | |
87 | ||
88 | void WindowAccessor::StartAt(unsigned int start, char chMask) { | |
89 | Platform::SendScintilla(id, SCI_STARTSTYLING, start, chMask); | |
90 | } | |
91 | ||
92 | void WindowAccessor::StartSegment(unsigned int pos) { | |
93 | startSeg = pos; | |
94 | } | |
95 | ||
96 | void WindowAccessor::ColourTo(unsigned int pos, int chAttr) { | |
97 | // Only perform styling if non empty range | |
98 | if (pos != startSeg - 1) { | |
99 | if (pos < startSeg) { | |
100 | Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg, pos); | |
101 | } | |
102 | ||
103 | if (validLen + (pos - startSeg + 1) >= bufferSize) | |
104 | Flush(); | |
105 | if (validLen + (pos - startSeg + 1) >= bufferSize) { | |
106 | // Too big for buffer so send directly | |
107 | Platform::SendScintilla(id, SCI_SETSTYLING, pos - startSeg + 1, chAttr); | |
108 | } else { | |
109 | if (chAttr != chWhile) | |
110 | chFlags = 0; | |
111 | chAttr |= chFlags; | |
112 | for (unsigned int i = startSeg; i <= pos; i++) { | |
113 | styleBuf[validLen++] = static_cast<char>(chAttr); | |
114 | } | |
115 | } | |
116 | } | |
117 | startSeg = pos+1; | |
118 | } | |
119 | ||
120 | void WindowAccessor::SetLevel(int line, int level) { | |
121 | Platform::SendScintilla(id, SCI_SETFOLDLEVEL, line, level); | |
122 | } | |
123 | ||
124 | void WindowAccessor::Flush() { | |
125 | startPos = extremePosition; | |
126 | lenDoc = -1; | |
127 | if (validLen > 0) { | |
88a8b04e | 128 | Platform::SendScintillaPointer(id, SCI_SETSTYLINGEX, validLen, |
a834585d | 129 | styleBuf); |
f6bcfd97 BP |
130 | validLen = 0; |
131 | } | |
132 | } | |
133 | ||
134 | int WindowAccessor::IndentAmount(int line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) { | |
135 | int end = Length(); | |
136 | int spaceFlags = 0; | |
88a8b04e RD |
137 | |
138 | // Determines the indentation level of the current line and also checks for consistent | |
f6bcfd97 | 139 | // indentation compared to the previous line. |
88a8b04e | 140 | // Indentation is judged consistent when the indentation whitespace of each line lines |
f6bcfd97 | 141 | // the same or the indentation of one line is a prefix of the other. |
88a8b04e | 142 | |
f6bcfd97 BP |
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 | } | |
88a8b04e | 169 | |
f6bcfd97 BP |
170 | *flags = spaceFlags; |
171 | indent += SC_FOLDLEVELBASE; | |
172 | // if completely empty line or the start of a comment... | |
173 | if (isspace(ch) || (pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)) ) | |
174 | return indent | SC_FOLDLEVELWHITEFLAG; | |
175 | else | |
176 | return indent; | |
177 | } | |
178 |