我获得了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
的方法。
我试过
但仍然没有结果。
答案 0 :(得分:2)
我不确定这种典型的方式。但是,您可以通过以下方式实现此目的:
Region(start, end)
添加到选择中。示例:
def run(self, edit, target):
if not target or target == "":
return
content = self.view.substr(sublime.Region(0, self.view.size()))
begin = content.find(target)
if begin == -1:
return
end = begin + len(target)
target_region = sublime.Region(begin, end)
self.view.sel().clear()
self.view.sel().add(target_region)
答案 1 :(得分:2)
你在API中有它,使用view.find(regex,pos)方法。
s = self.view.find("go_to_lineCommand", 0)
self.view.sel().add(s)
答案 2 :(得分:0)
龙华的答案可能有所改进 - 将移动光标添加到目标线。
class FindcustomCommand(sublime_plugin.TextCommand):
def _select(self):
self.view.sel().clear()
self.view.sel().add(self._target_region)
def run(self, edit):
TARGET = 'http://nabiraem'
# if not target or target == "":
# return
content = self.view.substr(sublime.Region(0, self.view.size()))
begin = content.find(TARGET)
if begin == -1:
return
end = begin + len(TARGET)
self._target_region = sublime.Region(begin, end)
self._select()
self.view.show(self._target_region) # scroll to selection