在Sublime Text Python插件中移动光标

时间:2015-07-14 19:26:56

标签: python sublimetext

我为Sublime Text写了一个简单的插件,它在光标位置插入标签或将它们包装在选定的文本周围:

import sublime, sublime_plugin
class mySimpleCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    sels = self.view.sel()
    for sel in sels:
      sel.start = sel.a if (sel.a < sel.b) else sel.b
      sel.end = sel.b if (sel.a < sel.b) else sel.a
      insert1Length = self.view.insert(edit, sel.start, '<tag>')
      self.view.insert(edit, sel.end + insert1Length, '</tag>')

但是如何在插入标签后移动光标?我查看了https://www.sublimetext.com/docs/2/api_reference.html和几个示例插件中的API文档,但仍然无法解决这个愚蠢的问题。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我遇到了同样的问题 - 在向其添加文本后将光标移动到插件中一行的末尾。 我用sergioFC的暗示修正了它:

# Place cursor at the end of the line
self.view.run_command("move_to", {"to": "eol"})

适合我。

答案 1 :(得分:0)

这是一个如何将光标移动到行尾的示例。概括起来应该很明显!

参考API reference

import sublime
import sublime_plugin


class MoveToEolCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # get the current "selection"
        sel = self.view.sel()

        # get the first insertion point, i.e. the cursor
        cursor_point = sel[0].begin()

        # get the region of the line we're on
        line_region = self.view.line(cursor_point)

        # clear the current selection as we're moving the cursor
        sel.clear()

        # set the selection to an empty region at the end of the line
        # i.e. move the cursor to the end of the line
        sel.add(sublime.Region(line_region.end(), line_region.end()))