Sublime Forum

Open Python files from selection or current line

#1

Hi folks,

I’ve written a plugin which parses all selections or the current line for import statements, tries to open them, bookmarks (temporarily, until the file is being closed) and highlights all referenced symbols - thus you can skim thru them like normal bookmarks. This let’s me create and open references extremely easy and without opening and traversing the directory tree. I’m using OSX but it should also work within Linux. I appreciate working patches for Windows.

This involves two files:

  • find_python_imports.py: A helper script to parse given content. Returns all imports (e.g. “import a”, “from a import b” or “app.code.some.thing” - see main() in file for many more variants!)
  • open_python_file.py: A text command which uses the script above to parse all selections or the current line and does the rest described at the top.

You can easily use this by putting the repo folder mentioned below into your “Packages” folder.
I’m using the following key binding (replace super with ctrl when under Linux/Windows):
{ “keys”: “super+shift+o”], “command”: “open_python_file” }

You might also want to make use of the virtualenv integration I’ve described in a previous forum post (Virtualenv). To combine these two you have to set up the “open_python_file_vrun_executable” variable in your “Global.sublime-settings” file. I’ve set it to the same value as my “exec_arg_list_wrapper” var.

You can now open some Python file from your project folder (or something else), highlight some import statements and let them open up in new tabs.

You can get the code here:
bitbucket.org/theblacklion/sublime_plugins/src

Have fun,
Oktay.

0 Likes

Open Magento PHP files from selection or current line
#2

I’d love to have this working for me but right now I’m getting this error:

stdout = stderr = The filename, directory name, or volume label syntax is incorrect. error: The filename, directory name, or volume label syntax is incorrect. Traceback (most recent call last): File ".\sublime_plugin.py", line 226, in run_ File ".\open_python_file.py", line 184, in run File ".\open_python_file.py", line 55, in run AttributeError: 'NoneType' object has no attribute 'is_loading'

This is in windoze. I’ll try later on OSX to see if it’s os specific.

0 Likes

#3

OSX works fine as long as I remove the header comments. Some issue with encoding.

0 Likes

#4

[quote=“marksteve”]I’d love to have this working for me but right now I’m getting this error:
This is in windoze. I’ll try later on OSX to see if it’s os specific.[/quote]

This is os specific :smile:. The command it tries to execute is valid under unix environments only. We need to discover the equivalent for Windows. Would be nice, if you could find that out… Take a look around line 100.

Plz be more specific about “header comments” and “issues with encoding”.

Thanks!

0 Likes

#5

this is a cool project, but yikes: bitbucket.org/theblacklion/subl … .py#cl-104
you should be able to do it from inside python itself - maybe imp.find_module: docs.python.org/library/imp.html#imp.find_module

0 Likes

#6

[quote=“lunixbochs”]this is a cool project, but yikes: bitbucket.org/theblacklion/subl … .py#cl-104
you should be able to do it from inside python itself - maybe imp.find_module: docs.python.org/library/imp.html#imp.find_module[/quote]

Thanks :smile:.
I know that this smells to heaven. The problem is that I wanted to have my wrapper script to be able to load my virtualenv. I’ll try to rework the code with your suggestion. I should be able to get rid of most of the shell dependencies…

0 Likes

#7

I’ve reworked the routine. Please take another look.

0 Likes

#8

hi!
can this be done for other languages as well? (e.g. LaTeX)

0 Likes