Home Download Buy Blog Forum Support

Get current line number or selection

Get current line number or selection

Postby ehamiter on Wed May 18, 2011 1:19 pm

I'm wanting to pass the current line number as an argument in a plugin script. Like so:

Code: Select all
import sublime_plugin
import sublime
import os

class BlameCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if len(self.view.file_name()) > 0: 
            lines = self.view.rowcol(self.view.sel()[0].begin()) ## <- this does not work

            folder_name, file_name = os.path.split(self.view.file_name())
            self.view.window().run_command('exec', {'cmd': ['git', 'blame', '-L', lines, file_name], 'working_dir': folder_name})
         
            sublime.status_message("line: " + lines + " git blame " + file_name)
           
    def is_enabled(self):
        return self.view.file_name() and len(self.view.file_name()) > 0


I can manually change "lines" to something like "15,20" and it will return lines 15-20. I'd like to set lines to either the current line, or the selected lines. Any tips?
ehamiter
 
Posts: 67
Joined: Wed Apr 27, 2011 2:42 am

Re: Get current line number or selection

Postby jps on Tue May 24, 2011 10:28 pm

rowcol() returns a (row, column) tuple, so you'll want something along the lines of:

Code: Select all
line, column = self.view.rowcol(self.view.sel()[0].begin())


Because it returns an int, rather than a string, you'll have to explicitly convert it when concatenating with a string:

Code: Select all
sublime.status_message("line: " + str(line) + " git blame " + file_name)
jps
Site Admin
 
Posts: 3071
Joined: Wed Mar 19, 2008 12:33 pm

Re: Get current line number or selection

Postby ehamiter on Wed May 25, 2011 3:58 am

Great, thanks Jon!
ehamiter
 
Posts: 67
Joined: Wed Apr 27, 2011 2:42 am


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot] and 32 guests