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: Grab comments above global symbols and show them on hover #34

5 changes: 4 additions & 1 deletion server/src/__tests__/fixtures/bbclass/baz.bbclass
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
DESCRIPTION = 'baz.bbclass'
DESCRIPTION = 'baz.bbclass'

# comment 1 for MYVAR in baz.bbclass
MYVAR = '456'
5 changes: 3 additions & 2 deletions server/src/__tests__/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,12 @@ describe('on hover', () => {

// 1. should show all comments above the symbols that don't have docs from yocto/bitbake
// 2. should show comments for all declarations for the same variable in the same file
// 3. should show comments for the same variable in the include files
// 3. The higher priority comments replace the lower ones according to the order: .bbclass, .conf, .inc, .bb, .bbappend

expect(shouldShow1).toEqual(
expect.objectContaining({
contents: expect.objectContaining({
value: expect.stringContaining(` comment 1 for custom variable MYVAR\n\nSource: ${DUMMY_URI_TRIMMED} \`L: 15\`\n___\n comment 2 for custom variable MYVAR\n\nSource: ${DUMMY_URI_TRIMMED} \`L: 17\`\n___\n comment 1 for MYVAR in bar.inc\n\nSource: ${FIXTURE_DOCUMENT.BAR_INC.uri.replace('file://', '')} \`L: 5\``)
value: expect.stringContaining(` comment 1 for MYVAR in baz.bbclass\n\nSource: ${FIXTURE_DOCUMENT.BAZ_BBCLASS.uri.replace('file://', '')} \`L: 4\``)
})
})
)
Expand Down
17 changes: 15 additions & 2 deletions server/src/connectionHandlers/onHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { analyzer } from '../tree-sitter/analyzer'
import { bitBakeDocScanner } from '../BitBakeDocScanner'
import { logger } from '../lib/src/utils/OutputLogger'
import { DIRECTIVE_STATEMENT_KEYWORDS } from '../lib/src/types/directiveKeywords'
import path from 'path'
import { type GlobalSymbolComments } from '../tree-sitter/declarations'

export async function onHoverHandler (params: HoverParams): Promise<Hover | null> {
const { position, textDocument } = params
Expand Down Expand Up @@ -105,8 +107,19 @@ function getGlobalSymbolComments (uri: string, word: string): string | null {
}
if (symbolComments[word] !== undefined) {
const allCommentsForSymbol = symbolComments[word]
if (allCommentsForSymbol.length > 0) {
return `${allCommentsForSymbol.map((item) => item.comments.map(comment => comment.slice(1)).join('\n') + `\n\nSource: ${item.uri.replace('file://', '')} \`L: ${item.line + 1}\``).join('\n___\n')}`
const priority = ['.bbclass', '.conf', '.inc', '.bb', '.bbappend']

let commentsToShow: GlobalSymbolComments[string] = []
// higher priority comments replace lower ones
priority.reverse().forEach((ext) => {
const commentsForExt = allCommentsForSymbol.filter((item) => path.parse(item.uri).ext === ext)
if (commentsForExt.length > 0) {
commentsToShow = commentsForExt
}
})

if (commentsToShow.length > 0) {
return `${commentsToShow.map((item) => item.comments.map(comment => comment.slice(1)).join('\n') + `\n\nSource: ${item.uri.replace('file://', '')} \`L: ${item.line + 1}\``).join('\n___\n')}`
}
}
}
Expand Down
Loading