Home Download Buy Blog Forum Support

Looping and replacing lines

Looping and replacing lines

Postby stephenl on Tue Sep 17, 2013 3:30 pm

I am trying to loop over a selection, grab the entire line and then replace the line with new text if a condition is met. I have figured out how to loop over the selected regions, grab each line, but I can't figure out how to replace the line with new text.

Here is the code I have so far:

Code: Select all
class TestingCommand(sublime_plugin.TextCommand):
    def run(self, edit):
            # Get a reference to the selections
            sel = self.view.sel()
            # Loop over the various selections
            for region in self.view.sel():
                # grab full lines regardless of where they start in the line
                line2 = self.view.line(region)
                # split them into individual lines
                lines = self.view.split_by_newlines(line2)
                for l in lines:
                    # return the contents as a string
                    s = self.view.substr(l)

Where the print statement is, I would like to replace the entire line. Loop and repeat. Not all lines would be changed or replaced.

Thanks for any help or direction.
Posts: 5
Joined: Sun Jun 10, 2012 3:40 am

Re: Looping and replacing lines

Postby adzenith on Thu Sep 19, 2013 1:37 am

Have you tried view.replace?
Posts: 1403
Joined: Mon Oct 19, 2009 9:12 pm

Re: Looping and replacing lines

Postby skuroda on Thu Sep 19, 2013 2:58 am

In addition to replace, be sure you start from the end of the list when doing your replace as to not mess up the positions stored by view.sel()
Posts: 692
Joined: Fri Oct 05, 2012 8:11 am

Re: Looping and replacing lines

Postby stephenl on Thu Sep 19, 2013 5:14 am

Thanks for the hint of starting backwards. I couldn't figure out how to not affect the positions.

Before I came across your hint I tried a different approach. I grabbed the line numbers of the selection, erased the contents of a line and then inserted the new content for the line and then moved to the new line. Code below:

Code: Select all
class DemotetCommand(sublime_plugin.TextCommand):
    def run(self, edit):
      ss = sublime.load_settings("Preferences.sublime-settings")
      ts = ss.get("tab_size",1)
      view = self.view
      sel = view.sel()
      line_nums = [view.rowcol(line.a)[0] for line in view.lines(sel[0])]
      for row in line_nums:
        pt = view.text_point(row, 0)
        line = view.line(pt)
        s = self.view.substr(line)
        s2 = s
        s =s.lstrip()
        if len(s) > 0:
            # check if the first character is a slash
            if s[0] == '\\':
                if int(s[1]) < 4:
                    new_num = (int(s[1])+1)
                    new_nums = str(new_num)
                    self.view.erase(edit, sublime.Region(pt + len(s2),pt))
                    self.view.insert(edit, pt, ' '*new_num*ts + s)
                    self.view.replace(edit, sublime.Region(pt+new_num*ts+1, pt+new_num*ts+2), new_nums)
Posts: 5
Joined: Sun Jun 10, 2012 3:40 am

Return to Technical Support

Who is online

Users browsing this forum: No registered users and 16 guests