Carmax
Carmax

Reputation: 2917

Sublime Text Plugin - How do I get the start point of a particular line?

I'm experimenting creating a plugin for sublime text (3) after looking at a few references and tutorials.

As a simple exercise I thought I'd create a vbscript commenting plugin - it should insert an apostrophe at the beginning of each line of selected text.

So far I've managed iterate selected regions and split them into separate lines. But I'm having trouble finding the start point of each line. (Called lineStart in my code below).

I think that variable is needed to find the exact point at which to insert the apostrophe. How do I get that?

import sublime, sublime_plugin

class AspCommentCommand(sublime_plugin.TextCommand):
    def run(self, edit):

        for selectedRegion in self.view.sel():
            selection = self.view.substr(selectedRegion)
            for line in selection.split('\n'):
                lineStart = ?????
                self.view.insert(edit, lineStart, "'")

Update

Hooray. I have managed to iterate the selected lines, and insert an apostrophe at the beginning of each one, using this code:

class AspAddCommentCommand(sublime_plugin.TextCommand):
    def run(self, edit):

        for selectedRegion in self.view.sel():
            selectedLines = self.view.lines(selectedRegion)
            adjustBy = 0
            for line in selectedLines:
                 insertPoint = line.begin() + adjustBy
                 self.view.insert(edit, insertPoint, "'")
                 adjustBy += 1

But after inserting an apostrophe on the first line, I then need to adjust the insert point of the next line to allow for the extra apostrophe character.

Is there a better way of approaching this?

Upvotes: 2

Views: 866

Answers (1)

jsj
jsj

Reputation: 9381

In a Sublime Text 3 plugin, you can get the start of a known line using

self.view.text_point(line - 1, 0)

In the documentation, they refer to row not line. Row means zero-based line number.

Upvotes: 2

Related Questions