add: Enabled completionItemprovider to work async (added Promise),

add: input, include, ... commands and parameters to *_symbols
This commit is contained in:
Daniel Kapla 2017-12-07 18:30:43 +01:00
parent 0bd3ed39a7
commit 8bce5ac877
4 changed files with 159 additions and 53 deletions

View File

@ -244,6 +244,12 @@ export default {
"h": { documentation: "hier" },
"l": { documentation: "low" },
"p": { documentation: "page" },
},
includegraphics: {
width: { detail: "width=<value>", insertText: "width=${0:<value>}", documentation: "scale graphic to the specified <value>.", },
height: { detail: "height=<value>", insertText: "height=${0:<value>}", documentation: "scale graphic to the specified <value>.", },
angle: { detail: "angle=<value>", insertText: "angle=${0:<value>}", documentation: "rotate graphic counterclockwise.", },
scale: { detail: "scale=<value>", insertText: "scale=${0:<value>}", documentation: "scale graphic by <value>.", },
}
};

View File

@ -3,13 +3,52 @@ import { latex_types } from "./symbol_types";
var preamble = "Define in preamble!";
export default {
"documentclass": { insertText: "documentclass", kind: latex_types.keyword },
"usepackage": { insertText: "usepackage{$1}", documentation: "usepackage", kind: latex_types.keyword },
"newenvironment": { insertText: "newenvironment{${1:name}}[$2]{$3}{$0}", documentation: "newenvironment", kind: latex_types.keyword },
"newcommand": { insertText: "newcommand{${1:name}}[$2]{$3}", documentation: "newcommand", kind: latex_types.keyword },
"renewcommand": { insertText: "renewcommand{${1:name}}[$2]{$3}", documentation: "renewcommand", kind: latex_types.keyword },
documentclass: {
insertText: "documentclass",
kind: latex_types.keyword
},
"DeclareMathOperator": { insertText: "DeclareMathOperator{${1:operatorcommand}}{${2:operatorname}}$0", detail: preamble, documentation: "Defines a new math operator. Use the stared version for operators with limits.", kind: latex_types.keyword },
"DeclareMathOperator*": { insertText: "DeclareMathOperator*{${1:operatorcommand}}{${2:operatorname}}$0", detail: preamble, documentation: "Defines a new math operator with limits.", kind: latex_types.keyword },
usepackage: {
insertText: "usepackage{$1}",
documentation: "usepackage",
kind: latex_types.keyword
},
newenvironment: {
insertText: "newenvironment{${1:name}}[$2]{$3}{$0}",
documentation: "newenvironment",
kind: latex_types.keyword
},
newcommand: {
insertText: "newcommand{${1:name}}[$2]{$3}",
documentation: "newcommand",
kind: latex_types.keyword
},
renewcommand: {
insertText: "renewcommand{${1:name}}[$2]{$3}",
documentation: "renewcommand",
kind: latex_types.keyword
},
includeonly: {
documentation: "After this command is executed in the preamble of the document, only \\include commands for the filenames which are listed in the argument of the \\includeonly command will be executed. Note that there must be no spaces between the filenames and the commas.",
kind: latex_types.keyword
},
input: {
documentation: "Includes the sourcecode from the target .tex file.",
kind: latex_types.keyword
},
DeclareMathOperator: {
insertText: "DeclareMathOperator{${1:operatorcommand}}{${2:operatorname}}$0",
detail: preamble,
documentation: "Defines a new math operator. Use the stared version for operators with limits.",
kind: latex_types.keyword
},
"DeclareMathOperator*": {
insertText: "DeclareMathOperator*{${1:operatorcommand}}{${2:operatorname}}$0",
detail: preamble,
documentation: "Defines a new math operator with limits.",
kind: latex_types.keyword
},
}

View File

@ -1,6 +1,30 @@
import { latex_types } from "./symbol_types";
export default {
input: {
detail: "\\input{<filename>}",
insertText: "input{${0:<filename>}}",
documentation: "Includes the sourcecode from the target .tex file.",
kind: latex_types.keyword
},
include: {
detail: "\\include{<filename>}",
insertText: "include{${0:<filename>}}",
documentation: "Includes the rendered content from the target .tex file on a new page. Note: If \\includeonly is specified in the preamble, all NOT specified files are ignored.",
kind: latex_types.keyword
},
includegraphics: {
detail: "\\includegraphics[<key>=<value>,...]{<filename>}\nRequired package \"graphicx\"",
insertText: "includegraphics[$1]{${0:<filename>}}",
documentation: "Use the command \\includegraphics[<key>=<value>,...]{<file>} to include <file> into your document. The optional parameter accepts a comma separated list of <key>'s and associated <value>'s. The <key>'s can be used to alter the width, height and rotation of the included graphic.",
kind: latex_types.keyword
},
verbatiminput: {
detail: "\\verbatiminput{<filename>}\nRequired package \"verbatim\"",
insertText: "verbatiminput{${0:<filename>}}",
documentation: "Allows to include raw ASCII text into your document as if it was inside a verbatim environment.",
kind: latex_types.keyword
},
"usepackage": { insertText: "usepackage{$1}", documentation: "usepackage", kind: latex_types.keyword },
"newenvironment": { insertText: "newenvironment{${1:name}}[$2]{$3}{$0}", documentation: "newenvironment", kind: latex_types.keyword },

View File

@ -8,6 +8,8 @@ import environment_symbols from '../dictionary/environment_symbols';
import tikz_symbols from '../dictionary/tikz_symbols';
import parameter_dictionary from '../dictionary/parameter_dictionary';
import * as child_process from 'child_process';
function convertToItemKind(type: latex_types): vscode.CompletionItemKind {
switch (type) {
case latex_types.symbol:
@ -220,53 +222,88 @@ export default class Provider implements vscode.CompletionItemProvider {
return filtert_symbols;
}
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.CompletionItem[] {
var line = document.lineAt(position);
if (line.isEmptyOrWhitespace) {
return [];
}
var line_text = line.text;
var line_before_pos = line_text.substring(0, position.character).trim();
var start_index: number;
var end_index: number;
var symbol_lable: string;
var symbol_parameter: string;
if ((start_index = line_before_pos.lastIndexOf("\\") + 1) > 0) {
if ((end_index = line_before_pos.indexOf("{", start_index)) > -1) {
symbol_lable = line_before_pos.substring(start_index, end_index);
symbol_parameter = line_before_pos.substring(end_index + 1);
switch (symbol_lable) {
case "begin":
case "end":
return this.filterSymbols(this.environment_symbols, symbol_parameter, position);
case "ref":
case "eqref":
case "pageref":
return this.searchAndCreateLableSymbols(document, symbol_parameter, position);
case "usepackage": // provide package symbols by first all usepackage parameters before the last ","
return this.filterSymbols(this.package_symbols, symbol_parameter.substring(symbol_parameter.lastIndexOf(",") + 1).trim(), position);
default: // default to NO suggestions
return [];
}
} else if ((end_index = line_before_pos.indexOf("[", start_index)) > -1) {
symbol_lable = line_before_pos.substring(start_index, end_index);
symbol_parameter = line_before_pos.substring(end_index + 1)
return this.filterParameters(symbol_lable, symbol_parameter.substring(symbol_parameter.lastIndexOf(",") + 1).trim(), position);
} else {
symbol_lable = line_before_pos.substring(start_index);
switch (this.getEnvironmentType(document, line_before_pos, position)) {
case "math":
return this.filterSymbols(this.math_symbols, symbol_lable, position);
case "tikz":
return this.filterSymbols(this.tikz_symbols, symbol_lable, position);
case "text":
return this.filterSymbols(this.text_symbols, symbol_lable, position);
default:
return this.filterSymbols(this.preamble_symbols, symbol_lable, position);
// // var p: Promise<string> = new Promise (function (resolve: (str: string) => void, reject: (str: string) => void) {
// // const argumentOfThen: string = "hello from Promise";
// // resolve(argumentOfThen);
// // }).then((result) => {
// // return result;
// // });
// /**
// * Encapsulates results from syncrone calculations in a promise and passes them throuw immediately
// * @param result The return value from .then's function
// */
// private promiseResult(result: vscode.CompletionItem[]): Thenable<vscode.CompletionItem[]> {
// return new Promise<vscode.CompletionItem[]>((resolve, reject) => { resolve(result); }).then((res) => res);
// }
private getFileNames(path: string, resolve: (completionItems: vscode.CompletionItem[]) => void, reject: (message: string) => void): void {
child_process.exec("ls " + path + " -t", (error: Error, stdout: string, stderr: string) => {
var fileNames = stdout.split("\n");
var completionItems = new Array<vscode.CompletionItem>();
for (var i = 0; i < fileNames.length; i++) {
completionItems.push(new vscode.CompletionItem(fileNames[i], vscode.CompletionItemKind.File));
}
resolve(completionItems);
});
}
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable<vscode.CompletionItem[]> {
return new Promise<vscode.CompletionItem[]>((resolve, reject) => {
var line = document.lineAt(position);
if (line.isEmptyOrWhitespace) {
return resolve([]);
}
var line_text = line.text;
var line_before_pos = line_text.substring(0, position.character).trim();
var start_index: number;
var end_index: number;
var symbol_lable: string;
var symbol_parameter: string;
if ((start_index = line_before_pos.lastIndexOf("\\") + 1) > 0) {
if ((end_index = line_before_pos.indexOf("{", start_index)) > -1) {
symbol_lable = line_before_pos.substring(start_index, end_index);
symbol_parameter = line_before_pos.substring(end_index + 1);
if ((end_index = symbol_lable.indexOf("[")) > -1) { // check for optional symbol parameters and ignore them if present
symbol_lable = symbol_lable.substring(0, end_index);
}
switch (symbol_lable) {
case "begin":
case "end":
return resolve(this.filterSymbols(this.environment_symbols, symbol_parameter, position));
case "ref":
case "eqref":
case "pageref":
return resolve(this.searchAndCreateLableSymbols(document, symbol_parameter, position));
case "usepackage": // provide package symbols by first all usepackage parameters before the last ","
return resolve(this.filterSymbols(this.package_symbols, symbol_parameter.substring(symbol_parameter.lastIndexOf(",") + 1).trim(), position));
case "input":
case "include":
case "includegraphics":
return this.getFileNames(symbol_parameter, resolve, reject); // runs async -> let getFileNames resolve / reject the promise
default: // default to NO suggestions
return resolve([]);
}
} else if ((end_index = line_before_pos.indexOf("[", start_index)) > -1) {
symbol_lable = line_before_pos.substring(start_index, end_index);
symbol_parameter = line_before_pos.substring(end_index + 1)
return resolve(this.filterParameters(symbol_lable, symbol_parameter.substring(symbol_parameter.lastIndexOf(",") + 1).trim(), position));
} else {
symbol_lable = line_before_pos.substring(start_index);
switch (this.getEnvironmentType(document, line_before_pos, position)) {
case "math":
return resolve(this.filterSymbols(this.math_symbols, symbol_lable, position));
case "tikz":
return resolve(this.filterSymbols(this.tikz_symbols, symbol_lable, position));
case "text":
return resolve(this.filterSymbols(this.text_symbols, symbol_lable, position));
default:
return resolve(this.filterSymbols(this.preamble_symbols, symbol_lable, position));
}
}
}
}
return [];
resolve([]);
}).then((res) => res);
}
}