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 char DocumentAccessor::StyleAt(int position
) {
52 return pdoc
->StyleAt(position
);
55 int DocumentAccessor::GetLine(int position
) {
56 return pdoc
->LineFromPosition(position
);
59 int DocumentAccessor::LineStart(int line
) {
60 return pdoc
->LineStart(line
);
63 int DocumentAccessor::LevelAt(int line
) {
64 return pdoc
->GetLevel(line
);
67 int DocumentAccessor::Length() {
69 lenDoc
= pdoc
->Length();
73 int DocumentAccessor::GetLineState(int line
) {
74 return pdoc
->GetLineState(line
);
77 int DocumentAccessor::SetLineState(int line
, int state
) {
78 return pdoc
->SetLineState(line
, state
);
81 void DocumentAccessor::StartAt(unsigned int start
, char chMask
) {
82 pdoc
->StartStyling(start
, chMask
);
83 startPosStyling
= start
;
86 void DocumentAccessor::StartSegment(unsigned int pos
) {
90 void DocumentAccessor::ColourTo(unsigned int pos
, int chAttr
) {
91 // Only perform styling if non empty range
92 if (pos
!= startSeg
- 1) {
94 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
97 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
99 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
100 // Too big for buffer so send directly
101 pdoc
->SetStyleFor(pos
- startSeg
+ 1, static_cast<char>(chAttr
));
103 if (chAttr
!= chWhile
)
106 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
107 PLATFORM_ASSERT((startPosStyling
+ validLen
) < Length());
108 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
115 void DocumentAccessor::SetLevel(int line
, int level
) {
116 pdoc
->SetLevel(line
, level
);
119 void DocumentAccessor::Flush() {
120 startPos
= extremePosition
;
123 pdoc
->SetStyles(validLen
, styleBuf
);
125 startPosStyling
+= validLen
;
129 int DocumentAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
133 // Determines the indentation level of the current line and also checks for consistent
134 // indentation compared to the previous line.
135 // Indentation is judged consistent when the indentation whitespace of each line lines
136 // the same or the indentation of one line is a prefix of the other.
138 int pos
= LineStart(line
);
139 char ch
= (*this)[pos
];
141 bool inPrevPrefix
= line
> 0;
142 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
143 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
145 char chPrev
= (*this)[posPrev
++];
146 if (chPrev
== ' ' || chPrev
== '\t') {
148 spaceFlags
|= wsInconsistent
;
150 inPrevPrefix
= false;
154 spaceFlags
|= wsSpace
;
158 if (spaceFlags
& wsSpace
)
159 spaceFlags
|= wsSpaceTab
;
160 indent
= (indent
/ 8 + 1) * 8;
166 indent
+= SC_FOLDLEVELBASE
;
167 // if completely empty line or the start of a comment...
168 if ((ch
== ' ' || ch
== '\t' || ch
== '\n' || ch
== '\r') ||
169 (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
170 return indent
| SC_FOLDLEVELWHITEFLAG
;