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() {
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.
32 return Platform::IsDBCSLeadByte(codePage
, ch
);
35 void DocumentAccessor::Fill(int position
) {
37 lenDoc
= pdoc
->Length();
38 startPos
= position
- slopSize
;
39 if (startPos
+ bufferSize
> lenDoc
)
40 startPos
= lenDoc
- bufferSize
;
43 endPos
= startPos
+ bufferSize
;
47 pdoc
->GetCharRange(buf
, startPos
, endPos
-startPos
);
48 buf
[endPos
-startPos
] = '\0';
51 bool DocumentAccessor::Match(int pos
, const char *s
) {
52 for (int i
=0; *s
; i
++) {
53 if (*s
!= SafeGetCharAt(pos
+i
))
60 char DocumentAccessor::StyleAt(int position
) {
61 // Mask off all bits which aren't in the 'mask'.
62 return static_cast<char>(pdoc
->StyleAt(position
) & mask
);
65 int DocumentAccessor::GetLine(int position
) {
66 return pdoc
->LineFromPosition(position
);
69 int DocumentAccessor::LineStart(int line
) {
70 return pdoc
->LineStart(line
);
73 int DocumentAccessor::LevelAt(int line
) {
74 return pdoc
->GetLevel(line
);
77 int DocumentAccessor::Length() {
79 lenDoc
= pdoc
->Length();
83 int DocumentAccessor::GetLineState(int line
) {
84 return pdoc
->GetLineState(line
);
87 int DocumentAccessor::SetLineState(int line
, int state
) {
88 return pdoc
->SetLineState(line
, state
);
91 void DocumentAccessor::StartAt(unsigned int start
, char chMask
) {
92 // Store the mask specified for use with StyleAt.
94 pdoc
->StartStyling(start
, chMask
);
95 startPosStyling
= start
;
98 void DocumentAccessor::StartSegment(unsigned int pos
) {
102 void DocumentAccessor::ColourTo(unsigned int pos
, int chAttr
) {
103 // Only perform styling if non empty range
104 if (pos
!= startSeg
- 1) {
105 if (pos
< startSeg
) {
106 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
109 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
111 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
112 // Too big for buffer so send directly
113 pdoc
->SetStyleFor(pos
- startSeg
+ 1, static_cast<char>(chAttr
));
115 if (chAttr
!= chWhile
)
118 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
119 PLATFORM_ASSERT((startPosStyling
+ validLen
) < Length());
120 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
127 void DocumentAccessor::SetLevel(int line
, int level
) {
128 pdoc
->SetLevel(line
, level
);
131 void DocumentAccessor::Flush() {
132 startPos
= extremePosition
;
135 pdoc
->SetStyles(validLen
, styleBuf
);
136 startPosStyling
+= validLen
;
141 int DocumentAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
145 // Determines the indentation level of the current line and also checks for consistent
146 // indentation compared to the previous line.
147 // Indentation is judged consistent when the indentation whitespace of each line lines
148 // the same or the indentation of one line is a prefix of the other.
150 int pos
= LineStart(line
);
151 char ch
= (*this)[pos
];
153 bool inPrevPrefix
= line
> 0;
154 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
155 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
157 char chPrev
= (*this)[posPrev
++];
158 if (chPrev
== ' ' || chPrev
== '\t') {
160 spaceFlags
|= wsInconsistent
;
162 inPrevPrefix
= false;
166 spaceFlags
|= wsSpace
;
170 if (spaceFlags
& wsSpace
)
171 spaceFlags
|= wsSpaceTab
;
172 indent
= (indent
/ 8 + 1) * 8;
178 indent
+= SC_FOLDLEVELBASE
;
179 // if completely empty line or the start of a comment...
180 if ((ch
== ' ' || ch
== '\t' || ch
== '\n' || ch
== '\r') ||
181 (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
182 return indent
| SC_FOLDLEVELWHITEFLAG
;