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 return pdoc
->StyleAt(position
);
64 int DocumentAccessor::GetLine(int position
) {
65 return pdoc
->LineFromPosition(position
);
68 int DocumentAccessor::LineStart(int line
) {
69 return pdoc
->LineStart(line
);
72 int DocumentAccessor::LevelAt(int line
) {
73 return pdoc
->GetLevel(line
);
76 int DocumentAccessor::Length() {
78 lenDoc
= pdoc
->Length();
82 int DocumentAccessor::GetLineState(int line
) {
83 return pdoc
->GetLineState(line
);
86 int DocumentAccessor::SetLineState(int line
, int state
) {
87 return pdoc
->SetLineState(line
, state
);
90 void DocumentAccessor::StartAt(unsigned int start
, char chMask
) {
91 pdoc
->StartStyling(start
, chMask
);
92 startPosStyling
= start
;
95 void DocumentAccessor::StartSegment(unsigned int pos
) {
99 void DocumentAccessor::ColourTo(unsigned int pos
, int chAttr
) {
100 // Only perform styling if non empty range
101 if (pos
!= startSeg
- 1) {
102 if (pos
< startSeg
) {
103 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
106 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
108 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
109 // Too big for buffer so send directly
110 pdoc
->SetStyleFor(pos
- startSeg
+ 1, static_cast<char>(chAttr
));
112 if (chAttr
!= chWhile
)
115 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
116 PLATFORM_ASSERT((startPosStyling
+ validLen
) < Length());
117 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
124 void DocumentAccessor::SetLevel(int line
, int level
) {
125 pdoc
->SetLevel(line
, level
);
128 void DocumentAccessor::Flush() {
129 startPos
= extremePosition
;
132 pdoc
->SetStyles(validLen
, styleBuf
);
133 startPosStyling
+= validLen
;
138 int DocumentAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
142 // Determines the indentation level of the current line and also checks for consistent
143 // indentation compared to the previous line.
144 // Indentation is judged consistent when the indentation whitespace of each line lines
145 // the same or the indentation of one line is a prefix of the other.
147 int pos
= LineStart(line
);
148 char ch
= (*this)[pos
];
150 bool inPrevPrefix
= line
> 0;
151 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
152 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
154 char chPrev
= (*this)[posPrev
++];
155 if (chPrev
== ' ' || chPrev
== '\t') {
157 spaceFlags
|= wsInconsistent
;
159 inPrevPrefix
= false;
163 spaceFlags
|= wsSpace
;
167 if (spaceFlags
& wsSpace
)
168 spaceFlags
|= wsSpaceTab
;
169 indent
= (indent
/ 8 + 1) * 8;
175 indent
+= SC_FOLDLEVELBASE
;
176 // if completely empty line or the start of a comment...
177 if ((ch
== ' ' || ch
== '\t' || ch
== '\n' || ch
== '\r') ||
178 (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
179 return indent
| SC_FOLDLEVELWHITEFLAG
;