|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import * as fse from "fs-extra"; |
| 5 | +import * as path from "path"; |
| 6 | +import { Uri, window, workspace, WorkspaceEdit } from "vscode"; |
| 7 | +import { NodeKind } from "../java/nodeData"; |
| 8 | +import { isJavaIdentifier, isKeyword } from "../utility"; |
| 9 | +import { DataNode } from "../views/dataNode"; |
| 10 | + |
| 11 | +export async function newJavaClass(node: DataNode): Promise<void> { |
| 12 | + const packageFsPath: string = Uri.parse(node.uri).fsPath; |
| 13 | + const className: string | undefined = await window.showInputBox({ |
| 14 | + placeHolder: "Input the class name", |
| 15 | + ignoreFocusOut: true, |
| 16 | + validateInput: async (value: string): Promise<string> => { |
| 17 | + const checkMessage: string = checkJavaQualifiedName(value); |
| 18 | + if (checkMessage) { |
| 19 | + return checkMessage; |
| 20 | + } |
| 21 | + |
| 22 | + if (await fse.pathExists(getNewFilePath(packageFsPath, value))) { |
| 23 | + return "Class already exists."; |
| 24 | + } |
| 25 | + |
| 26 | + return ""; |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + if (!className) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + // `workspace.applyEdit()` will trigger a workspace file event, and let the |
| 35 | + // vscode-java extension to handle the type: class, interface or enum. |
| 36 | + const workspaceEdit: WorkspaceEdit = new WorkspaceEdit(); |
| 37 | + const fsPath: string = getNewFilePath(packageFsPath, className); |
| 38 | + workspaceEdit.createFile(Uri.file(fsPath)); |
| 39 | + workspace.applyEdit(workspaceEdit); |
| 40 | +} |
| 41 | + |
| 42 | +function getNewFilePath(basePath: string, className: string): string { |
| 43 | + if (className.endsWith(".java")) { |
| 44 | + className = className.substr(0, className.length - ".java".length); |
| 45 | + } |
| 46 | + return path.join(basePath, ...className.split(".")) + ".java"; |
| 47 | +} |
| 48 | + |
| 49 | +export async function newPackage(node: DataNode): Promise<void> { |
| 50 | + let defaultValue: string; |
| 51 | + let packageRootPath: string; |
| 52 | + if (node.nodeData.kind === NodeKind.PackageRoot) { |
| 53 | + defaultValue = ""; |
| 54 | + packageRootPath = Uri.parse(node.uri).fsPath; |
| 55 | + } else if (node.nodeData.kind === NodeKind.Package) { |
| 56 | + defaultValue = node.nodeData.name + "."; |
| 57 | + const numberOfSegment: number = node.nodeData.name.split(".").length; |
| 58 | + packageRootPath = path.join(Uri.parse(node.uri).fsPath, ...Array(numberOfSegment).fill("..")); |
| 59 | + } else { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const packageName: string | undefined = await window.showInputBox({ |
| 64 | + value: defaultValue, |
| 65 | + placeHolder: "Input the package name", |
| 66 | + valueSelection: [defaultValue.length, defaultValue.length], |
| 67 | + ignoreFocusOut: true, |
| 68 | + validateInput: async (value: string): Promise<string> => { |
| 69 | + const checkMessage: string = checkJavaQualifiedName(value); |
| 70 | + if (checkMessage) { |
| 71 | + return checkMessage; |
| 72 | + } |
| 73 | + |
| 74 | + if (await fse.pathExists(getNewPackagePath(packageRootPath, value))) { |
| 75 | + return "Package already exists."; |
| 76 | + } |
| 77 | + |
| 78 | + return ""; |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + if (!packageName) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + await fse.ensureDir(getNewPackagePath(packageRootPath, packageName)); |
| 87 | +} |
| 88 | + |
| 89 | +function getNewPackagePath(packageRootPath: string, packageName: string): string { |
| 90 | + return path.join(packageRootPath, ...packageName.split(".")); |
| 91 | +} |
| 92 | + |
| 93 | +function checkJavaQualifiedName(value: string): string { |
| 94 | + if (!value || !value.trim()) { |
| 95 | + return "Input cannot be empty."; |
| 96 | + } |
| 97 | + |
| 98 | + for (const part of value.split(".")) { |
| 99 | + if (isKeyword(part)) { |
| 100 | + return `Keyword '${part}' cannot be used.`; |
| 101 | + } |
| 102 | + |
| 103 | + if (!isJavaIdentifier(part)) { |
| 104 | + return `Invalid Java qualified name.`; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return ""; |
| 109 | +} |
0 commit comments