diff --git a/src/providers/xref.provider.ts b/src/providers/xref.provider.ts index 5940ba42..9f5bafc4 100644 --- a/src/providers/xref.provider.ts +++ b/src/providers/xref.provider.ts @@ -1,3 +1,4 @@ +import * as path from 'path' import * as vscode from 'vscode' import { createContext, Context } from './createContext' @@ -31,20 +32,12 @@ function shouldProvide (context: Context, keyword :string): boolean { return occurence === context.position.character - keyword.length } -async function getLabelsFromAllWorkspaceFiles (): Promise { - const files = await vscode.workspace.findFiles('**/*.adoc') - return await getLabelsFromFiles(files) -} - -async function getLabelsFromFiles (files: vscode.Uri[]) { - let contentOfFilesConcatenated = '' - for (const uri of files) { - const data = await vscode.workspace.fs.readFile(uri) - contentOfFilesConcatenated += Buffer.from(data).toString('utf8') + '\n' - } - const labelsFromLegacyBlock = await getLabelsFromLegacyBlock(contentOfFilesConcatenated) - const labelsFromShorthandNotation = await getLabelsFromShorthandNotation(contentOfFilesConcatenated) - const labelsFromLonghandNotation = await getLabelsFromLonghandNotation(contentOfFilesConcatenated) +async function getIdsFromFile (file: vscode.Uri) { + const data = await vscode.workspace.fs.readFile(file) + const content = Buffer.from(data).toString('utf8') + const labelsFromLegacyBlock = await getLabelsFromLegacyBlock(content) + const labelsFromShorthandNotation = await getLabelsFromShorthandNotation(content) + const labelsFromLonghandNotation = await getLabelsFromLonghandNotation(content) return labelsFromLegacyBlock.concat(labelsFromShorthandNotation, labelsFromLonghandNotation) } @@ -88,14 +81,27 @@ async function provideCrossRef (context: Context): Promise label.match(search)) - .map((label) => ({ - label: `${label}[]`, - kind: vscode.CompletionItemKind.Reference, - })) + const completionItems: vscode.CompletionItem[] = [] + const workspacesAdocFiles = await vscode.workspace.findFiles('**/*.adoc') + for (const adocFile of workspacesAdocFiles) { + const labels = await getIdsFromFile(adocFile) + for (const label of labels) { + if (label.match(search)) { + if (adocFile.fsPath === context.document.uri.fsPath) { + completionItems.push(new vscode.CompletionItem( + label + '[]', + vscode.CompletionItemKind.Reference)) + } else { + completionItems.push(new vscode.CompletionItem( + path.relative(path.dirname(context.document.uri.fsPath), adocFile.fsPath) + '#' + label + '[]', + vscode.CompletionItemKind.Reference)) + } + } + } + } + + return completionItems } async function provideInternalRef (context: Context): Promise { @@ -108,9 +114,7 @@ async function provideInternalRef (context: Context): Promise label.match(search)) diff --git a/src/test/xrefCompletionProvider.test.ts b/src/test/xrefCompletionProvider.test.ts index a4cc3137..d20cb66e 100644 --- a/src/test/xrefCompletionProvider.test.ts +++ b/src/test/xrefCompletionProvider.test.ts @@ -28,11 +28,8 @@ suite('Xref CompletionsProvider', () => { 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[]', - }) + const filteredCompletionItems = completionsItems.filter((completionItem) => completionItem.label === 'fileToAppearInAutoComplete.adoc#anOldStyleID[]') + assert.deepStrictEqual(filteredCompletionItems[0], new vscode.CompletionItem('fileToAppearInAutoComplete.adoc#anOldStyleID[]', vscode.CompletionItemKind.Reference)) }) test('Should return ids declared using the shorthand syntax as completion after "xref:"', async () => { const fileToAutoComplete = vscode.Uri.file(`${root}/fileToAutoComplete.adoc`) @@ -45,13 +42,10 @@ suite('Xref CompletionsProvider', () => { 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[]', - }) + const filteredCompletionItems = completionsItems.filter((completionItem) => completionItem.label === 'fileToAppearInAutoComplete.adoc#aShortHandID[]') + assert.deepStrictEqual(filteredCompletionItems[0], new vscode.CompletionItem('fileToAppearInAutoComplete.adoc#aShortHandID[]', vscode.CompletionItemKind.Reference)) }) - test('Should return ids declared using the longhand syntax as completion after "xref:"', async () => { + test('Should return ids declared using the longhand syntax as completion after "xref:" from other document', async () => { const fileToAutoComplete = vscode.Uri.file(`${root}/fileToAutoComplete.adoc`) await vscode.workspace.fs.writeFile(fileToAutoComplete, Buffer.from('xref:')) createdFiles.push(fileToAutoComplete) @@ -62,11 +56,22 @@ suite('Xref CompletionsProvider', () => { const file = await vscode.workspace.openTextDocument(fileToAutoComplete) const completionsItems = await xrefProvider.provideCompletionItems(file, new Position(0, 5)) + const filteredCompletionItems = completionsItems.filter((completionItem) => completionItem.label === 'fileToAppearInAutoComplete.adoc#longHandID[]') + assert.deepStrictEqual(filteredCompletionItems[0], new vscode.CompletionItem( + 'fileToAppearInAutoComplete.adoc#longHandID[]', + vscode.CompletionItemKind.Reference)) + }) + test('Should return ids declared using the longhand syntax as completion after "xref:" from same document', async () => { + const fileToAutoComplete = vscode.Uri.file(`${root}/fileToAutoCompleteFromSameFile.adoc`) + await vscode.workspace.fs.writeFile(fileToAutoComplete, Buffer.from(`[id=longHandID] + +xref:`)) + createdFiles.push(fileToAutoComplete) + + const file = await vscode.workspace.openTextDocument(fileToAutoComplete) + const completionsItems = await xrefProvider.provideCompletionItems(file, new Position(2, 5)) const filteredCompletionItems = completionsItems.filter((completionItem) => completionItem.label === 'longHandID[]') - assert.deepStrictEqual(filteredCompletionItems[0], { - kind: vscode.CompletionItemKind.Reference, - label: 'longHandID[]', - }) + assert.deepStrictEqual(filteredCompletionItems[0], new vscode.CompletionItem('longHandID[]', vscode.CompletionItemKind.Reference)) }) test('Should return id for inlined anchor', async () => { const fileToAutoComplete = vscode.Uri.file(`${root}/fileToTestXrefAutoComplete.adoc`) @@ -78,10 +83,7 @@ xref:`)) const file = await vscode.workspace.openTextDocument(fileToAutoComplete) const completionsItems = await xrefProvider.provideCompletionItems(file, new Position(2, 5)) const filteredCompletionItems = completionsItems.filter((completionItem) => completionItem.label === 'anInlinedAnchor[]') - assert.deepStrictEqual(filteredCompletionItems[0], { - kind: vscode.CompletionItemKind.Reference, - label: 'anInlinedAnchor[]', - }) + assert.deepStrictEqual(filteredCompletionItems[0], new vscode.CompletionItem('anInlinedAnchor[]', vscode.CompletionItemKind.Reference)) }) test('Should return id for element in same document after <<', async () => { const fileToAutoComplete = vscode.Uri.file(`${root}/fileToTest<