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

648 Include path to completion item for xref #671

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
52 changes: 28 additions & 24 deletions src/providers/xref.provider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path'
import * as vscode from 'vscode'
import { createContext, Context } from './createContext'

Expand Down Expand Up @@ -31,20 +32,12 @@ function shouldProvide (context: Context, keyword :string): boolean {
return occurence === context.position.character - keyword.length
}

async function getLabelsFromAllWorkspaceFiles (): Promise<string[]> {
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)
}

Expand Down Expand Up @@ -88,14 +81,27 @@ async function provideCrossRef (context: Context): Promise<vscode.CompletionItem
textFullLine.lastIndexOf(':', position.character + 1) + 1,
indexOfNextWhiteSpace
)
const xrefLabels = await getLabelsFromAllWorkspaceFiles()

return xrefLabels
.filter((label) => 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<vscode.CompletionItem[]> {
Expand All @@ -108,9 +114,7 @@ async function provideInternalRef (context: Context): Promise<vscode.CompletionI
indexOfNextWhiteSpace
)

const files :vscode.Uri[] = []
files.push(document.uri)
const internalRefLabels = await getLabelsFromFiles(files)
const internalRefLabels = await getIdsFromFile(document.uri)

return internalRefLabels
.filter((label) => label.match(search))
Expand Down
40 changes: 21 additions & 19 deletions src/test/xrefCompletionProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand All @@ -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)
Expand All @@ -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`)
Expand All @@ -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<<AutoComplete.adoc`)
Expand Down