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"
21 #include "CharClassify.h"
24 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 Platform::IsDBCSLeadByte(codePage
, ch
);
36 void DocumentAccessor::Fill(int position
) {
38 lenDoc
= pdoc
->Length();
39 startPos
= position
- slopSize
;
40 if (startPos
+ bufferSize
> lenDoc
)
41 startPos
= lenDoc
- bufferSize
;
44 endPos
= startPos
+ bufferSize
;
48 pdoc
->GetCharRange(buf
, startPos
, endPos
-startPos
);
49 buf
[endPos
-startPos
] = '\0';
52 bool DocumentAccessor::Match(int pos
, const char *s
) {
53 for (int i
=0; *s
; i
++) {
54 if (*s
!= SafeGetCharAt(pos
+i
))
61 char DocumentAccessor::StyleAt(int position
) {
62 // Mask off all bits which aren't in the 'mask'.
63 return static_cast<char>(pdoc
->StyleAt(position
) & mask
);
66 int DocumentAccessor::GetLine(int position
) {
67 return pdoc
->LineFromPosition(position
);
70 int DocumentAccessor::LineStart(int line
) {
71 return pdoc
->LineStart(line
);
74 int DocumentAccessor::LevelAt(int line
) {
75 return pdoc
->GetLevel(line
);
78 int DocumentAccessor::Length() {
80 lenDoc
= pdoc
->Length();
84 int DocumentAccessor::GetLineState(int line
) {
85 return pdoc
->GetLineState(line
);
88 int DocumentAccessor::SetLineState(int line
, int state
) {
89 return pdoc
->SetLineState(line
, state
);
92 void DocumentAccessor::StartAt(unsigned int start
, char chMask
) {
93 // Store the mask specified for use with StyleAt.
95 pdoc
->StartStyling(start
, chMask
);
96 startPosStyling
= start
;
99 void DocumentAccessor::StartSegment(unsigned int pos
) {
103 void DocumentAccessor::ColourTo(unsigned int pos
, int chAttr
) {
104 // Only perform styling if non empty range
105 if (pos
!= startSeg
- 1) {
106 if (pos
< startSeg
) {
107 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
110 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
112 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
113 // Too big for buffer so send directly
114 pdoc
->SetStyleFor(pos
- startSeg
+ 1, static_cast<char>(chAttr
));
116 if (chAttr
!= chWhile
)
119 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
120 PLATFORM_ASSERT((startPosStyling
+ validLen
) < Length());
121 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
128 void DocumentAccessor::SetLevel(int line
, int level
) {
129 pdoc
->SetLevel(line
, level
);
132 void DocumentAccessor::Flush() {
133 startPos
= extremePosition
;
136 pdoc
->SetStyles(validLen
, styleBuf
);
137 startPosStyling
+= validLen
;
142 int DocumentAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
146 // Determines the indentation level of the current line and also checks for consistent
147 // indentation compared to the previous line.
148 // Indentation is judged consistent when the indentation whitespace of each line lines
149 // the same or the indentation of one line is a prefix of the other.
151 int pos
= LineStart(line
);
152 char ch
= (*this)[pos
];
154 bool inPrevPrefix
= line
> 0;
155 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
156 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
158 char chPrev
= (*this)[posPrev
++];
159 if (chPrev
== ' ' || chPrev
== '\t') {
161 spaceFlags
|= wsInconsistent
;
163 inPrevPrefix
= false;
167 spaceFlags
|= wsSpace
;
171 if (spaceFlags
& wsSpace
)
172 spaceFlags
|= wsSpaceTab
;
173 indent
= (indent
/ 8 + 1) * 8;
179 indent
+= SC_FOLDLEVELBASE
;
180 // if completely empty line or the start of a comment...
181 if ((ch
== ' ' || ch
== '\t' || ch
== '\n' || ch
== '\r') ||
182 (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
183 return indent
| SC_FOLDLEVELWHITEFLAG
;