我有一个适用于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
方法。
我尝试了以下操作:
- 在 Google 上查找:Sublime 插件查找和选择文本类似于 Vim
- 检查 API
但仍然没有结果。
iced coffee script
中有以下代码:a = (require './c/d/e/file.js').doMethod()
。我将光标移动到此行,按下快捷键,这个 Sublime 插件就会打开文件file.js
并为我选择doMethod
方法。 - Maxim Yefremovview#find
和view#find_all
。它们分别返回一个区域和一个区域数组。当然,最终结果与lhuang提供的插件相同。 - skuroda