xref: /MusicPlayer2/scintilla/lexlib/Accessor.cxx (revision 8af74909132ed5e696cb05b6689ae4baf14c1c96)
1*8af74909SZhong Yang // Scintilla source code edit control
2*8af74909SZhong Yang /** @file Accessor.cxx
3*8af74909SZhong Yang  ** Interfaces between Scintilla and lexers.
4*8af74909SZhong Yang  **/
5*8af74909SZhong Yang // Copyright 1998-2002 by Neil Hodgson <[email protected]>
6*8af74909SZhong Yang // The License.txt file describes the conditions under which this software may be distributed.
7*8af74909SZhong Yang 
8*8af74909SZhong Yang #include <cstdlib>
9*8af74909SZhong Yang #include <cassert>
10*8af74909SZhong Yang 
11*8af74909SZhong Yang #include "ILexer.h"
12*8af74909SZhong Yang #include "Scintilla.h"
13*8af74909SZhong Yang #include "SciLexer.h"
14*8af74909SZhong Yang 
15*8af74909SZhong Yang #include "PropSetSimple.h"
16*8af74909SZhong Yang #include "WordList.h"
17*8af74909SZhong Yang #include "LexAccessor.h"
18*8af74909SZhong Yang #include "Accessor.h"
19*8af74909SZhong Yang 
20*8af74909SZhong Yang using namespace Scintilla;
21*8af74909SZhong Yang 
Accessor(IDocument * pAccess_,PropSetSimple * pprops_)22*8af74909SZhong Yang Accessor::Accessor(IDocument *pAccess_, PropSetSimple *pprops_) : LexAccessor(pAccess_), pprops(pprops_) {
23*8af74909SZhong Yang }
24*8af74909SZhong Yang 
GetPropertyInt(const char * key,int defaultValue) const25*8af74909SZhong Yang int Accessor::GetPropertyInt(const char *key, int defaultValue) const {
26*8af74909SZhong Yang 	return pprops->GetInt(key, defaultValue);
27*8af74909SZhong Yang }
28*8af74909SZhong Yang 
IndentAmount(Sci_Position line,int * flags,PFNIsCommentLeader pfnIsCommentLeader)29*8af74909SZhong Yang int Accessor::IndentAmount(Sci_Position line, int *flags, PFNIsCommentLeader pfnIsCommentLeader) {
30*8af74909SZhong Yang 	const Sci_Position end = Length();
31*8af74909SZhong Yang 	int spaceFlags = 0;
32*8af74909SZhong Yang 
33*8af74909SZhong Yang 	// Determines the indentation level of the current line and also checks for consistent
34*8af74909SZhong Yang 	// indentation compared to the previous line.
35*8af74909SZhong Yang 	// Indentation is judged consistent when the indentation whitespace of each line lines
36*8af74909SZhong Yang 	// the same or the indentation of one line is a prefix of the other.
37*8af74909SZhong Yang 
38*8af74909SZhong Yang 	Sci_Position pos = LineStart(line);
39*8af74909SZhong Yang 	char ch = (*this)[pos];
40*8af74909SZhong Yang 	int indent = 0;
41*8af74909SZhong Yang 	bool inPrevPrefix = line > 0;
42*8af74909SZhong Yang 	Sci_Position posPrev = inPrevPrefix ? LineStart(line-1) : 0;
43*8af74909SZhong Yang 	while ((ch == ' ' || ch == '\t') && (pos < end)) {
44*8af74909SZhong Yang 		if (inPrevPrefix) {
45*8af74909SZhong Yang 			const char chPrev = (*this)[posPrev++];
46*8af74909SZhong Yang 			if (chPrev == ' ' || chPrev == '\t') {
47*8af74909SZhong Yang 				if (chPrev != ch)
48*8af74909SZhong Yang 					spaceFlags |= wsInconsistent;
49*8af74909SZhong Yang 			} else {
50*8af74909SZhong Yang 				inPrevPrefix = false;
51*8af74909SZhong Yang 			}
52*8af74909SZhong Yang 		}
53*8af74909SZhong Yang 		if (ch == ' ') {
54*8af74909SZhong Yang 			spaceFlags |= wsSpace;
55*8af74909SZhong Yang 			indent++;
56*8af74909SZhong Yang 		} else {	// Tab
57*8af74909SZhong Yang 			spaceFlags |= wsTab;
58*8af74909SZhong Yang 			if (spaceFlags & wsSpace)
59*8af74909SZhong Yang 				spaceFlags |= wsSpaceTab;
60*8af74909SZhong Yang 			indent = (indent / 8 + 1) * 8;
61*8af74909SZhong Yang 		}
62*8af74909SZhong Yang 		ch = (*this)[++pos];
63*8af74909SZhong Yang 	}
64*8af74909SZhong Yang 
65*8af74909SZhong Yang 	*flags = spaceFlags;
66*8af74909SZhong Yang 	indent += SC_FOLDLEVELBASE;
67*8af74909SZhong Yang 	// if completely empty line or the start of a comment...
68*8af74909SZhong Yang 	if ((LineStart(line) == Length()) || (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') ||
69*8af74909SZhong Yang 			(pfnIsCommentLeader && (*pfnIsCommentLeader)(*this, pos, end-pos)))
70*8af74909SZhong Yang 		return indent | SC_FOLDLEVELWHITEFLAG;
71*8af74909SZhong Yang 	else
72*8af74909SZhong Yang 		return indent;
73*8af74909SZhong Yang }
74