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.
17 #include "DocumentAccessor.h"
18 #include "SplitVector.h"
19 #include "Partitioning.h"
20 #include "RunStyles.h"
21 #include "CellBuffer.h"
22 #include "Scintilla.h"
23 #include "CharClassify.h"
24 #include "Decoration.h"
28 using namespace Scintilla
;
31 DocumentAccessor::~DocumentAccessor() {
34 bool DocumentAccessor::InternalIsLeadByte(char ch
) {
35 if (SC_CP_UTF8
== codePage
)
36 // For lexing, all characters >= 0x80 are treated the
37 // same so none is considered a lead byte.
40 return Platform::IsDBCSLeadByte(codePage
, ch
);
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 bool DocumentAccessor::Match(int pos
, const char *s
) {
60 for (int i
=0; *s
; i
++) {
61 if (*s
!= SafeGetCharAt(pos
+i
))
68 char DocumentAccessor::StyleAt(int position
) {
69 // Mask off all bits which aren't in the 'mask'.
70 return static_cast<char>(pdoc
->StyleAt(position
) & mask
);
73 int DocumentAccessor::GetLine(int position
) {
74 return pdoc
->LineFromPosition(position
);
77 int DocumentAccessor::LineStart(int line
) {
78 return pdoc
->LineStart(line
);
81 int DocumentAccessor::LevelAt(int line
) {
82 return pdoc
->GetLevel(line
);
85 int DocumentAccessor::Length() {
87 lenDoc
= pdoc
->Length();
91 int DocumentAccessor::GetLineState(int line
) {
92 return pdoc
->GetLineState(line
);
95 int DocumentAccessor::SetLineState(int line
, int state
) {
96 return pdoc
->SetLineState(line
, state
);
99 void DocumentAccessor::StartAt(unsigned int start
, char chMask
) {
100 // Store the mask specified for use with StyleAt.
102 pdoc
->StartStyling(start
, chMask
);
103 startPosStyling
= start
;
106 void DocumentAccessor::StartSegment(unsigned int pos
) {
110 void DocumentAccessor::ColourTo(unsigned int pos
, int chAttr
) {
111 // Only perform styling if non empty range
112 if (pos
!= startSeg
- 1) {
113 PLATFORM_ASSERT(pos
>= startSeg
);
114 if (pos
< startSeg
) {
118 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
120 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
121 // Too big for buffer so send directly
122 pdoc
->SetStyleFor(pos
- startSeg
+ 1, static_cast<char>(chAttr
));
124 if (chAttr
!= chWhile
)
127 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
128 PLATFORM_ASSERT((startPosStyling
+ validLen
) < Length());
129 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
136 void DocumentAccessor::SetLevel(int line
, int level
) {
137 pdoc
->SetLevel(line
, level
);
140 void DocumentAccessor::Flush() {
141 startPos
= extremePosition
;
144 pdoc
->SetStyles(validLen
, styleBuf
);
145 startPosStyling
+= validLen
;
150 int DocumentAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
154 // Determines the indentation level of the current line and also checks for consistent
155 // indentation compared to the previous line.
156 // Indentation is judged consistent when the indentation whitespace of each line lines
157 // the same or the indentation of one line is a prefix of the other.
159 int pos
= LineStart(line
);
160 char ch
= (*this)[pos
];
162 bool inPrevPrefix
= line
> 0;
163 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
164 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
166 char chPrev
= (*this)[posPrev
++];
167 if (chPrev
== ' ' || chPrev
== '\t') {
169 spaceFlags
|= wsInconsistent
;
171 inPrevPrefix
= false;
175 spaceFlags
|= wsSpace
;
179 if (spaceFlags
& wsSpace
)
180 spaceFlags
|= wsSpaceTab
;
181 indent
= (indent
/ 8 + 1) * 8;
187 indent
+= SC_FOLDLEVELBASE
;
188 // if completely empty line or the start of a comment...
189 if ((ch
== ' ' || ch
== '\t' || ch
== '\n' || ch
== '\r') ||
190 (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
191 return indent
| SC_FOLDLEVELWHITEFLAG
;
196 void DocumentAccessor::IndicatorFill(int start
, int end
, int indicator
, int value
) {
197 pdoc
->decorations
.SetCurrentIndicator(indicator
);
198 pdoc
->DecorationFillRange(start
, value
, end
- start
);