Skip to content

Commit f4d0e33

Browse files
authored
Fix: Folding for comments and different modes (#5533)
* fix: comments fold range determination in xml langs * fix: `getCommentFoldRange` * improve doc comments highlight rules * fix: lua comment/string folding ranges
1 parent 00346fd commit f4d0e33

22 files changed

+718
-278
lines changed

ace-internal.d.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,10 @@ export namespace Ace {
783783
closingBracketBlock(session: EditSession, bracket: string, row: number, column: number, typeRe?: RegExp): Range | undefined;
784784
}
785785

786-
type BehaviorAction = (state: string, action: string, editor: Editor, session: EditSession, text: string | Range) => ({ text: string, selection: number[] } | Range) & { [key: string]: any } | undefined;
786+
type BehaviorAction = (state: string | string[], action: string, editor: Editor, session: EditSession, text: string | Range) => ({
787+
text: string,
788+
selection: number[]
789+
} | Range) & { [key: string]: any } | undefined;
787790
type BehaviorMap = Record<string, Record<string, BehaviorAction>>;
788791

789792
interface Behaviour {
@@ -841,21 +844,21 @@ export namespace Ace {
841844

842845
getTokenizer(): Tokenizer;
843846

844-
toggleCommentLines(state: any,
847+
toggleCommentLines(state: string | string[],
845848
session: EditSession,
846849
startRow: number,
847850
endRow: number): void;
848851

849-
toggleBlockComment(state: any,
852+
toggleBlockComment(state: string | string[],
850853
session: EditSession,
851854
range: Range,
852855
cursor: Point): void;
853856

854-
getNextLineIndent(state: any, line: string, tab: string): string;
857+
getNextLineIndent(state: string | string[], line: string, tab: string): string;
855858

856-
checkOutdent(state: any, line: string, input: string): boolean;
859+
checkOutdent(state: string | string[], line: string, input: string): boolean;
857860

858-
autoOutdent(state: any, doc: EditSession, row: number): void;
861+
autoOutdent(state: string | string[], doc: EditSession, row: number): void;
859862

860863
// TODO implement WorkerClient types
861864
createWorker(session: EditSession): any;
@@ -864,7 +867,7 @@ export namespace Ace {
864867

865868
getKeywords(append?: boolean): Array<string | RegExp>;
866869

867-
getCompletions(state: string,
870+
getCompletions(state: string | string[],
868871
session: EditSession,
869872
pos: Point,
870873
prefix: string): Completion[];

src/background_tokenizer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BackgroundTokenizer {
2323
/**@type {false|number}*/
2424
this.running = false;
2525
this.lines = [];
26+
/**@type {string[]|string[][]}*/
2627
this.states = [];
2728
this.currentLine = 0;
2829
this.tokenizer = tokenizer;
@@ -176,7 +177,7 @@ class BackgroundTokenizer {
176177
/**
177178
* Returns the state of tokenization at the end of a row.
178179
* @param {Number} row The row to get state at
179-
* @returns {string}
180+
* @returns {string | string[]}
180181
**/
181182
getState(row) {
182183
if (this.currentLine == row)

src/edit_session.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ class EditSession {
272272
/**
273273
* {:BackgroundTokenizer.getState}
274274
* @param {Number} row The row to start at
275-
* @returns {string}
275+
* @returns {string | string[]}
276276
* @related BackgroundTokenizer.getState
277277
**/
278278
getState(row) {

src/edit_session/folding.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -716,36 +716,34 @@ function Folding() {
716716
if (dir != 1) {
717717
do {
718718
token = iterator.stepBackward();
719-
} while (token && re.test(token.type) && !/^comment.end/.test(token.type));
719+
} while (token && re.test(token.type));
720720
token = iterator.stepForward();
721721
}
722722

723723
range.start.row = iterator.getCurrentTokenRow();
724-
range.start.column = iterator.getCurrentTokenColumn() + (/^comment.start/.test(token.type) ? token.value.length : 2);
724+
range.start.column = iterator.getCurrentTokenColumn() + token.value.length;
725725

726726
iterator = new TokenIterator(this, row, column);
727+
var initState = this.getState(iterator.$row);
727728

728729
if (dir != -1) {
729730
var lastRow = -1;
730731
do {
731732
token = iterator.stepForward();
732733
if (lastRow == -1) {
733734
var state = this.getState(iterator.$row);
734-
if (!re.test(state))
735+
if (initState.toString() !== state.toString())
735736
lastRow = iterator.$row;
736737
} else if (iterator.$row > lastRow) {
737738
break;
738739
}
739-
} while (token && re.test(token.type) && !/^comment.start/.test(token.type));
740+
} while (token && re.test(token.type));
740741
token = iterator.stepBackward();
741742
} else
742743
token = iterator.getCurrentToken();
743744

744745
range.end.row = iterator.getCurrentTokenRow();
745746
range.end.column = iterator.getCurrentTokenColumn();
746-
if (!/^comment.end/.test(token.type)) {
747-
range.end.column += token.value.length - 2;
748-
}
749747
return range;
750748
}
751749
};

src/editor.js

+1
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,7 @@ class Editor {
983983
? [new Range(0, 0, session.doc.getLength() - 1, 0)]
984984
: this.selection.getAllRanges();
985985

986+
/**@type{string|string[]}*/
986987
var prevLineState = "";
987988
var prevLine = "";
988989
var lineIndent = "";

src/ext/static_highlight_test.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,16 @@ module.exports = {
3939
].join("\n");
4040
var mode = new JavaScriptMode();
4141
var result = highlighter.render(snippet, mode, theme);
42-
assert.equal(result.html, "<div class='ace-tomorrow'><div class='ace_static_highlight ace_show_gutter' style='counter-reset:ace_line 0'>"
43-
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc'>/** this is a function</span>\n</div>"
44-
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc'>*</span>\n</div>"
45-
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc'>*/</span>\n</div>"
46-
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span>\n</div>"
47-
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_storage ace_type'>function</span> <span class='ace_entity ace_name ace_function'>hello</span> <span class='ace_paren ace_lparen'>(</span><span class='ace_variable ace_parameter'>a</span><span class='ace_punctuation ace_operator'>,</span> <span class='ace_variable ace_parameter'>b</span><span class='ace_punctuation ace_operator'>,</span> <span class='ace_variable ace_parameter'>c</span><span class='ace_paren ace_rparen'>)</span> <span class='ace_paren ace_lparen'>{</span>\n</div>"
48-
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span> <span class='ace_storage ace_type'>console</span><span class='ace_punctuation ace_operator'>.</span><span class='ace_support ace_function ace_firebug'>log</span><span class='ace_paren ace_lparen'>(</span><span class='ace_identifier'>a</span> <span class='ace_keyword ace_operator'>*</span> <span class='ace_identifier'>b</span> <span class='ace_keyword ace_operator'>+</span> <span class='ace_identifier'>c</span> <span class='ace_keyword ace_operator'>+</span> <span class='ace_string'>&#39;sup$&#39;</span><span class='ace_paren ace_rparen'>)</span><span class='ace_punctuation ace_operator'>;</span>\n</div>"
49-
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_indent-guide'> </span><span class='ace_indent-guide'> </span> <span class='ace_comment'>//</span>\n</div>"
50-
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_indent-guide'> </span><span class='ace_indent-guide'> </span> <span class='ace_comment'>//</span>\n</div>"
51-
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_paren ace_rparen'>}</span>\n</div>"
52-
+ "</div></div>");
42+
assert.equal(result.html, `<div class='ace-tomorrow'><div class='ace_static_highlight ace_show_gutter' style='counter-reset:ace_line 0'><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc'>/**</span><span class='ace_comment ace_doc ace_body'> this is a function</span>
43+
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc ace_body'>*</span>
44+
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc'>*/</span>
45+
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span>
46+
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_storage ace_type'>function</span> <span class='ace_entity ace_name ace_function'>hello</span> <span class='ace_paren ace_lparen'>(</span><span class='ace_variable ace_parameter'>a</span><span class='ace_punctuation ace_operator'>,</span> <span class='ace_variable ace_parameter'>b</span><span class='ace_punctuation ace_operator'>,</span> <span class='ace_variable ace_parameter'>c</span><span class='ace_paren ace_rparen'>)</span> <span class='ace_paren ace_lparen'>{</span>
47+
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span> <span class='ace_storage ace_type'>console</span><span class='ace_punctuation ace_operator'>.</span><span class='ace_support ace_function ace_firebug'>log</span><span class='ace_paren ace_lparen'>(</span><span class='ace_identifier'>a</span> <span class='ace_keyword ace_operator'>*</span> <span class='ace_identifier'>b</span> <span class='ace_keyword ace_operator'>+</span> <span class='ace_identifier'>c</span> <span class='ace_keyword ace_operator'>+</span> <span class='ace_string'>&#39;sup$&#39;</span><span class='ace_paren ace_rparen'>)</span><span class='ace_punctuation ace_operator'>;</span>
48+
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_indent-guide'> </span><span class='ace_indent-guide'> </span> <span class='ace_comment'>//</span>
49+
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_indent-guide'> </span><span class='ace_indent-guide'> </span> <span class='ace_comment'>//</span>
50+
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_paren ace_rparen'>}</span>
51+
</div></div></div>`);
5352
assert.ok(!!result.css);
5453
next();
5554
},

src/mode/_test/tokens_drools.json

+45-9
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,25 @@
206206
["comment.doc","/**"]
207207
],[
208208
"doc-start",
209-
["comment.doc"," * "],
209+
[
210+
"comment.doc.body",
211+
" * "
212+
],
210213
["comment.doc.tag","@param"],
211-
["comment.doc"," name who we'll salute?"]
214+
[
215+
"comment.doc.body",
216+
" name who we'll salute?"
217+
]
212218
],[
213219
"start",
214-
["comment.doc"," */"]
220+
[
221+
"comment.doc.body",
222+
" "
223+
],
224+
[
225+
"comment.doc",
226+
"*/"
227+
]
215228
],[
216229
"start",
217230
["keyword","function"],
@@ -419,12 +432,25 @@
419432
["comment.doc","/**"]
420433
],[
421434
"java-doc-start",
422-
["comment.doc"," * "],
435+
[
436+
"comment.doc.body",
437+
" * "
438+
],
423439
["comment.doc.tag.storage.type","TODO"],
424-
["comment.doc"," There mus be better way"]
440+
[
441+
"comment.doc.body",
442+
" There mus be better way"
443+
]
425444
],[
426445
"java-start",
427-
["comment.doc"," */"]
446+
[
447+
"comment.doc.body",
448+
" "
449+
],
450+
[
451+
"comment.doc",
452+
"*/"
453+
]
428454
],[
429455
"java-start",
430456
["text"," "],
@@ -751,10 +777,20 @@
751777
["comment.doc","/**"]
752778
],[
753779
"doc-start",
754-
["comment.doc"," * Go Right"]
780+
[
781+
"comment.doc.body",
782+
" * Go Right"
783+
]
755784
],[
756785
"start",
757-
["comment.doc"," */"]
786+
[
787+
"comment.doc.body",
788+
" "
789+
],
790+
[
791+
"comment.doc",
792+
"*/"
793+
]
758794
],[
759795
"start",
760796
["keyword","rule"],
@@ -932,4 +968,4 @@
932968
["keyword","end"]
933969
],[
934970
"start"
935-
]]
971+
]]

0 commit comments

Comments
 (0)