register 'add dependency' command to Project Manager extension (#529)
* register 'add dependency' command to Project Manager extension Signed-off-by: Yan Zhang <yanzh@microsoft.com> * update wording Signed-off-by: Yan Zhang <yanzh@microsoft.com>
This commit is contained in:
Родитель
4148347e8d
Коммит
7d324a7029
|
@ -598,7 +598,7 @@
|
|||
},
|
||||
"util": {
|
||||
"version": "0.10.3",
|
||||
"resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz",
|
||||
"resolved": "http://registry.npmjs.org/util/-/util-0.10.3.tgz",
|
||||
"integrity": "sha1-evsa/lCAUkZInj23/g7TeTNqwPk=",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
|
@ -3676,7 +3676,7 @@
|
|||
},
|
||||
"json5": {
|
||||
"version": "0.5.1",
|
||||
"resolved": "https://registry.npmjs.org/json5/-/json5-0.5.1.tgz",
|
||||
"resolved": "http://registry.npmjs.org/json5/-/json5-0.5.1.tgz",
|
||||
"integrity": "sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=",
|
||||
"dev": true
|
||||
},
|
||||
|
|
|
@ -168,7 +168,8 @@
|
|||
{
|
||||
"command": "maven.project.addDependency",
|
||||
"title": "%contributes.commands.maven.project.addDependency%",
|
||||
"category": "Maven"
|
||||
"category": "Maven",
|
||||
"icon": "$(add)"
|
||||
},
|
||||
{
|
||||
"command": "maven.project.showDependencies",
|
||||
|
@ -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"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
|
@ -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.",
|
||||
|
|
|
@ -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 项目时要排除的文件夹。",
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
// 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";
|
||||
|
@ -9,10 +10,14 @@ 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();
|
||||
|
@ -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);
|
||||
|
@ -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));
|
||||
|
|
Загрузка…
Ссылка в новой задаче