Skip to content

Commit 0efdff7

Browse files
authored
Merge pull request #176 from rajbos/more-detections
detect implicit selections
2 parents ab71143 + 7fee955 commit 0efdff7

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/extension.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,14 @@ interface ModeUsage {
102102
}
103103

104104
interface ContextReferenceUsage {
105-
file: number; // #file references
106-
selection: number; // #selection references
107-
symbol: number; // #symbol references
108-
codebase: number; // #codebase references
109-
workspace: number; // @workspace references
110-
terminal: number; // @terminal references
111-
vscode: number; // @vscode references
105+
file: number; // #file references
106+
selection: number; // #selection references
107+
implicitSelection: number; // Implicit selections via inputState.selections
108+
symbol: number; // #symbol references
109+
codebase: number; // #codebase references
110+
workspace: number; // @workspace references
111+
terminal: number; // @terminal references
112+
vscode: number; // @vscode references
112113
}
113114

114115
interface McpToolUsage {
@@ -194,7 +195,7 @@ interface SessionLogData {
194195

195196
class CopilotTokenTracker implements vscode.Disposable {
196197
// Cache version - increment this when making changes that require cache invalidation
197-
private static readonly CACHE_VERSION = 8; // Skip sessions with 0 models in avg calculation (2026-02-02)
198+
private static readonly CACHE_VERSION = 9; // Added implicitSelection to ContextReferenceUsage (2026-02-02)
198199

199200
private diagnosticsPanel?: vscode.WebviewPanel;
200201
// Tracks whether the diagnostics panel has already received its session files
@@ -980,6 +981,7 @@ class CopilotTokenTracker implements vscode.Disposable {
980981
contextReferences: {
981982
file: 0,
982983
selection: 0,
984+
implicitSelection: 0,
983985
symbol: 0,
984986
codebase: 0,
985987
workspace: 0,
@@ -1079,6 +1081,7 @@ class CopilotTokenTracker implements vscode.Disposable {
10791081
// Merge context references
10801082
period.contextReferences.file += analysis.contextReferences.file;
10811083
period.contextReferences.selection += analysis.contextReferences.selection;
1084+
period.contextReferences.implicitSelection += analysis.contextReferences.implicitSelection || 0;
10821085
period.contextReferences.symbol += analysis.contextReferences.symbol;
10831086
period.contextReferences.codebase += analysis.contextReferences.codebase;
10841087
period.contextReferences.workspace += analysis.contextReferences.workspace;
@@ -1359,6 +1362,7 @@ class CopilotTokenTracker implements vscode.Disposable {
13591362
contextReferences: {
13601363
file: 0,
13611364
selection: 0,
1365+
implicitSelection: 0,
13621366
symbol: 0,
13631367
codebase: 0,
13641368
workspace: 0,
@@ -1392,13 +1396,23 @@ class CopilotTokenTracker implements vscode.Disposable {
13921396
// Handle VS Code incremental format - detect mode from session header
13931397
if (event.kind === 0 && event.v?.inputState?.mode?.kind) {
13941398
sessionMode = event.v.inputState.mode.kind;
1399+
1400+
// Detect implicit selections in initial state
1401+
if (event.v?.inputState?.selections && Array.isArray(event.v.inputState.selections) && event.v.inputState.selections.length > 0) {
1402+
analysis.contextReferences.implicitSelection++;
1403+
}
13951404
}
13961405

13971406
// Handle mode changes (kind: 1 with mode update)
13981407
if (event.kind === 1 && event.k?.includes('mode') && event.v?.kind) {
13991408
sessionMode = event.v.kind;
14001409
}
14011410

1411+
// Detect implicit selections in updates to inputState.selections
1412+
if (event.kind === 1 && event.k?.includes('selections') && Array.isArray(event.v) && event.v.length > 0) {
1413+
analysis.contextReferences.implicitSelection++;
1414+
}
1415+
14021416
// Handle VS Code incremental format - count requests as interactions
14031417
if (event.kind === 2 && event.k?.[0] === 'requests' && Array.isArray(event.v)) {
14041418
for (const request of event.v) {
@@ -1763,6 +1777,7 @@ class CopilotTokenTracker implements vscode.Disposable {
17631777
contextReferences: {
17641778
file: 0,
17651779
selection: 0,
1780+
implicitSelection: 0,
17661781
symbol: 0,
17671782
codebase: 0,
17681783
workspace: 0,
@@ -1805,7 +1820,7 @@ class CopilotTokenTracker implements vscode.Disposable {
18051820
modified: stat.mtime.toISOString(),
18061821
interactions: 0,
18071822
contextReferences: {
1808-
file: 0, selection: 0, symbol: 0, codebase: 0,
1823+
file: 0, selection: 0, implicitSelection: 0, symbol: 0, codebase: 0,
18091824
workspace: 0, terminal: 0, vscode: 0
18101825
},
18111826
firstInteraction: null,
@@ -2266,7 +2281,7 @@ class CopilotTokenTracker implements vscode.Disposable {
22662281
*/
22672282
private createEmptyContextRefs(): ContextReferenceUsage {
22682283
return {
2269-
file: 0, selection: 0, symbol: 0, codebase: 0,
2284+
file: 0, selection: 0, implicitSelection: 0, symbol: 0, codebase: 0,
22702285
workspace: 0, terminal: 0, vscode: 0
22712286
};
22722287
}

src/webview/usage/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type ModeUsage = { ask: number; edit: number; agent: number };
66
type ContextReferenceUsage = {
77
file: number;
88
selection: number;
9+
implicitSelection: number;
910
symbol: number;
1011
codebase: number;
1112
workspace: number;
@@ -67,7 +68,7 @@ function escapeHtml(text: string): string {
6768
}
6869

6970
function getTotalContextRefs(refs: ContextReferenceUsage): number {
70-
return refs.file + refs.selection + refs.symbol + refs.codebase +
71+
return refs.file + refs.selection + refs.implicitSelection + refs.symbol + refs.codebase +
7172
refs.workspace + refs.terminal + refs.vscode;
7273
}
7374

@@ -205,6 +206,7 @@ function renderLayout(stats: UsageAnalysisStats): void {
205206
padding: 12px;
206207
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
207208
}
209+
.stat-card[title] { cursor: help; }
208210
.stat-label { font-size: 11px; color: #b8b8b8; margin-bottom: 4px; }
209211
.stat-value { font-size: 20px; font-weight: 700; color: #f6f6f6; }
210212
.bar-chart {
@@ -320,6 +322,7 @@ function renderLayout(stats: UsageAnalysisStats): void {
320322
<div class="stats-grid">
321323
<div class="stat-card"><div class="stat-label">📄 #file</div><div class="stat-value">${stats.month.contextReferences.file}</div><div style="font-size: 10px; color: #999; margin-top: 4px;">Today: ${stats.today.contextReferences.file}</div></div>
322324
<div class="stat-card"><div class="stat-label">✂️ #selection</div><div class="stat-value">${stats.month.contextReferences.selection}</div><div style="font-size: 10px; color: #999; margin-top: 4px;">Today: ${stats.today.contextReferences.selection}</div></div>
325+
<div class="stat-card" title="Text selected in your editor providing passive context to Copilot"><div class="stat-label">✨ Implicit Selection</div><div class="stat-value">${stats.month.contextReferences.implicitSelection}</div><div style="font-size: 10px; color: #999; margin-top: 4px;">Today: ${stats.today.contextReferences.implicitSelection}</div></div>
323326
<div class="stat-card"><div class="stat-label">🔤 #symbol</div><div class="stat-value">${stats.month.contextReferences.symbol}</div><div style="font-size: 10px; color: #999; margin-top: 4px;">Today: ${stats.today.contextReferences.symbol}</div></div>
324327
<div class="stat-card"><div class="stat-label">🗂️ #codebase</div><div class="stat-value">${stats.month.contextReferences.codebase}</div><div style="font-size: 10px; color: #999; margin-top: 4px;">Today: ${stats.today.contextReferences.codebase}</div></div>
325328
<div class="stat-card"><div class="stat-label">📁 @workspace</div><div class="stat-value">${stats.month.contextReferences.workspace}</div><div style="font-size: 10px; color: #999; margin-top: 4px;">Today: ${stats.today.contextReferences.workspace}</div></div>

0 commit comments

Comments
 (0)