From 5323217e773a8aa46b8fcd793645e13d9a62bc8a Mon Sep 17 00:00:00 2001 From: Shi Chen Date: Mon, 24 May 2021 14:22:39 +0800 Subject: [PATCH 1/3] add "open text editor" --- package.json | 13 +++++++++++++ src/commands/index.ts | 1 + src/formatter-settings/index.ts | 11 +++++++++++ 3 files changed, 25 insertions(+) diff --git a/package.json b/package.json index 07326a67..63e08f4a 100644 --- a/package.json +++ b/package.json @@ -199,6 +199,12 @@ "command": "java.formatterSettings", "category": "Java", "title": "Open Java Formatter Settings with Preview" + }, + { + "command": "java.formatterSettings.showTextEditor", + "category": "Java", + "title": "Open Text Editor", + "icon": "$(go-to-file)" } ], "configuration": { @@ -253,6 +259,13 @@ "group": "9_configuration@20", "when": "view == javaProjectExplorer && viewItem =~ /java:project(?=.*?\\b\\+unmanagedFolder\\b)(?=.*?\\b\\+uri\\b)/" } + ], + "editor/title": [ + { + "command": "java.formatterSettings.showTextEditor", + "when": "activeCustomEditorId == java.formatterSettingsEditor && !readOnlyMode", + "group": "navigation" + } ] }, "customEditors": [ diff --git a/src/commands/index.ts b/src/commands/index.ts index aed5ed89..01e59b6f 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -33,6 +33,7 @@ export function initialize(context: vscode.ExtensionContext) { context.subscriptions.push(instrumentOperationAsVsCodeCommand("java.webview.runCommand", webviewCmdLinkHandler)); context.subscriptions.push(vscode.commands.registerCommand("java.welcome", instrumentCommand(context, "java.welcome", showWelcomeWebview))); context.subscriptions.push(vscode.commands.registerCommand("java.formatterSettings", instrumentCommand(context, "java.formatterSettings", () => javaFormatterSettingsEditorProvider.showFormatterSettingsEditor()))); + context.subscriptions.push(vscode.commands.registerCommand("java.formatterSettings.showTextEditor", (uri) => { javaFormatterSettingsEditorProvider.reopenWithTextEditor(uri); })); context.subscriptions.push(vscode.commands.registerCommand("java.classpathConfiguration", instrumentCommand(context, "java.classpathConfiguration", async () => { const showCustomizedView: boolean = await getExpService()?.getTreatmentVariableAsync(TreatmentVariables.VSCodeConfig, TreatmentVariables.CustomizedClasspathConfigurationView, true /*checkCache*/) || false; if (showCustomizedView) { diff --git a/src/formatter-settings/index.ts b/src/formatter-settings/index.ts index e6912cda..3a436c3c 100644 --- a/src/formatter-settings/index.ts +++ b/src/formatter-settings/index.ts @@ -30,6 +30,7 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi private readOnly: boolean = false; constructor(private readonly context: vscode.ExtensionContext) { + vscode.commands.executeCommand("setContext", "readOnlyMode", false); vscode.workspace.onDidChangeConfiguration(async (e) => { if (e.affectsConfiguration(`java.${JavaConstants.SETTINGS_URL_KEY}`) || e.affectsConfiguration(`java.${JavaConstants.SETTINGS_PROFILE_KEY}`)) { this.checkedProfileSettings = false; @@ -61,6 +62,14 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi vscode.commands.executeCommand("vscode.openWith", filePath, "java.formatterSettingsEditor"); } + public reopenWithTextEditor = instrumentOperation("formatter.showTextEditor", (operationId: string, uri: any) => { + sendInfo(operationId, { editor: "RawTextEditor" }); + if (uri instanceof vscode.Uri) { + this.webviewPanel?.dispose(); + vscode.commands.executeCommand("vscode.openWith", uri, "default"); + } + }); + public async resolveCustomTextEditor(document: vscode.TextDocument, webviewPanel: vscode.WebviewPanel, _token: vscode.CancellationToken): Promise { // restrict one webviewpanel only @@ -280,6 +289,7 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi return true; } this.readOnly = false; + vscode.commands.executeCommand("setContext", "readOnlyMode", false); if (!this.settingsUrl) { sendInfo(operationId, { formatterProfile: "undefined" }); await vscode.window.showInformationMessage("No active Formatter Profile found, do you want to create a default one?", @@ -294,6 +304,7 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi "Open in read-only mode", "Download and use it locally").then(async (result) => { if (result === "Open in read-only mode") { this.readOnly = true; + vscode.commands.executeCommand("setContext", "readOnlyMode", true); return true; } else if (result === "Download and use it locally") { this.downloadAndUse(this.settingsUrl!); From bbb6920fcb4b251cd7c2744b8d1fb5bc760d5a24 Mon Sep 17 00:00:00 2001 From: Shi Chen Date: Mon, 24 May 2021 15:05:13 +0800 Subject: [PATCH 2/3] address comments --- package.json | 8 +++++++- src/formatter-settings/index.ts | 6 +----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 63e08f4a..10b42685 100644 --- a/package.json +++ b/package.json @@ -241,6 +241,12 @@ } }, "menus": { + "commandPalette": [ + { + "command": "java.formatterSettings.showTextEditor", + "when": "false" + } + ], "view/title": [ { "command": "java.runtime", @@ -263,7 +269,7 @@ "editor/title": [ { "command": "java.formatterSettings.showTextEditor", - "when": "activeCustomEditorId == java.formatterSettingsEditor && !readOnlyMode", + "when": "activeCustomEditorId == java.formatterSettingsEditor", "group": "navigation" } ] diff --git a/src/formatter-settings/index.ts b/src/formatter-settings/index.ts index 3a436c3c..4a0fed00 100644 --- a/src/formatter-settings/index.ts +++ b/src/formatter-settings/index.ts @@ -30,7 +30,6 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi private readOnly: boolean = false; constructor(private readonly context: vscode.ExtensionContext) { - vscode.commands.executeCommand("setContext", "readOnlyMode", false); vscode.workspace.onDidChangeConfiguration(async (e) => { if (e.affectsConfiguration(`java.${JavaConstants.SETTINGS_URL_KEY}`) || e.affectsConfiguration(`java.${JavaConstants.SETTINGS_PROFILE_KEY}`)) { this.checkedProfileSettings = false; @@ -65,7 +64,6 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi public reopenWithTextEditor = instrumentOperation("formatter.showTextEditor", (operationId: string, uri: any) => { sendInfo(operationId, { editor: "RawTextEditor" }); if (uri instanceof vscode.Uri) { - this.webviewPanel?.dispose(); vscode.commands.executeCommand("vscode.openWith", uri, "default"); } }); @@ -87,7 +85,7 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi }; this.webviewPanel.onDidDispose(() => { this.webviewPanel = undefined; - }) + }); const resourceUri = this.context.asAbsolutePath("./out/assets/formatter-settings/index.html"); this.webviewPanel.webview.html = await loadTextFromFile(resourceUri); this.webviewPanel.webview.onDidReceiveMessage(async (e) => { @@ -289,7 +287,6 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi return true; } this.readOnly = false; - vscode.commands.executeCommand("setContext", "readOnlyMode", false); if (!this.settingsUrl) { sendInfo(operationId, { formatterProfile: "undefined" }); await vscode.window.showInformationMessage("No active Formatter Profile found, do you want to create a default one?", @@ -304,7 +301,6 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi "Open in read-only mode", "Download and use it locally").then(async (result) => { if (result === "Open in read-only mode") { this.readOnly = true; - vscode.commands.executeCommand("setContext", "readOnlyMode", true); return true; } else if (result === "Download and use it locally") { this.downloadAndUse(this.settingsUrl!); From afbaa86752c353a7ae0465d01459443b47857bea Mon Sep 17 00:00:00 2001 From: Shi Chen Date: Mon, 24 May 2021 15:58:56 +0800 Subject: [PATCH 3/3] instrument command --- src/commands/index.ts | 2 +- src/formatter-settings/index.ts | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/commands/index.ts b/src/commands/index.ts index 01e59b6f..b65ea056 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -33,7 +33,7 @@ export function initialize(context: vscode.ExtensionContext) { context.subscriptions.push(instrumentOperationAsVsCodeCommand("java.webview.runCommand", webviewCmdLinkHandler)); context.subscriptions.push(vscode.commands.registerCommand("java.welcome", instrumentCommand(context, "java.welcome", showWelcomeWebview))); context.subscriptions.push(vscode.commands.registerCommand("java.formatterSettings", instrumentCommand(context, "java.formatterSettings", () => javaFormatterSettingsEditorProvider.showFormatterSettingsEditor()))); - context.subscriptions.push(vscode.commands.registerCommand("java.formatterSettings.showTextEditor", (uri) => { javaFormatterSettingsEditorProvider.reopenWithTextEditor(uri); })); + context.subscriptions.push(vscode.commands.registerCommand("java.formatterSettings.showTextEditor", instrumentCommand(context, "java.formatterSettings.showTextEditor", javaFormatterSettingsEditorProvider.reopenWithTextEditor))); context.subscriptions.push(vscode.commands.registerCommand("java.classpathConfiguration", instrumentCommand(context, "java.classpathConfiguration", async () => { const showCustomizedView: boolean = await getExpService()?.getTreatmentVariableAsync(TreatmentVariables.VSCodeConfig, TreatmentVariables.CustomizedClasspathConfigurationView, true /*checkCache*/) || false; if (showCustomizedView) { diff --git a/src/formatter-settings/index.ts b/src/formatter-settings/index.ts index 4a0fed00..779cf265 100644 --- a/src/formatter-settings/index.ts +++ b/src/formatter-settings/index.ts @@ -61,12 +61,11 @@ export class JavaFormatterSettingsEditorProvider implements vscode.CustomTextEdi vscode.commands.executeCommand("vscode.openWith", filePath, "java.formatterSettingsEditor"); } - public reopenWithTextEditor = instrumentOperation("formatter.showTextEditor", (operationId: string, uri: any) => { - sendInfo(operationId, { editor: "RawTextEditor" }); + public reopenWithTextEditor(_context: vscode.ExtensionContext, _operationId: string, uri: any) { if (uri instanceof vscode.Uri) { vscode.commands.executeCommand("vscode.openWith", uri, "default"); } - }); + } public async resolveCustomTextEditor(document: vscode.TextDocument, webviewPanel: vscode.WebviewPanel, _token: vscode.CancellationToken): Promise {