Skip to content

Commit 073b7aa

Browse files
Sort order of scopes in scope visualizer
1 parent 3a2e88d commit 073b7aa

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

packages/cursorless-vscode/src/ScopeTreeProvider.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ export class ScopeTreeProvider implements TreeDataProvider<MyTreeItem> {
111111
this.visibleDisposable = disposableFrom(
112112
this.scopeProvider.onDidChangeScopeSupport((supportLevels) => {
113113
this.supportLevels = supportLevels;
114+
const editor = ide().activeTextEditor;
115+
this.selection =
116+
editor != null && editor.selections.length === 1
117+
? editor.selections[0]
118+
: null;
114119
this._onDidChangeTreeData.fire();
115120
}),
116121
this.scopeVisualizer.onDidChangeScopeType(() => {
@@ -190,25 +195,7 @@ export class ScopeTreeProvider implements TreeDataProvider<MyTreeItem> {
190195
isEqual(supportLevel.scopeType, this.scopeVisualizer.scopeType),
191196
),
192197
)
193-
.sort((a, b) => {
194-
if (
195-
a.scopeTypeInfo.spokenForm.type !== b.scopeTypeInfo.spokenForm.type
196-
) {
197-
// Scopes with no spoken form are sorted to the bottom
198-
return a.scopeTypeInfo.spokenForm.type === "error" ? 1 : -1;
199-
}
200-
201-
if (
202-
a.scopeTypeInfo.isLanguageSpecific !==
203-
b.scopeTypeInfo.isLanguageSpecific
204-
) {
205-
// Then language-specific scopes are sorted to the top
206-
return a.scopeTypeInfo.isLanguageSpecific ? -1 : 1;
207-
}
208-
209-
// Then alphabetical by label
210-
return a.label.label.localeCompare(b.label.label);
211-
});
198+
.sort(treeItemComparator);
212199
}
213200

214201
private getSelectedScopeTypes(): ScopeSupportTreeItem[] {
@@ -236,14 +223,14 @@ export class ScopeTreeProvider implements TreeDataProvider<MyTreeItem> {
236223
};
237224
})
238225
.filter(({ length }) => length > -1)
239-
.sort((a, b) => a.length - b.length)
240226
.map(({ supportLevel, length }) => {
241-
console.log(serializeScopeType(supportLevel.scopeType), length);
242227
return new ScopeSupportTreeItem(
243228
supportLevel,
244229
isEqual(supportLevel.scopeType, this.scopeVisualizer.scopeType),
230+
length,
245231
);
246-
});
232+
})
233+
.sort(treeItemComparator);
247234
}
248235

249236
private getScopeSupportInfo(scopeSupport: ScopeSupport): ScopeSupportInfo[] {
@@ -270,10 +257,10 @@ function getSupportCategories(): (
270257
| SelectedCategoryTreeItem
271258
)[] {
272259
return [
260+
new SelectedCategoryTreeItem(),
273261
new SupportCategoryTreeItem(ScopeSupport.supportedAndPresentInEditor),
274262
new SupportCategoryTreeItem(ScopeSupport.supportedButNotPresentInEditor),
275263
new SupportCategoryTreeItem(ScopeSupport.unsupported),
276-
new SelectedCategoryTreeItem(),
277264
];
278265
}
279266

@@ -289,6 +276,7 @@ class ScopeSupportTreeItem extends TreeItem {
289276
constructor(
290277
public readonly scopeTypeInfo: ScopeTypeInfo,
291278
isVisualized: boolean,
279+
public priority: number = 0,
292280
) {
293281
let label: string;
294282
let tooltip: string;
@@ -378,7 +366,7 @@ class SupportCategoryTreeItem extends TreeItem {
378366
case ScopeSupport.supportedButNotPresentInEditor:
379367
label = "Supported";
380368
description = "but not present in active editor";
381-
collapsibleState = TreeItemCollapsibleState.Expanded;
369+
collapsibleState = TreeItemCollapsibleState.Collapsed;
382370
break;
383371
case ScopeSupport.unsupported:
384372
label = "Unsupported";
@@ -394,7 +382,7 @@ class SupportCategoryTreeItem extends TreeItem {
394382

395383
class SelectedCategoryTreeItem extends TreeItem {
396384
constructor() {
397-
super("Selected", TreeItemCollapsibleState.Collapsed);
385+
super("Selected", TreeItemCollapsibleState.Expanded);
398386
this.description = "scopes";
399387
}
400388
}
@@ -455,3 +443,25 @@ function getSmallestTargetLength(
455443
}
456444
return length ?? -1;
457445
}
446+
447+
function treeItemComparator(a: ScopeSupportTreeItem, b: ScopeSupportTreeItem) {
448+
// First by priority (lower number is higher priority)
449+
if (a.priority !== b.priority) {
450+
return a.priority - b.priority;
451+
}
452+
453+
// Scopes with no spoken form are sorted to the bottom
454+
if (a.scopeTypeInfo.spokenForm.type !== b.scopeTypeInfo.spokenForm.type) {
455+
return a.scopeTypeInfo.spokenForm.type === "error" ? 1 : -1;
456+
}
457+
458+
// Then language-specific scopes are sorted to the top
459+
if (
460+
a.scopeTypeInfo.isLanguageSpecific !== b.scopeTypeInfo.isLanguageSpecific
461+
) {
462+
return a.scopeTypeInfo.isLanguageSpecific ? -1 : 1;
463+
}
464+
465+
// Then alphabetical by label
466+
return a.label.label.localeCompare(b.label.label);
467+
}

0 commit comments

Comments
 (0)