Skip to content

Commit

Permalink
Fix: Folding for comments and different modes (#5533)
Browse files Browse the repository at this point in the history
* fix: comments fold range determination in xml langs

* fix: `getCommentFoldRange`

* improve doc comments highlight rules

* fix: lua comment/string folding ranges
  • Loading branch information
mkslanc committed May 24, 2024
1 parent 00346fd commit f4d0e33
Show file tree
Hide file tree
Showing 22 changed files with 718 additions and 278 deletions.
17 changes: 10 additions & 7 deletions ace-internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,10 @@ export namespace Ace {
closingBracketBlock(session: EditSession, bracket: string, row: number, column: number, typeRe?: RegExp): Range | undefined;
}

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

interface Behaviour {
Expand Down Expand Up @@ -841,21 +844,21 @@ export namespace Ace {

getTokenizer(): Tokenizer;

toggleCommentLines(state: any,
toggleCommentLines(state: string | string[],
session: EditSession,
startRow: number,
endRow: number): void;

toggleBlockComment(state: any,
toggleBlockComment(state: string | string[],
session: EditSession,
range: Range,
cursor: Point): void;

getNextLineIndent(state: any, line: string, tab: string): string;
getNextLineIndent(state: string | string[], line: string, tab: string): string;

checkOutdent(state: any, line: string, input: string): boolean;
checkOutdent(state: string | string[], line: string, input: string): boolean;

autoOutdent(state: any, doc: EditSession, row: number): void;
autoOutdent(state: string | string[], doc: EditSession, row: number): void;

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

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

getCompletions(state: string,
getCompletions(state: string | string[],
session: EditSession,
pos: Point,
prefix: string): Completion[];
Expand Down
3 changes: 2 additions & 1 deletion src/background_tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BackgroundTokenizer {
/**@type {false|number}*/
this.running = false;
this.lines = [];
/**@type {string[]|string[][]}*/
this.states = [];
this.currentLine = 0;
this.tokenizer = tokenizer;
Expand Down Expand Up @@ -176,7 +177,7 @@ class BackgroundTokenizer {
/**
* Returns the state of tokenization at the end of a row.
* @param {Number} row The row to get state at
* @returns {string}
* @returns {string | string[]}
**/
getState(row) {
if (this.currentLine == row)
Expand Down
2 changes: 1 addition & 1 deletion src/edit_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class EditSession {
/**
* {:BackgroundTokenizer.getState}
* @param {Number} row The row to start at
* @returns {string}
* @returns {string | string[]}
* @related BackgroundTokenizer.getState
**/
getState(row) {
Expand Down
12 changes: 5 additions & 7 deletions src/edit_session/folding.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,36 +716,34 @@ function Folding() {
if (dir != 1) {
do {
token = iterator.stepBackward();
} while (token && re.test(token.type) && !/^comment.end/.test(token.type));
} while (token && re.test(token.type));
token = iterator.stepForward();
}

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

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

if (dir != -1) {
var lastRow = -1;
do {
token = iterator.stepForward();
if (lastRow == -1) {
var state = this.getState(iterator.$row);
if (!re.test(state))
if (initState.toString() !== state.toString())
lastRow = iterator.$row;
} else if (iterator.$row > lastRow) {
break;
}
} while (token && re.test(token.type) && !/^comment.start/.test(token.type));
} while (token && re.test(token.type));
token = iterator.stepBackward();
} else
token = iterator.getCurrentToken();

range.end.row = iterator.getCurrentTokenRow();
range.end.column = iterator.getCurrentTokenColumn();
if (!/^comment.end/.test(token.type)) {
range.end.column += token.value.length - 2;
}
return range;
}
};
Expand Down
1 change: 1 addition & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ class Editor {
? [new Range(0, 0, session.doc.getLength() - 1, 0)]
: this.selection.getAllRanges();

/**@type{string|string[]}*/
var prevLineState = "";
var prevLine = "";
var lineIndent = "";
Expand Down
21 changes: 10 additions & 11 deletions src/ext/static_highlight_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ module.exports = {
].join("\n");
var mode = new JavaScriptMode();
var result = highlighter.render(snippet, mode, theme);
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'>/** this is a function</span>\n</div>"
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc'>*</span>\n</div>"
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc'>*/</span>\n</div>"
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span>\n</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>\n</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>\n</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>\n</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>\n</div>"
+ "<div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_paren ace_rparen'>}</span>\n</div>"
+ "</div></div>");
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>
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc ace_body'>*</span>
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_comment ace_doc'>*/</span>
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span>
</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>
</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>
</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>
</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>
</div><div class='ace_line'><span class='ace_gutter ace_gutter-cell'></span><span class='ace_paren ace_rparen'>}</span>
</div></div></div>`);
assert.ok(!!result.css);
next();
},
Expand Down
54 changes: 45 additions & 9 deletions src/mode/_test/tokens_drools.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,25 @@
["comment.doc","/**"]
],[
"doc-start",
["comment.doc"," * "],
[
"comment.doc.body",
" * "
],
["comment.doc.tag","@param"],
["comment.doc"," name who we'll salute?"]
[
"comment.doc.body",
" name who we'll salute?"
]
],[
"start",
["comment.doc"," */"]
[
"comment.doc.body",
" "
],
[
"comment.doc",
"*/"
]
],[
"start",
["keyword","function"],
Expand Down Expand Up @@ -419,12 +432,25 @@
["comment.doc","/**"]
],[
"java-doc-start",
["comment.doc"," * "],
[
"comment.doc.body",
" * "
],
["comment.doc.tag.storage.type","TODO"],
["comment.doc"," There mus be better way"]
[
"comment.doc.body",
" There mus be better way"
]
],[
"java-start",
["comment.doc"," */"]
[
"comment.doc.body",
" "
],
[
"comment.doc",
"*/"
]
],[
"java-start",
["text"," "],
Expand Down Expand Up @@ -751,10 +777,20 @@
["comment.doc","/**"]
],[
"doc-start",
["comment.doc"," * Go Right"]
[
"comment.doc.body",
" * Go Right"
]
],[
"start",
["comment.doc"," */"]
[
"comment.doc.body",
" "
],
[
"comment.doc",
"*/"
]
],[
"start",
["keyword","rule"],
Expand Down Expand Up @@ -932,4 +968,4 @@
["keyword","end"]
],[
"start"
]]
]]
Loading

0 comments on commit f4d0e33

Please sign in to comment.