Skip to content

Commit

Permalink
Merge pull request #3767 from James-Yu/exclude-package-intellisense
Browse files Browse the repository at this point in the history
Exclude package intellisense
  • Loading branch information
James-Yu authored Mar 11, 2023
2 parents 7237e50 + bf4795a commit 911ada6
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 20 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions src/providers/completer/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ 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)
}))
}

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 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
Expand Down
12 changes: 11 additions & 1 deletion src/providers/completer/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ export class Environment implements IProvider {
private readonly packageEnvsAsCommand = new Map<string, CmdEnvSuggestion[]>()
private readonly packageEnvsForBegin= new Map<string, CmdEnvSuggestion[]>()

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 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 || ''
Expand Down
40 changes: 25 additions & 15 deletions src/providers/completer/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,45 @@ 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 => {
const included = lw.cacher.get(tex)?.elements.package
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
}
Expand Down
27 changes: 26 additions & 1 deletion test/suites/04_intellisense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -187,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'},
Expand Down Expand Up @@ -321,6 +322,30 @@ suite('Intellisense test suite', () => {
assert.ok(!suggestions.labels.includes('algorithm2e'))
})

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'},
{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'},
Expand Down

0 comments on commit 911ada6

Please sign in to comment.