Skip to content

Commit

Permalink
Add config utilities and unit tests (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-wiemer authored Oct 5, 2024
1 parent cec9490 commit 5a64a7d
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 104 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"typescript.tsc.autoDetect": "off",
"typescript.preferences.quoteStyle": "single",
"git.ignoreLimitWarning": true,
"AutoHotkey2.InterpreterPath": "c:\\Program Files\\AutoHotkey\\v2\\AutoHotkey64.exe"
"AutoHotkey2.InterpreterPath": "c:\\Program Files\\AutoHotkey\\v2\\AutoHotkey64.exe",
"typescript.tsdk": "node_modules\\typescript\\lib"
}
111 changes: 110 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -634,25 +634,38 @@
},
"scripts": {
"check-types": "tsc",
"build-cli": "node esbuild.mjs --cli",
"build:cli": "npm run build -- --cli",
"build": "node esbuild.mjs",
"clean:out": "rimraf out",
"precompile-ts": "npm run clean:out",
"compile-ts": "tsc --noEmit false --outDir out",
"eslint": "eslint --fix",
"package": "vsce package",
"publish": "vsce publish",
"test": "vscode-test",
"test-grammar": "vscode-tmgrammar-snap tmgrammar-test/*.ahk",
"validate": "node esbuild.mjs && npm run test && npm run test-grammar",
"vscode:prepublish": "npm run validate",
"watch": "node esbuild.mjs --dev",
"watch-web": "node esbuild.mjs --web"
"test": "npm run test:grammar && npm run test:unit && npm run test:e2e",
"pretest:e2e": "npm run build",
"test:e2e": "vscode-test",
"test:grammar": "vscode-tmgrammar-snap tmgrammar-test/*.ahk",
"pretest:unit": "npm run compile-ts",
"test:unit": "mocha",
"vscode:prepublish": "npm run test",
"watch": "npm run build -- --dev",
"watch-web": "npm run build -- --web"
},
"mocha": {
"spec": "out/**/*.spec.js"
},
"devDependencies": {
"@types/mocha": "^10.0.8",
"@types/node": "^20.14.0",
"@types/vscode": "1.93.0",
"@vscode/test-cli": "^0.0.10",
"@vscode/test-electron": "^2.4.1",
"@vscode/vsce": "^2.32.0",
"esbuild": "^0.24.0",
"eslint": "^9.11.0",
"mocha": "^10.7.3",
"rimraf": "^6.0.1",
"typescript": "5.5.4",
"typescript-eslint": "^8.7.0",
"vscode-tmgrammar-test": "^0.1.3"
Expand Down
14 changes: 7 additions & 7 deletions server/src/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import { URI } from 'vscode-uri';
import { builtin_ahkv1_commands, builtin_variable, builtin_variable_h } from './constants';
import { action, completionitem, diagnostic, warn } from './localize';
import {
a_vars, ahk_version, ahkuris, ahkvars, alpha_3, connection, extsettings,
a_vars, ahk_version, ahkuris, ahkvars, alpha_3, connection,
hoverCache, isahk2_h, lexers, libdirs, libfuncs, locale, openAndParse, openFile,
restorePath, rootdir, setTextDocumentLanguage, symbolProvider, utils, workspaceFolders
} from './common';
import { ActionType, FormatOptions } from '../../util/src/config';
import { ActionType, ahklsConfig, CfgKey, FormatOptions, getCfg } from '../../util/src/config';

export interface ParamInfo {
offset: number
Expand Down Expand Up @@ -1155,7 +1155,7 @@ export class Lexer {
begin_line = true, requirev2 = false, maybev1 = 0, lst = { ...EMPTY_TOKEN }, currsymbol = last_comment_fr = undefined;
parser_pos = 0, last_LF = -1, customblocks = { region: [], bracket: [] }, continuation_sections_mode = false, h = isahk2_h;
this.clear(), includetable = this.include, comments = {}, sharp_offsets = [];
callWithoutParentheses = extsettings.Warn?.CallWithoutParentheses;
callWithoutParentheses = ahklsConfig.Warn?.CallWithoutParentheses;
try {
const rs = utils.get_RCDATA('#2');
rs && (includetable[rs.uri] = rs.path);
Expand Down Expand Up @@ -1194,7 +1194,7 @@ export class Lexer {
if (requirev2)
return false;
_this.maybev1 ??= maybev1 = 1;
switch (_this.actionwhenv1 ??= extsettings.ActionWhenV1IsDetected) {
switch (_this.actionwhenv1 ??= getCfg(CfgKey.ActionWhenV1Detected)) {
case 'SkipLine': {
if (!allow_skip)
return true;
Expand Down Expand Up @@ -3671,7 +3671,7 @@ export class Lexer {
nexttoken(), parse_pair('(', ')');
const pc = tokens[tk.previous_pair_pos!]?.paraminfo?.count ?? 0;
if (pc !== 1)
extsettings.Diagnostics.ParamsCheck && _this.addDiagnostic(diagnostic.paramcounterr(1, pc), fc.offset, parser_pos - fc.offset);
ahklsConfig.Diagnostics.ParamsCheck && _this.addDiagnostic(diagnostic.paramcounterr(1, pc), fc.offset, parser_pos - fc.offset);
else if (result.length > l && lk.type === 'TK_WORD') {
const vr = result.at(-1) as Variable;
if (lk.content === vr.name && lk.offset === _this.document.offsetAt(vr.range.start))
Expand Down Expand Up @@ -6315,7 +6315,7 @@ export class Lexer {
return;
let workfolder: string;
if (!dir) {
for (workfolder of extsettings.WorkingDirs)
for (workfolder of ahklsConfig.WorkingDirs)
if (this.uri.startsWith(workfolder)) {
dir = restorePath(URI.parse(workfolder).fsPath.replace(/[\\/]$/, ''));
break;
Expand Down Expand Up @@ -6373,7 +6373,7 @@ export class Lexer {
}

private addSymbolFolding(symbol: AhkSymbol, first_brace: number) {
const l1 = extsettings.SymbolFoldingFromOpenBrace ? this.document.positionAt(first_brace).line : symbol.range.start.line;
const l1 = ahklsConfig.SymbolFoldingFromOpenBrace ? this.document.positionAt(first_brace).line : symbol.range.start.line;
const l2 = symbol.range.end.line - 1;
const ranges = this.foldingranges;
if (l1 < l2) {
Expand Down
14 changes: 7 additions & 7 deletions server/src/browserServerMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
hoverProvider, initahk2cache, Lexer, lexers, loadahk2, loadlocalize, prepareRename, rangeFormatting,
referenceProvider, renameProvider, SemanticTokenModifiers, semanticTokensOnFull, semanticTokensOnRange,
SemanticTokenTypes, set_ahk_h, set_Connection, set_dirname, set_locale, set_version, set_WorkspaceFolders,
signatureProvider, symbolProvider, typeFormatting, update_settings, workspaceSymbolProvider
signatureProvider, symbolProvider, typeFormatting, updateConfig, workspaceSymbolProvider
} from './common';
import { AHKLSSettings, configPrefix } from '../../util/src/config';
import { AHKLSConfig, CfgKey, configPrefix, getCfg } from '../../util/src/config';

const languageServer = 'ahk2-language-server';
const messageReader = new BrowserMessageReader(self);
Expand Down Expand Up @@ -75,12 +75,12 @@ connection.onInitialize(params => {
result.capabilities.workspace = { workspaceFolders: { supported: true } };
}

const configs: AHKLSSettings = params.initializationOptions;
const initialConfig: AHKLSConfig = params.initializationOptions;
set_ahk_h(true);
set_locale(params.locale);
set_dirname(configs.extensionUri!);
set_dirname(getCfg(CfgKey.ExtensionUri, initialConfig));
loadlocalize();
update_settings(configs);
updateConfig(initialConfig);
set_WorkspaceFolders(workspaceFolders);
set_version('3.0.0');
initahk2cache();
Expand All @@ -105,14 +105,14 @@ connection.onInitialized(() => {
});

connection.onDidChangeConfiguration(async change => {
let newset: AHKLSSettings | undefined = change?.settings;
let newset: AHKLSConfig | undefined = change?.settings;
if (hasConfigurationCapability && !newset)
newset = await connection.workspace.getConfiguration(configPrefix);
if (!newset) {
connection.window.showWarningMessage('Failed to obtain the configuration');
return;
}
update_settings(newset);
updateConfig(newset);
set_WorkspaceFolders(workspaceFolders);
});

Expand Down
5 changes: 3 additions & 2 deletions server/src/commandProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { CancellationToken, ExecuteCommandParams, Position, Range, SymbolKind } from 'vscode-languageserver';
import {
AhkSymbol, ClassNode, FuncNode, Lexer, Property, Token, Variable,
connection, extsettings, find_class, generate_type_annotation,
connection, find_class, generate_type_annotation,
join_types, lexers, parse_include, restorePath, semanticTokensOnFull,
traverse_include, update_include_cache
} from './common';
import { CfgKey, getCfg } from '../../util/src/config';

function checkCommand(cmd: string) {
if (extsettings.commands?.includes(cmd))
if (getCfg(CfgKey.Commands)?.includes(cmd))
return true;
connection?.console.warn(`Command '${cmd}' is not implemented!`);
return false;
Expand Down
Loading

0 comments on commit 5a64a7d

Please sign in to comment.