Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'New Java File' menu to file explorer & adjust the existing file explorer menu order #820

Merged
merged 1 commit into from
Feb 23, 2024
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
2 changes: 1 addition & 1 deletion .azure-pipelines/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extends:
- task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@2
displayName: ESRP CodeSigning
inputs:
ConnectedServiceName: vscjavaci_codesign
ConnectedServiceName: vscjavaci_esrp_codesign
FolderPath: server
Pattern: com.microsoft.jdtls.ext.*.jar
signConfigType: inlineSignParams
Expand Down
2 changes: 1 addition & 1 deletion .azure-pipelines/rc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extends:
- task: SFP.build-tasks.custom-build-task-1.EsrpCodeSigning@2
displayName: ESRP CodeSigning
inputs:
ConnectedServiceName: vscjavaci_codesign
ConnectedServiceName: vscjavaci_esrp_codesign
FolderPath: server
Pattern: com.microsoft.jdtls.ext.*.jar
signConfigType: inlineSignParams
Expand Down
47 changes: 44 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -547,17 +547,22 @@
{
"command": "java.view.package.revealInProjectExplorer",
"when": "resourceFilename =~ /(.*\\.gradle)|(.*\\.gradle\\.kts)|(pom\\.xml)$/ && java:serverMode == Standard",
"group": "navigation@100"
"group": "1_javaactions"
},
{
"command": "java.view.package.revealInProjectExplorer",
"when": "resourceExtname == .java && java:serverMode == Standard",
"group": "navigation@100"
"group": "1_javaactions"
},
{
"command": "_java.project.create.from.fileexplorer.menu",
"when": "explorerResourceIsFolder",
"group": "navigation@10"
"group": "1_javaactions"
},
{
"submenu": "javaProject.newJavaFile",
"when": "explorerResourceIsFolder",
"group": "1_javaactions"
}
],
"editor/title": [
Expand Down Expand Up @@ -791,6 +796,38 @@
"group": "new2@40",
"when": "view == javaProjectExplorer && (viewItem =~ /java:(file|folder|project)/ || viewItem =~ /java:(packageRoot)(?=.*?\\b\\+resource\\b)/)"
}
],
"javaProject.newJavaFile": [
{
"command": "java.view.package.newJavaClass",
"group": "new@10",
"when": "explorerResourceIsFolder"
jdneo marked this conversation as resolved.
Show resolved Hide resolved
},
{
"command": "java.view.package.newJavaInterface",
"group": "new@20",
"when": "explorerResourceIsFolder"
},
{
"command": "java.view.package.newJavaEnum",
"group": "new@30",
"when": "explorerResourceIsFolder"
},
{
"command": "java.view.package.newJavaRecord",
"group": "new@40",
"when": "explorerResourceIsFolder"
},
{
"command": "java.view.package.newJavaAnnotation",
"group": "new@50",
"when": "explorerResourceIsFolder"
},
{
"command": "java.view.package.newJavaAbstractClass",
"group": "new@60",
"when": "explorerResourceIsFolder"
}
]
},
"submenus": [
Expand All @@ -805,6 +842,10 @@
{
"id": "javaProject.new",
"label": "%contributes.submenus.javaProject.new%"
},
{
"id": "javaProject.newJavaFile",
"label": "%contributes.commands.java.view.menus.file.newJavaClass%"
}
],
"views": {
Expand Down
74 changes: 65 additions & 9 deletions src/explorerCommands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { NodeKind } from "../java/nodeData";
import { DataNode } from "../views/dataNode";
import { resourceRoots } from "../views/packageRootNode";
import { checkJavaQualifiedName } from "./utility";
import { sendError, setUserError } from "vscode-extension-telemetry-wrapper";
import { sendError, sendInfo, setUserError } from "vscode-extension-telemetry-wrapper";

// tslint:disable no-var-requires
const stringInterpolate = require("fmtr");
Expand Down Expand Up @@ -134,7 +134,7 @@ export async function newJavaFile(): Promise<void> {
return newUntitledJavaFile();
}

const includeRecord = !(await isVersionLessThan(Uri.file(packageFsPath).toString(), 16));
const includeRecord = isLanguageServerReady() && !(await isVersionLessThan(Uri.file(packageFsPath).toString(), 16));
const supportedTypes: string[] = JavaType.getDisplayNames(true, includeRecord);
const typeName: string | undefined = await window.showQuickPick(supportedTypes,
{
Expand All @@ -148,12 +148,18 @@ export async function newJavaFile(): Promise<void> {
newJavaFile0(packageFsPath, JavaType.fromDisplayName(typeName));
}

// Create a new Java file from the context menu of Java Projects view.
export async function newJavaFileWithSpecificType(javaType: JavaType, node?: DataNode): Promise<void> {
// Create a new Java file from the context menu of Java Projects view or File Explorer.
export async function newJavaFileWithSpecificType(javaType: JavaType, node?: DataNode | Uri): Promise<void> {
sendInfo("", {
"triggernewfilefrom": (node instanceof Uri && node?.fsPath) ? "fileExplorer" : "javaProjectExplorer",
"javatype": javaType.label,
});
let packageFsPath: string | undefined;
if (!node) {
packageFsPath = await inferPackageFsPath();
} else {
} else if (node instanceof Uri && node?.fsPath) { // File Explorer
packageFsPath = node?.fsPath;
} else if (node instanceof DataNode) { // Java Projects view
if (!node?.uri || !canCreateClass(node)) {
return;
}
Expand Down Expand Up @@ -292,19 +298,27 @@ async function newUntitledJavaFile(): Promise<void> {
textEditor.insertSnippet(new SnippetString(snippets.join("\n")));
}

async function inferPackageFsPath(): Promise<string> {
function isLanguageServerReady(): boolean {
const javaLanguageSupport: Extension<any> | undefined = extensions.getExtension(ExtensionName.JAVA_LANGUAGE_SUPPORT);
if (!javaLanguageSupport || !javaLanguageSupport.isActive) {
return "";
return false;
}

const extensionApi: any = javaLanguageSupport.exports;
if (!extensionApi) {
return "";
return false;
}

if (extensionApi.serverMode !== "Standard" || extensionApi.status !== "Started") {
return "";
return false;
}

return true;
}

async function inferPackageFsPath(): Promise<string> {
if (!isLanguageServerReady()) {
return getPackageFsPathFromActiveEditor();
}

let sourcePaths: string[] | undefined;
Expand Down Expand Up @@ -342,6 +356,25 @@ async function inferPackageFsPath(): Promise<string> {
return "";
}

function getPackageFsPathFromActiveEditor() {
if (!window.activeTextEditor) {
return "";
}

const fileUri: Uri = window.activeTextEditor.document.uri;
const workspaceFolder: WorkspaceFolder | undefined = workspace.getWorkspaceFolder(fileUri);
if (!workspaceFolder) {
return "";
}

const filePath: string = window.activeTextEditor.document.uri.fsPath;
if (filePath.endsWith(".java")) {
return path.dirname(filePath);
}

return "";
}

function canCreateClass(node: DataNode): boolean {
if (node.nodeData.kind === NodeKind.Project ||
node.nodeData.kind === NodeKind.PackageRoot ||
Expand Down Expand Up @@ -383,6 +416,10 @@ async function isVersionLessThan(fileUri: string, targetVersion: number): Promis
}

async function resolvePackageName(filePath: string): Promise<string> {
if (!isLanguageServerReady()) {
return guessPackageName(filePath);
}

let sourcePaths: string[] = [];
const result: IListCommandResult =
await commands.executeCommand<IListCommandResult>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.LIST_SOURCEPATHS);
Expand All @@ -404,6 +441,25 @@ async function resolvePackageName(filePath: string): Promise<string> {
return "";
}

function guessPackageName(filePath: string): string {
const packagePath: string = path.dirname(filePath);
const knownSourcePathPrefixes: string[] = [
"src/main/java/",
"src/test/java/",
"src\\main\\java\\",
"src\\test\\java\\",
];

for (const prefix of knownSourcePathPrefixes) {
const index: number = packagePath.lastIndexOf(prefix);
if (index > -1) {
return packagePath.substring(index + prefix.length).replace(/[\\\/]/g, ".");
}
}

return "";
}

function isPrefix(parentPath: string, filePath: string): boolean {
const relative = path.relative(parentPath, filePath);
return !relative || (!relative.startsWith('..') && !path.isAbsolute(relative));
Expand Down
12 changes: 6 additions & 6 deletions src/views/dependencyExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,22 @@ export class DependencyExplorer implements Disposable {
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW, async (node: DataNode) => {
newResource(node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_CLASS, async (node?: DataNode) => {
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_CLASS, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.CLASS, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_INTERFACE, async (node?: DataNode) => {
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_INTERFACE, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.INTERFACE, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_ENUM, async (node?: DataNode) => {
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_ENUM, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.ENUM, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_RECORD, async (node?: DataNode) => {
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_RECORD, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.RECORD, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_ANNOTATION, async (node?: DataNode) => {
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_ANNOTATION, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.ANNOTATION, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_ABSTRACT_CLASS, async (node?: DataNode) => {
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_JAVA_ABSTRACT_CLASS, async (node?: DataNode | Uri) => {
newJavaFileWithSpecificType(JavaType.ABSTRACT_CLASS, node);
}),
instrumentOperationAsVsCodeCommand(Commands.VIEW_PACKAGE_NEW_FILE, async (node: DataNode) => {
Expand Down
Loading