EditorSuggest eats tabs and other characters - probably shouldn't?

Hi there!

I would have raised this as a bug report, but it can’t be reproduced with a barebones vault as EditorSuggest functionality isn’t implemented anywhere by default.

Steps to reproduce

  • Trigger an EditorSuggest popup while typing
  • Press tab

Expected result

The line should be indented

Actual result

The line isn’t indented

Similar behaviour can be seen with the home and end keys not functioning.
Pressing escape to dismiss the popup, then pressing tab indents the line as expected.


Additional information

I understand that some keys are reserved for navigating the popup items, but I think tab, home, end keys should be being passed through to the editor to allow users to type unimpeded (unless I’m just missing what they’re used for!).

Relevant bug report in the tasks plugin (includes video demonstration):

Thanks!

You can indent the line by adding this in the constructor:

        this.scope.register([], "Tab", () => {
            const editor = this.context?.editor;
            if (editor) {
                editor.replaceRange("\t", {line: editor.getCursor().line, ch: 0});
            }
        });
1 Like

Ah that’s very helpful thank you! I’ll leave this thread open as it would be good to get clarification whether this is intended functionality or a bug. I’ll submit this change for discussion in obsidian-tasks in the meantime.

If it is a bug that got fixed, then this workaround would lead to double indenting which would be more annoying than none!

1 Like

You can probably avoid that by preventing default.

Return false to automatically preventDefault

1 Like

Ah yeah I think that would do the trick, thank you again!

Another issue has cropped up - respecting user preferences regarding tab indents vs spaces. I can’t see an API call that takes care of indenting for us - anyone have any ideas other than inspecting the vault settings and inserting the appropriate characters?

I spoke too soon! I found editor.exec('indentMore')

1 Like

Here’s the snippet I’ve ended up with in the constructor of the class implementing EditorSuggest, which restores indent functionality:

// EditorSuggestor swallows tabs while the suggestor popup is open
// This is a hack to support indenting while popup is open
app.scope.register([], 'Tab', () => {
    const editor = this.context?.editor;
    if (editor) {
        editor.exec('indentMore');
        // Returning false triggers preventDefault
        // Should prevent double indent if tabs start to get passed through
        return false;
    }
    return true;
});
2 Likes