【问题标题】:Sublime Text autocomplete window closes when scrolling off the list滚动列表时关闭 Sublime Text 自动完成窗口
【发布时间】:2014-10-05 11:49:02
【问题描述】:

当使用 updown 滚动自动完成列表时,如果您在任一方向上走得太远(例如没有更多建议),列表将关闭。

我想要的行为是在到达末尾而不是关闭时让列表换行。

通过分配这个热键向下滚动很容易解决这个问题:

{ "keys": ["down"], "command": "auto_complete", "context":
    [ { "key": "auto_complete_visible" } ]
},

这是因为auto_complete 命令具有每次调用时向下滚动的内置功能,这就是热键起作用的原因。

...但是向上滚动是不同的。我尝试了大约 20 种不同的热键和宏组合,但均未成功。

我几乎可以肯定实现这种行为的唯一方法是使用插件,但不幸的是我的 Python 技能水平为零。

如果重要的话,我会手动调用自动完成功能 ctrl+space(自动弹出窗口被禁用)。

我正在使用 Sublime Text 2。

【问题讨论】:

  • 试用插件时请告诉我。
  • 我刚刚测试过它,它也适用于 Sublime Text 2。
  • 抱歉耽搁了。今天下午我会试试你的插件。如果它很好,我会按照你的方式滑动这 50 分。感谢您的努力!
  • 现在有一种使用新的auto_complete_cycle 设置的简单易用的方法。我已经更新了我的答案。

标签: autocomplete sublimetext2 sublimetext3 sublimetext


【解决方案1】:

最佳解决方案:使用 auto_complete_cycle 设置(2015 年 3 月 26 日添加):

请使用这个新的简单解决方案,而不是 python 插件

2015 年 3 月 24 日发布的 Sublime Text 新版本有一个名为 auto_complete_cycle 的新设置来实现此行为。将其设置为 true 以遍历自动完成结果。

"auto_complete_cycle": true

最糟糕的旧解决方案:这个自定义插件

我刚刚制作了这个插件,它在 Linux Mint 中的 Sublime Text 3 上运行良好。我没有在 Sublime Text 2 中测试过它,但认为插件系统是一样的,所以它也应该在那个版本上工作。使用的解决方法不是很漂亮,但很有效。

import sublime, sublime_plugin

class UpArrowInAutoCompleteCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.settings().set('autoCompleteFlag',True)
        self.view.settings().set('initialPoint', self.view.sel()[0].begin())

        """ Move one line up """
        self.view.run_command('move', {"by": "lines", "forward": False});
        """ Auto-complete was opened and up arrow was pressed, so if the cursor changes
        (on_selection_modified will be triggered) we have gone outside the list. 
        If we were not in the first element on_selection_modified will not be triggered, so
        we turn of the flag"""
        sublime.set_timeout(lambda: self.view.settings().set('autoCompleteFlag', False),300)


class AutoCompleteSelectionModifiedTriggerCommand(sublime_plugin.EventListener):
    def on_selection_modified(self, view):
        if view.settings().get('autoCompleteFlag'):
            """ If the up arrow was pressed and on_selection_modified 
            has been triggered, then we know that we were in the first element
            of the list and we hitted the up arrow"""
            view.settings().set('autoCompleteFlag', False)
            initialPoint = view.settings().get('initialPoint')

            """ We don't know how many words the auto_complete has, so, 
            in order to calculate that number, we move down in the list
            till we get outside the list. After that we make the list appear
            again and move down n-1 times to go (and stay) to the last line """
            view.sel().clear()
            view.sel().add(initialPoint)
            view.run_command('auto_complete')

            numLines = 0
            while view.sel()[0].begin() == initialPoint:
                view.run_command('move', {"by": "lines", "forward": True})
                numLines += 1
                if numLines == 401:
                    return

            if numLines == 0:
                return

            view.sel().clear()
            view.sel().add(initialPoint)
            view.run_command('auto_complete')

            numLine = 0
            while numLine < (numLines-1):
                view.run_command('move', {"by": "lines", "forward": True})
                numLine += 1

要使插件使用Tools>new Plugin并粘贴代码。然后将其保存在 Packages/User 文件夹中。您可以使用 Preferences>Browse Packages 找到 Packages 文件夹,用户文件夹位于该文件夹中。

为了让它工作,我在我的用户键绑定文件中添加了这个绑定(第二个是你自己的绑定):

{
    "keys": ["up"],
    "command": "up_arrow_in_auto_complete",
    "context": [{
        "key": "auto_complete_visible",
        "operator": "equal",
        "operand": true
    }]
}, {
    "keys": ["down"],
    "command": "auto_complete",
    "context": [{
        "key": "auto_complete_visible"
    }]
}

编辑:这是一个示例结果:

【讨论】:

  • 它没有用。我尝试了 x86 和 x64 版本的 Sublime Text 2.0.1 和 2.0.2(总共 4 个不同的版本)。一旦向上滚动应该换行,光标就会消失,Sublime Text 需要单击鼠标才能重新获得焦点。以下是生成的两条错误消息:error-pic-1error-pic-2
  • 我无法重现该错误(在 Windows 7 上的 ST2.0.2 x64 和 Linux Mint 上的 ST3 x64 中测试)。那么,当您在列表的第一个元素中并按向上箭头时,列表永远不会换行?还是仅当出现该错误窗口时列表才换行?您是否尝试过禁用第二张图片所涉及的设置?
  • 当我找到空闲时间时,我一定会再试一次。第二张图片描述了这个插件。我不介意听到其他尝试过它的人的反馈。到目前为止,两个赞成票告诉我问题是我的,不知何故。
  • 它适用于 2.0.2!我的问题是 2.0.1(x86 和 x64)。我尝试了 2.0.2,但来自沙箱,这可能是我的问题。由于我还在使用 2.0.1,是时候升级了。我讨厌升级。感谢您的努力,非常感谢!
  • 加 1 - 对于图像示例,这给出了答案的提示
猜你喜欢
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多