Sublime Forum

Get current line number or selection

#1

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

[code]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[/code]

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?

0 Likes

#2

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

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:

sublime.status_message("line: " + str(line) + " git blame " + file_name)
0 Likes

#3

Great, thanks Jon!

0 Likes