Skip to content

Commit

Permalink
Tweak completions for environment as name
Browse files Browse the repository at this point in the history
  • Loading branch information
tamuratak committed Sep 8, 2023
1 parent 8a2ca29 commit f7b9a8b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/providers/completer/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>('intellisense.package.extra', []))
if (languageId === 'latex-expl3') {
extraPackages.push('latex-document')
extraPackages.push('expl3')
Expand Down
57 changes: 32 additions & 25 deletions src/providers/completer/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
})
}
Expand All @@ -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[] {
Expand Down
1 change: 0 additions & 1 deletion src/providers/completer/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
*/
Expand Down

0 comments on commit f7b9a8b

Please sign in to comment.