Restrict packages being loaded for certain filetypes


#1

Hi,
I am used to vim-plugs on demand plugin loading feature like this:

" On-demand loading
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

Is there any similar feature in atom? I am a bit annoyed of go plugins being active in python files and so on. I would be able to e.g. restrict go plugins only being active in go files. Is there any (undocumented) feature to do this?

Thanks!


#2

A package developer is perfectly capable of making a package turn itself off and on based on the active file’s grammar. Depending on the package, you may be able to toggle it with an init.coffee script.

Which packages are causing issues?


#3

That sounds like that I am looking for. :slight_smile: I will try that out.

It is not issues, it is more annoyance. For example, I do not need Go plugins loaded, when I am editing Python files and the other way round. In vim I can configure certain plugins only being loaded for special filetypes. That might also decrease the startup time of atom.


#4

Maybe. I don’t know yet because I don’t know which packages you’re concerned with.

Packages are loaded per window. Atom can’t know what type of files you’re editing as it’s starting up.

That might also decrease the startup time of atom.

Look at timecop (Packages -> Timecop -> Show) and see how much impact those packages are really having.


#5

It is quite generic. I, e.g., only want to enable package XYZ for filetype A, since package XYZ annoys me with, let’s say a sidebar that is completely useless for filetype A. Since you keep requesting an example, here it is: https://atom.io/packages/go-plus

It loads quite a bunch of stuff that is only useful for Go files. I do not need these packages loaded, when I edit plaintext or Python files. Disabling those packages manually every time in the settings panel is a bit odd; I just need some setting/script that enables those packages only for Go files.

Since I am a generically thinking person, that would make sense for me for any plugin. Such that, I can get rid of packages (=disable) that I do not need for the current session/window.


#6

In the process of investigating this, I discovered the package-switch package, which might be useful for you. It doesn’t have the capability to turn packages on and off based on the specific file, but on a per-project basis.

For the main query, here’s my suggestion: you can disable all packages that you want to have selectively active. You can then turn them on or off with atom.packages.enablePackage() and atom.packages.disablePackage(). The result is going to be permanent, so you would have to re-disable each package when you’re done with it. You can do this, for example, whenever atom.workspace.getActiveTextEditor().getGrammar() returns source.go or source.py. You would want to watch for both the onDidChangeActiveTextEditor() and onDidChangeGrammar() events and have each of those call the function that knows how to swap between packages.

I am a generically thinking person

I’m not. I work best targeting a specific case and then expanding my thinking from there.


#7

Thx! Just on the phone atm. I also found this:


#8

I have tried out a simple script (I am not yet that coffescript guru), and I can confirm that this approach works:

atom.workspace.observeTextEditors (editor) ->
    editor.onDidChangeGrammar (grammar) ->
        if grammar.name == "Go"
            atom.packages.enablePackage("go-plus")
        else
            atom.packages.disablePackage("go-plus")

The logic is to be extended in order to configure several packages/grammars; but it shows that it works. Thanks for that hint @DamnedScholar!


#9

I’m not either. Some of the abstractions in CS are difficult for me to remember, so I make fairly regular use of coffeescript.org in order to check my work. CS is easier to type, but JS is easier for me to read since I learned coding on C++/Java.


#10

I had same problem with package autocomplete-hack and php files, this is my solution

atom.workspace.onDidChangeActiveTextEditor (editor) ->
fileExtensions = /\.hh$/

if editor.getPath().match(fileExtensions)
    atom.packages.enablePackage("autocomplete-hack")
else
    atom.packages.disablePackage("autocomplete-hack")

#11

Make sure to indent:

atom.workspace.onDidChangeActiveTextEditor (editor) ->
    fileExtensions = /\.txt$/

    if editor.getPath().match(fileExtensions)
        atom.packages.disablePackage("symbols-tree-view")
    else
        atom.packages.enablePackage("symbols-tree-view")