AutomaticPackageReloader icon indicating copy to clipboard operation
AutomaticPackageReloader copied to clipboard

option for single file reload

Open timotheecour opened this issue 5 years ago • 4 comments

based on my experience with https://github.com/Varriount/NimLime/issues/115#issuecomment-436120327 where the simple

import sublime_plugin
sublime_plugin.reload_plugin('NimLime.core.commands.nimcheck')

worked whereas using package_reloader_reload did not, how about adding a command which would call sublime_plugin.reload_plugin on the current module (eg: mypkg.foo.bar), where mypkg.foo.bar is inferred automatically from view.file_name()

that would be pretty useful in cases where either package_reloader_reload doesn't work (as in NimLime), as well as in cases where reloading all modules in a package is somehow undesirable ; the user would be in control by letting him choose the single module reload vs whole package reload.

The question is: do you know a simple way to go from file name view.file_name() to mypkg.foo.bar ?

this could be maybe as simple as: iterate over parent dir dir until there's no dir/__init__.py so that pathToModule("/pathto/foo/baz/mypkg/foo/bar.py") = "mypkg.foo.bar"

EDIT

I tried, it works fine:

def fileToMod(path):
  assert os.path.splitext(path)[1] == ".py"
  mod = ""
  while True:
    (head, tail) = os.path.split(path)
    if tail == '':
      return mod
    name = os.path.splitext(tail)[0]
    if mod == "":
      mod = name
    else:
      mod = name + '.' + mod

    initF = os.path.join(head,'__init__.py')
    if not os.path.exists(initF):
      return mod

    path = head

[EDIT]

based on this discussion https://github.com/Varriount/NimLime/pull/121#issuecomment-436159603 it appears __init__.py is not required so I guess I can try alternative approach for fileToMod suggested here https://github.com/Varriount/NimLime/pull/121#issuecomment-436159603 ; @randy3k let me know if you have any comments on existance of top-level __init__.py

[EDIT]

ok, i implemented the alternative approach which doesn't require top-level __init__.py and it works

timotheecour avatar Nov 06 '18 04:11 timotheecour

This already seems to be implemented - I can call "Reload Current Package" while looking at any file in NimLime and have the package reloaded. The relevant code is implemented here.

Or do you mean, reload an individual file in a Sublime Text plugin?

Varriount avatar Nov 06 '18 14:11 Varriount

yes, I mean reload an individual file in a Sublime Text plugin ; because it's useful in some cases when Reload Current Package isn't desirable as I mentioned in top post

timotheecour avatar Nov 06 '18 19:11 timotheecour

I don’t think we should reload only the file being edited. The edited file may have been imported as a dependence by other files.

randy3k avatar Nov 06 '18 22:11 randy3k

Only reloading the file being edited is highly volatile and the fact that it works in your case is more or less coincidence (or well, not really, since the behavior is to be expected, but it's not proper). The reason being that now every command is defined twice (once in the surface plugin, the one in the package root, and once by directly loading the module where the commands are defined), but the latter takes precedence. If you were to do this with event listeners, you'd have two of them active.

The plugin system is not designed to be used this way. Plugin classes that interface with the sublime_plugin API must be exposed in a root plugin module, otherwise they will be ignored. Attempting to circumvent this by calling sublime.reload_pluign manually will create all sorts of shadow bugs and issues that you may or may not encounter during your dev session and will most likely leave you agonizing due to weird issues.

The best way to reload a package's plugins is still to unload and reload all its modules.

FichteFoll avatar Nov 07 '18 00:11 FichteFoll