From 1f0379a42970df00bd5a030688cba09ae828ce7d Mon Sep 17 00:00:00 2001 From: Guillaume Grossetie Date: Tue, 8 Oct 2024 09:26:07 +0200 Subject: [PATCH] resolves #898 enable/disable Antora support using commands (#900) --- i18n/fra/package.i18n.json | 4 ++- package.json | 26 +++++++++++++++- package.nls.json | 6 +++- src/commands/antoraSupport.ts | 37 +++++++++++++++++++++++ src/commands/index.ts | 1 + src/extension.ts | 11 ++++++- src/features/antora/antoraSupport.ts | 15 ++------- src/test/antoraCompletionProvider.test.ts | 4 +-- src/test/antoraSupport.test.ts | 6 ++-- src/test/asciidocParser.test.ts | 4 +-- src/test/workspaceHelper.ts | 5 +-- 11 files changed, 92 insertions(+), 27 deletions(-) create mode 100644 src/commands/antoraSupport.ts diff --git a/i18n/fra/package.i18n.json b/i18n/fra/package.i18n.json index 9a0f6bd8..bd381476 100644 --- a/i18n/fra/package.i18n.json +++ b/i18n/fra/package.i18n.json @@ -38,6 +38,7 @@ "asciidoc.antora.title": "Antora", "asciidoc.antora.enableAntoraSupport.desc": "Active le support [Antora](https://antora.org/).", + "asciidoc.antora.showEnableAntoraPrompt.desc": "Affiche une fenêtre contextuelle permettant d'activer le support [Antora](https://antora.org/) quand un fichier antora.yml est trouvé.", "asciidoc.pdf.title": "PDF", "asciidoc.pdf.engine.desc": "Contrôle le moteur PDF à utiliser pour l'export PDF.", @@ -78,5 +79,6 @@ "asciidoc.useWorkspaceRoot.desc": "**Obsolète:** Quand on est dans un espace de travail, utilise la racine de l'espace de travail en tant que répertoire de base pour Asciidoctor", "asciidoc.useWorkspaceRoot.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.useWorkspaceRootAsBaseDirectory#` et n'a plus aucun effet.", "asciidoc.use_kroki.desc": "**Obsolète:** Active l'extension Kroki afin de générer des diagrammes.", - "asciidoc.use_kroki.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.extensions.enableKroki#` et n'a plus aucun effet." + "asciidoc.use_kroki.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.extensions.enableKroki#` et n'a plus aucun effet.", + "asciidoc.antora.enableAntoraSupport.deprecationMessage": "Ce paramètre a été remplacé par `#asciidoc.antora.showEnableAntoraPrompt#` et n'a plus aucun effet." } diff --git a/package.json b/package.json index 5842af25..9692d737 100644 --- a/package.json +++ b/package.json @@ -126,6 +126,16 @@ "title": "%asciidoc.saveDocbook.title%", "category": "AsciiDoc", "icon": "$(file-code)" + }, + { + "command": "asciidoc.enableAntoraSupport", + "title": "%asciidoc.enableAntoraSupport.title%", + "category": "AsciiDoc" + }, + { + "command": "asciidoc.disableAntoraSupport", + "title": "%asciidoc.disableAntoraSupport.title%", + "category": "AsciiDoc" } ], "menus": { @@ -224,6 +234,14 @@ { "command": "asciidoc.preview.toggleLock", "when": "asciidocPreviewFocus" + }, + { + "command": "asciidoc.disableAntoraSupport", + "when": "antoraSupportEnabled" + }, + { + "command": "asciidoc.enableAntoraSupport", + "when": "!antoraSupportEnabled" } ] }, @@ -555,7 +573,13 @@ "asciidoc.antora.enableAntoraSupport": { "type": "boolean", "default": false, - "markdownDescription": "%asciidoc.antora.enableAntoraSupport.desc%" + "markdownDescription": "%asciidoc.antora.enableAntoraSupport.desc%", + "markdownDeprecationMessage": "%asciidoc.antora.enableAntoraSupport.deprecationMessage%" + }, + "asciidoc.antora.showEnableAntoraPrompt": { + "type": "boolean", + "default": false, + "markdownDescription": "%asciidoc.antora.showEnableAntoraPrompt.desc%" } } } diff --git a/package.nls.json b/package.nls.json index b47e00f7..8a5573d9 100644 --- a/package.nls.json +++ b/package.nls.json @@ -15,6 +15,8 @@ "asciidoc.showPreviewSecuritySelector.title": "Manage Preview Security Settings", "asciidoc.showAsciidoctorExtensionsTrustModeSelector.title": "Manage Asciidoctor.js Extensions Trust Mode", "asciidoc.showSource.title": "Show Source", + "asciidoc.enableAntoraSupport.title": "Enable Antora support in this workspace", + "asciidoc.disableAntoraSupport.title": "Disable Antora support in this workspace", "asciidoc.preview.asciidoctorAttributes.desc": "Sets Asciidoctor attributes used in the preview. Attributes need to be written as an object of type {string: string}.", "asciidoc.preview.refreshInterval.desc": "Interval in milliseconds between two consecutive updates of the preview. The value 0 means it will only update the preview on save.", @@ -76,7 +78,9 @@ "asciidoc.useWorkspaceRoot.deprecationMessage": "This setting has been replaced by `#asciidoc.useWorkspaceRootAsBaseDirectory#` and no longer has any effect.", "asciidoc.use_kroki.desc": "**Deprecated:** Enables Kroki integration to generate diagrams.", "asciidoc.use_kroki.deprecationMessage": "This setting has been replaced by `#asciidoc.extensions.enableKroki#` and no longer has any effect.", + "asciidoc.antora.enableAntoraSupport.deprecationMessage": "This setting has been replaced by `#asciidoc.antora.showEnableAntoraPrompt#` and no longer has any effect.", "asciidoc.antora.title": "Antora", - "asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support." + "asciidoc.antora.enableAntoraSupport.desc": "Enable [Antora](https://antora.org/) support.", + "asciidoc.antora.showEnableAntoraPrompt.desc": "Show a prompt to enable [Antora](https://antora.org/) support when an antora.yml file is detected." } diff --git a/src/commands/antoraSupport.ts b/src/commands/antoraSupport.ts new file mode 100644 index 00000000..9a7ac248 --- /dev/null +++ b/src/commands/antoraSupport.ts @@ -0,0 +1,37 @@ +import { Command } from '../commandManager' +import { Memento, commands } from 'vscode' +import { AsciidocPreviewManager } from '../features/previewManager' + +export const antoraSupportEnabledContextKey = 'antoraSupportEnabled' + +export class EnableAntoraSupport implements Command { + public readonly id = 'asciidoc.enableAntoraSupport' + + public constructor (private readonly workspaceState: Memento, private readonly asciidocPreviewManager: AsciidocPreviewManager) { + } + + public execute () { + this.workspaceState.update('antoraSupportSetting', true) + .then(() => { + commands.executeCommand('setContext', antoraSupportEnabledContextKey, true).then(() => { + this.asciidocPreviewManager.refresh(true) + }) + }) + } +} + +export class DisableAntoraSupport implements Command { + public readonly id = 'asciidoc.disableAntoraSupport' + + public constructor (private readonly workspaceState: Memento, private readonly asciidocPreviewManager: AsciidocPreviewManager) { + } + + public execute () { + this.workspaceState.update('antoraSupportSetting', false) + .then(() => { + commands.executeCommand('setContext', antoraSupportEnabledContextKey, false).then(() => { + this.asciidocPreviewManager.refresh(true) + }) + }) + } +} diff --git a/src/commands/index.ts b/src/commands/index.ts index 3c45ca17..a2fdc3e5 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -14,3 +14,4 @@ export { ExportAsPDF } from './exportAsPDF' export { PasteImage } from './pasteImage' export { SaveHTML } from './saveHTML' export { SaveDocbook } from './saveDocbook' +export { EnableAntoraSupport, DisableAntoraSupport } from './antoraSupport' diff --git a/src/extension.ts b/src/extension.ts index 33f1a1d1..cc21855f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -23,6 +23,7 @@ import { AsciidoctorDiagnostic } from './features/asciidoctorDiagnostic' import { AsciidocEngine } from './asciidocEngine' import { AsciidocIncludeItemsLoader, AsciidocLoader } from './asciidocLoader' import { AsciidoctorIncludeItems } from './features/asciidoctorIncludeItems' +import { antoraSupportEnabledContextKey } from './commands/antoraSupport' export async function activate (context: vscode.ExtensionContext) { // Set context as a global as some tests depend on it @@ -74,7 +75,7 @@ export async function activate (context: vscode.ExtensionContext) { const previewManager = new AsciidocPreviewManager(contentProvider, logger, contributionProvider) context.subscriptions.push(previewManager) context.subscriptions.push(new AsciidocTargetPathAutoCompletionMonitor(asciidocLoader)) - context.subscriptions.push(await AntoraSupportManager.getInstance(context.workspaceState)) + context.subscriptions.push(AntoraSupportManager.getInstance(context.workspaceState)) context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(selector, symbolProvider)) context.subscriptions.push(vscode.languages.registerDocumentLinkProvider(selector, new LinkProvider(asciidocIncludeItemsLoader))) context.subscriptions.push(vscode.languages.registerWorkspaceSymbolProvider(new AsciidocWorkspaceSymbolProvider(symbolProvider))) @@ -100,6 +101,14 @@ export async function activate (context: vscode.ExtensionContext) { commandManager.register(new commands.ShowPreviewCommand(previewManager)) commandManager.register(new commands.SaveHTML(asciidocEngine)) commandManager.register(new commands.SaveDocbook(asciidocEngine)) + commandManager.register(new commands.EnableAntoraSupport(context.workspaceState, previewManager)) + commandManager.register(new commands.DisableAntoraSupport(context.workspaceState, previewManager)) + + const antoraSupportSetting = context.workspaceState.get('antoraSupportSetting') + if (antoraSupportSetting === true || antoraSupportSetting === false) { + await vscode.commands.executeCommand('setContext', antoraSupportEnabledContextKey, antoraSupportSetting) + previewManager.refresh(true) + } context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(async (e) => { if (e.affectsConfiguration('asciidoc.registerAsciidoctorExtensions')) { diff --git a/src/features/antora/antoraSupport.ts b/src/features/antora/antoraSupport.ts index 41668f5b..ce3cfc49 100644 --- a/src/features/antora/antoraSupport.ts +++ b/src/features/antora/antoraSupport.ts @@ -115,14 +115,10 @@ export class AntoraSupportManager implements vscode.Disposable { } AntoraSupportManager.instance = new AntoraSupportManager() AntoraSupportManager.workspaceState = workspaceState - const workspaceConfiguration = vscode.workspace.getConfiguration('asciidoc', null) // look for Antora support setting in workspace state const isEnableAntoraSupportSettingDefined = workspaceState.get('antoraSupportSetting') if (isEnableAntoraSupportSettingDefined === true) { - const enableAntoraSupport = workspaceConfiguration.get('antora.enableAntoraSupport') - if (enableAntoraSupport === true) { - AntoraSupportManager.instance.registerFeatures() - } + AntoraSupportManager.instance.registerFeatures() } else if (isEnableAntoraSupportSettingDefined === undefined) { // choice has not been made const onDidOpenAsciiDocFileAskAntoraSupport = vscode.workspace.onDidOpenTextDocument(async (textDocument) => { @@ -134,9 +130,8 @@ export class AntoraSupportManager implements vscode.Disposable { yesAnswer, noAnswer ) - const enableAntoraSupport = answer === yesAnswer ? true : (answer === noAnswer ? false : undefined) + const enableAntoraSupport = answer === yesAnswer await workspaceState.update('antoraSupportSetting', enableAntoraSupport) - await workspaceConfiguration.update('antora.enableAntoraSupport', enableAntoraSupport) if (enableAntoraSupport) { AntoraSupportManager.instance.registerFeatures() } @@ -157,14 +152,10 @@ export class AntoraSupportManager implements vscode.Disposable { } public isEnabled (): Boolean { - const workspaceConfiguration = vscode.workspace.getConfiguration('asciidoc', null) // look for Antora support setting in workspace state const isEnableAntoraSupportSettingDefined = AntoraSupportManager.workspaceState.get('antoraSupportSetting') if (isEnableAntoraSupportSettingDefined === true) { - const enableAntoraSupport = workspaceConfiguration.get('antora.enableAntoraSupport') - if (enableAntoraSupport === true) { - return true - } + return true } // choice has not been made or Antora is explicitly disabled return false diff --git a/src/test/antoraCompletionProvider.test.ts b/src/test/antoraCompletionProvider.test.ts index a17fb172..7d30f18d 100644 --- a/src/test/antoraCompletionProvider.test.ts +++ b/src/test/antoraCompletionProvider.test.ts @@ -3,7 +3,7 @@ import * as vscode from 'vscode' import assert from 'assert' import AntoraCompletionProvider from '../features/antora/antoraCompletionProvider' import { Position } from 'vscode' -import { createDirectory, createFile, disableAntoraSupport, enableAntoraSupport, removeFiles } from './workspaceHelper' +import { resetAntoraSupport, createDirectory, createFile, enableAntoraSupport, removeFiles } from './workspaceHelper' suite('Antora CompletionsProvider', () => { const createdFiles: vscode.Uri[] = [] @@ -57,7 +57,7 @@ link:help.adoc[] }) assert.strictEqual(completionsItems[2].insertText, '{false}') } finally { - await disableAntoraSupport() + await resetAntoraSupport() } }) }) diff --git a/src/test/antoraSupport.test.ts b/src/test/antoraSupport.test.ts index 55b7bbec..6e20d00b 100644 --- a/src/test/antoraSupport.test.ts +++ b/src/test/antoraSupport.test.ts @@ -3,7 +3,7 @@ import * as vscode from 'vscode' import * as assert from 'assert' import 'mocha' import { findAntoraConfigFile, getAntoraDocumentContext } from '../features/antora/antoraSupport' -import { createDirectories, createDirectory, createFile, createLink, disableAntoraSupport, enableAntoraSupport, removeFiles } from './workspaceHelper' +import { resetAntoraSupport, createDirectories, createDirectory, createFile, createLink, enableAntoraSupport, removeFiles } from './workspaceHelper' import { extensionContext } from './helper' import { getDefaultWorkspaceFolderUri } from '../util/workspace' @@ -158,7 +158,7 @@ version: '7.1' throw err } finally { await removeFiles(createdFiles) - await disableAntoraSupport() + await resetAntoraSupport() } } }) @@ -191,7 +191,7 @@ version: ~ throw err } finally { await removeFiles(createdFiles) - await disableAntoraSupport() + await resetAntoraSupport() } }) }) diff --git a/src/test/asciidocParser.test.ts b/src/test/asciidocParser.test.ts index f0c73618..5c090f34 100644 --- a/src/test/asciidocParser.test.ts +++ b/src/test/asciidocParser.test.ts @@ -10,7 +10,7 @@ import { AsciidoctorExtensions } from '../features/asciidoctorExtensions' import { AsciidoctorDiagnostic } from '../features/asciidoctorDiagnostic' import { AsciidoctorExtensionsSecurityPolicyArbiter } from '../security' import { InMemoryDocument } from './inMemoryDocument' -import { createDirectory, createFile, disableAntoraSupport, enableAntoraSupport, removeFiles } from './workspaceHelper' +import { resetAntoraSupport, createDirectory, createFile, enableAntoraSupport, removeFiles } from './workspaceHelper' class TestWebviewResourceProvider implements WebviewResourceProvider { asWebviewUri (resource: vscode.Uri): vscode.Uri { @@ -78,7 +78,7 @@ asciidoc: assert.strictEqual(result.html.includes('

Download from the Visual Studio Code Marketplace.

'), true) } finally { await removeFiles(createdFiles) - await disableAntoraSupport() + await resetAntoraSupport() } }) }) diff --git a/src/test/workspaceHelper.ts b/src/test/workspaceHelper.ts index 56a6c164..80a697c9 100644 --- a/src/test/workspaceHelper.ts +++ b/src/test/workspaceHelper.ts @@ -67,12 +67,9 @@ export async function createLink (existingPathSegments: string[], newPathSegment } export async function enableAntoraSupport () { - const workspaceConfiguration = vscode.workspace.getConfiguration('asciidoc', null) - await workspaceConfiguration.update('antora.enableAntoraSupport', true) await extensionContext.workspaceState.update('antoraSupportSetting', true) } -export async function disableAntoraSupport () { +export async function resetAntoraSupport () { await extensionContext.workspaceState.update('antoraSupportSetting', undefined) - await vscode.workspace.getConfiguration('asciidoc', null).update('antora.enableAntoraSupport', undefined) }