Skip to content

Commit

Permalink
Chore: prepare to handle scan results
Browse files Browse the repository at this point in the history
The client passes the scan results and the file uri to the server for analyzer to use existing functions to analyze
  • Loading branch information
WilsonZiweiWang committed Jan 12, 2024
1 parent 0146377 commit b1d001c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
18 changes: 13 additions & 5 deletions client/src/driver/BitbakeRecipeScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import { type LanguageClient } from 'vscode-languageclient/node'
import { logger } from '../lib/src/utils/OutputLogger'
import { runBitbakeTask } from '../ui/BitbakeCommands'
import { type BitbakeCustomExecution, type BitbakeTaskProvider } from '../ui/BitbakeTaskProvider'
import { RequestMethod } from '../lib/src/types/requests'
import { RequestMethod, type RequestParams } from '../lib/src/types/requests'

export class BitbakeRecipeScanner {
private isPending: boolean = false
private _languageClient: LanguageClient | undefined
private _currentUriForScan: string = ''

public scanResults: string = ''
public processedScanResults: Record<string, unknown> | undefined

async scan (chosenRecipe: string, taskProvider: BitbakeTaskProvider, uri: any): Promise<void> {
logger.debug(`[BitbakeRecipeScanner] Scanning recipe env: ${uri}`)
this._currentUriForScan = uri

async scan (chosenRecipe: string, taskProvider: BitbakeTaskProvider): Promise<void> {
const scanRecipeEnvTask = new vscode.Task(
{ type: 'bitbake', recipes: [chosenRecipe], options: { parseOnly: true, env: true } },
vscode.TaskScope.Workspace,
Expand All @@ -26,7 +31,7 @@ export class BitbakeRecipeScanner {

const runningTasks = vscode.tasks.taskExecutions
if (runningTasks.some((execution) => execution.task.name === scanRecipeEnvTask.name)) {
logger.debug('Bitbake parsing task is already running')
logger.debug('[BitbakeRecipeScanner] Recipe scan is already running')
this.isPending = true
}

Expand All @@ -38,7 +43,7 @@ export class BitbakeRecipeScanner {
if (e.execution.task.name === 'Bitbake: Scan recipe env') {
if (this.isPending) {
this.isPending = false
await this.scan(e.execution.task.definition.recipes?.[0] ?? '', taskProvider)
await this.scan(e.execution.task.definition.recipes?.[0] ?? '', taskProvider, this._currentUriForScan)
}

const executionEngine = e.execution.task.execution as BitbakeCustomExecution
Expand All @@ -49,7 +54,10 @@ export class BitbakeRecipeScanner {
} else {
if (this.scanResults !== '') {
logger.debug('[onDidEndTask] Sending recipe environment to the server')
void this._languageClient.sendRequest(RequestMethod.ProcessRecipeScanResults, { scanResults: this.scanResults })
const requestParam: RequestParams['ProcessRecipeScanResults'] = { scanResults: this.scanResults, uri: this._currentUriForScan }
const processedScanResults = await this._languageClient.sendRequest(RequestMethod.ProcessRecipeScanResults, requestParam)

logger.debug('processedScanResults: ' + JSON.stringify(processedScanResults))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/ui/BitbakeCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function scanRecipeCommand (bitbakeWorkspace: BitbakeWorkspace, taskProvid

bitbakeSanity = true

await bitbakeRecipeScanner.scan(chosenRecipe, taskProvider)
await bitbakeRecipeScanner.scan(chosenRecipe, taskProvider, uri)
}

async function runTaskCommand (bitbakeWorkspace: BitbakeWorkspace, bitbakeDriver: BitbakeDriver, uri?: any, task?: any): Promise<void> {
Expand Down
3 changes: 2 additions & 1 deletion server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ connection.onRequest(RequestMethod.getLinksInDocument, (params: RequestParams['g
})

connection.onRequest(RequestMethod.ProcessRecipeScanResults, (param: RequestParams['ProcessRecipeScanResults']) => {
logger.debug(`[OnRequest] <ProcessRecipeScanResults> ${param.scanResults.length}`)
logger.debug(`[OnRequest] <ProcessRecipeScanResults> uri: ${JSON.stringify(param.uri)}`)
analyzer.processRecipeScanResults(param.scanResults, param.uri)
})

connection.listen()
Expand Down
59 changes: 59 additions & 0 deletions server/src/tree-sitter/analyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import fs from 'fs'
import path from 'path'
import { bitBakeProjectScannerClient } from '../BitbakeProjectScannerClient'
import { bitBakeDocScanner } from '../BitBakeDocScanner'
import { type RequestResult } from '../lib/src/types/requests'

export interface AnalyzedDocument {
version: number // TextDocument is mutable and its version updates as the document updates
Expand Down Expand Up @@ -701,6 +702,64 @@ export default class Analyzer {
}
}
}

public processRecipeScanResults (scanResults: string, uri: any): RequestResult['ProcessRecipeScanResults'] {
let uriString
if (typeof uri === 'string') {
uriString = uri
}

if (uri.fsPath !== undefined) { // vscode.Uri
uriString = uri.fsPath
}

if (typeof uriString !== 'string') {
logger.debug('[ProcessRecipeScanResults] Cannot obtain the file uri, abort processing scan results')
return undefined
}

if (!uriString.startsWith('file://')) {
uriString = 'file://' + uriString
}

if (this.uriToAnalyzedDocument[uriString] === undefined) {
logger.debug(`[ProcessRecipeScanResults] Analyzed document for ${uriString} not found, abort processing scan results`)
return undefined
}

const lines = scanResults.split('\n')
const index = lines.findIndex((line) => line.includes('INCLUDE HISTORY'))
if (index === -1) {
logger.debug('[Analyzer] Cannot find INCLUDE HISTORY in scan results, abort processing scan results')
return undefined
}

const validTexts = lines.slice(index).join('\n')
const scanResultdocumentUri = uriString + '.scanned'
const scanResultDocument = TextDocument.create(
scanResultdocumentUri,
'bitbake',
0,
validTexts
)

this.analyze({ document: scanResultDocument, uri: scanResultdocumentUri })

const scanResultDocumentSymbols = this.getGlobalDeclarationSymbols(scanResultdocumentUri)
if (scanResultDocumentSymbols.length > 0) {
logger.debug(`symbol length for scan result document: ${scanResultDocumentSymbols.length}`)
}

// Symbol information on the file that was requested to be scanned
const originalSymbols = this.getGlobalDeclarationSymbols(uriString)

if (originalSymbols.length > 0) {
logger.debug(`symbol length for original document: ${originalSymbols.length}`)
}

// TODO: Further parsing of the scan results using both documents
return undefined
}
}

export const analyzer: Analyzer = new Analyzer()

0 comments on commit b1d001c

Please sign in to comment.