Skip to content

Commit

Permalink
Chore: Add ext in label; prioritize completion items by recipe folder…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
WilsonZiweiWang committed Nov 15, 2023
1 parent 62dfd31 commit 0bbfde6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
64 changes: 44 additions & 20 deletions server/src/__tests__/completions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,22 +404,41 @@ describe('On Completion', () => {
})

it('provides suggestions for direcitive statement after keywords "include", "inherit" and "requrie" are typed', async () => {
jest.spyOn(bitBakeProjectScanner, 'includes', 'get').mockReturnValue([{
name: 'init-manager-none',
path: {
root: '/',
dir: '/home/projects/poky/meta/conf/distro/include',
base: 'init-manager-none.inc',
ext: '.inc',
name: 'init-manager-none'
const documentUri = 'file:///home/projects/poky/meta/conf-2/path/to/dummy.bb'
jest.spyOn(bitBakeProjectScanner, 'includes', 'get').mockReturnValue([
{
name: 'init-manager-none',
path: {
root: '/',
dir: '/home/projects/poky/meta/conf/distro/include',
base: 'init-manager-none.inc',
ext: '.inc',
name: 'init-manager-none'
},
extraInfo: 'layer: core',
layerInfo: {
name: 'core',
path: '/home/projects/poky/meta',
priority: 5
}
},
extraInfo: 'layer: core',
layerInfo: {
name: 'core',
path: '/home/projects/poky/meta',
priority: 5
{
name: 'init-manager-none-2',
path: {
root: '/',
dir: '/home/projects/poky/meta/conf-2/distro/include', // Note that this fake path is under the same "conf-2" folder as the documentUri
base: 'init-manager-none-2.inc',
ext: '.inc',
name: 'init-manager-none-2'
},
extraInfo: 'layer: core',
layerInfo: {
name: 'core',
path: '/home/projects/poky/meta',
priority: 5
}
}
}])
])

jest.spyOn(bitBakeProjectScanner, 'classes', 'get').mockReturnValue([{
name: 'copyleft_filter',
Expand All @@ -439,13 +458,13 @@ describe('On Completion', () => {
}])

await analyzer.analyze({
uri: DUMMY_URI,
uri: documentUri,
document: FIXTURE_DOCUMENT.COMPLETION
})

const resultForInclude = onCompletionHandler({
textDocument: {
uri: DUMMY_URI
uri: documentUri
},
position: {
line: 10,
Expand All @@ -455,7 +474,7 @@ describe('On Completion', () => {

const resultForRequire = onCompletionHandler({
textDocument: {
uri: DUMMY_URI
uri: documentUri
},
position: {
line: 11,
Expand All @@ -465,7 +484,7 @@ describe('On Completion', () => {

const resultForInherit = onCompletionHandler({
textDocument: {
uri: DUMMY_URI
uri: documentUri
},
position: {
line: 12,
Expand All @@ -477,19 +496,24 @@ describe('On Completion', () => {
expect.arrayContaining([
expect.objectContaining(
{
label: 'init-manager-none',
label: 'init-manager-none.inc',
kind: 8,
insertText: 'conf/distro/include/init-manager-none.inc'
}
)
])
)

const index1 = resultForInclude.findIndex((item) => item.label === 'init-manager-none.inc')
const index2 = resultForInclude.findIndex((item) => item.label === 'init-manager-none-2.inc')
// Since the path of "init-manager-none-2.inc" is under the same "conf-2" folder as the documentUri, it should be suggested first
expect(index2).toBeLessThan(index1)

expect(resultForRequire).toEqual(
expect.arrayContaining([
expect.objectContaining(
{
label: 'init-manager-none',
label: 'init-manager-none.inc',
kind: 8,
insertText: 'conf/distro/include/init-manager-none.inc'
}
Expand Down
19 changes: 17 additions & 2 deletions server/src/connectionHandlers/onCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ import { VARIABLE_FLAGS } from '../completions/variable-flags'
import { bitBakeProjectScanner } from '../BitBakeProjectScanner'
import type { ElementInfo } from '../lib/src/types/BitbakeScanResult'

let documentUri = ''

export function onCompletionHandler (textDocumentPositionParams: TextDocumentPositionParams): CompletionItem[] {
const wordPosition = {
line: textDocumentPositionParams.position.line,
// Go one character back to get completion on the current word. This is used as a parameter in descendantForPosition()
character: Math.max(textDocumentPositionParams.position.character - 1, 0)
}

const documentUri = textDocumentPositionParams.textDocument.uri
documentUri = textDocumentPositionParams.textDocument.uri

const word = analyzer.wordAtPointFromTextPosition({
...textDocumentPositionParams,
Expand Down Expand Up @@ -222,7 +224,7 @@ function convertElementInfoListToCompletionItemList (elementInfoList: ElementInf
for (const element of elementInfoList) {
const filePath = getFilePath(element, fileType)
const completionItem: CompletionItem = {
label: element.name,
label: element.name + (element.path?.ext === '.inc' ? '.inc' : ''),
labelDetails: {
description: filePath ?? fileType
},
Expand All @@ -234,6 +236,19 @@ function convertElementInfoListToCompletionItemList (elementInfoList: ElementInf
completionItems.push(completionItem)
}

if (completionItems.length > 0) {
const docUriSplit = documentUri.replace('file://', '').split('/')
const condition = (item: CompletionItem): boolean => {
if (item.insertText === undefined || item.insertText.split('.')[0] === item.label.split('.')[0]) {
return false
} else {
return docUriSplit.includes(item.insertText.split('/')[0])
}
}

completionItems.sort((a, b) => Number(condition(b)) - Number(condition(a)))
}

return completionItems
}

Expand Down

0 comments on commit 0bbfde6

Please sign in to comment.