Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix completion after xref: for old double-square bracket notation #667

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking for null, previously there was an exception caught by the VS Code framework.

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[]',
})
})
})