Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from "vscode";
import { registerCodeLensProvider } from "./providers/codeLensProvider";
import { UnifiedFunctionMetrics } from "./metricsAnalyzer/metricsAnalyzerFactory";
import { ConfigurationManager } from "./configuration";

/** Shared output channel for function complexity details (created once, reused). */
let detailsChannel: vscode.OutputChannel | undefined;
Expand All @@ -13,7 +12,7 @@ let detailsChannel: vscode.OutputChannel | undefined;
*/
function showFunctionDetails(
func?: UnifiedFunctionMetrics,
_uri?: vscode.Uri
uri?: vscode.Uri
): void {
if (!func) {
return;
Expand All @@ -23,9 +22,14 @@ function showFunctionDetails(
detailsChannel = vscode.window.createOutputChannel("Code Metrics Details");
}

const config = ConfigurationManager.getConfiguration(uri);
const status = ConfigurationManager.getComplexityStatus(func.complexity, config);

detailsChannel.clear();
detailsChannel.appendLine(`Function: ${func.name}`);
detailsChannel.appendLine(`Cognitive Complexity: ${func.complexity}`);
detailsChannel.appendLine(
`Cognitive Complexity: ${func.complexity} ${status.icon} ${status.text}`
);
detailsChannel.appendLine(
`Location: lines ${func.startLine + 1}–${func.endLine + 1}`
);
Expand Down
24 changes: 11 additions & 13 deletions src/providers/codeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ export class MetricsCodeLensProvider implements vscode.CodeLensProvider {
return [];
}

// Bail out early if VS Code has already cancelled this request (e.g. the user
// typed another character while analysis was pending). This avoids wasting a
// potentially expensive tree-sitter parse whose result would be discarded anyway.
if (token.isCancellationRequested) {
return [];
}

try {
const analysisKey = `${document.uri.toString()}#${document.languageId}#${document.version}`;
let functions = this.analysisCache.get(analysisKey);
Comment on lines +149 to 153
Expand Down Expand Up @@ -203,19 +210,10 @@ export class MetricsCodeLensProvider implements vscode.CodeLensProvider {
document: vscode.TextDocument,
config: CodeMetricsConfig
): vscode.CodeLens[] {
const codeLenses: vscode.CodeLens[] = [];

functions.forEach((func) => {
// Only show code lens for functions with complexity > 0
if (func.complexity > 0) {
const codeLens = this.createCodeLens(func, document, config);
if (codeLens) {
codeLenses.push(codeLens);
}
}
});

return codeLenses;
return functions
.filter((func) => func.complexity > 0)
.map((func) => this.createCodeLens(func, document, config))
.filter((lens): lens is vscode.CodeLens => lens !== undefined);
Comment on lines +213 to +216
}

private createCodeLens(
Expand Down
Loading