1// Scintilla source code edit control
2/** @file LexIndent.cxx
3 ** Lexer for no language. Used for indentation-based folding of files.
4 **/
5// The License.txt file describes the conditions under which this software may be distributed.
6
7#include <stdlib.h>
8#include <string.h>
9#include <stdio.h>
10#include <stdarg.h>
11#include <assert.h>
12#include <ctype.h>
13
14#include <string>
15#include <string_view>
16
17#include "ILexer.h"
18#include "Scintilla.h"
19#include "SciLexer.h"
20
21#include "WordList.h"
22#include "LexAccessor.h"
23#include "Accessor.h"
24#include "StyleContext.h"
25#include "CharacterSet.h"
26#include "LexerModule.h"
27
28using namespace Lexilla;
29
30static void ColouriseIndentDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[],
31 Accessor &styler) {
32 // Indent language means all style bytes are 0 so just mark the end - no need to fill in.
33 if (length > 0) {
34 styler.StartAt(startPos + length - 1);
35 styler.StartSegment(startPos + length - 1);
36 styler.ColourTo(startPos + length - 1, 0);
37 }
38}
39
40static void FoldIndentDoc(Sci_PositionU startPos, Sci_Position length, int /* initStyle */, WordList *[], Accessor &styler) {
41 int visibleCharsCurrent, visibleCharsNext;
42 int levelCurrent, levelNext;
43 Sci_PositionU i, lineEnd;
44 Sci_PositionU lengthDoc = startPos + length;
45 Sci_Position lineCurrent = styler.GetLine(startPos);
46
47 i = styler.LineStart(lineCurrent );
48 lineEnd = styler.LineStart(lineCurrent+1)-1;
49 if(lineEnd>=lengthDoc) lineEnd = lengthDoc-1;
50 while(styler[lineEnd]=='\n' || styler[lineEnd]=='\r') lineEnd--;
51 for(visibleCharsCurrent=0, levelCurrent=SC_FOLDLEVELBASE; !visibleCharsCurrent && i<=lineEnd; i++){
52 if(isspacechar(styler[i])) levelCurrent++;
53 else visibleCharsCurrent=1;
54 }
55
56 for(; i<lengthDoc; lineCurrent++) {
57 i = styler.LineStart(lineCurrent+1);
58 lineEnd = styler.LineStart(lineCurrent+2)-1;
59 if(lineEnd>=lengthDoc) lineEnd = lengthDoc-1;
60 while(styler[lineEnd]=='\n' || styler[lineEnd]=='\r') lineEnd--;
61 for(visibleCharsNext=0, levelNext=SC_FOLDLEVELBASE; !visibleCharsNext && i<=lineEnd; i++){
62 if(isspacechar(styler[i])) levelNext++;
63 else visibleCharsNext=1;
64 }
65 int lev = levelCurrent;
66 if(!visibleCharsCurrent) lev |= SC_FOLDLEVELWHITEFLAG;
67 else if(levelNext > levelCurrent) lev |= SC_FOLDLEVELHEADERFLAG;
68 styler.SetLevel(lineCurrent, lev);
69 levelCurrent = levelNext;
70 visibleCharsCurrent = visibleCharsNext;
71 }
72}
73
74LexerModule lmIndent(SCLEX_INDENT, ColouriseIndentDoc, "indent", FoldIndentDoc);
75