forked from yoctoproject/vscode-bitbake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from savoirfairelinux/Add-hover
Feat: Add definition on hover for variables defined by BitBake
- Loading branch information
Showing
4 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import path from 'path' | ||
import fs from 'fs' | ||
|
||
type SuffixType = 'layer' | 'providedItem' | undefined | ||
|
||
export interface VariableInfos { | ||
name: string | ||
definition: string | ||
validFiles?: RegExp[] // Files on which the variable is defined. If undefined, the variable is defined in all files. | ||
suffixType?: SuffixType | ||
} | ||
|
||
type VariableInfosOverride = Partial<VariableInfos> | ||
|
||
// Infos that can't be parsed properly from the doc | ||
const variableInfosOverrides: Record<string, VariableInfosOverride> = { | ||
BBFILE_PATTERN: { | ||
suffixType: 'layer' | ||
}, | ||
LAYERDEPENDS: { | ||
suffixType: 'layer' | ||
}, | ||
LAYERDIR: { | ||
validFiles: [/^.*\/conf\/layer.conf$/] | ||
}, | ||
LAYERDIR_RE: { | ||
validFiles: [/^.*\/conf\/layer.conf$/] | ||
}, | ||
LAYERVERSION: { | ||
suffixType: 'layer' | ||
}, | ||
PREFERRED_PROVIDER: { | ||
suffixType: 'providedItem' | ||
} | ||
} | ||
|
||
const variablesFolder = 'doc/bitbake-user-manual/bitbake-user-manual-ref-variables.rst' | ||
const variablesRegexForDoc = /^ {3}:term:`(?<name>[A-Z_]*?)`\n(?<definition>.*?)(?=^ {3}:term:|$(?!\n))/gsm | ||
|
||
export class BitBakeDocScanner { | ||
private _variablesInfos: Record<string, VariableInfos> = {} | ||
private _variablesRegex = /(?!)/g // Initialize with dummy regex that won't match anything so we don't have to check for undefined | ||
|
||
get variablesInfos (): Record<string, VariableInfos> { | ||
return this._variablesInfos | ||
} | ||
|
||
get variablesRegex (): RegExp { | ||
return this._variablesRegex | ||
} | ||
|
||
parse (pathToBitbakeFolder: string): void { | ||
const file = fs.readFileSync(path.join(pathToBitbakeFolder, variablesFolder), 'utf8') | ||
for (const match of file.matchAll(variablesRegexForDoc)) { | ||
const name = match.groups?.name | ||
// Naive silly inneficient incomplete conversion to markdown | ||
const definition = match.groups?.definition | ||
.replace(/^ {3}/gm, '') | ||
.replace(/:term:|:ref:/g, '') | ||
.replace(/\.\. (note|important)::/g, (_match, p1) => { return `**${p1}**` }) | ||
.replace(/::/g, ':') | ||
.replace(/``/g, '`') | ||
if (name === undefined || definition === undefined) { | ||
return | ||
} | ||
this._variablesInfos[name] = { | ||
name, | ||
definition, | ||
...variableInfosOverrides[name] | ||
} | ||
} | ||
const variablesNames = Object.keys(this._variablesInfos) | ||
// Sort from longuest to shortest in order to make the regex greedy | ||
// Otherwise it would match B before BB_PRESERVE_ENV | ||
variablesNames.sort((a, b) => b.length - a.length) | ||
const variablesRegExpString = `(${variablesNames.join('|')})` | ||
this._variablesRegex = new RegExp(variablesRegExpString, 'gi') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters