Home Download Buy Blog Forum Support

Detecting Indentation and Shifting

Detecting Indentation and Shifting

Postby wesf90 on Sun Jul 15, 2012 8:11 pm

When using self.view.replace, what is the simplest way to replace one group of text with another, but keep the same base indentation?

For example, replace the <li> lines (which have a single indentation):
Code: Select all
<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
</ul>


With this:
Code: Select all
<b>Some code to replace with</b>


However, when I replace, I want the text to be indented just like the <li>'s were. Not because they are nested in a <ul>, but because the original text was simply indented once.

Make sense? Thanks!
wesf90
 
Posts: 5
Joined: Sun Jul 15, 2012 12:39 am

Re: Detecting Indentation and Shifting

Postby C0D312 on Sun Jul 15, 2012 9:40 pm

You have a couple of options. You could use regex to preserve the space and tabs or you could try running the reindent command after the replacing.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Detecting Indentation and Shifting

Postby wesf90 on Sun Jul 15, 2012 10:28 pm

Well, I'm replacing a range. So the user would select the 3 lines of <li> with the cursor, then the .replace function would run. How would I regex or re-indent this since it's in a range?

Let's say this is my original text:
Code: Select all
          <p>
            <b>Name:</b>
            <%= @accessory.name %>
          </p>

          <p>
            <b>Description:</b>
            <%= @accessory.description %>
          </p>

          <p>
            <b>Price:</b>
            <%= @accessory.price %>
          </p>


I select and want to replace the entire code above with:
Code: Select all
<b>Hello!</b>


However, see how the original code is indented? I want the same amount of indention prepended to the replacement text. Hopefully that is a little clearer than the example in my previous post.

Looking in ST2's Default/indentation.py package, there are some ideas, but I'm not sure of the best way to implement them into my own plugin. Can they be executed into my plugin? If so, I'm not sure how nor which function to call necessarily. The plugin is finished, I just want to try and polish it up before putting it in my repo.
wesf90
 
Posts: 5
Joined: Sun Jul 15, 2012 12:39 am


Return to Plugin Development

Who is online

Users browsing this forum: Majestic-12 [Bot] and 6 guests