Skip to content

Commit

Permalink
Refactor: Move onCodeLens to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
idillon-sfl committed May 22, 2024
1 parent ae7fc93 commit 2644a34
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
38 changes: 38 additions & 0 deletions server/src/connectionHandlers/onCodeLens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2023 Savoir-faire Linux. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as LSP from 'vscode-languageserver/node'
import { analyzer } from '../tree-sitter/analyzer'

export async function onCodeLensHandler (params: LSP.CodeLensParams, enableCodeLensReferencesOnFunctions: boolean): Promise<LSP.CodeLens[]> {
const codeLenses: LSP.CodeLens[] = []
const uri = params.textDocument.uri

if (!enableCodeLensReferencesOnFunctions) {
return []
}

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
}

export function onCodeLensResolveHandler (codeLens: LSP.CodeLens): LSP.CodeLens {
return codeLens
}
34 changes: 5 additions & 29 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { expandSettingPath } from './lib/src/BitbakeSettings'
import { onReferenceHandler } from './connectionHandlers/onReference'
import { type BitbakeScanResult } from './lib/src/types/BitbakeScanResult'
import { onPrepareRenameHandler, onRenameRequestHandler } from './connectionHandlers/onRename'
import { onCodeLensHandler, onCodeLensResolveHandler } from './connectionHandlers/onCodeLens'

// Create a connection for the server. The connection uses Node's IPC as a transport
export const connection: Connection = createConnection(ProposedFeatures.all)
Expand Down Expand Up @@ -161,36 +162,11 @@ disposables.push(
analyzer.clearRecipeLocalFiles()
}),

connection.onCodeLens(async (params): Promise<LSP.CodeLens[]> => {
const codeLenses: LSP.CodeLens[] = []
const uri = params.textDocument.uri

if (!enableCodeLensReferencesOnFunctions) {
return []
}

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.onCodeLens(
async (params) => await onCodeLensHandler(params, enableCodeLensReferencesOnFunctions)
),

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

connection.onRequest(
RequestMethod.EmbeddedLanguageTypeOnPosition,
Expand Down

0 comments on commit 2644a34

Please sign in to comment.