Skip to content

Commit

Permalink
Feat: Handle hover on second parameter for Python functions accessing…
Browse files Browse the repository at this point in the history
… the datastore
  • Loading branch information
idillon-sfl committed Jan 5, 2024
1 parent 92accaf commit 323260e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
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

0 comments on commit 323260e

Please sign in to comment.