最佳解决方案:使用 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"
}]
}
编辑:这是一个示例结果: