1 // SciTE - Scintilla based Text Editor
2 // Accessor.cxx - rapid easy access to contents of a Scintilla
3 // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
15 #include "DocumentAccessor.h"
16 #include "CellBuffer.h"
17 #include "Scintilla.h"
20 DocumentAccessor::~DocumentAccessor() {
23 bool DocumentAccessor::InternalIsLeadByte(char ch
) {
25 // TODO: support DBCS under GTK+
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 IsDBCSLeadByteEx(codePage
, ch
);
39 void DocumentAccessor::Fill(int position
) {
41 lenDoc
= pdoc
->Length();
42 startPos
= position
- slopSize
;
43 if (startPos
+ bufferSize
> lenDoc
)
44 startPos
= lenDoc
- bufferSize
;
47 endPos
= startPos
+ bufferSize
;
51 pdoc
->GetCharRange(buf
, startPos
, endPos
-startPos
);
52 buf
[endPos
-startPos
] = '\0';
55 char DocumentAccessor::StyleAt(int position
) {
56 return pdoc
->StyleAt(position
);
59 int DocumentAccessor::GetLine(int position
) {
60 return pdoc
->LineFromPosition(position
);
63 int DocumentAccessor::LineStart(int line
) {
64 return pdoc
->LineStart(line
);
67 int DocumentAccessor::LevelAt(int line
) {
68 return pdoc
->GetLevel(line
);
71 int DocumentAccessor::Length() {
73 lenDoc
= pdoc
->Length();
77 int DocumentAccessor::GetLineState(int line
) {
78 return pdoc
->GetLineState(line
);
81 int DocumentAccessor::SetLineState(int line
, int state
) {
82 return pdoc
->SetLineState(line
, state
);
85 void DocumentAccessor::StartAt(unsigned int start
, char chMask
) {
86 pdoc
->StartStyling(start
, chMask
);
89 void DocumentAccessor::StartSegment(unsigned int pos
) {
93 void DocumentAccessor::ColourTo(unsigned int pos
, int chAttr
) {
94 // Only perform styling if non empty range
95 if (pos
!= startSeg
- 1) {
97 Platform::DebugPrintf("Bad colour positions %d - %d\n", startSeg
, pos
);
100 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
)
102 if (validLen
+ (pos
- startSeg
+ 1) >= bufferSize
) {
103 // Too big for buffer so send directly
104 pdoc
->SetStyleFor(pos
- startSeg
+ 1, static_cast<char>(chAttr
));
106 if (chAttr
!= chWhile
)
109 for (unsigned int i
= startSeg
; i
<= pos
; i
++) {
110 styleBuf
[validLen
++] = static_cast<char>(chAttr
);
117 void DocumentAccessor::SetLevel(int line
, int level
) {
118 pdoc
->SetLevel(line
, level
);
121 void DocumentAccessor::Flush() {
122 startPos
= extremePosition
;
125 pdoc
->SetStyles(validLen
, styleBuf
);
130 int DocumentAccessor::IndentAmount(int line
, int *flags
, PFNIsCommentLeader pfnIsCommentLeader
) {
134 // Determines the indentation level of the current line and also checks for consistent
135 // indentation compared to the previous line.
136 // Indentation is judged consistent when the indentation whitespace of each line lines
137 // the same or the indentation of one line is a prefix of the other.
139 int pos
= LineStart(line
);
140 char ch
= (*this)[pos
];
142 bool inPrevPrefix
= line
> 0;
143 int posPrev
= inPrevPrefix
? LineStart(line
-1) : 0;
144 while ((ch
== ' ' || ch
== '\t') && (pos
< end
)) {
146 char chPrev
= (*this)[posPrev
++];
147 if (chPrev
== ' ' || chPrev
== '\t') {
149 spaceFlags
|= wsInconsistent
;
151 inPrevPrefix
= false;
155 spaceFlags
|= wsSpace
;
159 if (spaceFlags
& wsSpace
)
160 spaceFlags
|= wsSpaceTab
;
161 indent
= (indent
/ 8 + 1) * 8;
167 indent
+= SC_FOLDLEVELBASE
;
168 // if completely empty line or the start of a comment...
169 if (isspace(ch
) || (pfnIsCommentLeader
&& (*pfnIsCommentLeader
)(*this, pos
, end
-pos
)) )
170 return indent
| SC_FOLDLEVELWHITEFLAG
;