feat: recursively apply quick actions codelens (#1925)

* feat ux improvements on control plane settings

* feat: recursively apply quick actions codelens
This commit is contained in:
Patrick Erichsen 2024-08-05 13:36:23 -07:00 • committed by GitHub
parent 0ff5efcd5a
commit d5155dac69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -48,13 +48,14 @@ export class QuickActionsCodeLensProvider implements vscode.CodeLensProvider {
/**
* Defines which code elements are eligible for Quick Actions.
*
* Right now, we only allow functions, methods and classes
* to keep things simple.
* Right now, we only allow functions, methods, constructors
* and classes to keep things simple.
*/
static quickActionSymbolKinds = [
quickActionSymbolKinds = [
vscode.SymbolKind.Function,
vscode.SymbolKind.Method,
vscode.SymbolKind.Class,
vscode.SymbolKind.Constructor,
];
customQuickActionsConfig?: QuickActionConfig[];
@ -94,6 +95,43 @@ export class QuickActionsCodeLensProvider implements vscode.CodeLensProvider {
return [quickEdit];
}
getSymbolsFrom(symbol: vscode.DocumentSymbol): vscode.DocumentSymbol[] {
if (symbol.children.length === 0) {
return [symbol];
}
const symbols: vscode.DocumentSymbol[] = [];
symbols.push(symbol);
for (const children of symbol.children) {
if (children.children.length === 0) {
symbols.push(children);
} else {
symbols.push(...this.getSymbolsFrom(children));
}
}
return symbols;
}
async getAllSymbols(uri: vscode.Uri) {
const docSymbols = await vscode.commands.executeCommand<
Array<vscode.DocumentSymbol> | undefined
>("vscode.executeDocumentSymbolProvider", uri);
if (!docSymbols) {
return [];
}
const symbols = docSymbols.flatMap((symbol) => this.getSymbolsFrom(symbol));
const filteredSmybols = symbols?.filter((def) =>
this.quickActionSymbolKinds.includes(def.kind),
);
return filteredSmybols;
}
async provideCodeLenses(
document: vscode.TextDocument,
): Promise<vscode.CodeLens[]> {
@ -109,19 +147,9 @@ export class QuickActionsCodeLensProvider implements vscode.CodeLensProvider {
return [];
}
const symbols = await vscode.commands.executeCommand<
Array<vscode.DocumentSymbol> | undefined
>("vscode.executeDocumentSymbolProvider", document.uri);
const symbols = await this.getAllSymbols(document.uri);
if (!symbols) {
return [];
}
const filteredSmybols = symbols?.filter((def) =>
QuickActionsCodeLensProvider.quickActionSymbolKinds.includes(def.kind),
);
return filteredSmybols.flatMap(({ range }) => {
return symbols.flatMap(({ range }) => {
const commands: vscode.Command[] = !!this.customQuickActionsConfig
? this.getCustomCommands(range, this.customQuickActionsConfig)
: this.getDefaultCommand(range);