Skip to content

Commit cba281b

Browse files
committed
Refactor Find Symbol to match the new tests sidebar
1 parent a45743b commit cba281b

File tree

5 files changed

+103
-84
lines changed

5 files changed

+103
-84
lines changed

src/client_disposable.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import {
1010
import registerFormatDocument from "./commands/format_document.ts";
1111
import registerCache from "./commands/cache.ts";
1212
import registerRenameSymbol from "./commands/rename_symbol.ts";
13-
import registerPaletteFindSymbol from "./commands/palette_find_symbol.ts";
14-
import registerSymbolSidebarFindSymbol from "./commands/sidebar_find_symbol.ts";
13+
import registerFindSymbol from "./find_symbol/register.ts";
1514
import syntaxes from "./syntaxes.ts";
1615

1716
const FORMAT_ON_SAVE_CONFIG_KEY = "co.gwil.deno.config.formatOnSave";
@@ -140,11 +139,7 @@ export async function makeClientDisposable(
140139
clientDisposable.add(registerFormatDocument(client));
141140
clientDisposable.add(registerCache(client));
142141
clientDisposable.add(registerRenameSymbol(client));
143-
144-
// palette Find Symbol command
145-
clientDisposable.add(registerPaletteFindSymbol());
146-
// sidebar Find Symbol command
147-
clientDisposable.add(registerSymbolSidebarFindSymbol(client));
142+
clientDisposable.add(registerFindSymbol(client));
148143

149144
nova.workspace.onDidAddTextEditor((editor) => {
150145
const editorDisposable = new CompositeDisposable();

src/commands/palette_find_symbol.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/commands/symbols.sidebar.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {
2+
LanguageClient,
3+
NotificationRequest,
4+
nova,
5+
wrapCommand,
6+
} from "../nova_utils.ts";
7+
import SymbolDataProvider from "../find_symbol/SymbolDataProvider.ts";
8+
import { lsp } from "../../deps.ts";
9+
10+
export function registerPaletteFindSymbol() {
11+
return nova.commands.register(
12+
"co.gwil.deno.commands.find",
13+
wrapCommand(maybeFindSymbol),
14+
);
15+
16+
async function maybeFindSymbol() {
17+
try {
18+
await nova.commands.invoke("co.gwil.deno.sidebars.symbols.commands.find");
19+
} catch (err) {
20+
// I don't think this ever happens.
21+
console.error(err);
22+
}
23+
24+
const sidebarReminderNotificationRequest = new NotificationRequest(
25+
"co.gwil.deno.notifications.checkTheSidebar",
26+
);
27+
sidebarReminderNotificationRequest.title =
28+
"Symbols are shown in the Deno Symbols sidebar.";
29+
sidebarReminderNotificationRequest.body =
30+
"To see your search's results, check the Deno Symbols sidebar.";
31+
nova.notifications.add(sidebarReminderNotificationRequest);
32+
}
33+
}
34+
35+
export function registerSidebarFindSymbol(
36+
client: LanguageClient,
37+
symbolDataProvider: SymbolDataProvider,
38+
) {
39+
return nova.commands.register(
40+
"co.gwil.deno.sidebars.symbols.commands.find",
41+
wrapCommand(findSymbol),
42+
);
43+
44+
async function findSymbol() {
45+
if (
46+
// @ts-expect-error: The Nova types are outdated.
47+
!(nova.workspace.context as Configuration).get("shouldDisplayFeatures")
48+
) {
49+
const failureNotificationReq = new NotificationRequest(
50+
"co.gwil.deno.notifications.findSymbolUnavailable",
51+
);
52+
failureNotificationReq.title = "Find Symbol is unavailable.";
53+
failureNotificationReq.body =
54+
"Open a TypeScript, JavaScript, JSX or TSX file.";
55+
nova.notifications.add(failureNotificationReq);
56+
return;
57+
}
58+
59+
const query = await new Promise((resolve) =>
60+
nova.workspace.showInputPalette(
61+
"Type the name of a variable, class or function.",
62+
{},
63+
resolve,
64+
)
65+
) as string | null | undefined;
66+
67+
// This happens if the user exits the palette, for example, by pressing Escape.
68+
if (!query) return;
69+
70+
symbolDataProvider.displaySymbols(query, getSymbols);
71+
72+
async function getSymbols(query: string) {
73+
const params = { query };
74+
const response = await client.sendRequest(
75+
"workspace/symbol",
76+
params,
77+
) as lsp.SymbolInformation[] | null;
78+
return response;
79+
}
80+
}
81+
}

src/commands/sidebar_find_symbol.ts renamed to src/find_symbol/SymbolDataProvider.ts

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ class Symbol implements Element {
204204
editor.scrollToPosition(range.start);
205205
}
206206
}
207-
class SymbolDataProvider implements TreeDataProvider<Element> {
208-
private treeView: TreeView<Element>;
207+
export default class SymbolDataProvider implements TreeDataProvider<Element> {
208+
treeView: TreeView<Element>;
209209

210210
// The locations need to be stored separately to enable the sidebar to reload infrequently.
211211
private locations: Map<number, lsp.Location>;
@@ -383,52 +383,3 @@ class SymbolDataProvider implements TreeDataProvider<Element> {
383383
return element.toTreeItem();
384384
}
385385
}
386-
387-
export default function registerFindSymbol(client: LanguageClient) {
388-
return nova.commands.register(
389-
"co.gwil.deno.sidebars.symbols.commands.find",
390-
wrapCommand(findSymbol),
391-
);
392-
393-
async function findSymbol() {
394-
if (!symbolDataProvider) {
395-
symbolDataProvider = new SymbolDataProvider();
396-
}
397-
398-
if (
399-
// @ts-expect-error: The Nova types are outdated.
400-
!(nova.workspace.context as Configuration).get("shouldDisplayFeatures")
401-
) {
402-
const failureNotificationReq = new NotificationRequest(
403-
"co.gwil.deno.notifications.findSymbolUnavailable",
404-
);
405-
failureNotificationReq.title = "Find Symbol is unavailable.";
406-
failureNotificationReq.body =
407-
"Open a TypeScript, JavaScript, JSX or TSX file.";
408-
nova.notifications.add(failureNotificationReq);
409-
return;
410-
}
411-
412-
const query = await new Promise((resolve) =>
413-
nova.workspace.showInputPalette(
414-
"Type the name of a variable, class or function.",
415-
{},
416-
resolve,
417-
)
418-
) as string | null | undefined;
419-
420-
// This happens if the user exits the palette, for example, by pressing Escape.
421-
if (!query) return;
422-
423-
symbolDataProvider.displaySymbols(query, getSymbols);
424-
425-
async function getSymbols(query: string) {
426-
const params = { query };
427-
const response = await client.sendRequest(
428-
"workspace/symbol",
429-
params,
430-
) as lsp.SymbolInformation[] | null;
431-
return response;
432-
}
433-
}
434-
}

src/find_symbol/register.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { CompositeDisposable, LanguageClient } from "../nova_utils.ts";
2+
import {
3+
registerPaletteFindSymbol,
4+
registerSidebarFindSymbol,
5+
} from "../commands/symbols.sidebar.ts";
6+
import SymbolDataProvider from "./SymbolDataProvider.ts";
7+
8+
export default function registerSymbolsSidebar(client: LanguageClient) {
9+
const symbolsDisposable = new CompositeDisposable();
10+
11+
const dataProvider = new SymbolDataProvider();
12+
symbolsDisposable.add(dataProvider.treeView);
13+
14+
symbolsDisposable.add(registerSidebarFindSymbol(client, dataProvider));
15+
symbolsDisposable.add(registerPaletteFindSymbol());
16+
17+
return symbolsDisposable;
18+
}

0 commit comments

Comments
 (0)