Skip to content

Commit

Permalink
Feat: Add codeLens for bitbake function references
Browse files Browse the repository at this point in the history
  • Loading branch information
WilsonZiweiWang committed Apr 30, 2024
1 parent aff0395 commit 8395fc3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions client/src/ui/BitbakeCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export function registerBitbakeCommands (context: vscode.ExtensionContext, bitba
vscode.commands.registerCommand('bitbake.start-toaster-in-browser', async () => { await startToasterInBrowser(bitBakeProjectScanner.bitbakeDriver) }),
vscode.commands.registerCommand('bitbake.stop-toaster', async () => { await stopToaster(bitBakeProjectScanner.bitbakeDriver) }),
vscode.commands.registerCommand('bitbake.clear-workspace-state', async () => { await clearAllWorkspaceState(context) }),
vscode.commands.registerCommand('bitbake.codeLens.showReferences', async (uri, position) => { await showReferences(uri, position) }),

// Handles enqueued parsing requests (onSave)
vscode.tasks.onDidEndTask((e) => {
if (e.execution.task.name === 'Bitbake: Parse') {
Expand All @@ -69,6 +71,21 @@ export function registerBitbakeCommands (context: vscode.ExtensionContext, bitba
)
}

async function showReferences (uri: any, position: any): Promise<void> {
if (typeof uri !== 'string' || position === undefined) {
return
}

let _position: vscode.Position
try {
_position = new vscode.Position(position.line, position.character)
const locations = await vscode.commands.executeCommand('vscode.executeReferenceProvider', vscode.Uri.parse(uri), position)
await vscode.commands.executeCommand('editor.action.showReferences', vscode.Uri.parse(uri), _position, locations)
} catch (error: any) {
void vscode.window.showErrorMessage('Failed to show references: ' + JSON.stringify(error))
}
}

async function clearAllWorkspaceState (context: vscode.ExtensionContext, key?: string): Promise<void> {
for (const key of context.workspaceState.keys()) {
await context.workspaceState.update(key, undefined).then(
Expand Down
31 changes: 31 additions & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* ------------------------------------------------------------------------------------------ */

import path from 'path'
import * as LSP from 'vscode-languageserver/node'
import {
type Connection,
type InitializeResult,
Expand Down Expand Up @@ -107,6 +108,9 @@ disposables.push(
semanticTokensProvider: {
legend,
full: true
},
codeLensProvider: {
resolveProvider: true
}
}
}
Expand Down Expand Up @@ -148,6 +152,33 @@ disposables.push(
analyzer.clearRecipeLocalFiles()
}),

connection.onCodeLens(async (params): Promise<LSP.CodeLens[]> => {
const codeLenses: LSP.CodeLens[] = []
const uri = params.textDocument.uri
// TODO: check the setting to see if showing the references code lens is allowed
const allSymbols = analyzer.getGlobalDeclarationSymbolsForUri(uri)
allSymbols.forEach((symbol) => {
if (symbol.kind === LSP.SymbolKind.Function) {
const codeLens = LSP.CodeLens.create(symbol.location.range)

codeLens.command = {
title: 'Show References',
command: 'bitbake.codeLens.showReferences',
arguments: [uri, symbol.location.range.start]
}

codeLens.data = { uri, position: symbol.location.range.start }

codeLenses.push(codeLens)
}
})
return codeLenses
}),

connection.onCodeLensResolve((codeLens): LSP.CodeLens => {
return codeLens
}),

connection.onRequest(
RequestMethod.EmbeddedLanguageTypeOnPosition,
async ({ uriString, position }: RequestParams['EmbeddedLanguageTypeOnPosition']): RequestResult['EmbeddedLanguageTypeOnPosition'] => {
Expand Down

0 comments on commit 8395fc3

Please sign in to comment.