Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investiate alternate method of triggering NodeRequirer. #38

Open
dantman opened this issue Feb 25, 2015 · 2 comments
Open

Investiate alternate method of triggering NodeRequirer. #38

dantman opened this issue Feb 25, 2015 · 2 comments

Comments

@dantman
Copy link
Contributor

dantman commented Feb 25, 2015

Some of the bigger Sublime plugins seem to do some pretty complex stuff with the APIs sublime exposes (and they're not all listed in the official documentation). Creating intuitive interfaces in Sublime seems to be a matter of creatively (ab)using the APIs.

One idea I can think of is attempting to provide a different way of triggering NodeRequirer. The current method of doing it is with Ctrl+Shift+I. And personally, I hate keyboard combos. It's another thing to memorize and it forces me to temporarily break my hands' typing position in order to hit the keys.

Here's one creative idea for a method of triggering NodeRequirer by abusing two sublime APIs in combination:

  • We add one (or more) NodeRequirer.sublime-snippet snippets. These will be something like require(${1:[module]}).
  • Using on_modify/on_modify_async or maybe intercepting the commit_completion command we watch for a selection that matches [module].
  • If that selection matches we look at the surrounding text to see if we're in the correct snippet result.
  • If so we trigger the NodeRequirer panel.

From the user perspective it should look like:

  1. They started typing require.
  2. A require - NodeRequirer showed up in their completions.
  3. They hit tab/enter, whatever is configured for completions.
  4. NodeRequirer popped up asking what module to require.
@dantman
Copy link
Contributor Author

dantman commented Feb 25, 2015

It does indeed seem possible to do this.

Here's an initial experiment:

class NodeRequirerListener(sublime_plugin.EventListener):
    def on_post_text_command(self, view, command_name, args):
        if command_name not in ('commit_completion', 'insert_best_completion'):
            return

        cursor = view.sel()[0]
        selection_text = view.substr(sublime.Region(cursor.begin(), cursor.end())).strip()

        if selection_text == '[module]':
            prev_text = view.substr(sublime.Region(max(0, cursor.begin() - 8), cursor.begin())).strip()
            next_text = view.substr(sublime.Region(cursor.end(), cursor.end() + 1)).strip()
            if prev_text == 'require(' and next_text == ')':
                view.run_command('simple_require')

And test snippet:

<snippet>
    <content><![CDATA[
require(${1:[module]})
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>require</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <!-- <scope>source.javascript</scope> -->
</snippet>

@ganemone
Copy link
Owner

Feel free to create a PR. I would definitely check it out. Just not high on my priority list right now. Looks cool though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants