1 // Scintilla source code edit control
2 /** @file DocumentAccessor.cxx
3 ** Rapid easy access to contents of a Scintilla.
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.
18 #include "DocumentAccessor.h"
19 #include "CellBuffer.h"
20 #include "Scintilla.h"
23 DocumentAccessor::~DocumentAccessor() {
27 bool DocumentAccessor::InternalIsLeadByte(char ch
) {
28 if (SC_CP_UTF8
== codePage
)
29 // For lexing, all characters >= 0x80 are treated the
30 // same so none is considered a lead byte.
33 return IsDBCSLeadByteEx(codePage
, ch
);
36 // PLAT_GTK or PLAT_WX
37 // TODO: support DBCS under GTK+ and WX
38 bool DocumentAccessor::InternalIsLeadByte(char) {
43 void DocumentAccessor::Fill(int position
) {
45 lenDoc
= pdoc
->Length();
46 startPos
= position
- slopSize
;
47 if (startPos
+ bufferSize
> lenDoc
)
48 startPos
= lenDoc
- bufferSize
;
51 endPos
= startPos
+ bufferSize
;
55 pdoc
->GetCharRange(buf
, startPos
, endPos
-startPos
);
56 buf
[endPos
-startPos
] = '\0';
59 char DocumentAccessor::StyleAt(int position
) {
60 return pdoc
->StyleAt(position
);
63 int DocumentAccessor::GetLine(int position
) {
64 return pdoc
->LineFromPosition(position
);
67 int DocumentAccessor::LineStart(int line
) {
68 return pdoc
->LineStart(line
);
71 int DocumentAccessor::LevelAt(int line
) {
72 return pdoc
->GetLevel(line
);
75 int DocumentAccessor::Length() {
77 lenDoc
= pdoc
->Length();
81 int DocumentAccessor::GetLineState(int line
) {
82 return pdoc
->GetLineState(line
);
85 int DocumentAccessor::SetLineState(int line
, int state
) {
86 return pdoc
->SetLineState(line
, state
);
89 void DocumentAccessor::StartAt(unsigned int start
, char chMask
) {
90 pdoc
->StartStyling(start
, chMask
);
93 void DocumentAccessor::StartSegment(unsigned int pos
) {
97 void DocumentAccessor::ColourTo(unsigned int pos
, int chAttr
) {
98 // Only perform styling if non empty range
99 if (pos
!= startSeg
- 1) {
100 if (pos
< startSeg
) {
101 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
104 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
106 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
107 // Too big for buffer so send directly
108 pdoc
->SetStyleFor(pos
- startSeg
+ 1, static_cast<char>(chAttr
));
110 if (chAttr
!= chWhile
)
113 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
114 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
121 void DocumentAccessor::SetLevel(int line
, int level
) {
122 pdoc
->SetLevel(line
, level
);
125 void DocumentAccessor::Flush() {
126 startPos
= extremePosition
;
129 pdoc
->SetStyles(validLen
, styleBuf
);
134 int DocumentAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
138 // Determines the indentation level of the current line and also checks for consistent
139 // indentation compared to the previous line.
140 // Indentation is judged consistent when the indentation whitespace of each line lines
141 // the same or the indentation of one line is a prefix of the other.
143 int pos
= LineStart(line
);
144 char ch
= (*this)[pos
];
146 bool inPrevPrefix
= line
> 0;
147 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
148 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
150 char chPrev
= (*this)[posPrev
++];
151 if (chPrev
== ' ' || chPrev
== '\t') {
153 spaceFlags
|= wsInconsistent
;
155 inPrevPrefix
= false;
159 spaceFlags
|= wsSpace
;
163 if (spaceFlags
& wsSpace
)
164 spaceFlags
|= wsSpaceTab
;
165 indent
= (indent
/ 8 + 1) * 8;
171 indent
+= SC_FOLDLEVELBASE
;
172 // if completely empty line or the start of a comment...
173 if ((ch
== ' ' || ch
== '\t' || ch
== '\n' || ch
== '\r') ||
174 (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
175 return indent
| SC_FOLDLEVELWHITEFLAG
;