Skip to content

Commit

Permalink
resolves #898 enable/disable Antora support using commands (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie authored Oct 8, 2024
1 parent 6a69acb commit 1f0379a
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 27 deletions.
4 changes: 3 additions & 1 deletion i18n/fra/package.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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."
}
26 changes: 25 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -224,6 +234,14 @@
{
"command": "asciidoc.preview.toggleLock",
"when": "asciidocPreviewFocus"
},
{
"command": "asciidoc.disableAntoraSupport",
"when": "antoraSupportEnabled"
},
{
"command": "asciidoc.enableAntoraSupport",
"when": "!antoraSupportEnabled"
}
]
},
Expand Down Expand Up @@ -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%"
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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."
}
37 changes: 37 additions & 0 deletions src/commands/antoraSupport.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
}
}
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
11 changes: 10 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
Expand All @@ -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')) {
Expand Down
15 changes: 3 additions & 12 deletions src/features/antora/antoraSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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()
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/test/antoraCompletionProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []
Expand Down Expand Up @@ -57,7 +57,7 @@ link:help.adoc[]
})
assert.strictEqual(completionsItems[2].insertText, '{false}')
} finally {
await disableAntoraSupport()
await resetAntoraSupport()
}
})
})
6 changes: 3 additions & 3 deletions src/test/antoraSupport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -158,7 +158,7 @@ version: '7.1'
throw err
} finally {
await removeFiles(createdFiles)
await disableAntoraSupport()
await resetAntoraSupport()
}
}
})
Expand Down Expand Up @@ -191,7 +191,7 @@ version: ~
throw err
} finally {
await removeFiles(createdFiles)
await disableAntoraSupport()
await resetAntoraSupport()
}
})
})
4 changes: 2 additions & 2 deletions src/test/asciidocParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -78,7 +78,7 @@ asciidoc:
assert.strictEqual(result.html.includes('<p>Download from the <a href="https://marketplace.visualstudio.com/vscode" data-href="https://marketplace.visualstudio.com/vscode">Visual Studio Code Marketplace</a>.</p>'), true)
} finally {
await removeFiles(createdFiles)
await disableAntoraSupport()
await resetAntoraSupport()
}
})
})
5 changes: 1 addition & 4 deletions src/test/workspaceHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 1f0379a

Please sign in to comment.