Home Download Buy Blog Forum Support

Open Python files from selection or current line

Open Python files from selection or current line

Postby theblacklion on Sun Feb 20, 2011 11:02 pm

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 (viewtopic.php?f=5&t=1699). 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:
https://bitbucket.org/theblacklion/sublime_plugins/src

Have fun,
Oktay.
theblacklion
 
Posts: 67
Joined: Sat Feb 05, 2011 10:07 pm
Location: Hannover, Germany

Re: Open Python files from selection or current line

Postby marksteve on Mon Feb 21, 2011 9:33 am

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

Code: Select all
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.
marksteve
 
Posts: 43
Joined: Thu Sep 30, 2010 6:44 am

Re: Open Python files from selection or current line

Postby marksteve on Mon Feb 21, 2011 9:42 am

OSX works fine as long as I remove the header comments. Some issue with encoding.
marksteve
 
Posts: 43
Joined: Thu Sep 30, 2010 6:44 am

Re: Open Python files from selection or current line

Postby theblacklion on Mon Feb 21, 2011 9:49 am

marksteve wrote: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.


This is os specific :). 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!
theblacklion
 
Posts: 67
Joined: Sat Feb 05, 2011 10:07 pm
Location: Hannover, Germany

Re: Open Python files from selection or current line

Postby lunixbochs on Tue Feb 22, 2011 3:15 pm

this is a cool project, but yikes: https://bitbucket.org/theblacklion/subl ... .py#cl-104
you should be able to do it from inside python itself - maybe imp.find_module: http://docs.python.org/library/imp.html#imp.find_module
lunixbochs
 
Posts: 91
Joined: Fri Oct 08, 2010 10:18 pm

Re: Open Python files from selection or current line

Postby theblacklion on Tue Feb 22, 2011 6:46 pm

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


Thanks :).
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...
theblacklion
 
Posts: 67
Joined: Sat Feb 05, 2011 10:07 pm
Location: Hannover, Germany

Re: Open Python files from selection or current line

Postby theblacklion on Tue Feb 22, 2011 8:29 pm

I've reworked the routine. Please take another look.
theblacklion
 
Posts: 67
Joined: Sat Feb 05, 2011 10:07 pm
Location: Hannover, Germany

Re: Open Python files from selection or current line

Postby sfranky on Mon Aug 08, 2011 7:51 pm

hi!
can this be done for other languages as well? (e.g. LaTeX)
sfranky
 
Posts: 96
Joined: Mon Oct 19, 2009 10:06 pm


Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 5 guests

cron