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

Complete the path instead just names for include and require statements #77

Merged
merged 4 commits into from
Nov 16, 2023
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
73 changes: 50 additions & 23 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,26 @@ describe('On Completion', () => {
expect.arrayContaining([
expect.objectContaining(
{
label: 'init-manager-none',
kind: 8
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',
kind: 8
label: 'init-manager-none.inc',
kind: 8,
insertText: 'conf/distro/include/init-manager-none.inc'
}
)
])
Expand All @@ -500,7 +526,8 @@ describe('On Completion', () => {
expect.objectContaining(
{
label: 'copyleft_filter',
kind: 7
kind: 7,
insertText: 'copyleft_filter'
}
)
])
Expand Down
2 changes: 1 addition & 1 deletion server/src/__tests__/semanticTokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { analyzer } from '../tree-sitter/analyzer'
import { generateParser } from '../tree-sitter/parser'
import { getParsedTokens, TOKEN_LEGEND } from '../semanticTokensProvider'
import { getParsedTokens, TOKEN_LEGEND } from '../semanticTokens'
import { FIXTURE_DOCUMENT } from './fixtures/fixtures'

const DUMMY_URI = 'dummy_uri'
Expand Down
34 changes: 25 additions & 9 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,32 +224,46 @@ 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
},
// TODO1: Construct the file path which should be relative to the current document for insertText.
// TODO2: Limit what should be shown in the completion list for directive statment?
// insertText: filePath ?? element.name,
insertText: filePath ?? element.name,
documentation: element.extraInfo,
data: element,
kind: completionItemKind
}
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
}

function getFilePath (elementInfo: ElementInfo, fileType: string): string | undefined {
if (fileType === 'inc' || fileType === 'bbclass') {
if (fileType === 'inc') {
const path = elementInfo.path
let pathAsString = path?.dir.replace(elementInfo.layerInfo?.path as string, '')
if (pathAsString !== undefined && pathAsString.startsWith('/')) {
if (path === undefined) {
return undefined
}
let pathAsString = path.dir.replace(elementInfo.layerInfo?.path ?? '', '')
if (pathAsString.startsWith('/')) {
pathAsString = pathAsString.slice(1)
}

return pathAsString + '/' + elementInfo?.path?.base
return pathAsString + '/' + path.base
}
return undefined
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { generateEmbeddedLanguageDocs, getEmbeddedLanguageDocInfosOnPosition } f
import { embeddedLanguageDocsManager } from './embedded-languages/documents-manager'
import { RequestMethod, type RequestParams, type RequestResult } from './lib/src/types/requests'
import { NotificationMethod, type NotificationParams } from './lib/src/types/notifications'
import { getSemanticTokens, legend } from './semanticTokensProvider'
import { getSemanticTokens, legend } from './semanticTokens'
// Create a connection for the server. The connection uses Node's IPC as a transport
const connection: Connection = createConnection(ProposedFeatures.all)
const documents = new TextDocuments<TextDocument>(TextDocument)
Expand Down