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

Feat: Handle hover on second parameter for Python functions accessing the datastore #48

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
3 changes: 2 additions & 1 deletion server/src/__tests__/fixtures/hover.bb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ include dummy.inc
require dummy.inc

python (){
d.getVar("DESCRIPTION")
d.getVar("DESCRIPTION", "DESCRIPTION")
d.setVar('DESCRIPTION', 'value')
d.renameVar("DESCRIPTION", "DESCRIPTION")
b.getVar('DESCRIPTION')
d.test('DESCRIPTION')
d.getVar("FOO")
Expand Down
48 changes: 38 additions & 10 deletions server/src/__tests__/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ describe('on hover', () => {
uri: DUMMY_URI
},
position: {
line: 40,
character: 19
line: 37,
character: 37
}
})

Expand All @@ -361,8 +361,8 @@ describe('on hover', () => {
uri: DUMMY_URI
},
position: {
line: 46,
character: 20
line: 41,
character: 19
}
})

Expand All @@ -371,7 +371,17 @@ describe('on hover', () => {
uri: DUMMY_URI
},
position: {
line: 44,
line: 47,
character: 20
}
})

const shouldShow6 = await onHoverHandler({
textDocument: {
uri: DUMMY_URI
},
position: {
line: 45,
character: 14
}
})
Expand All @@ -381,8 +391,8 @@ describe('on hover', () => {
uri: DUMMY_URI
},
position: {
line: 37,
character: 14
line: 35,
character: 33
}
})

Expand All @@ -392,7 +402,7 @@ describe('on hover', () => {
},
position: {
line: 38,
character: 12
character: 14
}
})

Expand All @@ -402,7 +412,7 @@ describe('on hover', () => {
},
position: {
line: 39,
character: 19
character: 12
}
})

Expand All @@ -411,7 +421,17 @@ describe('on hover', () => {
uri: DUMMY_URI
},
position: {
line: 48,
line: 40,
character: 19
}
})

const shouldNotShow5 = await onHoverHandler({
textDocument: {
uri: DUMMY_URI
},
position: {
line: 49,
character: 10
}
})
Expand Down Expand Up @@ -451,10 +471,18 @@ describe('on hover', () => {
}
})

expect(shouldShow6).toEqual({
contents: {
kind: 'markdown',
value: '**DESCRIPTION**\n___\n A long description for the recipe.\n\n'
}
})

expect(shouldNotShow1).toBe(null)
expect(shouldNotShow2).toBe(null)
expect(shouldNotShow3).toBe(null)
expect(shouldNotShow4).toBe(null)
expect(shouldNotShow5).toBe(null)
})

it('should show comments above the global declarations', async () => {
Expand Down
27 changes: 25 additions & 2 deletions server/src/tree-sitter/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,31 @@ export default class Analyzer {
if (functionName === undefined || !(bitBakeDocScanner.pythonDatastoreFunction.includes(functionName))) {
return false
}
const variable = match?.groups?.params?.split(',')[0]?.trim().replace(/('|")/g, '')
return variable === n?.text

// Example for d.getVar('FOO'):
// n.text: FOO
// n.parent.text: 'FOO'
// n.parent.previousSibling.text: (
const isFirstParameter = n?.parent?.previousSibling?.text === '('
if (isFirstParameter) {
const firstParameter = match?.groups?.params?.split(',')[0]?.trim().replace(/('|")/g, '')
return firstParameter === n?.text
}

// Example for d.renameVar('FOO', 'BAR'):
// n.text: BAR
// n.parent.text: 'BAR'
// n.parent.previousSibling.text: ,
// n.parent.previousSibling.previousSibling.text: 'FOO'
// n.parent.previousSibling.previousSibling.previousSibling.text: (
const isSecondParameter = n?.parent?.previousSibling?.previousSibling?.previousSibling?.text === '('
// d.renameVar is the only function for which the second parameter could be a Yocto defined variable
if (functionName === 'renameVar' && isSecondParameter) {
const secondParameter = match?.groups?.params?.split(',')[1]?.trim().replace(/('|")/g, '')
return secondParameter === n?.text
}

return false
}

/**
Expand Down
Loading