Home Download Buy Blog Forum Support

Open File from code

Open File from code

Postby jbrooksuk on Wed Feb 16, 2011 11:05 am

Hi all,

One of the things that I actually miss from Dreamweaver is the ability to open files when my cursor is on a file string, inside my code, pressing Ctrl+D will open the file in a new tab, allowing me to edit it.

I'd like to recreate this behaviour in ST, reducing the amount of clicking I have to do.

Any ideas on how I could do this? I'm thinking I'd have to set regions between two quotes, single and double, and check the structure of the text to see if it's a "file", then open it?

Yay, nay?

James
jbrooksuk
 
Posts: 775
Joined: Sun Apr 11, 2010 10:37 am
Location: England

Re: Open File from code

Postby adzenith on Wed Feb 16, 2011 5:04 pm

I'd do something like this:
Code: Select all
    self.view.run_command("expand_selection", {"to":"scope"})
    text = self.view.substr(self.view.sel()[0])
    self.view.run_command("show_overlay", {"overlay":"goto","show_files": True, "text": text})

...except I can't get self.view.run_command("show_overlay") to actually show an overlay. Maybe Jon can comment?
Also, this would only open the overlay with the filename pre-pasted in the box. If you want to open a file manually I think you'll need to actually do some path-wrangling.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Open File from code

Postby jbjornson on Fri Feb 18, 2011 1:37 am

The plugin below seems to work for me. If you have some text selected it will assume the selection is a file path. If no text is selected the it will grab the whole line and assume that is a file path. If the file doesn't exist then nothing happens. Theoretically it should work with multiple selections but i have never tried it.

Code: Select all
import sublime, sublime_plugin
import os.path

class OpenFilenameUnderCursor(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            if region.empty():
               line = self.view.line(region)
               filepath = self.view.substr(line).strip()
            else:
               filepath = self.view.substr(region)

            if os.path.isfile(filepath):
                print "Opening file '%s'" % (filepath)
                self.view.window().open_file(filepath)
            else:
                print "File does not exist: '%s'" % (filepath)


...using a key mapping something like this:

Code: Select all
    { "keys": ["alt+o"], "command": "open_filename_under_cursor" },
jbjornson
 
Posts: 350
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: Open File from code

Postby jbrooksuk on Fri Feb 18, 2011 8:10 am

jbjornson wrote:The plugin below seems to work for me. If you have some text selected it will assume the selection is a file path. If no text is selected the it will grab the whole line and assume that is a file path. If the file doesn't exist then nothing happens. Theoretically it should work with multiple selections but i have never tried it.

Code: Select all
import sublime, sublime_plugin
import os.path

class OpenFilenameUnderCursor(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            if region.empty():
               line = self.view.line(region)
               filepath = self.view.substr(line).strip()
            else:
               filepath = self.view.substr(region)

            if os.path.isfile(filepath):
                print "Opening file '%s'" % (filepath)
                self.view.window().open_file(filepath)
            else:
                print "File does not exist: '%s'" % (filepath)


...using a key mapping something like this:

Code: Select all
    { "keys": ["alt+o"], "command": "open_filename_under_cursor" },

Awesome :D

I'll be making a few adjustments, then I'll get it posted here :)
jbrooksuk
 
Posts: 775
Joined: Sun Apr 11, 2010 10:37 am
Location: England


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 7 guests