Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@
{
"command": "maven.project.addDependency",
"title": "%contributes.commands.maven.project.addDependency%",
"category": "Maven"
"category": "Maven",
"icon": "$(add)"
},
{
"command": "maven.project.showDependencies",
Expand Down Expand Up @@ -389,6 +390,11 @@
"command": "maven.explorer.refresh",
"when": "view == mavenProjects && viewItem == Menu",
"group": "inline"
},
{
"command": "maven.project.addDependency",
"when": "view == javaProjectExplorer && viewItem =~ /java:container(?=.*?\\b\\+maven\\b)/",
"group": "inline"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"contributes.commands.maven.plugin.debug": "Debug",
"contributes.commands.maven.view.hierarchical": "Switch to hierarchical view",
"contributes.commands.maven.view.flat": "Switch to flat view",
"contributes.commands.maven.project.addDependency": "Add a dependency",
"contributes.commands.maven.project.addDependency": "Add a dependency...",
"contributes.commands.maven.project.showDependencies": "Show dependencies",
"contributes.views.explorer.mavenProjects": "Maven",
"configuration.maven.excludedFolders": "Specifies file path pattern of folders to exclude while searching for Maven projects.",
Expand Down
2 changes: 1 addition & 1 deletion package.nls.zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"contributes.commands.maven.plugin.debug": "调试",
"contributes.commands.maven.view.hierarchical": "切换到分层视图",
"contributes.commands.maven.view.flat": "切换到扁平视图",
"contributes.commands.maven.project.addDependency": "添加依赖",
"contributes.commands.maven.project.addDependency": "添加依赖",
"contributes.commands.maven.project.showDependencies": "显示所有依赖",
"contributes.views.explorer.mavenProjects": "Maven",
"configuration.maven.excludedFolders": "指定搜索 Maven 项目时要排除的文件夹。",
Expand Down
15 changes: 7 additions & 8 deletions src/handlers/addDependencyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
// Licensed under the MIT license.

import * as fse from "fs-extra";
import * as path from "path";
import * as vscode from "vscode";
import { MavenProject } from "../explorer/model/MavenProject";
import { UserError } from "../utils/errorUtils";
import { ElementNode, getNodesByTag, XmlTagName } from "../utils/lexerUtils";
import { getArtifacts, IArtifactMetadata } from "../utils/requestUtils";
import { selectProjectIfNecessary } from "../utils/uiUtils";

export async function addDependencyHandler(options?: { pomPath?: string }): Promise<void> {
export async function addDependencyHandler(options?: any): Promise<void> {
let pomPath: string;
if (options && options.pomPath) {
// for nodes from Maven explorer
pomPath = options.pomPath;
} else if (options && options.projectBasePath) {
// for "Maven dependencies" nodes from Project Manager
pomPath = path.join(options.projectBasePath, "pom.xml");
} else {
// select a project(pomfile)
const selectedProject: MavenProject | undefined = await selectProjectIfNecessary();
Expand Down Expand Up @@ -52,10 +57,6 @@ export async function addDependencyHandler(options?: { pomPath?: string }): Prom
}

async function addDependency(pomPath: string, gid: string, aid: string, version: string): Promise<void> {
if (!vscode.window.activeTextEditor) {
throw new UserError("No POM file is open.");
}

// Find out <dependencies> node and insert content.
const contentBuf: Buffer = await fse.readFile(pomPath);
const projectNodes: ElementNode[] = getNodesByTag(contentBuf.toString(), XmlTagName.Project);
Expand Down Expand Up @@ -95,10 +96,8 @@ async function insertDependency(pomPath: string, targetNode: ElementNode, gid: s
return;
}

const targetRange: vscode.Range = new vscode.Range(insertPosition, insertPosition);
const textEdit: vscode.TextEdit = new vscode.TextEdit(targetRange, targetText);
const edit: vscode.WorkspaceEdit = new vscode.WorkspaceEdit();
edit.set(currentDocument.uri, [textEdit]);
edit.insert(currentDocument.uri, insertPosition, targetText);
await vscode.workspace.applyEdit(edit);
const endingPosition: vscode.Position = currentDocument.positionAt(currentDocument.offsetAt(insertPosition) + targetText.length);
textEditor.revealRange(new vscode.Range(insertPosition, endingPosition));
Expand Down