【发布时间】:2013-11-12 08:53:49
【问题描述】:
我得到了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)
我想改进它,让我将光标移动到包含指定字符串的第一行。这就像在文件中搜索:
例如,如果传递给它的字符串"class go_to_lineCommand" 插件必须将光标移动到第 17 行:
并可能选择字符串class go_to_lineCommand。
问题归结为找到regionWithGivenString,然后我可以选择它:
self.view.sel().add(regionWithGivenString)
但不知道获取regionWithGivenString的方法。
我试过了
- 在谷歌上找到:sublime plugin find and select text
- 检查api
但还是没有结果。
【问题讨论】:
-
你为什么需要插件呢?为什么不能只使用 Ctrl+G/Ctrl+F?
-
它只是用于快速导航的大插件的一部分:想象一下我在
iced coffee script:a = (require './c/d/e/file.js').doMethod()中获得了代码。我将光标移到这一行,按快捷键,这个 sublime 插件会打开文件file.js并为我选择doMethod方法。 -
我知道您已经有了解决方案,但只是想我也会在文档方面为您指明正确的方向。看看
view#find和view#find_all。它们分别返回一个区域和一个区域数组。当然,最终结果会和 lhuang 提供的插件一样。
标签: python plugins sublimetext2 sublimetext sublimetext3