From 8bce5ac8776fc2832592e6b957dba0522043d400 Mon Sep 17 00:00:00 2001 From: Daniel Kapla Date: Thu, 7 Dec 2017 18:30:43 +0100 Subject: [PATCH] add: Enabled completionItemprovider to work async (added Promise), add: input, include, ... commands and parameters to *_symbols --- dictionary/parameter_dictionary.ts | 6 ++ dictionary/preamble_symbols.ts | 55 +++++++++++-- dictionary/text_symbols.ts | 24 ++++++ src/completionItemProvider.ts | 127 +++++++++++++++++++---------- 4 files changed, 159 insertions(+), 53 deletions(-) diff --git a/dictionary/parameter_dictionary.ts b/dictionary/parameter_dictionary.ts index 117c72e..2cd8f22 100644 --- a/dictionary/parameter_dictionary.ts +++ b/dictionary/parameter_dictionary.ts @@ -244,6 +244,12 @@ export default { "h": { documentation: "hier" }, "l": { documentation: "low" }, "p": { documentation: "page" }, + }, + includegraphics: { + width: { detail: "width=", insertText: "width=${0:}", documentation: "scale graphic to the specified .", }, + height: { detail: "height=", insertText: "height=${0:}", documentation: "scale graphic to the specified .", }, + angle: { detail: "angle=", insertText: "angle=${0:}", documentation: "rotate graphic counterclockwise.", }, + scale: { detail: "scale=", insertText: "scale=${0:}", documentation: "scale graphic by .", }, } }; diff --git a/dictionary/preamble_symbols.ts b/dictionary/preamble_symbols.ts index b6424a0..eb716e2 100644 --- a/dictionary/preamble_symbols.ts +++ b/dictionary/preamble_symbols.ts @@ -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 + }, } diff --git a/dictionary/text_symbols.ts b/dictionary/text_symbols.ts index 87d829d..ff51a95 100644 --- a/dictionary/text_symbols.ts +++ b/dictionary/text_symbols.ts @@ -1,6 +1,30 @@ import { latex_types } from "./symbol_types"; export default { + input: { + detail: "\\input{}", + insertText: "input{${0:}}", + documentation: "Includes the sourcecode from the target .tex file.", + kind: latex_types.keyword + }, + include: { + detail: "\\include{}", + insertText: "include{${0:}}", + 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[=,...]{}\nRequired package \"graphicx\"", + insertText: "includegraphics[$1]{${0:}}", + documentation: "Use the command \\includegraphics[=,...]{} to include into your document. The optional parameter accepts a comma separated list of 's and associated 's. The 's can be used to alter the width, height and rotation of the included graphic.", + kind: latex_types.keyword + }, + verbatiminput: { + detail: "\\verbatiminput{}\nRequired package \"verbatim\"", + insertText: "verbatiminput{${0:}}", + 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 }, diff --git a/src/completionItemProvider.ts b/src/completionItemProvider.ts index 69c8995..ca881e0 100644 --- a/src/completionItemProvider.ts +++ b/src/completionItemProvider.ts @@ -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 = 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 { + // return new Promise((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(); + 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 { + return new Promise((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); } } \ No newline at end of file