Skip to content

option for single file reload #18

@timotheecour

Description

@timotheecour

based on my experience with nim-lang/NimLime#115 (comment) 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 nim-lang/NimLime#121 (comment) it appears __init__.py is not required so I guess I can try alternative approach for fileToMod suggested here nim-lang/NimLime#121 (comment) ; @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions