Callback for change of language


#1

Is there a way to register a callback when the language of the current text editor changes?

Currently, I retrieve a setting from atom.config every time the editor changes, using the scope of the editor. This setting won’t get changed if the user changes the language of the current text editor to a language with a different config setting.


#2

You can just use the onDidChangeGrammar event:

const editor = atom.workspace.getActiveTextEditor()

editor.onDidChangeGrammar((grammar) => {
  // do something
})

#3

I looked for something like onDidChangeGrammar? Must have missed it. Thanks!

On a separate note, why do you distinctly mark editor as a constant?


#4

why do you distinctly mark editor as a constant

As an habit. If I don’t plan to change the content of a variable I use const so that I can catch if I’m trying to write in it afterward. In that case, as I generally hook on the observeTextEditors event to register stuff for the workspace editors the content of editor inside the callback should never change. And once you get used to using const everywhere, it become clearer than a variable is intended to be changed later when you’re using let or var.