Skip to content

Commit 4f3e025

Browse files
authored
Use language icons for language-specific scopes (#2006)
- Fixes #1982 - Thanks to @usernamehw and @aeschli for the code and advice! <img width="976" alt="image" src="https://github.com/cursorless-dev/cursorless/assets/755842/2b1bd496-da49-4ce2-877a-07be45e482fb"> ## Checklist - [-] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [-] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [-] I have not broken the cheatsheet
1 parent 6b45a52 commit 4f3e025

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

packages/cursorless-vscode/src/ScopeTreeProvider.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {
2525
ThemeIcon,
2626
TreeItem,
2727
TreeItemCollapsibleState,
28+
extensions,
29+
window,
2830
} from "vscode";
2931
import { URI } from "vscode-uri";
3032
import {
@@ -261,7 +263,22 @@ class ScopeSupportTreeItem extends TreeItem {
261263
};
262264

263265
if (scopeTypeInfo.isLanguageSpecific) {
264-
this.iconPath = new ThemeIcon("code");
266+
const languageId = window.activeTextEditor?.document.languageId;
267+
if (languageId != null) {
268+
const fileExtension = getLanguageExtensionSampleFromLanguageId(
269+
window.activeTextEditor!.document.languageId,
270+
);
271+
if (fileExtension != null) {
272+
this.resourceUri = URI.parse(
273+
"cursorless-dummy://dummy/dummy" + fileExtension,
274+
);
275+
}
276+
}
277+
278+
if (this.resourceUri == null) {
279+
// Fall back to a generic icon
280+
this.iconPath = new ThemeIcon("code");
281+
}
265282
}
266283
}
267284
}
@@ -300,3 +317,29 @@ class SupportCategoryTreeItem extends TreeItem {
300317
}
301318

302319
type MyTreeItem = ScopeSupportTreeItem | SupportCategoryTreeItem;
320+
321+
/**
322+
* Get file extension example from vscode [Language Id](https://code.visualstudio.com/docs/languages/identifiers)
323+
* Would've been easier with https://github.com/microsoft/vscode/issues/109919
324+
* Example:
325+
* - 'typescript' => '.ts'
326+
* FIXME: Maybe memoise?
327+
*/
328+
export function getLanguageExtensionSampleFromLanguageId(
329+
languageId: string,
330+
): string | undefined {
331+
for (const extension of extensions.all) {
332+
const languages: { id: string; extensions: string[] }[] | undefined =
333+
extension.packageJSON?.contributes?.languages;
334+
335+
if (!languages) {
336+
continue;
337+
}
338+
339+
for (const contributedLanguage of languages) {
340+
if (contributedLanguage.id === languageId) {
341+
return contributedLanguage.extensions[0];
342+
}
343+
}
344+
}
345+
}

0 commit comments

Comments
 (0)