Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete big paren commands: 'left(' -> '\left( ... \right)' #2397

Merged
merged 2 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions data/commands.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"snippet": "left[${1}\\right]",
"detail": "\\left[ ... \\right]"
},
"left\\{": {
"command": "left\\{",
"left{": {
"command": "left{",
"snippet": "left\\{${1}\\right\\\\}",
"detail": "\\left\\{ ... \\right\\}"
},
Expand All @@ -37,8 +37,8 @@
"snippet": "bigl[${1}\\bigr]",
"detail": "\\bigl[ ... \\bigr]"
},
"bigl\\{": {
"command": "bigl\\{",
"bigl{": {
"command": "bigl{",
"snippet": "bigl\\{${1}\\bigr\\\\}",
"detail": "\\bigl\\{ ... \\bigr\\}"
},
Expand All @@ -52,8 +52,8 @@
"snippet": "Bigl[${1}\\Bigr]",
"detail": "\\Bigl[ ... \\Bigr]"
},
"Bigl\\{": {
"command": "Bigl\\{",
"Bigl{": {
"command": "Bigl{",
"snippet": "Bigl\\{${1}\\Bigr\\\\}",
"detail": "\\Bigl\\{ ... \\Bigr\\}"
},
Expand All @@ -67,8 +67,8 @@
"snippet": "biggl[${1}\\biggr]",
"detail": "\\biggl[ ... \\biggr]"
},
"biggl\\{": {
"command": "biggl\\{",
"biggl{": {
"command": "biggl{",
"snippet": "biggl\\{${1}\\biggr\\\\}",
"detail": "\\biggl\\{ ... \\biggr\\}"
},
Expand All @@ -82,8 +82,8 @@
"snippet": "Biggl[${1}\\Biggr]",
"detail": "\\Biggl[ ... \\Biggr]"
},
"Biggl\\{": {
"command": "Biggl\\{",
"Biggl{": {
"command": "Biggl{",
"snippet": "Biggl\\{${1}\\Biggr\\\\}",
"detail": "\\Biggl\\{ ... \\Biggr\\}"
},
Expand Down
19 changes: 10 additions & 9 deletions src/providers/completer/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@ export class Command implements IProvider {
this.environment.getDefaultEnvs(EnvSnippetType.AsCommand).forEach(cmd => {
this.defaultCmds.push(cmd)
})

// Handle special commands with brackets
const bracketCmds = ['(', '[', '{', 'left(', 'left[', 'left\\{']
this.defaultCmds.filter(cmd => bracketCmds.includes(this.getCmdName(cmd))).forEach(cmd => {
this.bracketCmds[cmd.label.slice(1)] = cmd
})

}

provideFrom(_type: string, _result: RegExpMatchArray, args: {document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext}) {
return this.provide(args.document.languageId, args.document, args.position)
provideFrom(_type: string, result: RegExpMatchArray, args: {document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, context: vscode.CompletionContext}) {
const suggestions = this.provide(args.document.languageId, args.document, args.position)
// Commands ending with (, { or [ are not filtered properly by vscode intellisense. So we do it by hand.
if (result[0].match(/[({[]$/)) {
const exactSuggestion = suggestions.filter(entry => entry.label === result[0])
if (exactSuggestion.length > 0) {
return exactSuggestion
}
}
return suggestions
}

private provide(languageId: string, document?: vscode.TextDocument, position?: vscode.Position): vscode.CompletionItem[] {
Expand Down
16 changes: 1 addition & 15 deletions src/providers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,10 @@ export class Completer implements vscode.CompletionItemProvider {
token: vscode.CancellationToken,
context: vscode.CompletionContext
): vscode.CompletionItem[] | undefined {
const invokeChar = document.lineAt(position.line).text[position.character - 1]
const currentLine = document.lineAt(position.line).text
if (position.character > 1 && currentLine[position.character - 1] === '\\' && currentLine[position.character - 2] === '\\') {
return
}
if (this.command.bracketCmds && this.command.bracketCmds[invokeChar]) {
if (position.character > 1 && currentLine[position.character - 2] === '\\') {
const mathSnippet = Object.assign({}, this.command.bracketCmds[invokeChar])
if (vscode.workspace.getConfiguration('editor', document.uri).get('autoClosingBrackets') &&
(currentLine.length > position.character && [')', ']', '}'].includes(currentLine[position.character]))) {
mathSnippet.range = new vscode.Range(position.translate(0, -1), position.translate(0, 1))
} else {
mathSnippet.range = new vscode.Range(position.translate(0, -1), position)
}
return [mathSnippet]
}
}

const line = document.lineAt(position.line).text.substr(0, position.character)
// Note that the order of the following array affects the result.
// 'command' must be at the last because it matches any commands.
Expand Down Expand Up @@ -171,7 +157,7 @@ export class Completer implements vscode.CompletionItemProvider {
provider = this.environment
break
case 'command':
reg = args.document.languageId === 'latex-expl3' ? /\\([a-zA-Z_@]*(?::[a-zA-Z]*)?)$/ : /\\([a-zA-Z]*)$/
reg = args.document.languageId === 'latex-expl3' ? /\\([a-zA-Z_@]*(?::[a-zA-Z]*)?)$/ : /\\([a-zA-Z]*|(?:left|[Bb]ig{1,2}l)?[({[]?)$/
provider = this.command
break
case 'package':
Expand Down
18 changes: 18 additions & 0 deletions syntax/syntax.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@
],
"autoClosingPairs": [
["\\begin{", ""],
["\\left(", ""],
["\\left[", ""],
["\\left{", ""],
["\\bigl(", ""],
["\\bigl[", ""],
["\\bigl{", ""],
["\\Bigl(", ""],
["\\Bigl[", ""],
["\\Bigl{", ""],
["\\biggl(", ""],
["\\biggl[", ""],
["\\biggl{", ""],
["\\Biggl(", ""],
["\\Biggl[", ""],
["\\Biggl{", ""],
["\\(", ""],
["\\[", ""],
["\\{", ""],
["@(", ""],
["@[", ""],
["@{", ""],
Expand Down