gpt4 book ai didi

plugins - 在 Sublime Text 2 插件中移动光标/点

转载 作者:行者123 更新时间:2023-12-02 14:24:50 25 4
gpt4 key购买 nike

我正在为 Sublime Text 编写一个插件,它可以将光标移动到文档的开头。

复古模式有一个针对此类事情的键绑定(bind):

{ "keys": ["g", "g"], "command": "set_motion", "args": {
"motion": "vi_goto_line",
"motion_args": {"repeat": 1, "explicit_repeat": true, "extend": true,
"ending": "bof" },
"linewise": true },
"context": [{"key": "setting.command_mode"}]
}

如何实现相同的效果或从插件调用相同的命令?

最佳答案

在默认插件文件夹中,有一个名为 goto_line.py 的插件,它几乎可以完成此操作。

import sublime, sublime_plugin

class PromptGotoLineCommand(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 GotoLineCommand(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)

关于plugins - 在 Sublime Text 2 插件中移动光标/点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12635644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com