diff --git a/src/components/completionupdaterlib/commandupdaterlib/commandfinder.ts b/src/components/completionupdaterlib/commandupdaterlib/commandfinder.ts index 9a328803..b0bb3c5f 100644 --- a/src/components/completionupdaterlib/commandupdaterlib/commandfinder.ts +++ b/src/components/completionupdaterlib/commandupdaterlib/commandfinder.ts @@ -53,7 +53,8 @@ export class CommandFinder { if (isTriggerSuggestNeeded(node.name)) { command = { title: 'Post-Action', command: 'editor.action.triggerSuggest' } } - const cmd = new CmdEnvSuggestion(`\\${node.name}`, + const cmd = new CmdEnvSuggestion( + `\\${node.name}`, this.whichPackageProvidesCommand(node.name), { name: node.name, args: this.getArgsFromNode(node) }, CommandKind, diff --git a/src/providers/completer/command.ts b/src/providers/completer/command.ts index 3e8b9ea1..6849f309 100644 --- a/src/providers/completer/command.ts +++ b/src/providers/completer/command.ts @@ -318,7 +318,7 @@ export class Command implements IProvider, ICommand { getExtraPkgs(languageId: string): string[] { const configuration = vscode.workspace.getConfiguration('latex-toybox') - const extraPackages = Array.from(configuration.get('intellisense.package.extra') as string[]) + const extraPackages = Array.from(configuration.get('intellisense.package.extra', [])) if (languageId === 'latex-expl3') { extraPackages.push('latex-document') extraPackages.push('expl3') diff --git a/src/providers/completer/environment.ts b/src/providers/completer/environment.ts index c57428dc..8975ec26 100644 --- a/src/providers/completer/environment.ts +++ b/src/providers/completer/environment.ts @@ -147,47 +147,42 @@ export class Environment implements IProvider { _: RegExpMatchArray | undefined, args: {document: vscode.TextDocument, position: vscode.Position} ) { - const payload = {document: args.document, position: args.position} - return this.provide(payload.document, payload.position) + return this.provide(args.document, args.position) } - private provide(document: vscode.TextDocument, position: vscode.Position): vscode.CompletionItem[] { + private provide(document: vscode.TextDocument, position: vscode.Position) { if (vscode.window.activeTextEditor === undefined) { return [] } + if (vscode.window.activeTextEditor.selections.length > 1) { + return [] + } let snippetType: EnvSnippetType = EnvSnippetType.ForBegin + let range: vscode.Range | undefined // \begin{|} \end{|} or \begin{ab|} - if (vscode.window.activeTextEditor.selections.length > 1 || document.lineAt(position.line).text.slice(position.character).match(/^[a-zA-Z*]*}/)) { + if (document.lineAt(position.line).text.slice(position.character).match(/^[a-zA-Z*]*}/)) { snippetType = EnvSnippetType.AsName + range = document.getWordRangeAtPosition(position, /[a-zA-Z*]+/) } // Extract cached envs and add to default ones - const suggestions: vscode.CompletionItem[] = Array.from(this.getDefaultEnvs(snippetType)) + const suggestions: CmdEnvSuggestion[] = Array.from(this.getDefaultEnvs(snippetType)) const envList: string[] = this.getDefaultEnvs(snippetType).map(env => env.label) // Insert package environments const configuration = vscode.workspace.getConfiguration('latex-toybox') if (configuration.get('intellisense.package.enabled')) { const extraPackages = this.extension.completer.command.getExtraPkgs(document.languageId) - if (extraPackages) { - extraPackages.forEach(pkg => { - this.getEnvFromPkg(pkg, snippetType).forEach(env => { - if (!envList.includes(env.label)) { - suggestions.push(env) - envList.push(env.label) - } - }) - }) - } - this.extension.manager.getIncludedTeX().forEach(tex => { - const pkgs = this.extension.manager.getCachedContent(tex)?.element.package - pkgs?.forEach(pkg => { - this.getEnvFromPkg(pkg, snippetType).forEach(env => { - if (!envList.includes(env.label)) { - suggestions.push(env) - envList.push(env.label) - } - }) + const pkgsInFile = this.extension.manager.getIncludedTeX().map(tex => { + const pkg = this.extension.manager.getCachedContent(tex)?.element.package + return pkg ? Array.from(pkg) : [] + }).flat(); + [...extraPackages, ...pkgsInFile].forEach(pkg => { + this.getEnvFromPkg(pkg, snippetType).forEach(env => { + if (!envList.includes(env.label)) { + suggestions.push(env) + envList.push(env.label) + } }) }) } @@ -209,7 +204,19 @@ export class Environment implements IProvider { }) }) - return suggestions + if (snippetType === EnvSnippetType.AsName) { + return suggestions.map(sugg => { + if (range) { + const newSugg = sugg.clone() + newSugg.range = range + return newSugg + } else { + return sugg + } + }) + } else { + return suggestions + } } provideEnvsAsCommandInFile(filePath: string, cmdDuplicationDetector: CommandNameDuplicationDetector): CmdEnvSuggestion[] { diff --git a/src/providers/completer/interface.ts b/src/providers/completer/interface.ts index 3c9340c2..b183fcee 100644 --- a/src/providers/completer/interface.ts +++ b/src/providers/completer/interface.ts @@ -4,7 +4,6 @@ import type {CommandSignatureDuplicationDetector} from './commandlib/commandlib' import { latexParser } from 'latex-utensils' export interface IProvider { - /** * Returns the array of completion items. Should be called only from `Completer.completion`. */