Skip to content

Commit

Permalink
resolves asciidoctor#691 append AsciiDoc attributes defined in antora…
Browse files Browse the repository at this point in the history
….yml
  • Loading branch information
ggrossetie committed Jan 22, 2023
1 parent 7707c1f commit 1f0114d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 22 deletions.
9 changes: 7 additions & 2 deletions src/asciidocParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AsciidocPreviewConfigurationManager } from './features/previewConfig'
import { SkinnyTextDocument } from './util/document'
import { IncludeItems } from './asciidoctorFindIncludeProcessor'
import { AsciidocContributionProvider } from './asciidocExtensions'
import { getAntoraDocumentContext } from './features/antora/antoraSupport'
import { AntoraSupportManager, getAntoraDocumentContext } from './features/antora/antoraSupport'
import { WebviewResourceProvider } from './util/resources'
import { getAsciidoctorConfigContent } from './features/asciidoctorConfig'

Expand Down Expand Up @@ -180,10 +180,15 @@ export class AsciidocParser {
attributes.env = 'vscode'
attributes['relfilesuffix@'] = '.adoc'

const antoraSupport = await AntoraSupportManager.getInstance(context.workspaceState)
const antoraAttributes = await antoraSupport.getAttributes(doc.uri)
const baseDir = AsciidocParser.getBaseDir(doc.fileName)
const templateDirs = this.getTemplateDirs()
const options: { [key: string]: any } = {
attributes,
attributes: {
...attributes,
...antoraAttributes,
},
backend: 'webview-html5',
extension_registry: registry,
header_footer: true,
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import AsciidocFoldingRangeProvider from './features/foldingProvider'
import { AntoraSupportManager } from './features/antora/antoraSupport'
import { DropImageIntoEditorProvider } from './features/dropIntoEditor'

export function activate (context: vscode.ExtensionContext) {
export async function activate (context: vscode.ExtensionContext) {
// Set context as a global as some tests depend on it
(global as any).testExtensionContext = context
const contributionProvider = getAsciidocExtensionContributions(context)
Expand Down Expand Up @@ -53,7 +53,7 @@ export function activate (context: vscode.ExtensionContext) {
const previewManager = new AsciidocPreviewManager(contentProvider, logger, contributionProvider)
context.subscriptions.push(previewManager)
context.subscriptions.push(new AsciidocFileIncludeAutoCompletionMonitor())
context.subscriptions.push(new AntoraSupportManager(context.workspaceState))
context.subscriptions.push(await AntoraSupportManager.getInstance(context.workspaceState))

context.subscriptions.push(vscode.languages.registerDocumentSymbolProvider(selector, symbolProvider))
context.subscriptions.push(vscode.languages.registerDocumentLinkProvider(selector, new LinkProvider()))
Expand Down
79 changes: 61 additions & 18 deletions src/features/antora/antoraSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export class AntoraDocumentContext {
}
return undefined
}

public getComponents () {
return this.antoraContext.contentCatalog.getComponents()
}

public getImages () {
return this.antoraContext.contentCatalog.findBy({ family: 'image' })
}
}

export class AntoraContext {
Expand Down Expand Up @@ -65,18 +73,28 @@ export class AntoraContext {
}

export class AntoraSupportManager implements vscode.Disposable {
// eslint-disable-next-line no-use-before-define
private static instance: AntoraSupportManager
private static workspaceState: Memento
private readonly _disposables: vscode.Disposable[] = []

public constructor (private readonly context: Memento) {
this.context = context
private constructor () {
}

public static getInstance (workspaceState: Memento) {
if (AntoraSupportManager.instance) {
AntoraSupportManager.workspaceState = workspaceState
return AntoraSupportManager.instance
}
AntoraSupportManager.instance = new AntoraSupportManager()
AntoraSupportManager.workspaceState = workspaceState
const workspaceConfiguration = vscode.workspace.getConfiguration('asciidoc', null)
// look for Antora support setting in workspace state
const workspaceState: vscode.Memento = this.context
const isEnableAntoraSupportSettingDefined = workspaceState.get('antoraSupportSetting')
if (isEnableAntoraSupportSettingDefined === true) {
const enableAntoraSupport = workspaceConfiguration.get('antora.enableAntoraSupport')
if (enableAntoraSupport === true) {
this.activate()
AntoraSupportManager.instance.registerFeatures()
}
} else if (isEnableAntoraSupportSettingDefined === undefined) {
// choice has not been made
Expand All @@ -93,26 +111,51 @@ export class AntoraSupportManager implements vscode.Disposable {
const enableAntoraSupport = answer === yesAnswer ? true : (answer === noAnswer ? false : undefined)
await workspaceConfiguration.update('antora.enableAntoraSupport', enableAntoraSupport)
if (enableAntoraSupport) {
this.activate()
AntoraSupportManager.instance.registerFeatures()
}
// do not ask again to avoid bothering users
onDidOpenAsciiDocFileAskAntoraSupport.dispose()
}
})
this._disposables.push(onDidOpenAsciiDocFileAskAntoraSupport)
AntoraSupportManager.instance._disposables.push(onDidOpenAsciiDocFileAskAntoraSupport)
}
}

private activate (): void {
const completionProvider = vscode.languages.registerCompletionItemProvider(
{
language: 'asciidoc',
scheme: 'file',
},
new AntoraCompletionProvider(),
'{'
public static async isEnabled (workspaceState: Memento): Promise<Boolean> {
return (await AntoraSupportManager.getInstance(workspaceState)).isEnabled()
}

public async getAttributes (textDocumentUri: Uri): Promise<{ [key: string]: string }> {
const antoraEnabled = this.isEnabled()
if (antoraEnabled) {
return getAttributes(textDocumentUri)
}
return {}
}

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
}
}
// choice has not been made or Antora is explicitly disabled
return false
}

private registerFeatures (): void {
const attributesCompletionProvider = vscode.languages.registerCompletionItemProvider({
language: 'asciidoc',
scheme: 'file',
},
new AntoraCompletionProvider(),
'{'
)
this._disposables.push(completionProvider)
this._disposables.push(attributesCompletionProvider)
}

public dispose (): void {
Expand Down Expand Up @@ -159,11 +202,11 @@ export async function getAntoraConfig (textDocumentUri: Uri): Promise<AntoraConf
}

export async function getAttributes (textDocumentUri: Uri): Promise<{ [key: string]: string }> {
const doc = await getAntoraConfig(textDocumentUri)
if (doc === undefined) {
const antoraConfig = await getAntoraConfig(textDocumentUri)
if (antoraConfig === undefined) {
return {}
}
return doc.config.asciidoc?.attributes || {}
return antoraConfig.config.asciidoc?.attributes || {}
}

export async function getAntoraDocumentContext (textDocumentUri: Uri, workspaceState: Memento): Promise<AntoraDocumentContext | undefined> {
Expand Down
1 change: 1 addition & 0 deletions test-workspace/antora/multiComponents/api/antora.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ asciidoc:
source-language: asciidoc@
xrefstyle: short@
example-caption: false
experimental: ''
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
= Page 6

This document is inside a directory which has the same name as the workspace.

kbd:[shift+ctrl+a]

0 comments on commit 1f0114d

Please sign in to comment.