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.
16 #include "DocumentAccessor.h"
17 #include "CellBuffer.h"
18 #include "Scintilla.h"
21 DocumentAccessor::~DocumentAccessor() {
25 bool DocumentAccessor::InternalIsLeadByte(char ch
) {
26 if (SC_CP_UTF8
== codePage
)
27 // For lexing, all characters >= 0x80 are treated the
28 // same so none is considered a lead byte.
31 return IsDBCSLeadByteEx(codePage
, ch
);
34 // PLAT_GTK or PLAT_WX
35 // TODO: support DBCS under GTK+ and WX
36 bool DocumentAccessor::InternalIsLeadByte(char) {
41 void DocumentAccessor::Fill(int position
) {
43 lenDoc
= pdoc
->Length();
44 startPos
= position
- slopSize
;
45 if (startPos
+ bufferSize
> lenDoc
)
46 startPos
= lenDoc
- bufferSize
;
49 endPos
= startPos
+ bufferSize
;
53 pdoc
->GetCharRange(buf
, startPos
, endPos
-startPos
);
54 buf
[endPos
-startPos
] = '\0';
57 char DocumentAccessor::StyleAt(int position
) {
58 return pdoc
->StyleAt(position
);
61 int DocumentAccessor::GetLine(int position
) {
62 return pdoc
->LineFromPosition(position
);
65 int DocumentAccessor::LineStart(int line
) {
66 return pdoc
->LineStart(line
);
69 int DocumentAccessor::LevelAt(int line
) {
70 return pdoc
->GetLevel(line
);
73 int DocumentAccessor::Length() {
75 lenDoc
= pdoc
->Length();
79 int DocumentAccessor::GetLineState(int line
) {
80 return pdoc
->GetLineState(line
);
83 int DocumentAccessor::SetLineState(int line
, int state
) {
84 return pdoc
->SetLineState(line
, state
);
87 void DocumentAccessor::StartAt(unsigned int start
, char chMask
) {
88 pdoc
->StartStyling(start
, chMask
);
91 void DocumentAccessor::StartSegment(unsigned int pos
) {
95 void DocumentAccessor::ColourTo(unsigned int pos
, int chAttr
) {
96 // Only perform styling if non empty range
97 if (pos
!= startSeg
- 1) {
99 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
102 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
104 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
105 // Too big for buffer so send directly
106 pdoc
->SetStyleFor(pos
- startSeg
+ 1, static_cast<char>(chAttr
));
108 if (chAttr
!= chWhile
)
111 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
112 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
119 void DocumentAccessor::SetLevel(int line
, int level
) {
120 pdoc
->SetLevel(line
, level
);
123 void DocumentAccessor::Flush() {
124 startPos
= extremePosition
;
127 pdoc
->SetStyles(validLen
, styleBuf
);
132 int DocumentAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
136 // Determines the indentation level of the current line and also checks for consistent
137 // indentation compared to the previous line.
138 // Indentation is judged consistent when the indentation whitespace of each line lines
139 // the same or the indentation of one line is a prefix of the other.
141 int pos
= LineStart(line
);
142 char ch
= (*this)[pos
];
144 bool inPrevPrefix
= line
> 0;
145 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
146 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
148 char chPrev
= (*this)[posPrev
++];
149 if (chPrev
== ' ' || chPrev
== '\t') {
151 spaceFlags
|= wsInconsistent
;
153 inPrevPrefix
= false;
157 spaceFlags
|= wsSpace
;
161 if (spaceFlags
& wsSpace
)
162 spaceFlags
|= wsSpaceTab
;
163 indent
= (indent
/ 8 + 1) * 8;
169 indent
+= SC_FOLDLEVELBASE
;
170 // if completely empty line or the start of a comment...
171 if (isspace(ch
) || (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
172 return indent
| SC_FOLDLEVELWHITEFLAG
;