comparison src/qscintilla-2-matlab-fold.patch @ 4341:6e4a1de60e8b

qscintilla: add patch for matlab code folding (Bug #42569) * src/qscintilla-2-matlab-fold.patch: new patch file * dist-files.mk: add qscintilla-2-matlab-fold.patch
author John D
date Thu, 19 Jan 2017 08:13:20 -0500
parents
children
comparison
equal deleted inserted replaced
4340:c202df909032 4341:6e4a1de60e8b
1 diff -ur QScintilla_gpl-2.9.3.orig/lexers/LexMatlab.cpp QScintilla_gpl-2.9.3/lexers/LexMatlab.cpp
2 --- QScintilla_gpl-2.9.3.orig/lexers/LexMatlab.cpp 2016-11-15 10:56:48.705675755 -0500
3 +++ QScintilla_gpl-2.9.3/lexers/LexMatlab.cpp 2016-11-15 18:03:03.938533646 -0500
4 @@ -15,6 +15,9 @@
5 **
6 ** Changes by John Donoghue 2014/08/01
7 ** - fix allowed transpose ' after {} operator
8 + **
9 + ** Changes by John Donoghue 2016/11/15
10 + ** - update matlab code folding
11 **/
12 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
13 // The License.txt file describes the conditions under which this software may be distributed.
14 @@ -48,15 +51,27 @@
15 static bool IsOctaveCommentChar(int c) {
16 return (c == '%' || c == '#') ;
17 }
18 -
19 -static bool IsMatlabComment(Accessor &styler, int pos, int len) {
20 - return len > 0 && IsMatlabCommentChar(styler[pos]) ;
21 +static inline int LowerCase(int c) {
22 + if (c >= 'A' && c <= 'Z')
23 + return 'a' + c - 'A';
24 + return c;
25 }
26
27 -static bool IsOctaveComment(Accessor &styler, int pos, int len) {
28 - return len > 0 && IsOctaveCommentChar(styler[pos]) ;
29 +static int CheckKeywordFoldPoint(char *str) {
30 + if (strcmp ("if", str) == 0 ||
31 + strcmp ("for", str) == 0 ||
32 + strcmp ("switch", str) == 0 ||
33 + strcmp ("try", str) == 0 ||
34 + strcmp ("do", str) == 0 ||
35 + strcmp ("parfor", str) == 0 ||
36 + strcmp ("function", str) == 0)
37 + return 1;
38 + if (strncmp("end", str, 3) == 0 ||
39 + strcmp("until", str) == 0)
40 + return -1;
41 + return 0;
42 }
43 -
44 +
45 static void ColouriseMatlabOctaveDoc(
46 unsigned int startPos, int length, int initStyle,
47 WordList *keywordlists[], Accessor &styler,
48 @@ -245,58 +260,83 @@
49 ColouriseMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsOctaveCommentChar, false);
50 }
51
52 -static void FoldMatlabOctaveDoc(unsigned int startPos, int length, int,
53 +static void FoldMatlabOctaveDoc(unsigned int startPos, int length, int initStyle,
54 WordList *[], Accessor &styler,
55 - bool (*IsComment)(Accessor&, int, int)) {
56 -
57 - int endPos = startPos + length;
58 + bool (*IsComment)(int)) {
59
60 - // Backtrack to previous line in case need to fix its fold status
61 - int lineCurrent = styler.GetLine(startPos);
62 - if (startPos > 0) {
63 - if (lineCurrent > 0) {
64 - lineCurrent--;
65 - startPos = styler.LineStart(lineCurrent);
66 + unsigned int endPos = startPos + length;
67 + int visibleChars = 0;
68 + unsigned int lineCurrent = styler.GetLine(startPos);
69 + int levelCurrent = SC_FOLDLEVELBASE;
70 + if (lineCurrent > 0)
71 + levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
72 + int levelNext = levelCurrent;
73 + char chNext = styler[startPos];
74 + int styleNext = styler.StyleAt(startPos);
75 + int style = initStyle;
76 + char word[100];
77 + int wordlen = 0;
78 + for (unsigned int i = startPos; i < endPos; i++) {
79 + char ch = chNext;
80 + chNext = styler.SafeGetCharAt(i + 1);
81 +
82 + style = styleNext;
83 + styleNext = styler.StyleAt(i + 1);
84 + bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
85 +
86 + // a line that starts with a comment
87 + if (style == SCE_MATLAB_COMMENT && IsComment(ch) && visibleChars == 0) {
88 + // start/end of block comment
89 + if (chNext == '{')
90 + levelNext ++;
91 + if (chNext == '}')
92 + levelNext --;
93 }
94 - }
95 - int spaceFlags = 0;
96 - int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsComment);
97 - char chNext = styler[startPos];
98 - for (int i = startPos; i < endPos; i++) {
99 - char ch = chNext;
100 - chNext = styler.SafeGetCharAt(i + 1);
101 -
102 - if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
103 - int lev = indentCurrent;
104 - int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsComment);
105 - if (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
106 - // Only non whitespace lines can be headers
107 - if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
108 - lev |= SC_FOLDLEVELHEADERFLAG;
109 - } else if (indentNext & SC_FOLDLEVELWHITEFLAG) {
110 - // Line after is blank so check the next - maybe should continue further?
111 - int spaceFlags2 = 0;
112 - int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsComment);
113 - if ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
114 - lev |= SC_FOLDLEVELHEADERFLAG;
115 - }
116 - }
117 + // keyword
118 + if(style == SCE_MATLAB_KEYWORD) {
119 + word[wordlen++] = static_cast<char>(LowerCase(ch));
120 + if (wordlen == 100) { // prevent overflow
121 + word[0] = '\0';
122 + wordlen = 1;
123 + }
124 + if (styleNext != SCE_MATLAB_KEYWORD) {
125 + word[wordlen] = '\0';
126 + wordlen = 0;
127 +
128 + levelNext += CheckKeywordFoldPoint(word);
129 + }
130 + }
131 + if (!IsASpace(ch))
132 + visibleChars++;
133 + if (atEOL || (i == endPos-1)) {
134 + int levelUse = levelCurrent;
135 + int lev = levelUse | levelNext << 16;
136 + if (visibleChars == 0)
137 + lev |= SC_FOLDLEVELWHITEFLAG;
138 + if (levelUse < levelNext)
139 + lev |= SC_FOLDLEVELHEADERFLAG;
140 + if (lev != styler.LevelAt(lineCurrent)) {
141 + styler.SetLevel(lineCurrent, lev);
142 }
143 - indentCurrent = indentNext;
144 - styler.SetLevel(lineCurrent, lev);
145 lineCurrent++;
146 + levelCurrent = levelNext;
147 + if (atEOL && (i == static_cast<unsigned int>(styler.Length() - 1))) {
148 + // There is an empty line at end of file so give it same level and empty
149 + styler.SetLevel(lineCurrent, (levelCurrent | levelCurrent << 16) | SC_FOLDLEVELWHITEFLAG);
150 + }
151 + visibleChars = 0;
152 }
153 - }
154 + }
155 }
156
157 static void FoldMatlabDoc(unsigned int startPos, int length, int initStyle,
158 WordList *keywordlists[], Accessor &styler) {
159 - FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsMatlabComment);
160 + FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsMatlabCommentChar);
161 }
162
163 static void FoldOctaveDoc(unsigned int startPos, int length, int initStyle,
164 WordList *keywordlists[], Accessor &styler) {
165 - FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsOctaveComment);
166 + FoldMatlabOctaveDoc(startPos, length, initStyle, keywordlists, styler, IsOctaveCommentChar);
167 }
168
169 static const char * const matlabWordListDesc[] = {