1

我想创建sublime text 3可以移动到打开文件的最后一行的插件。现在我可以按它的号码去排队:

import sublime, sublime_plugin

class prompt_goto_lineCommand(sublime_plugin.WindowCommand):

    def run(self):
        self.window.show_input_panel("Goto Line:", "", self.on_done, None, None)
        pass

    def on_done(self, text):
        try:
            line = int(text)
            if self.window.active_view():
                self.window.active_view().run_command("goto_line", {"line": line} )
        except ValueError:
            pass

class go_to_lineCommand(sublime_plugin.TextCommand):

    def run(self, edit, line):
        # Convert from 1 based to a 0 based line number
        line = int(line) - 1

        # Negative line numbers count from the end of the buffer
        if line < 0:
            lines, _ = self.view.rowcol(self.view.size())
            line = lines + line + 1

        pt = self.view.text_point(line, 0)

        self.view.sel().clear()
        self.view.sel().add(sublime.Region(pt))

        self.view.show(pt)

但我不知道最后一行的数量。如何从对象中获取它sublime_plugin.WindowCommand?或者也许是另一种将光标移动到最后一行而不获取其编号的方法?我试图在api 文档中找到?但没有结果。

4

2 回答 2

3

对于那些不打算专门构建插件并且正在使用 Sublime Text 3 的人,您可能有兴趣了解ctrlend. 我也不太确定 Sublime Text 2 是否存在。

当您特别希望构建一个插件时,您可能会执行一些类似内置 Sublime Text 3 命令的操作,作为解决方案的替代go_to_line方案。

{ "keys": ["ctrl+end"], "command": "move_to", "args": {"to": "eof", "extend": false} }

于 2014-10-04T01:13:44.863 回答
2

中的代码go_to_lineCommand已经向您展示了如何计算最后一个行号。self.view.size()返回文件中的字符数。所以返回文档self.view.rowcol(self.view.size())中最后一个的行号和列号。point顺便说一句,AFAIK,apoint就像数组中的索引。

因此,您可以通过计算最后一行号来转到最后一行,或者只使用0作为行号。

view.run_command("go_to_line", {'line':'0'})
于 2013-10-30T05:56:12.453 回答