将选择扩展到自定义行

时间:2016-04-27 01:30:08

标签: sublimetext2 sublimetext3 sublimetext

我想知道是否有开箱即用的方式,或者是一个可以在SublimeText3中实现以下行为的插件。

我想将插入符号放在某一行。然后选择所有文本,直到另一个行号。行数应该是可变的。

例如,将插入符号设置为10,然后将选择范围扩展到第21行或第104行。

我讨厌按住键或使用鼠标进行此操作。

1 个答案:

答案 0 :(得分:3)

我写了一个简单的插件,允许您输入一行进行选择,直到通过input_panel

Demo

特点:

  • 双向工作
  • 尊重当前选择
  • 仅在有一个选择时执行

设置信息:

@ GitHub

代码:

import sublime, sublime_plugin

class SelectToLineCommand( sublime_plugin.TextCommand ):

    def run( self, edit ):

        window     = self.view.window()
        selections = self.view.sel()

        if len( selections ) != 1:
            return

        self.currentSelection = selections[0]

        if self.currentSelection.a > self.currentSelection.b:
            self.currentSelection = sublime.Region( self.currentSelection.b, self.currentSelection.a )

        window.show_input_panel( "Select To Line Number", "", self.get_LineNumber, None, None )

    def get_LineNumber( self, userInput ):

        lineToRow_Offset = 1
        row = int( userInput ) - lineToRow_Offset
        selectionEnd_Row = self.view.text_point( row, 0 )

        currentSelection = self.currentSelection

        if selectionEnd_Row >= currentSelection.b:
            selectionStart = currentSelection.a
            selectionEnd   = self.view.line( selectionEnd_Row ).b
        elif selectionEnd_Row < currentSelection.a:
            selectionStart = currentSelection.b
            selectionEnd   = self.view.line( selectionEnd_Row ).a

        newSelection = sublime.Region( selectionStart, selectionEnd )

        self.view.selection.clear()
        self.view.selection.add( newSelection )