Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Add activationHooks + Trigger {grammar-package-name}:grammar-used Hook When A text-editor-element's Grammar Is Set - #7638

Merged
nathansobo merged 2 commits into
atom:masterfrom
atom-community-archive:jf-dispatch-grammar-used
Jul 9, 2015
Merged

nathansobo merged 2 commits into
atom:masterfrom
atom-community-archive:jf-dispatch-grammar-used

Conversation

@joefitzgerald

Copy link
Copy Markdown
Contributor

This PR:

  • Adds activationHooks to package.json
  • Triggers the {grammar-package-name}:grammar-used hook when a text-editor-element's grammar is set

This allows package authors to defer package activation for language-specific packages until the first use of the grammar(s) their package serves. It also leaves open the possibility to trigger other activation hooks in the future with minimal code.

/cc @kevinsawicki

@park9140

park9140 commented Jul 1, 2015

Copy link
Copy Markdown

👍 This is awesome.
@basarat, atom typescript needs all it's initialization behind this.

Comment thread spec/text-editor-spec.coffee Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this console.log committed by accident 👼?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, console.log shouldn't be there. This is WIP, I'm moving some stuff around and will clean up history.

@joefitzgerald joefitzgerald changed the title Dispatch {grammar-package-name}:grammar-used Command When Grammar Changes [WIP] Dispatch {grammar-package-name}:grammar-used Command When Grammar Changes Jul 1, 2015
@joefitzgerald joefitzgerald changed the title [WIP] Dispatch {grammar-package-name}:grammar-used Command When Grammar Changes Dispatch {grammar-package-name}:grammar-used Command When Grammar Changes Jul 1, 2015
@joefitzgerald

Copy link
Copy Markdown
Contributor Author

Ready for review.

@nathansobo

Copy link
Copy Markdown
Contributor

@joefitzgerald I only want to use commands for communication from the user to Atom, not for communication from Atom to packages.

I would be open to a method on text editor such as TextEditor::onDidChangeGrammar instead of this however.

@joefitzgerald

Copy link
Copy Markdown
Contributor Author

@nathansobo this is the only way we can have activationCommands kick in on first use of a grammar. This allows us to defer activation of packages that are language-specific until the first use of the grammar for that language. onDidChangeGrammar doesn't achieve that objective.

@thedaniel

Copy link
Copy Markdown
Contributor

👎

I support a hard line that commands should be user-invoked. If this delayed activation thing is really, truly needed, I'd support baking this into core as activationGrammar in package.json or something.

@nathansobo

Copy link
Copy Markdown
Contributor

@joefitzgerald Got it. I wasn't fully understanding what you're trying to do here.

Yeah, echoing what @thedaniel is saying, I'm worried about expanding the role of commands in this way. I see some ways forward though...

  • As @thedaniel suggested, some grammar specific activation hook in packages. This is nice and focused, but may be too narrow. What happens when packages need to activate on some other event?
  • Adding a new, more flexible package activation API. This would be something like atom.packages.activate('grammar:.foo.bar') that referenced a new top-level package property (perhaps bring back activationEvents?). The key difference here is we wouldn't expose an API for responding to these events. They would be purely for package activation (write-only).

@park9140

park9140 commented Jul 1, 2015

Copy link
Copy Markdown

@nathansobo, @thedaniel, you make good points.

Delayed activation is a simple thing to achieve for code that we control inside our activation method, by not activating any package functionality on activation but just bind a event listener for any given event we want to activate on.

This doesn't work for any dynamically activated functionality like keybinds and services however.

What if we could return a promise from our activation function that determined when to activate the other automatic features of our package.

If we add onGrammarLoaded to https://github.com/atom/first-mate/blob/v4.1.7/src/grammar-registry.coffee#L36?

Then delayed activation based on grammar is something like

activate: ->
  return new Promise resolve -> 
    grammerLoadOff() = atom.grammars.onGrammarLoaded (grammar) ->
      if grammar == "coffeeScript"
       grammerLoadOff()
       @customInitialization();
       resolve()

It could even be a separate fuction like shouldActivate rather than extending default activation.

Keeping in mind I'm not sure if onGrammarLoaded is quite appropriate for this situation.

@joefitzgerald

Copy link
Copy Markdown
Contributor Author

@park9140 you don't want to activate on load of a grammar as - I believe - all grammars are loaded on startup? Core team, is that accurate?

I do like the idea of allowing activate to return a promise. It would somewhat resolve #4059 also. I'm unsure how activationEvents were implemented before - how did it hook into the emitters in all the things?

/cc @bolinfest

@joefitzgerald

Copy link
Copy Markdown
Contributor Author

Q: [...] how did it hook into the emitters in all the things?

A:

atom/src/package.coffee

Lines 334 to 341 in 8c2bcf3

subscribeToActivationEvents: ->
return unless @metadata.activationEvents?
if _.isArray(@metadata.activationEvents)
atom.workspaceView.command(event, @handleActivationEvent) for event in @metadata.activationEvents
else if _.isString(@metadata.activationEvents)
atom.workspaceView.command(@metadata.activationEvents, @handleActivationEvent)
else
atom.workspaceView.command(event, selector, @handleActivationEvent) for event, selector of @metadata.activationEvents

It didn't it registered on workspaceView. So, is that really what we'd want to go back to? Or should there be something like atom.commands (i.e.atom.events)?

@joefitzgerald joefitzgerald changed the title Dispatch {grammar-package-name}:grammar-used Command When Grammar Changes Add activationTriggers + Trigger {grammar-package-name}:grammar-used When A text-editor-element's Grammar Is Set Jul 2, 2015
@joefitzgerald joefitzgerald changed the title Add activationTriggers + Trigger {grammar-package-name}:grammar-used When A text-editor-element's Grammar Is Set Add activationHooks + Trigger {grammar-package-name}:grammar-used Hook When A text-editor-element's Grammar Is Set Jul 2, 2015
@joefitzgerald

Copy link
Copy Markdown
Contributor Author

OK folks, I've pivoted to a new approach that leverages activationHooks in package.json and leaves the option open for adding other hooks in the future. Please review.

Comment thread spec/package-manager-spec.coffee Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we shouldn't need the workspace element to be on the DOM here. Is this a leftover?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

@nathansobo

Copy link
Copy Markdown
Contributor

Everything seems pretty straightforward except where the grammar hook is being triggered and how we're going to format the string. I just want to put some thought into our scheme.

I think I see where you're going with using the package-name prefix in the hook name. However, there's also the grammar scope name, and a single language package could contain multiple grammars. So we're losing a bit of information here. That said, this does lay down a nice pattern for packages to add their own hooks when we expose the activation hook API.

@joefitzgerald

Copy link
Copy Markdown
Contributor Author

I think I see where you're going with using the package-name prefix in the hook name. However, there's also the grammar scope name, and a single language package could contain multiple grammars.

I could just emit once for the package name and then again for the grammar scope name?

@nathansobo

Copy link
Copy Markdown
Contributor

I could just emit once for the package name and then again for the grammar scope name?

I guess let's just start with the package name and see where it goes.

- Trigger the `{grammar-package-name}:grammar-used` hook when grammar is set for tokenized-buffer
@joefitzgerald

Copy link
Copy Markdown
Contributor Author

@nathansobo I moved the triggering of activationHooks to tokenized-buffer. Please review.

@nathansobo

Copy link
Copy Markdown
Contributor

Looking good. Running a build on our internal CI and giving core team members one last chance to check this out. If all goes well, will merge this afternoon.

@thedaniel

Copy link
Copy Markdown
Contributor

Don't forget to doc it over in atom/docs after it lands!

nathansobo pushed a commit that referenced this pull request Jul 9, 2015
Add activationHooks + Trigger `{grammar-package-name}:grammar-used` Hook When A text-editor-element's Grammar Is Set
@nathansobo
nathansobo merged commit c7d9914 into atom:master Jul 9, 2015
@nathansobo
nathansobo deleted the jf-dispatch-grammar-used branch July 9, 2015 20:42
@nathansobo

Copy link
Copy Markdown
Contributor

@joefitzgerald Would you mind opening a PR documenting this?

@nathansobo

Copy link
Copy Markdown
Contributor

Thanks by the way. I really appreciate the thought you're putting into leveling up our package system.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants