Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit b365a62

Browse files
committed
extract context key usage into util
1 parent b7cf421 commit b365a62

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/extension.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as vscode from 'vscode';
77
import { EditorHighlights } from './editorHighlights';
88
import { History, HistoryItem } from './history';
9-
import { CallItem, CallsDirection, CallsModel, FileItem, getPreviewChunks, getRequestRange, ItemSource, ReferenceItem, ReferencesModel, RichCallsDirection } from './models';
9+
import { CallItem, CallsDirection, CallsModel, Context, FileItem, getPreviewChunks, getRequestRange, ItemSource, ReferenceItem, ReferencesModel, RichCallsDirection } from './models';
1010
import { TreeDataProviderWrapper, TreeItem } from './provider';
1111

1212
export function activate(context: vscode.ExtensionContext) {
@@ -19,7 +19,7 @@ export function activate(context: vscode.ExtensionContext) {
1919

2020
const revealView = async () => {
2121
// upon first interaction set the reference list as active and reveal it
22-
await vscode.commands.executeCommand('setContext', 'reference-list.isActive', true);
22+
await Context.IsActive.set(true);
2323
await vscode.commands.executeCommand(`${viewId}.focus`);
2424
};
2525

@@ -42,8 +42,10 @@ export function activate(context: vscode.ExtensionContext) {
4242
// update state
4343
view.message = undefined;
4444
editorHighlights.setModel(model);
45-
vscode.commands.executeCommand('setContext', 'reference-list.hasResult', Boolean(model));
46-
vscode.commands.executeCommand('setContext', 'reference-list.source', model?.source);
45+
46+
// update context
47+
Context.HasResult.set(Boolean(model));
48+
Context.Source.set(model?.source);
4749

4850
revealView();
4951
provider.update(newModel || history);

src/history.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7+
import { Context } from './models';
78

89
export class HistoryItem {
910

@@ -46,7 +47,7 @@ export class History {
4647
// sure to update the order for re-run queries
4748
this._items.delete(item.id);
4849
this._items.set(item.id, item);
49-
vscode.commands.executeCommand('setContext', 'reference-list.hasHistory', true);
50+
Context.HasHistory.set(true);
5051
}
5152
}
5253

@@ -56,6 +57,6 @@ export class History {
5657

5758
clear(): void {
5859
this._items.clear();
59-
vscode.commands.executeCommand('setContext', 'reference-list.hasHistory', false);
60+
Context.HasHistory.set(false);
6061
}
6162
}

src/models.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ export function getPreviewChunks(doc: vscode.TextDocument, range: vscode.Range,
2929
return { before, inside, after };
3030
}
3131

32+
export class Context<V> {
33+
34+
static IsActive = new Context<boolean>('reference-list.isActive');
35+
static Source = new Context<ItemSource | 'callHierarchy' | undefined>('reference-list.source');
36+
static HasResult = new Context<boolean>('reference-list.hasResult');
37+
static HasHistory = new Context<boolean>('reference-list.hasHistory');
38+
static CallHierarchyMode = new Context<'showOutgoing' | 'showIncoming'>('references-view.callHierarchyMode');
39+
40+
private constructor(readonly name: string) { }
41+
42+
async set(value: V) {
43+
vscode.commands.executeCommand('setContext', this.name, value);
44+
}
45+
}
46+
3247
export const enum ItemSource {
3348
References = 'vscode.executeReferenceProvider',
3449
Implementations = 'vscode.executeImplementationProvider',
@@ -318,7 +333,7 @@ export class RichCallsDirection {
318333

319334
set value(value: CallsDirection) {
320335
this._value = value;
321-
vscode.commands.executeCommand('setContext', 'references-view.callHierarchyMode', this._value === CallsDirection.Incoming ? 'showIncoming' : 'showOutgoing');
336+
Context.CallHierarchyMode.set(this._value === CallsDirection.Incoming ? 'showIncoming' : 'showOutgoing');
322337
this._mem.update(RichCallsDirection._key, value);
323338
}
324339
}

0 commit comments

Comments
 (0)