Skip to content

Commit a586ab4

Browse files
committed
SC_MenuEvent.addTShellCommandToMenu(): Fix accessing a menu item possibly too early.
Not directly related to #70, but this was noticed during it, and as I haven't noticed this glitch causing problems in practise, there's no need to create a separate issue. The original plan was to create a separate method, but then parsing_results would have become inaccessible.
1 parent ca28c38 commit a586ab4

File tree

1 file changed

+28
-33
lines changed

1 file changed

+28
-33
lines changed

src/events/SC_MenuEvent.ts

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,41 @@
1919

2020
import {SC_WorkspaceEvent} from "./SC_WorkspaceEvent";
2121
import {ShellCommandParsingProcess, TShellCommand} from "../TShellCommand";
22-
import {
23-
Menu,
24-
MenuItem,
25-
} from "obsidian";
22+
import {Menu} from "obsidian";
2623
import {ParsingResult} from "../variables/parseVariables";
2724

2825
export abstract class SC_MenuEvent extends SC_WorkspaceEvent {
2926

3027
protected async addTShellCommandToMenu(t_shell_command: TShellCommand, menu: Menu) {
3128
// Create the menu item as soon as possible. (If it's created after 'await parsing_process.process()' below, it won't be shown in the menu for some reason, at least in Obsidian 0.16.1).
32-
// No title is set here, it will be set later.
33-
let menu_item: MenuItem;
34-
menu.addItem(item => menu_item = item
35-
.setIcon(t_shell_command.getIconId()) // Icon id can be null.
36-
.onClick(async () => {
37-
await this.trigger(
38-
t_shell_command,
39-
parsing_process,
40-
);
41-
}),
42-
);
43-
44-
// Parse shell command variables to get a title
45-
let title = t_shell_command.getAliasOrShellCommand(); // May contain unparsed variables.
46-
let parsing_process: ShellCommandParsingProcess;
47-
if (this.plugin.settings.preview_variables_in_command_palette) {
48-
// Start a parsing process
49-
parsing_process = t_shell_command.createParsingProcess(this);
50-
if (await parsing_process.process()) {
51-
// Parsing succeeded.
52-
const parsing_results = parsing_process.getParsingResults();
53-
const aliasParsingResult: ParsingResult = parsing_results["alias"] as ParsingResult; // as ParsingResult: Tells TypeScript that the object exists.
54-
const shellCommandParsingResult: ParsingResult = parsing_results["shell_command"] as ParsingResult; // as ParsingResult: Tells TypeScript that the object exists.
55-
title = aliasParsingResult.parsed_content || shellCommandParsingResult.parsed_content as string; // Try to use a parsed alias, but if no alias is available, use a parsed shell command instead. as string = parsed shell command always exist when the parsing itself has succeeded.
29+
menu.addItem(async menuItem => {
30+
// Parse shell command variables to get a title
31+
let title = t_shell_command.getAliasOrShellCommand(); // May contain unparsed variables.
32+
let parsing_process: ShellCommandParsingProcess;
33+
if (this.plugin.settings.preview_variables_in_command_palette) {
34+
// Start a parsing process
35+
parsing_process = t_shell_command.createParsingProcess(this);
36+
if (await parsing_process.process()) {
37+
// Parsing succeeded.
38+
const parsing_results = parsing_process.getParsingResults();
39+
const aliasParsingResult: ParsingResult = parsing_results["alias"] as ParsingResult; // as ParsingResult: Tells TypeScript that the object exists.
40+
const shellCommandParsingResult: ParsingResult = parsing_results["shell_command"] as ParsingResult; // as ParsingResult: Tells TypeScript that the object exists.
41+
title = aliasParsingResult.parsed_content || shellCommandParsingResult.parsed_content as string; // Try to use a parsed alias, but if no alias is available, use a parsed shell command instead. as string = parsed shell command always exist when the parsing itself has succeeded.
42+
}
43+
// If parsing process fails, the failed process can be passed to this.trigger(). The execution will eventually be cancelled and error messages displayed (if displaying is allowed).
44+
menuItem.setTitle(title);
5645
}
57-
// If parsing process fails, the failed process can be passed to this.trigger(). The execution will eventually be cancelled and error messages displayed (if displaying is allowed).
58-
}
5946

60-
// Update menu item title.
61-
// @ts-ignore. Suppress for now. // FIXME: Fix in a separate commit by moving the whole title generation block to a new method and call that method from the menu.addItem()'s callback function.
62-
menu_item.setTitle(title);
47+
// Icon and onClick handler.
48+
menuItem
49+
.setIcon(t_shell_command.getIconId()) // Icon id can be null.
50+
.onClick(async () => {
51+
await this.trigger(
52+
t_shell_command,
53+
parsing_process,
54+
);
55+
})
56+
;
57+
});
6358
}
6459
}

0 commit comments

Comments
 (0)