Skip to content

Commit

Permalink
Fix completion after xref: for old double-square bracket notation
Browse files Browse the repository at this point in the history
part of asciidoctor#648

- it was broken during enforcing code style, the regular expression was
not correctly converted
asciidoctor@cd477c7
- added a test
- The previous was not very nice in the sense that it is not inserting
the file path but only the label, but maybe it is enough if there is the
right include?

Signed-off-by: Aurélien Pupier <apupier@redhat.com>
  • Loading branch information
apupier committed Nov 18, 2022
1 parent f758bf4 commit f09d2f7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/providers/xref.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ function shouldProvide (context: Context): boolean {
}

async function getLabels (): Promise<string[]> {
const regex = /\\[\\[(\\w+)\\]\\]/g
const labels = await vscode.workspace.findFiles('**/*.adoc').then((files) =>
files
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')
.match(regex)
.map((result) => result.replace('[[', '').replace(']]', ''))
)
const matched = contentOfFilesConcatenated.match(regex)
if (matched) {
return matched.map((result) => result.replace('[[', '').replace(']]', ''))
}
return []
})
return labels
}

Expand Down
36 changes: 36 additions & 0 deletions src/test/xrefCompletionProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'mocha'
import * as vscode from 'vscode'
import assert from 'assert'
import { xrefProvider } from '../providers/xref.provider'
import { Position } from 'vscode'

let root

suite('Xref CompletionsProvider', () => {
const createdFiles: vscode.Uri[] = []
setup(() => {
root = vscode.workspace.workspaceFolders[0].uri.fsPath
})
teardown(async () => {
for (const createdFile of createdFiles) {
await vscode.workspace.fs.delete(createdFile)
}
})
test('Should return other ids from old style double-brackets 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('[[anOldStyleID]]'))
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 === 'anOldStyleID[]')
assert.deepStrictEqual(filteredCompletionItems[0], {
kind: vscode.CompletionItemKind.Reference,
label: 'anOldStyleID[]',
})
})
})

0 comments on commit f09d2f7

Please sign in to comment.