Skip to content

Commit

Permalink
feat: recursively apply quick actions codelens (continuedev#1925)
Browse files Browse the repository at this point in the history
* feat ux improvements on control plane settings

* feat: recursively apply quick actions codelens
  • Loading branch information
Patrick-Erichsen authored Aug 5, 2024
1 parent 0ff5efc commit d5155da
Showing 1 changed file with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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[]> {
Expand All @@ -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);

if (!symbols) {
return [];
}

const filteredSmybols = symbols?.filter((def) =>
QuickActionsCodeLensProvider.quickActionSymbolKinds.includes(def.kind),
);
const symbols = await this.getAllSymbols(document.uri);

return filteredSmybols.flatMap(({ range }) => {
return symbols.flatMap(({ range }) => {
const commands: vscode.Command[] = !!this.customQuickActionsConfig
? this.getCustomCommands(range, this.customQuickActionsConfig)
: this.getDefaultCommand(range);
Expand Down

0 comments on commit d5155da

Please sign in to comment.