Skip to content

Commit 1b904f9

Browse files
committed
v2.6.9
1 parent 343ecaa commit 1b904f9

File tree

4 files changed

+130
-113
lines changed

4 files changed

+130
-113
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "AutoHotkey v2 Language Support, based on vscode-lsp.",
55
"author": "thqby",
66
"publisher": "thqby",
7-
"version": "2.6.8",
7+
"version": "2.6.9",
88
"license": "LGPLv3.0",
99
"categories": [
1010
"Formatters",

server/src/codeActionProvider.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export async function codeActionProvider(params: CodeActionParams & { indent?: s
88
const uri = params.textDocument.uri, lex = lexers[uri.toLowerCase()];
99
if (!lex || token.isCancellationRequested || lex.diag_pending !== undefined) return;
1010
const acts: CodeAction[] = [], replaces: Record<string, TextEdit[]> = {}, parens: TextEdit[] = [];
11-
const { document, linepos, tokens } = lex, { context: { diagnostics, only }, indent, range } = params;
11+
const { document, line_ranges, tokens } = lex, { context: { diagnostics, only }, indent, range } = params;
1212
const has_refactor = only?.includes(CodeActionKind.Refactor) !== false;
1313
let m;
1414

@@ -68,11 +68,19 @@ export async function codeActionProvider(params: CodeActionParams & { indent?: s
6868
const so = document.offsetAt(start);
6969
const tab = indent || configCache.FormatOptions?.indent_string || '\t';
7070
const space = configCache.FormatOptions?.space_in_other === false ? '' : ' ';
71-
for (const line in linepos) {
72-
if (line as unknown as number < sl) continue;
73-
let bo = linepos[line], tk, bk;
71+
let l = 0, r = line_ranges.length - 1, rl = l, rr = r, i;
72+
while (l <= r) {
73+
const [a, b] = line_ranges[i = (l + r) >> 1];
74+
if (a < sl)
75+
l = rl = i + 1;
76+
else if (r = i - 1, b > eo)
77+
rr = r;
78+
}
79+
for (i = rl; i <= rr; i++) {
80+
// eslint-disable-next-line prefer-const
81+
let [line, bo] = line_ranges[i];
7482
if (bo > eo) break;
75-
bk = tokens[bo];
83+
let tk, bk = tokens[bo];
7684
if (!bk.body_start) continue;
7785
while ((bk = tokens[bk.body_start!]).body_start && bk.offset <= so)
7886
bo = bk.offset;
@@ -93,7 +101,7 @@ export async function codeActionProvider(params: CodeActionParams & { indent?: s
93101
}
94102
});
95103
}
96-
end = { line: parseInt(line) + 1, character: 0 };
104+
end = { line: line + 1, character: 0 };
97105
const o = document.offsetAt(end);
98106
tk = lex.findToken(o, true);
99107
if (tk.type === TokenType.Reserved && words.includes(tk.content.toLowerCase()))

server/src/formattingProvider.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,26 @@ export async function typeFormatting(params: DocumentOnTypeFormattingParams): Pr
4949
}
5050
});
5151
}
52-
} else if ((pp = lex.linepos[line - 1]) !== undefined) {
53-
const range = { start: lex.document.positionAt(pp), end: { line: line - 1, character: s.length } };
54-
const newText = lex.beautify(opts, range).trim();
55-
result = [{ range, newText }];
56-
indent_string = opts.indent_string;
57-
if (linetexts[1].substring(0, character) !== indent_string)
58-
result.push({
59-
newText: indent_string,
60-
range: { start: { line, character: 0 }, end: { line, character } }
61-
});
62-
} else if (!s) {
63-
if (linetexts[0] !== (linetexts[1] = linetexts[1].substring(0, character))) {
64-
if (!linetexts[0]) {
65-
tk = lex.findToken(lex.document.offsetAt(position));
66-
if (tk.type === TokenType.String || (tk.type & TokenType.Comment))
67-
return;
68-
const b = [TokenType.EOF, TokenType.BracketStart, TokenType.BlockStart];
69-
while ((tk = tk.previous_token!)) {
70-
if (b.includes(tk.type))
71-
break;
72-
}
73-
if (b.includes(tk?.type, 1))
74-
return;
52+
} else if (s && !/^(;|\/\*)|[ \t];/.test(s.trimStart())) {
53+
const start = { line, character: 0 }, offset = lex.document.offsetAt(start);
54+
const rgs = lex.line_ranges, ll = line - 1;
55+
let l = 0, r = rgs.length - 1, i;
56+
while (l <= r) {
57+
const [a, b] = rgs[i = (l + r) >> 1];
58+
if (ll > a)
59+
l = i + 1;
60+
else if (ll < a && offset <= b)
61+
r = i - 1;
62+
else if (lex.findStrOrComment(offset))
63+
break;
64+
else {
65+
const range = { start: lex.document.positionAt(b), end: { line: ll, character: s.length } };
66+
const newText = lex.beautify(opts, range).trim();
67+
result = [{ range, newText }];
68+
if (linetexts[1].substring(0, character) !== (indent_string = opts.indent_string))
69+
result.push({ newText: indent_string, range: { start, end: position } });
70+
break;
7571
}
76-
result = [{ newText: linetexts[0], range: { start: { line, character: 0 }, end: { line, character } } }];
7772
}
7873
}
7974
return result;
@@ -93,7 +88,7 @@ export async function typeFormatting(params: DocumentOnTypeFormattingParams): Pr
9388
}
9489

9590
function format_end_with_brace(pos: Position): TextEdit[] | undefined {
96-
tk = lex.tokens[lex.document.offsetAt({ line: pos.line, character: pos.character - 1 })];
91+
tk = lex.tokens[lex.document.offsetAt(pos) - 1];
9792
pp = tk?.previous_pair_pos;
9893
if (pp !== undefined) {
9994
while ((tk = lex.tokens[pp])?.previous_pair_pos !== undefined)

0 commit comments

Comments
 (0)