Sublime Forum

Detecting Indentation and Shifting

#1

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

  • lines (which have a single indentation):

    [code]

    • One
    • Two
    • Three
    [/code]

    With this:

    <b>Some code to replace with</b>

    However, when I replace, I want the text to be indented just like the

  • 's were. Not because they are nested in a
      , but because the original text was simply indented once.

      Make sense? Thanks!

  • 0 Likes

    #2

    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.

    0 Likes

    #3

    Well, I’m replacing a range. So the user would select the 3 lines of

  • 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]


    Name:
    <%= @accessory.name %>

          <p>
            <b>Description:</b>
            <%= @accessory.description %>
          </p>
    
          <p>
            <b>Price:</b>
            <%= @accessory.price %>
          </p>[/code]
    

    I select and want to replace the entire code above with:

    <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.

  • 0 Likes