Search code examples
pythonpython-3.xsublimetext3sublimetextsublime-text-plugin

sublime plugin - get region or delete word


I'm new in Sublime Plugin development.

Here is what I want to do

  • First write something
  • Then remove or replace something(removing would be better)

So here is my code :

class MyCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        print("load")
        self.view.insert(edit, 0, "test")
        self.view.replace(edit, 0, "te") #self.view.replace(edit, region, "te")

I run this command on shell :

view.run_command("my")

Now it gives me error.

How can I resolve this ?

How can I get region or just remove some word?


Solution

  • The view.insert() method takes as an argument the point in the document at which you would like to perform the insert:

    Insert Help

    A point is literally just an offset (in characters) into the document; so a point of 0 is the very first thing in the document, 2 would be two characters in, and so on. That part is working for you.

    The part that's broken is the call to view.replace(), which is giving you this error (though the path will be different):

    Traceback (most recent call last):
      File "C:\Program Files\Sublime Text 3\sublime_plugin.py", line 1088, in run_
        return self.run(edit)
      File "C:\Users\micro\AppData\Roaming\Sublime Text 3\Packages\User\untitled.py", line 9, in run
        self.view.replace(edit, 0, "te") #self.view.replace(edit, region, "te")
      File "C:\Program Files\Sublime Text 3\sublime.py", line 837, in replace
        sublime_api.view_replace(self.view_id, edit.edit_token, r, text)
    TypeError: Region required
    

    The reason for that is that the view.replace() method replaces a section of text with your string; thus you can't provide it with just a single position because that's not enough:

    Replace help

    Your call is providing a point, which is not expected and thus you get an error. You instead need to provide a Region instead:

    Region documentation

    The Region class wraps a start (a) and end (b) position that reference a span of text in the buffer. A region can be "forward" if b is greater than a or "backward" if b is less than a. An example of that is selected text. If the cursor is on the right hand side of the selection, it's "forward", but if the cursor is on the left side of the selection, it's "backward".

    It's also possible for a region to be empty, in which case the a and b values are the same. The length of such a region is 0 because it doesn't span any text. An example of that is just a cursor sitting in your document with no selection; it's representing a selection that starts and stops at the same point.

    You can construct a region using the constructor above; depending on what you want to do, you need to construct the region in a different way.

    For example, you can replace the line you have above with this:

    self.view.replace(edit, sublime.Region(0), "te")
    

    That tells Sublime to replace the text starting at position 0 and spanning through to position 0 with the text te. In this case that will just insert the characters at the start of the buffer, because the region is empty (and hence it's replacing nothing with something).

    You could also do this:

    self.view.replace(edit, sublime.Region(0, 4), "te")
    

    Now the region spans the characters 0 through 4, so it will replace the test you inserted with just te (i.e. it looks like you erased the last two characters of test.

    If your goal is purely to remove text, then you want view.erase() instead:

    Erase help

    As seen here, you give the erase method a region and it removes all of the text in that region. So if you wanted to remove the text test that you added, you would do:

    self.view.erase(edit, sublime.Region(0, 4))
    

    Now the result of the command is as if it never inserted anything at all.