【问题标题】:Sublime Text: select/edit all occurrences of variable *scoped to function*Sublime Text:选择/编辑所有出现的变量*作用域*
【发布时间】:2018-01-26 03:27:51
【问题描述】:

Sublime 提供以下功能:

  • 选择/编辑所有出现的变量(快速查找全部;在 Windows 上按 alt+F3)
  • 逐一选择每个匹配项,然后编辑总和(快速添加下一个;在 Windows 上按 ctrl+d)

我想要什么:

  • 选择/编辑函数范围内的所有匹配项

注意:我已阅读此相关链接 (Sublime Text: Select all instances of a variable and edit variable name),但没有看到有关如何将编辑限制在函数范围内的答案。

【问题讨论】:

  • 您使用的是哪种语言语法(突出显示)?根据答案,可能有办法编写一个小插件来做到这一点
  • @KeithHall 干杯,基思;我正在使用 PHP

标签: sublimetext3


【解决方案1】:

您可以创建一个相当简单的插件来执行此操作,使用“快速查找全部”,然后删除不在当前函数内的所有选择。

  • 从工具菜单 -> 开发人员 -> 新插件...
  • 将新选项卡的内容替换为以下内容:
import sublime
import sublime_plugin


class SelectWordInFunctionCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        prev_sel = [sel for sel in view.sel()]
        function_region = next((region for region in view.find_by_selector('meta.function') if region.contains(view.sel()[0])), None)
        if function_region:
            #word = view.expand_by_class(view.sel()[0].begin(), sublime.CLASS_WORD_START + sublime.CLASS_WORD_END)
            view.window().run_command('find_all_under')
            sel = [sel for sel in view.sel() if function_region.contains(sel)]
            view.sel().clear()
            view.sel().add_all(sel if any(sel) else prev_sel)
        else:
            view.window().status_message('Not inside a function')
  • 不要使用find_all_under 命令,而是使用select_word_in_function - 您可以创建一个键绑定来仅在函数定义中执行此操作:
{ "keys": ["alt+f3"], "command": "select_word_in_function", "context":
    [
        { "key": "selector", "operator": "equal", "operand": "meta.function", "match_all": true },
    ]
},

免责声明:这绝对适用于 ST build 3142 中的 PHP 和其他限定整个函数的语法,但是对于其他不能/不能的语法,可能需要使用不同的方法来检测函数的开始和结束位置不要这样。

【讨论】:

    猜你喜欢
    • 2013-05-26
    • 1970-01-01
    • 2012-08-23
    • 2014-08-29
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    相关资源
    最近更新 更多