Skip to content

Commit

Permalink
Provide completion in more cases. (#212)
Browse files Browse the repository at this point in the history
* Always provide code snippet.

* Refine completion for groupId/artifactId/version.
  • Loading branch information
Eskibear authored Jan 4, 2019
1 parent 14ca4dd commit 5b3a349
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"configuration.maven.terminal.customEnv": "Specifies an array of environment variable names and values. These environment variable values will be added to the terminal session before Maven is first executed.",
"configuration.maven.terminal.customEnv.environmentVariable": "Name of the environment variable to set.",
"configuration.maven.terminal.customEnv.value": "Value of the environment variable to set.",
"configuration.maven.completion.enabled": "Specifies whether to enable completion for POM files.",
"configuration.maven.completion.enabled": "Specifies whether to enable completion for POM files. (Reload Window to take effect)",
"configuration.maven.view": "Specifies the way of viewing Maven projects."
}
24 changes: 15 additions & 9 deletions src/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as vscode from "vscode";
import Lexx from "xml-zero-lexer";

const dependencySnippet: vscode.SnippetString = new vscode.SnippetString(["<dependency>", "\t<groupId>$1</groupId>", "\t<artifactId>$2</artifactId>", "</dependency>$0"].join("\n"));
const pluginSnippet: vscode.SnippetString = new vscode.SnippetString(["<plugin>", "\t<groupId>$1</groupId>", "\t<artifactId>$2</artifactId>", "</plugin>$0"].join("\n"));

class CompletionProvider implements vscode.CompletionItemProvider {
public localRepository: string;
Expand Down Expand Up @@ -44,10 +45,6 @@ class CompletionProvider implements vscode.CompletionItemProvider {
}

public provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, _token: vscode.CancellationToken, _context: vscode.CompletionContext): vscode.ProviderResult<vscode.CompletionItem[] | vscode.CompletionList> {
if (!this.metadata) {
return null;
}

const range: vscode.Range = new vscode.Range(new vscode.Position(0, 0), position);
const text: string = document.getText(range);
const tokens: number[][] = Lexx(text);
Expand All @@ -56,18 +53,18 @@ class CompletionProvider implements vscode.CompletionItemProvider {
return null;
}

if (currentNode.tag === "groupId" && currentNode.parent && currentNode.parent.tag === "dependency") {
if (currentNode.tag === "groupId") {
return this.completeForGroupId(document, position, currentNode);
}
if (currentNode.tag === "artifactId" && currentNode.parent && currentNode.parent.tag === "dependency") {
if (currentNode.tag === "artifactId" && currentNode.parent) {
const groupIdNode: ElementNode = currentNode.parent.children.find(elem => elem.tag === "groupId");
if (!groupIdNode) {
return null;
}

return this.completeForArtifactId(document, position, currentNode, groupIdNode.text);
}
if (currentNode.tag === "version" && currentNode.parent && currentNode.parent.tag === "dependency") {
if (currentNode.tag === "version" && currentNode.parent) {
const groupIdNode: ElementNode = currentNode.parent.children.find(elem => elem.tag === "groupId");
if (!groupIdNode) {
return null;
Expand All @@ -85,10 +82,19 @@ class CompletionProvider implements vscode.CompletionItemProvider {
snippetItem.insertText = dependencySnippet;
return new vscode.CompletionList([snippetItem], false);
}
if (currentNode.tag === "plugins") {
const snippetItem: vscode.CompletionItem = new vscode.CompletionItem("plugin", vscode.CompletionItemKind.Snippet);
snippetItem.insertText = pluginSnippet;
return new vscode.CompletionList([snippetItem], false);
}
return null;
}

private completeForGroupId(document: vscode.TextDocument, position: vscode.Position, groupIdNode: ElementNode): vscode.CompletionList {
if (!this.metadata) {
return null;
}

const validGroupIds: string[] = Object.keys(this.metadata);
const targetRange: vscode.Range = new vscode.Range(document.positionAt(groupIdNode.offset), position);
const groupIdItems: vscode.CompletionItem[] = validGroupIds.map(gid => {
Expand All @@ -101,7 +107,7 @@ class CompletionProvider implements vscode.CompletionItemProvider {
}

private completeForArtifactId(document: vscode.TextDocument, position: vscode.Position, artifactIdNode: ElementNode, groupId: string): vscode.CompletionList {
if (!groupId) {
if (!this.metadata || !groupId) {
return null;
}

Expand All @@ -122,7 +128,7 @@ class CompletionProvider implements vscode.CompletionItemProvider {
}

private completeForVersion(document: vscode.TextDocument, position: vscode.Position, versionNode: ElementNode, groupId: string, artifactId: string): vscode.CompletionList {
if (!groupId || !artifactId) {
if (!this.metadata || !groupId || !artifactId) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ async function doActivate(_operationId: string, context: vscode.ExtensionContext
})
);
// completion item provider
context.subscriptions.push(vscode.languages.registerCompletionItemProvider([{ language: "xml", scheme: "file", pattern: "**/pom.xml" }], completionProvider, "."));
if (vscode.workspace.getConfiguration("maven", null).get<boolean>("completion.enabled")) {
context.subscriptions.push(vscode.languages.registerCompletionItemProvider([{ language: "xml", scheme: "file", pattern: "**/pom.xml" }], completionProvider, "."));
vscode.window.withProgress({ location: vscode.ProgressLocation.Window }, (progress) => {
progress.report({ message: "Updating local Maven repository indices" });
return completionProvider.initialize();
Expand Down

0 comments on commit 5b3a349

Please sign in to comment.