Skip to content

Commit

Permalink
Fixed [#294]
Browse files Browse the repository at this point in the history
  • Loading branch information
thqby committed Mar 12, 2023
1 parent df0b1ba commit 827cf62
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 64 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.9.3
- 修复[#294](https://github.com/thqby/vscode-autohotkey2-lsp/issues/294)

## 1.9.2
- 修复[#295](https://github.com/thqby/vscode-autohotkey2-lsp/issues/295)
- 修复[#296](https://github.com/thqby/vscode-autohotkey2-lsp/issues/296)
Expand Down Expand Up @@ -137,8 +140,8 @@

## 1.6.4
- 增加jsdoc类型标注
- 增加brace样式, `one_true_brace=-1`
- 修复函数定义未应用`one_true_brace`样式
- 增加brace样式, `brace_style=-1`
- 修复函数定义未应用`brace_style`样式
- 代码块补全应用格式化设置指定的样式[#194](https://github.com/thqby/vscode-autohotkey2-lsp/issues/194)
- 修复[#191](https://github.com/thqby/vscode-autohotkey2-lsp/issues/191)
- 修复[#193](https://github.com/thqby/vscode-autohotkey2-lsp/issues/193)
Expand All @@ -152,7 +155,7 @@

## 1.6.2
- 优化局部格式化
- 增加格式化选项`one_true_brace`
- 增加格式化选项`brace_style`
- 修复自定义设置未生效

## 1.6.1
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ node install.js
"indent_string": "\t",
"keep_array_indentation": true,
"max_preserve_newlines": 2,
"one_true_brace": "1", // or "0" or "-1"
"brace_style": "One True Brace", // or "Allman" or "One True Brace Variant"
"preserve_newlines": true,
"space_after_double_colon": true,
"space_before_conditional": true,
Expand Down Expand Up @@ -481,7 +481,7 @@ node install.js
"indent_string": "\t",
"keep_array_indentation": true,
"max_preserve_newlines": 2,
"one_true_brace": "1", // or "0" or "-1"
"brace_style": "One True Brace", // or "Allman" or "One True Brace Variant"
"preserve_newlines": true,
"space_after_double_colon": true,
"space_before_conditional": true,
Expand Down
35 changes: 15 additions & 20 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
SnippetString,
StatusBarAlignment,
StatusBarItem,
TextEditor,
Uri,
window,
workspace,
Expand All @@ -35,8 +34,8 @@ import { existsSync, readdirSync, statSync, unlinkSync, writeFileSync } from 'fs

let client: LanguageClient, outputchannel: OutputChannel, ahkStatusBarItem: StatusBarItem;
let ahkprocess: child_process.ChildProcess | undefined;
let ahkpath_cur = '', server_is_ready = false, zhcn = false;
const ahkconfig = workspace.getConfiguration('AutoHotkey2');
let ahkconfig = workspace.getConfiguration('AutoHotkey2');
let ahkpath_cur: string = ahkconfig.InterpreterPath, server_is_ready = false, zhcn = false;
const textdecoders: TextDecoder[] = [new TextDecoder('utf8', { fatal: true }), new TextDecoder('utf-16le', { fatal: true })];

export async function activate(context: ExtensionContext) {
Expand Down Expand Up @@ -106,6 +105,8 @@ export async function activate(context: ExtensionContext) {
commands: Object.keys(request_handlers), ...ahkconfig
}
};
if (ahkconfig.FormatOptions?.one_true_brace !== undefined)
window.showWarningMessage('configuration "AutoHotkey2.FormatOptions.one_true_brace" is deprecated!\nplease use "AutoHotkey2.FormatOptions.brace_style"');

// Create the language client and start the client.
client = new LanguageClient('ahk2', 'AutoHotkey2', serverOptions, clientOptions);
Expand All @@ -125,11 +126,10 @@ export async function activate(context: ExtensionContext) {
function update_extensions_info() {
debugexts = {};
for (const ext of extensions.all) {
if (ext.id.match(/ahk|autohotkey/i) && ext.packageJSON?.contributes?.debuggers) {
for (const debuger of ext.packageJSON.contributes.debuggers)
if (debuger.type)
debugexts[debuger.type] = ext.id;
}
let type;
if (ext.extensionKind === 1 && /ahk|autohotkey/i.test(ext.id) &&
(type = ext.packageJSON?.contributes?.debuggers?.[0]))
debugexts[type] = ext.id;
}
extlist = Object.values(debugexts);
languages.getLanguages().then(all => langs = all);
Expand Down Expand Up @@ -190,7 +190,7 @@ function decode(buf: Buffer) {
}

async function runCurrentScriptFile(selection = false): Promise<void> {
let editor = window.activeTextEditor, executePath = ahkpath_cur || ahkconfig.InterpreterPath as string;
let editor = window.activeTextEditor, executePath = ahkpath_cur || workspace.getConfiguration('AutoHotkey2').InterpreterPath as string;
if (!editor) return;
if (executePath && !executePath.includes(':'))
executePath = resolve(workspace.getWorkspaceFolder(editor.document.uri)?.uri.fsPath ?? '', executePath);
Expand Down Expand Up @@ -264,7 +264,7 @@ async function stopRunningScript(wait = false) {
async function compileScript() {
let editor = window.activeTextEditor;
if (!editor) return;
let cmd = '', cmdop = ahkconfig.CompilerCMD as string;
let cmd = '', cmdop = workspace.getConfiguration('AutoHotkey2').CompilerCMD as string;
let ws = workspace.getWorkspaceFolder(editor.document.uri)?.uri.fsPath ?? '';
let compilePath = findfile(['Compiler\\Ahk2Exe.exe', '..\\Compiler\\Ahk2Exe.exe'], ws);
let executePath = ahkpath_cur || getInterpreterPath().path;
Expand Down Expand Up @@ -334,8 +334,7 @@ async function quickHelp() {
window.showErrorMessage(zhcn ? `"AutoHotkey.chm"未找到!` : `"AutoHotkey.chm" was not found!`);
return;
}
const executePath = ahkconfig.InterpreterPath as string;
if (executePath && existsSync(executePath)) {
if (ahkpath_cur && existsSync(ahkpath_cur)) {
let script = `
#DllLoad oleacc.dll
chm_hwnd := 0, chm_path := '${helpPath}', DetectHiddenWindows(true), !(WinGetExStyle(top := WinExist('A')) & 8) && (top := 0)
Expand Down Expand Up @@ -370,15 +369,15 @@ if ${!!word} && !DllCall('oleacc\\AccessibleObjectFromWindow', 'ptr', ctl, 'uint
if (ahkStatusBarItem.text.endsWith('[UIAccess]')) {
let file = resolve(__dirname, 'temp.ahk');
writeFileSync(file, script, { encoding: 'utf-8' });
child_process.execSync(`"${executePath}" /ErrorStdOut ${file}`);
child_process.execSync(`"${ahkpath_cur}" /ErrorStdOut ${file}`);
unlinkSync(file);
} else
child_process.execSync(`"${executePath}" /ErrorStdOut *`, { input: script });
child_process.execSync(`"${ahkpath_cur}" /ErrorStdOut *`, { input: script });
}
}

async function begindebug(extlist: string[], debugexts: any, params = false, attach = false) {
let editor = window.activeTextEditor, executePath = ahkpath_cur || ahkconfig.InterpreterPath as string;
let editor = window.activeTextEditor, executePath = ahkpath_cur;
if (!editor) return;
let extname: string | undefined;
if (executePath && !executePath.includes(':'))
Expand All @@ -397,7 +396,7 @@ async function begindebug(extlist: string[], debugexts: any, params = false, att
} else if (extlist.length === 1)
extname = extlist[0];
else {
let def = ahkconfig.DefaultDebugger as string;
let def = workspace.getConfiguration('AutoHotkey2').DefaultDebugger as string;
extname = extlist.includes(def) ? def : await window.showQuickPick(extlist);
}
if (extname) {
Expand Down Expand Up @@ -580,8 +579,6 @@ function findfile(files: string[], workspace: string) {

async function onDidChangegetInterpreter() {
let path = ahkpath_cur;
if (!path.toLowerCase().endsWith('.exe') || !existsSync(path))
path = ahkconfig.InterpreterPath as string;
if (path.toLowerCase().endsWith('.exe') && existsSync(path)) {
if (path !== ahkStatusBarItem.tooltip) {
ahkStatusBarItem.tooltip = path;
Expand All @@ -591,8 +588,6 @@ async function onDidChangegetInterpreter() {
ahkStatusBarItem.text = (zhcn ? '选择AutoHotkey2解释器' : 'Select AutoHotkey2 Interpreter');
ahkStatusBarItem.tooltip = undefined, path = '';
}
if (path !== ahkpath_cur && (ahkpath_cur = path, server_is_ready))
commands.executeCommand('ahk2.resetinterpreterpath', ahkpath_cur);
}

async function updateVersionInfo() {
Expand Down
32 changes: 15 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "AutoHotkey v2 Language Support, based on vscode-lsp.",
"author": "thqby",
"publisher": "thqby",
"version": "1.9.2",
"version": "1.9.3",
"license": "LGPLv3.0",
"categories": [
"Formatters",
Expand Down Expand Up @@ -169,11 +169,22 @@
"scope": "window",
"type": "object",
"properties": {
"brace_style": {
"type": "string",
"enum": [
"One True Brace",
"Allman",
"One True Brace Variant"
],
"enumDescriptions": [
"if 1 {\n} else {\n}",
"if 1\n{\n}\nelse\n{\n}",
"if 1 {\n}\nelse {\n}"
]
},
"break_chained_methods": {
"type": "boolean",
"default": false,
"enum": [true, false],
"enumDescriptions": ["obj.method()\n.method2()", "obj.method().method2()"]
"default": false
},
"ignore_comment": {
"type": "boolean",
Expand All @@ -194,19 +205,6 @@
"type": "number",
"default": 2
},
"one_true_brace": {
"type": "string",
"enum": [
"1",
"0",
"-1"
],
"enumDescriptions": [
"if 1 {\n} else {\n}",
"if 1\n{\n}\nelse\n{\n}",
"if 1 {\n}\nelse {\n}"
]
},
"preserve_newlines": {
"type": "boolean",
"default": true
Expand Down
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ahk2.compile": "Compile Script",
"ahk2.compilercmd": "Compiler command line options. If ahk2exe.exe is not specified, the default path will be used. ${execPath} is equal to the currently selected AutoHotkey.exe",
"ahk2.completefunctionparens": "Parentheses are added to function completion when there is no '(' or '[' on the right; otherwise move the cursor to the right",
"ahk2.completioncommitcharacters": "Characters which commit auto completion",
"ahk2.completioncommitcharacters": "Characters which commit auto completion, such as '.('",
"ahk2.debug": "Debug Script",
"ahk2.debug.attach": "Attach Running Script",
"ahk2.debug.params": "Debug Script with Params",
Expand Down
2 changes: 1 addition & 1 deletion package.nls.zh-cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"ahk2.compile": "编译脚本",
"ahk2.compilercmd": "编译器命令行选项. 如果未指定ahk2exe.exe, 将使用默认路径. ${execPath}等价于当前选择的AutoHotkey.exe",
"ahk2.completefunctionparens": "当右侧不存在'('或'['时, 给函数补全添加括号; 否则向右移动光标",
"ahk2.completioncommitcharacters": "提交自动完成的字符",
"ahk2.completioncommitcharacters": "提交自动完成的字符, 例如'.('",
"ahk2.debug": "调试脚本",
"ahk2.debug.attach": "附加到运行中的脚本",
"ahk2.debug.params": "带参调试",
Expand Down
31 changes: 15 additions & 16 deletions server/src/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ export interface Token {
}

export interface FormatOptions {
brace_style?: number
break_chained_methods?: boolean
ignore_comment?: boolean
indent_string?: string
keep_array_indentation?: boolean
keyword_start_with_uppercase?: boolean
max_preserve_newlines?: number
one_true_brace?: number
preserve_newlines?: boolean
space_before_conditional?: boolean
space_after_double_colon?: boolean
Expand Down Expand Up @@ -360,8 +360,6 @@ export class Lexer {
space_in_paren: false,
wrap_line_length: 0
} as FormatOptions, options);
if (isNaN(opt.one_true_brace = Number(opt.one_true_brace)))
delete opt.one_true_brace;

last_type = last_last_text = last_text = '', begin_line = true, lst = EMPTY_TOKEN;
last_LF = -1, end_pos = input_length, ck = _this.get_token(0);
Expand Down Expand Up @@ -2651,7 +2649,7 @@ export class Lexer {
nexttoken();
if (tk.content === ':') {
k = Object.assign({}, lk), k.content = k.content.slice(1, -1), k.offset++, k.length -= 2;
_this.addDiagnostic(diagnostic.invalidpropname(), lk.offset, lk.length);
!h && _this.addDiagnostic(diagnostic.invalidpropname(), lk.offset, lk.length);
return true;
}
return isobj = false;
Expand Down Expand Up @@ -4319,12 +4317,12 @@ export class Lexer {

if (previous_flags.in_case_statement && !had_comment && last_type === 'TK_LABEL' && /^(default)?:$/.test(last_text))
flags.indentation_level--, flags.case_body = null, trim_newlines();
if (opt.one_true_brace === 0 || input_wanted_newline && opt.preserve_newlines && !opt.one_true_brace)
if (opt.brace_style === 0 || input_wanted_newline && opt.preserve_newlines && !opt.brace_style)
print_newline(true);

let need_newline = !just_added_newline();
print_token(), indent();
if (need_newline || opt.one_true_brace !== undefined)
if (need_newline || opt.brace_style !== undefined)
print_newline();
else output_space_before_token = space_in_other;
}
Expand All @@ -4351,7 +4349,7 @@ export class Lexer {
else if (input_wanted_newline && opt.preserve_newlines || !(flags.mode === MODE.ObjectLiteral && keep_object_line))
print_newline();
}
} else if (opt.one_true_brace !== undefined || input_wanted_newline)
} else if (opt.brace_style !== undefined || input_wanted_newline)
print_newline();

restore_mode();
Expand All @@ -4361,7 +4359,7 @@ export class Lexer {
if (!is_obj) {
if (previous_flags.case_body === null)
indent();
if (opt.one_true_brace !== undefined)
if (opt.brace_style !== undefined)
print_newline(true);
output_space_before_token = space_in_other;
}
Expand Down Expand Up @@ -4474,7 +4472,7 @@ export class Lexer {
}
if (maybe_need_newline) {
trim_newlines();
if (flags.last_text !== '}' || opt.one_true_brace as number < 1 || input_wanted_newline && opt.preserve_newlines && !opt.one_true_brace)
if (flags.last_text !== '}' || opt.brace_style! < 1 || input_wanted_newline && opt.preserve_newlines && !opt.brace_style)
print_newline(true);
else output_space_before_token = space_in_other;
} else if (input_wanted_newline)
Expand Down Expand Up @@ -4691,7 +4689,7 @@ export class Lexer {
// block comment starts with a new line
print_newline(true);
if (flags.mode === MODE.Statement) {
let nk = _this.tokens[ck.previous_token?.next_token_offset as number];
let nk = _this.tokens[ck.previous_token?.next_token_offset!];
if (!nk || !flags.in_expression && !is_line_continue(nk.previous_token ?? EMPTY_TOKEN, nk))
print_newline();
else if (flags.had_comment < 2)
Expand Down Expand Up @@ -4735,7 +4733,7 @@ export class Lexer {
return;
print_newline(true);
if (flags.mode === MODE.Statement) {
let nk = _this.tokens[ck.previous_token?.next_token_offset as number];
let nk = _this.tokens[ck.previous_token?.next_token_offset!];
if (!nk || !flags.in_expression && !is_line_continue(nk.previous_token ?? EMPTY_TOKEN, nk))
print_newline();
else if (flags.had_comment < 2)
Expand Down Expand Up @@ -4979,13 +4977,13 @@ export class Lexer {
case 'TK_END_EXPR':
case 'TK_END_BLOCK':
tk = lk, ps[lk.content]++, psn++;
if ((lk = tokens[lk.previous_pair_pos as number]) && (lk.content !== '}' || lk.data))
if ((lk = tokens[lk.previous_pair_pos!]) && (lk.content !== '}' || lk.data))
lk.next_pair_pos = tk.offset;
else tk = EMPTY_TOKEN, lk = undefined;
break;
case 'TK_OPERATOR':
if (tk = lk, lk.content === '%') {
if (lk = tokens[lk.previous_pair_pos as number])
if (lk = tokens[lk.previous_pair_pos!])
lk.next_pair_pos = tk.offset;
else tk = EMPTY_TOKEN;
break;
Expand All @@ -5008,12 +5006,12 @@ export class Lexer {
case 'TK_NUMBER': s += '#number', tk = tokens[tk.next_token_offset]; break;
case 'TK_START_BLOCK':
t = (tk.data as ClassNode)?.name ?? '#object';
s += ' ' + t, tk = tokens[tk.next_pair_pos as number], tk = tokens[tk.next_token_offset];
s += ' ' + t, tk = tokens[tk.next_pair_pos!], tk = tokens[tk.next_token_offset];
break;
case 'TK_START_EXPR':
s += tk.content === '[' ? '[]' : '()';
tk = tokens[tk.next_pair_pos as number];
tk = tokens[tk.next_token_offset as number];
tk = tokens[tk.next_pair_pos!];
tk = tokens[tk.next_token_offset!];
break;
default: s += (tk.prefix_is_whitespace ? ' ' : '') + tk.content, tk = tokens[tk.next_token_offset]; break;
}
Expand Down Expand Up @@ -6046,6 +6044,7 @@ export function formatMarkdowndetail(node: DocumentSymbol, name?: string, overlo
s = m[2].replace(/\s*<caption>(.*)/, (...m) => { details.push('\n*@example* ' + m[1]); return ''; });
if (s !== m[2])
break;
details.push('\n*@example*');
}
details.push('```ahk2' + (s ? '\n' + s : '')), code = 2;
break;
Expand Down
12 changes: 11 additions & 1 deletion server/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export let utils = {
export let locale = 'en-us';
export type Maybe<T> = T | undefined;

export enum LibIncludeType {
enum LibIncludeType {
'Disabled',
'Local',
'User and Standard',
Expand Down Expand Up @@ -403,6 +403,16 @@ export function update_settings(configs: AHKLSSettings) {
configs.AutoLibInclude = LibIncludeType[configs.AutoLibInclude] as unknown as LibIncludeType;
else if (typeof configs.AutoLibInclude === 'boolean')
configs.AutoLibInclude = configs.AutoLibInclude ? 3 : 0;
if (typeof configs.FormatOptions.brace_style === 'string')
switch (configs.FormatOptions.brace_style) {
case '0':
case 'Allman': configs.FormatOptions.brace_style = 0; break;
case '1':
case 'One True Brace': configs.FormatOptions.brace_style = 1; break;
case '-1':
case 'One True Brace Variant': configs.FormatOptions.brace_style = -1; break;
default: delete configs.FormatOptions.brace_style; break;
}
try {
update_commentTags(configs.CommentTags);
} catch (e: any) {
Expand Down
Loading

0 comments on commit 827cf62

Please sign in to comment.