Skip to content

Commit

Permalink
Provide completion after xref for ids defined with shorthand and
Browse files Browse the repository at this point in the history
longhand notation

I.e. with [#myId] and [id=myId]

part of asciidoctor#648

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier committed Nov 18, 2022
1 parent f09d2f7 commit 62bd83e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
45 changes: 34 additions & 11 deletions src/providers/xref.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,41 @@ function shouldProvide (context: Context): boolean {
}

async function getLabels (): Promise<string[]> {
const files = await vscode.workspace.findFiles('**/*.adoc')
const contentOfFilesConcatenated = files
.map((uri) => readFileSync(uri.path).toString('utf-8'))
.join('\n')
const labelsFromLegacyBlock = await getLabelsFromLegacyBlock(contentOfFilesConcatenated)
const labelsFromShorthandNotation = await getLabelsFromShorthandNotation(contentOfFilesConcatenated)
const labelsFromLonghandNotation = await getLabelsFromLonghandNotation(contentOfFilesConcatenated)
return labelsFromLegacyBlock.concat(labelsFromShorthandNotation, labelsFromLonghandNotation)
}

async function getLabelsFromLonghandNotation (content: string): Promise<string[]> {
const regex = /\[id=(\w+)\]/g
const matched = content.match(regex)
if (matched) {
return matched.map((result) => result.replace('[id=', '').replace(']', ''))
}
return []
}

async function getLabelsFromShorthandNotation (content: string): Promise<string[]> {
const regex = /\[#(\w+)\]/g
const matched = content.match(regex)
if (matched) {
return matched.map((result) => result.replace('[#', '').replace(']', ''))
}
return []
}

async function getLabelsFromLegacyBlock (content: string): Promise<string[]> {
const regex = /\[\[(\w+)\]\]/g
const labels = await vscode.workspace.findFiles('**/*.adoc').then((files) => {
const contentOfFilesConcatenated = files
.map((uri) => readFileSync(uri.path).toString('utf-8'))
.join('\n')
const matched = contentOfFilesConcatenated.match(regex)
if (matched) {
return matched.map((result) => result.replace('[[', '').replace(']]', ''))
}
return []
})
return labels
const matched = content.match(regex)
if (matched) {
return matched.map((result) => result.replace('[[', '').replace(']]', ''))
}
return []
}

/**
Expand Down
37 changes: 36 additions & 1 deletion src/test/xrefCompletionProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { Position } from 'vscode'
let root

suite('Xref CompletionsProvider', () => {
const createdFiles: vscode.Uri[] = []
let createdFiles: vscode.Uri[] = []
setup(() => {
root = vscode.workspace.workspaceFolders[0].uri.fsPath
})
teardown(async () => {
for (const createdFile of createdFiles) {
await vscode.workspace.fs.delete(createdFile)
}
createdFiles = []
})
test('Should return other ids from old style double-brackets as completion after "xref:"', async () => {
const fileToAutoComplete = vscode.Uri.file(`${root}/fileToAutoComplete.adoc`)
Expand All @@ -33,4 +34,38 @@ suite('Xref CompletionsProvider', () => {
label: 'anOldStyleID[]',
})
})
test('Should return ids declared using the shorthand syntax as completion after "xref:"', async () => {
const fileToAutoComplete = vscode.Uri.file(`${root}/fileToAutoComplete.adoc`)
await vscode.workspace.fs.writeFile(fileToAutoComplete, Buffer.from('xref:'))
createdFiles.push(fileToAutoComplete)

const fileThatShouldAppearInAutoComplete = vscode.Uri.file(`${root}/fileToAppearInAutoComplete.adoc`)
await vscode.workspace.fs.writeFile(fileThatShouldAppearInAutoComplete, Buffer.from('[#aShortHandID]'))
createdFiles.push(fileThatShouldAppearInAutoComplete)

const file = await vscode.workspace.openTextDocument(fileToAutoComplete)
const completionsItems = await xrefProvider.provideCompletionItems(file, new Position(0, 5))
const filteredCompletionItems = completionsItems.filter((completionItem) => completionItem.label === 'aShortHandID[]')
assert.deepStrictEqual(filteredCompletionItems[0], {
kind: vscode.CompletionItemKind.Reference,
label: 'aShortHandID[]',
})
})
test('Should return ids declared using the longhand syntax as completion after "xref:"', async () => {
const fileToAutoComplete = vscode.Uri.file(`${root}/fileToAutoComplete.adoc`)
await vscode.workspace.fs.writeFile(fileToAutoComplete, Buffer.from('xref:'))
createdFiles.push(fileToAutoComplete)

const fileThatShouldAppearInAutoComplete = vscode.Uri.file(`${root}/fileToAppearInAutoComplete.adoc`)
await vscode.workspace.fs.writeFile(fileThatShouldAppearInAutoComplete, Buffer.from('[id=longHandID]'))
createdFiles.push(fileThatShouldAppearInAutoComplete)

const file = await vscode.workspace.openTextDocument(fileToAutoComplete)
const completionsItems = await xrefProvider.provideCompletionItems(file, new Position(0, 5))
const filteredCompletionItems = completionsItems.filter((completionItem) => completionItem.label === 'longHandID[]')
assert.deepStrictEqual(filteredCompletionItems[0], {
kind: vscode.CompletionItemKind.Reference,
label: 'longHandID[]',
})
})
})

0 comments on commit 62bd83e

Please sign in to comment.