From 3671ad8f469e503b020df6fe5c5743b9774d915f Mon Sep 17 00:00:00 2001 From: James-Yu Date: Fri, 10 Mar 2023 10:15:26 +0800 Subject: [PATCH 1/3] Add a new config intellisense.package.exclude --- package.json | 9 ++++++ src/providers/completer/command.ts | 8 ++++-- src/providers/completer/environment.ts | 12 +++++++- src/providers/completer/package.ts | 40 ++++++++++++++++---------- 4 files changed, 50 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index eb5a06beb..34e2d3ae1 100644 --- a/package.json +++ b/package.json @@ -1974,6 +1974,15 @@ "default": [], "markdownDescription": "List of extra packages to always add to the auto-completion mechanism. When `#latex-workshop.intellisense.package.enabled#` is set to `true`, the commands and environments defined in these extra packages will be added to the intellisense suggestions." }, + "latex-workshop.intellisense.package.exclude": { + "scope": "window", + "type": "array", + "items": { + "type": "string" + }, + "default": [], + "markdownDescription": "List of packages to exclude from the auto-completion mechanism. When `#latex-workshop.intellisense.package.enabled#` is set to `true`, the commands and environments defined in these packages will not be added to the intellisense suggestions. This setting has a higher priority over `#latex-workshop.intellisense.package.extra#`. You may include the string \"lw-default\" in the list to remove all default commands and environments." + }, "latex-workshop.intellisense.package.env.enabled": { "scope": "window", "type": "boolean", diff --git a/src/providers/completer/command.ts b/src/providers/completer/command.ts index 13c3b8789..27f48f6a1 100644 --- a/src/providers/completer/command.ts +++ b/src/providers/completer/command.ts @@ -48,7 +48,8 @@ export class Command implements IProvider { constructor() { lw.registerDisposable(vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => { - if (!e.affectsConfiguration('latex-workshop.intellisense.command.user')) { + if (!e.affectsConfiguration('latex-workshop.intellisense.command.user') || + !e.affectsConfiguration('latex-workshop.intellisense.package.exclude')) { return } this.initialize(lw.completer.environment) @@ -56,8 +57,9 @@ export class Command implements IProvider { } initialize(environment: Environment) { - const cmds = JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/commands.json`, {encoding: 'utf8'})) as {[key: string]: CmdType} - const maths = (JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/packages/tex.json`, {encoding: 'utf8'})) as PkgType).cmds + const loadDefault = (vscode.workspace.getConfiguration('latex-workshop').get('intellisense.package.exclude') as string[]).includes('lw-default') + const cmds = loadDefault ? JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/commands.json`, {encoding: 'utf8'})) as {[key: string]: CmdType} : {} + const maths = loadDefault ? (JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/packages/tex.json`, {encoding: 'utf8'})) as PkgType).cmds: {} Object.assign(maths, cmds) Object.entries(maths).forEach(([key, cmd]) => { cmd.command = key diff --git a/src/providers/completer/environment.ts b/src/providers/completer/environment.ts index 0af8edb26..6a05b91dc 100644 --- a/src/providers/completer/environment.ts +++ b/src/providers/completer/environment.ts @@ -44,8 +44,18 @@ export class Environment implements IProvider { private readonly packageEnvsAsCommand = new Map() private readonly packageEnvsForBegin= new Map() + constructor() { + lw.registerDisposable(vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => { + if (!e.affectsConfiguration('latex-workshop.intellisense.package.exclude')) { + return + } + this.initialize() + })) + } + initialize() { - const envs = JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/environments.json`, {encoding: 'utf8'})) as {[key: string]: EnvType} + const loadDefault = (vscode.workspace.getConfiguration('latex-workshop').get('intellisense.package.exclude') as string[]).includes('lw-default') + const envs = loadDefault ? JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/environments.json`, {encoding: 'utf8'})) as {[key: string]: EnvType}: {} Object.entries(envs).forEach(([key, env]) => { env.name = env.name || key env.snippet = env.snippet || '' diff --git a/src/providers/completer/package.ts b/src/providers/completer/package.ts index bb6067347..ec4761f69 100644 --- a/src/providers/completer/package.ts +++ b/src/providers/completer/package.ts @@ -56,14 +56,19 @@ export class Package implements IProvider { getPackagesIncluded(languageId: string): {[packageName: string]: string[]} { const packages: {[packageName: string]: string[]} = {} - if (['latex', 'latex-expl3'].includes(languageId)) { - packages['latex-document'] = [] - } - if (languageId === 'latex-expl3') { - packages['expl3'] = [] + const config = vscode.workspace.getConfiguration('latex-workshop') + const excluded = config.get('intellisense.package.exclude') as string[] + if (!excluded.includes('lw-default')) { + if (['latex', 'latex-expl3'].includes(languageId)) { + packages['latex-document'] = [] + } + if (languageId === 'latex-expl3') { + packages['expl3'] = [] + } } - (vscode.workspace.getConfiguration('latex-workshop').get('intellisense.package.extra') as string[]) + (config.get('intellisense.package.extra') as string[]) + .filter(packageName => !excluded.includes(packageName)) .forEach(packageName => packages[packageName] = []) lw.cacher.getIncludedTeX().forEach(tex => { @@ -71,20 +76,25 @@ export class Package implements IProvider { if (included === undefined) { return } - Object.entries(included).forEach(([packageName, options]) => packages[packageName] = options) + Object.entries(included) + .filter(([packageName, ]) => !excluded.includes(packageName)) + .forEach(([packageName, options]) => packages[packageName] = options) }) while (true) { let newPackageInserted = false - Object.entries(packages).forEach(([packageName, options]) => Object.keys(this.getPackageDeps(packageName)).forEach(includeName => { - const dependOptions = this.getPackageDeps(packageName)[includeName] - const hasOption = dependOptions.length === 0 - || options.filter(option => dependOptions.includes(option)).length > 0 - if (packages[includeName] === undefined && hasOption) { - packages[includeName] = [] - newPackageInserted = true + Object.entries(packages).forEach(([packageName, options]) => Object.keys(this.getPackageDeps(packageName)) + .filter(includeName => !excluded.includes(includeName)) + .forEach(includeName => { + const dependOptions = this.getPackageDeps(packageName)[includeName] + const hasOption = dependOptions.length === 0 + || options.filter(option => dependOptions.includes(option)).length > 0 + if (packages[includeName] === undefined && hasOption) { + packages[includeName] = [] + newPackageInserted = true + } } - })) + )) if (!newPackageInserted) { break } From 9169b5ef433008829c3266100ddd5d5804b39895 Mon Sep 17 00:00:00 2001 From: James-Yu Date: Fri, 10 Mar 2023 10:22:03 +0800 Subject: [PATCH 2/3] Add `intellisense.package.exclude` tests --- test/suites/04_intellisense.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/suites/04_intellisense.test.ts b/test/suites/04_intellisense.test.ts index 38d21a932..defb3c28b 100644 --- a/test/suites/04_intellisense.test.ts +++ b/test/suites/04_intellisense.test.ts @@ -37,6 +37,7 @@ suite('Intellisense test suite', () => { await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.label.keyval', undefined) await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.argumentHint.enabled', undefined) await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.command.user', undefined) + await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.package.exclude', undefined) }) test.run('check default environment .json completion file', () => { @@ -321,6 +322,30 @@ suite('Intellisense test suite', () => { assert.ok(!suggestions.labels.includes('algorithm2e')) }) + test.only('intellisense with config `intellisense.package.exclude`', async (fixture: string) => { + await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.package.exclude', ['lw-default', 'import', 'listings']) + await test.load(fixture, [ + {src: 'intellisense/base.tex', dst: 'main.tex'}, + {src: 'intellisense/sub.tex', dst: 'sub/s.tex'} + ]) + let suggestions = test.suggest(0, 1) + assert.ok(!suggestions.labels.includes('\\date{}')) + assert.ok(!suggestions.labels.includes('\\lstinline')) + assert.ok(!suggestions.labels.includes('\\lstinline')) + + await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.package.exclude', ['lw-default']) + suggestions = test.suggest(0, 1) + assert.ok(!suggestions.labels.includes('\\date{}')) + assert.ok(suggestions.labels.includes('\\lstinline')) + assert.ok(suggestions.labels.includes('\\lstinline')) + + await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.package.exclude', ['import', 'listings']) + suggestions = test.suggest(0, 1) + assert.ok(suggestions.labels.includes('\\date{}')) + assert.ok(!suggestions.labels.includes('\\lstinline')) + assert.ok(!suggestions.labels.includes('\\lstinline')) + }) + test.run('argument intellisense of \\documentclass, \\usepackage, commands, and environments', async (fixture: string) => { await test.load(fixture, [ {src: 'intellisense/base.tex', dst: 'main.tex'}, From bf4795a7c54ec435c0c3bc94a0735c8115902278 Mon Sep 17 00:00:00 2001 From: James-Yu Date: Fri, 10 Mar 2023 10:28:12 +0800 Subject: [PATCH 3/3] Fix kindergarten logic errors --- src/providers/completer/command.ts | 8 ++++---- src/providers/completer/environment.ts | 4 ++-- test/suites/04_intellisense.test.ts | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/providers/completer/command.ts b/src/providers/completer/command.ts index 27f48f6a1..47d36a001 100644 --- a/src/providers/completer/command.ts +++ b/src/providers/completer/command.ts @@ -48,7 +48,7 @@ export class Command implements IProvider { constructor() { lw.registerDisposable(vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => { - if (!e.affectsConfiguration('latex-workshop.intellisense.command.user') || + if (!e.affectsConfiguration('latex-workshop.intellisense.command.user') && !e.affectsConfiguration('latex-workshop.intellisense.package.exclude')) { return } @@ -57,9 +57,9 @@ export class Command implements IProvider { } initialize(environment: Environment) { - const loadDefault = (vscode.workspace.getConfiguration('latex-workshop').get('intellisense.package.exclude') as string[]).includes('lw-default') - const cmds = loadDefault ? JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/commands.json`, {encoding: 'utf8'})) as {[key: string]: CmdType} : {} - const maths = loadDefault ? (JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/packages/tex.json`, {encoding: 'utf8'})) as PkgType).cmds: {} + const excludeDefault = (vscode.workspace.getConfiguration('latex-workshop').get('intellisense.package.exclude') as string[]).includes('lw-default') + const cmds = excludeDefault ? {} : JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/commands.json`, {encoding: 'utf8'})) as {[key: string]: CmdType} + const maths = excludeDefault ? {} : (JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/packages/tex.json`, {encoding: 'utf8'})) as PkgType).cmds Object.assign(maths, cmds) Object.entries(maths).forEach(([key, cmd]) => { cmd.command = key diff --git a/src/providers/completer/environment.ts b/src/providers/completer/environment.ts index 6a05b91dc..1a6ec3b5d 100644 --- a/src/providers/completer/environment.ts +++ b/src/providers/completer/environment.ts @@ -54,8 +54,8 @@ export class Environment implements IProvider { } initialize() { - const loadDefault = (vscode.workspace.getConfiguration('latex-workshop').get('intellisense.package.exclude') as string[]).includes('lw-default') - const envs = loadDefault ? JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/environments.json`, {encoding: 'utf8'})) as {[key: string]: EnvType}: {} + const excludeDefault = (vscode.workspace.getConfiguration('latex-workshop').get('intellisense.package.exclude') as string[]).includes('lw-default') + const envs = excludeDefault ? {} : JSON.parse(fs.readFileSync(`${lw.extensionRoot}/data/environments.json`, {encoding: 'utf8'})) as {[key: string]: EnvType} Object.entries(envs).forEach(([key, env]) => { env.name = env.name || key env.snippet = env.snippet || '' diff --git a/test/suites/04_intellisense.test.ts b/test/suites/04_intellisense.test.ts index defb3c28b..a25562e10 100644 --- a/test/suites/04_intellisense.test.ts +++ b/test/suites/04_intellisense.test.ts @@ -188,7 +188,7 @@ suite('Intellisense test suite', () => { assert.ok(!snippet.value.includes('${1:')) }) - test.run('command intellisense with config `intellisense.command.user`', async (fixture: string) => { + test.only('command intellisense with config `intellisense.command.user`', async (fixture: string) => { await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.command.user', {'mycommand[]{}': 'notsamecommand[${2:option}]{$TM_SELECTED_TEXT$1}', 'parbox{}{}': 'defchanged', 'overline{}': ''}) await test.load(fixture, [ {src: 'intellisense/base.tex', dst: 'main.tex'}, @@ -322,7 +322,7 @@ suite('Intellisense test suite', () => { assert.ok(!suggestions.labels.includes('algorithm2e')) }) - test.only('intellisense with config `intellisense.package.exclude`', async (fixture: string) => { + test.run('intellisense with config `intellisense.package.exclude`', async (fixture: string) => { await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.package.exclude', ['lw-default', 'import', 'listings']) await test.load(fixture, [ {src: 'intellisense/base.tex', dst: 'main.tex'},