在升华文本Python插件中移动光标

2024-03-28 09:34:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我为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文档和几个示例插件,但仍然未能解决这个愚蠢的问题。有人能帮忙吗?在


Tags: 标记self插件viewifeditpluginstart
2条回答

下面是一个如何将光标移动到行尾的示例。应该怎样概括才是显而易见的!在

关于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()))

我也遇到了同样的问题——在插件中添加文本后,将光标移到一行的末尾。 我用塞尔吉奥夫的暗示修复了它:

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

对我有用。在

相关问题 更多 >