Home Download Buy Blog Forum Support

Click Keybindings

Click Keybindings

Postby Patrick Otten on Thu Jan 19, 2012 6:37 pm

I've made a plugin that allows me to copy the current file's path as a commented string into the clipboard. I then can paste this into a relevant spot in my code for easy file reference/access. I then created a plugin that is binded to super+shift+l that basically takes the line the cursor is currently on and checks to see if it is a path and if the path exists and then opens that file. In addition to placing the cursor at the line and then using the keybinding, I'd like to just be able to double click the line and it would run the same plugin, like how double clicking works when you search for something in a project and it shows you all the results.

Is this possible?
Patrick Otten
 
Posts: 9
Joined: Wed Dec 21, 2011 6:43 pm

Re: Click Keybindings

Postby quarnster on Thu Jan 19, 2012 7:00 pm

Yes, create a file named "Default.sublime-mousemap" in your plugin directory looking something like this:

Code: Select all
[
    {
        "button": "button1", "count": 2,
        "press_command": "drag_select",
        "press_args": {"by": "words"},
        "command": "your_command_here"
    }
]


"your_command_here" will now run when double clicking.
quarnster
 
Posts: 389
Joined: Tue Nov 29, 2011 11:34 am

Re: Click Keybindings

Postby Patrick Otten on Thu Jan 19, 2012 7:25 pm

Awesome! Thank You. For some reason the count 2 wasn't working, but count 3 did as well as what I ended up going with which is 1 click with a ctrl+alt modifier.

Also, I had to use
Code: Select all
"press_args": {"by": "lines"}
instead of
Code: Select all
"press_args": {"by": "words"}


Here is the final:
Code: Select all
[   
    {
        "button": "button1", "count": 1,
        "modifiers": ["alt", "ctrl"],
        "press_command": "drag_select",
        "press_args": {"by": "lines"},
        "command": "your_command_here"
    }
]
Patrick Otten
 
Posts: 9
Joined: Wed Dec 21, 2011 6:43 pm

Re: Click Keybindings

Postby bblue on Mon Jan 23, 2012 12:08 am

Hi Patrick,
could you please share such a plug-in?
I'd appreciate it so much (I'm actually trying to do something similar…).

Many thanks in advance,
--
bblue
bblue
 
Posts: 26
Joined: Mon Oct 10, 2011 10:35 am

Re: Click Keybindings

Postby Patrick Otten on Tue Jan 24, 2012 6:41 pm

FilePathToClipboard.py
Code: Select all
import sublime_plugin
import subprocess

def getClipboardData():
    p = subprocess.Popen(['pbpaste'], stdout=subprocess.PIPE)
    data = p.stdout.read()
    return data

def setClipboardData(data):
    p = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
    p.stdin.write(data)
    p.stdin.close()

class FilePathToClipboardCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        setClipboardData("/* " + self.view.file_name().replace("/Users/your_username", "~") + " */")


Keybinding I setup:
Code: Select all
{ "keys": ["super+shift+c"], "command": "file_path_to_clipboard" }


OpenPaths.py
Code: Select all
import sublime_plugin
import os
from os import path, access, R_OK  # W_OK for write permission.
import re


class OpenWordFilePathCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        searchPath = self.view.file_name().strip(path.basename(self.view.file_name()))
        searchPath = searchPath.strip("controllers/")
        myFile = None
        for region in self.view.sel():
            if not region.empty():
                myFile = self.view.substr(region).strip(" \n\t\r")
                myFile += ".php"

        for subdir, dirs, files in os.walk("/" + searchPath + "/"):
            for file in files:
                if file == myFile:
                    myFilePath = os.path.join(subdir, file)
                    print myFilePath
                    if path.exists(myFilePath) and path.isfile(myFilePath) and access(myFilePath, R_OK):
                        self.view.window().open_file(myFilePath)
                    else:
                        print "Either file is missing or is not readable"


class OpenLineFilePathCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        for region in self.view.sel():
            if not region.empty():
                str1 = self.view.substr(region).strip(" \n\t\r")
            else:
                line = self.view.line(region)
                str1 = self.view.substr(line).strip(" \n\t\r")
            r = re.compile('/\*(.*?)\*/')
            m = r.search(str1)
            if m:
                filePath = m.group(1).strip(" \n\t\r")
                filePath = path.expanduser(filePath)
                if path.exists(filePath) and path.isfile(filePath) and access(filePath, R_OK):
                    self.view.window().open_file(filePath)
                else:
                    print "Either file is missing or is not readable"
            else:
                print "This is not a file path"


The OpenLineFilePathCommand will open a complete path that the FilePathToClipboardCommand gives you.

The OpenWordFilePathCommand basically moves back a directory to look in my views directory for the one word file I've selected. I did this because it works for most cases in my workflow and removed the need to have a commented file path in my code most of the time.

Here is the mousemap file
Code: Select all
[   
    {
        "button": "button1", "count": 1,
        "modifiers": ["alt", "ctrl"],
        "press_command": "drag_select",
        "press_args": {"by": "words"},
        "command": "open_word_file_path"
    },
    {
        "button": "button1", "count": 1,
        "modifiers": ["alt", "ctrl", "super"],
        "press_command": "drag_select",
        "press_args": {"by": "lines"},
        "command": "open_line_file_path"
    }
]


All of this stuff may only work for Mac. I don't use windows so I haven't checked to see if it works.
Patrick Otten
 
Posts: 9
Joined: Wed Dec 21, 2011 6:43 pm

Re: Click Keybindings

Postby bblue on Tue Jan 24, 2012 10:54 pm

Thank you so much!
bblue
 
Posts: 26
Joined: Mon Oct 10, 2011 10:35 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 9 guests