Exemple #1
0
 def test(c):
     opts = TestConfig(wrap_column=c.wid, indent=c.ind)
     text = Text(c.text)
     sel = (0, len(c.text))
     ctok = c.comment if c.ind else None
     output = "\n".join(wraplines(iterlines(text, sel), opts, ctok))
     eq_(output, c.result)
Exemple #2
0
def indent_lines(editor, args):
    indent_mode = editor.document.indent_mode
    if indent_mode == const.INDENT_MODE_TAB:
        istr = "\t"
    else:
        istr = " " * editor.document.indent_size
    textview = editor.text_view
    sel = editor.selection
    text = editor.text
    if sel.length == 0:
        size = len(istr)
        if size == 1:
            seltext = istr
        else:
            line_start = text.line_range(sel)[0]
            seltext = istr[: size - (sel.location - line_start) % size]
        select = False
    else:

        def indent(line):
            if line.strip():
                return istr + line
            return line.lstrip(" \t")

        sel = text.line_range(sel)
        seltext = "".join(indent(line) for line in iterlines(text, sel))
        select = True
    if textview.shouldChangeTextInRange_replacementString_(sel, seltext):
        text[sel] = seltext
        textview.didChangeText()
        if select:
            editor.selection = (sel[0], len(seltext))
        else:
            textview.scrollRangeToVisible_(editor.selection)
Exemple #3
0
def wrap_selected_lines(editor, options):
    sel = editor.text.line_range(editor.selection)
    eol = editor.document.eol
    comment_token = re.escape(editor.document.comment_token)
    lines = iterlines(editor.text, sel)
    output = eol.join(wraplines(lines, options, comment_token))
    editor.put(output, sel, select=True)
Exemple #4
0
def indent_lines(textview, sender, args):
    indent_mode = textview.doc_view.document.indent_mode
    if indent_mode == const.INDENT_MODE_TAB:
        istr = "\t"
    else:
        istr = " " * textview.doc_view.document.indent_size
    sel = textview.selectedRange()
    text = textview.string()
    if sel.length == 0:
        size = len(istr)
        if size == 1:
            seltext = istr
        else:
            line_start = text.lineRangeForRange_(sel).location
            seltext = istr[:size - (sel.location - line_start) % size]
        select = False
    else:
        def indent(line):
            if line.strip():
                return istr + line
            return line.lstrip(" \t")
        sel = text.lineRangeForRange_(sel)
        seltext = "".join(indent(line) for line in iterlines(text, sel))
        select = True
    if textview.shouldChangeTextInRange_replacementString_(sel, seltext):
        textview.textStorage().replaceCharactersInRange_withString_(sel, seltext)
        textview.didChangeText()
        if select:
            textview.setSelectedRange_((sel[0], len(seltext)))
        else:
            textview.scrollRangeToVisible_((sel[0] + len(seltext), 0))
Exemple #5
0
    def test(c):
        m = Mocker()
        opts = "<options>"
        tv = m.mock(TextView)
        ts = tv.textStorage() >> m.mock(ak.NSTextStorage)
        wrap = m.replace(mod, 'wraplines')
        iterlines = m.replace("editxt.command.wraplines.iterlines")
        text = tv.string() >> fn.NSString.stringWithString_(c.text)
        sel = (0, len(text)) if c.sel is None else c.sel
        sel = text.lineRangeForRange_(tv.selectedRange() >> sel)
        eol = tv.doc_view.document.eol >> m.mock()
        lines = iterlines(text, sel) >> "<lines>"
        eol.join(wrap(lines, opts, tv) >> [c.result]) >> c.result
        tv.shouldChangeTextInRange_replacementString_(sel, c.result) >> True
        output = []

        def callback(range, text):
            output.append(text)

        expect(ts.replaceCharactersInRange_withString_(
            sel, c.result)).call(callback)
        tv.didChangeText()
        tv.setSelectedRange_((sel[0], len(c.result)))
        with m:
            wrap_selected_lines(tv, opts)
            eq_(c.result, output[0])
Exemple #6
0
def is_comment_range(text, range, comment_token):
    comments = 0
    for i, line in enumerate(iterlines(text, range)):
        if i > 1:
            break
        if line.strip().startswith(comment_token):
            comments += 1
    return comments == 2 or (i < 1 and comments)
Exemple #7
0
def is_comment_range(text, range, comment_token):
    comments = 0
    for i, line in enumerate(iterlines(text, range)):
        if i > 1:
            break
        if line.strip().startswith(comment_token):
            comments += 1
    return comments == 2 or (i < 1 and comments)
Exemple #8
0
def wrap_selected_lines(textview, options):
    text = textview.string()
    sel = text.lineRangeForRange_(textview.selectedRange())
    eol = textview.editor.document.eol
    lines = iterlines(text, sel)
    output = eol.join(wraplines(lines, options, textview))
    if textview.shouldChangeTextInRange_replacementString_(sel, output):
        textview.textStorage().replaceCharactersInRange_withString_(sel, output)
        textview.didChangeText()
        textview.setSelectedRange_((sel[0], len(output)))
Exemple #9
0
def wrap_selected_lines(textview, options):
    text = textview.string()
    sel = text.lineRangeForRange_(textview.selectedRange())
    eol = textview.doc_view.document.eol
    lines = iterlines(text, sel)
    output = eol.join(wraplines(lines, options, textview))
    if textview.shouldChangeTextInRange_replacementString_(sel, output):
        textview.textStorage().replaceCharactersInRange_withString_(
            sel, output)
        textview.didChangeText()
        textview.setSelectedRange_((sel[0], len(output)))
Exemple #10
0
def join_lines(editor, args):
    """Join selected lines into a single line"""
    if args is None:
        args = join_lines.arg_parser.default_options()
    eol = editor.document.eol
    if not editor.selection[1]:
        beep()
        return
    rng = editor.text.line_range(editor.selection)
    lines = iterlines(editor.text, rng)
    text = args.delimiter.join(_unsplit(lines, eol)) + eol
    editor.put(text, rng, select=True)
Exemple #11
0
 def test(c):
     m = Mocker()
     tv = m.mock(TextView)
     if c.ind:
         tv.doc_view.document.comment_token >> c.comment
     opts = TestConfig(wrap_column=c.wid, indent=c.ind)
     text = fn.NSString.stringWithString_(c.text)
     sel = (0, len(c.text))
     with m:
         if c._get("debug", False):
             import pdb; pdb.set_trace()
         output = "\n".join(wraplines(iterlines(text, sel), opts, tv))
         eq_(c.result, output)
Exemple #12
0
def split_text(editor, args):
    """Split the current line of (or selected) text into separate lines

    The current line is split if there is no selection. If there is a
    selection then all lines touched by the selection are split. The
    indentation level of the first line is used for all new lines.
    """
    if args is None:
        args = split_text.arg_parser.default_options()
    eol = editor.document.eol
    rng = editor.text.line_range(editor.selection)
    lines = iterlines(editor.text, rng)
    text = eol.join(_split(lines, args.pattern, eol)) + eol
    editor.put(text, rng, select=True)
Exemple #13
0
 def test(c):
     m = Mocker()
     tv = m.mock(TextView)
     if c.ind:
         tv.doc_view.document.comment_token >> c.comment
     opts = TestConfig(wrap_column=c.wid, indent=c.ind)
     text = fn.NSString.stringWithString_(c.text)
     sel = (0, len(c.text))
     with m:
         if c._get("debug", False):
             import pdb
             pdb.set_trace()
         output = "\n".join(wraplines(iterlines(text, sel), opts, tv))
         eq_(c.result, output)
Exemple #14
0
def _comment_text(editor, args, pad):
    textview = editor.text_view
    text = editor.text
    sel = text.line_range(editor.selection)
    comment_token = editor.document.comment_token
    if not comment_token:
        return
    if is_comment_range(text, sel, comment_token):
        func = uncomment_line
    else:
        func = comment_line
    args = (comment_token, editor.document.indent_mode, editor.document.indent_size, pad)
    seltext = "".join(func(line, *args) for line in iterlines(text, sel))
    if textview.shouldChangeTextInRange_replacementString_(sel, seltext):
        editor.text[sel] = seltext
        editor.selection = (sel[0], len(seltext))
        textview.didChangeText()
Exemple #15
0
def _comment_text(textview, sender, args, pad):
    text = textview.string()
    sel = text.lineRangeForRange_(textview.selectedRange())
    comment_token = textview.doc_view.document.comment_token
    if is_comment_range(text, sel, comment_token):
        func = uncomment_line
    else:
        func = comment_line
    args = (
        comment_token,
        textview.doc_view.document.indent_mode,
        textview.doc_view.document.indent_size,
        pad,
    )
    seltext = "".join(func(line, *args) for line in iterlines(text, sel))
    if textview.shouldChangeTextInRange_replacementString_(sel, seltext):
        textview.textStorage().replaceCharactersInRange_withString_(sel, seltext)
        textview.setSelectedRange_((sel[0], len(seltext)))
        textview.didChangeText()
Exemple #16
0
def grab(editor, args):
    """Collect lines matching a pattern"""
    if args is None:
        from editxt.commands import show_command_bar
        show_command_bar(editor, "grab ")
        return
    elif args.pattern is None:
        raise CommandError("please specify a pattern to match")
    if args.invert:
        norm = lambda m: not m
    else:
        norm = lambda m: m
    scope = editor.selection if args.scope == "selection" else (0,)
    regex = re.compile(args.pattern, args.pattern.flags)
    lines = []
    for line in iterlines(editor.document.text, scope):
        if norm(regex.search(line)):
            lines.append(line)
    if lines:
        return "".join(lines)
    beep()
Exemple #17
0
def dedent_lines(textview, sender, args):
    def dedent(line, spt=textview.doc_view.document.indent_size):
        if not line.strip():
            return line.lstrip(" \t")
        if line.startswith("\t"):
            return line[1:]
        remove = 0
        linelen = len(line)
        for i in range(spt):
            if i < linelen and line[i] == " ":
                remove += 1
            else:
                break
        return line[remove:]
    text = textview.string()
    sel = text.lineRangeForRange_(textview.selectedRange())
    seltext = "".join(dedent(line) for line in iterlines(text, sel))
    if len(seltext) != sel.length:
        if textview.shouldChangeTextInRange_replacementString_(sel, seltext):
            textview.textStorage().replaceCharactersInRange_withString_(sel, seltext)
            textview.setSelectedRange_((sel[0], len(seltext)))
            textview.didChangeText()
Exemple #18
0
    def test(c):
        m = Mocker()
        opts = "<options>"
        editor = m.mock()
        wrap = m.replace(mod, 'wraplines')
        iterlines = m.replace("editxt.command.wraplines.iterlines")
        text = Text(c.text)
        (editor.text << text).count(2)
        sel = (0, len(text)) if c.sel is None else c.sel
        sel = text.line_range(editor.selection >> sel)
        eol = editor.document.eol >> m.mock()
        tok = editor.document.comment_token >> "x"
        lines = iterlines(text, sel) >> "<lines>"
        eol.join(wrap(lines, opts, tok) >> [c.result]) >> c.result
        output = []

        def callback(text, range, select):
            output.append(text)
        (editor.put(c.result, sel, select=True) << True).call(callback)
        with m:
            wrap_selected_lines(editor, opts)
            eq_(c.result, output[0])
Exemple #19
0
 def test(c):
     m = Mocker()
     opts = "<options>"
     tv = m.mock(TextView)
     ts = tv.textStorage() >> m.mock(ak.NSTextStorage)
     wrap = m.replace(mod, 'wraplines')
     iterlines = m.replace("editxt.command.wraplines.iterlines")
     text = tv.string() >> fn.NSString.stringWithString_(c.text)
     sel = (0, len(text)) if c.sel is None else c.sel
     sel = text.lineRangeForRange_(tv.selectedRange() >> sel)
     eol = tv.doc_view.document.eol >> m.mock()
     lines = iterlines(text, sel) >> "<lines>"
     eol.join(wrap(lines, opts, tv) >> [c.result]) >> c.result
     tv.shouldChangeTextInRange_replacementString_(sel, c.result) >> True
     output = []
     def callback(range, text):
         output.append(text)
     expect(ts.replaceCharactersInRange_withString_(sel, c.result)).call(callback)
     tv.didChangeText()
     tv.setSelectedRange_((sel[0], len(c.result)))
     with m:
         wrap_selected_lines(tv, opts)
         eq_(c.result, output[0])
Exemple #20
0
def dedent_lines(editor, args):
    def dedent(line, spt=editor.document.indent_size):
        if not line.strip():
            return line.lstrip(" \t")
        if line.startswith("\t"):
            return line[1:]
        remove = 0
        linelen = len(line)
        for i in range(spt):
            if i < linelen and line[i] == " ":
                remove += 1
            else:
                break
        return line[remove:]

    textview = editor.text_view
    text = editor.text
    sel = text.line_range(editor.selection)
    seltext = "".join(dedent(line) for line in iterlines(text, sel))
    if len(seltext) != sel.length:
        if textview.shouldChangeTextInRange_replacementString_(sel, seltext):
            text[sel] = seltext
            editor.selection = (sel[0], len(seltext))
            textview.didChangeText()