def expand_partial_comments_with_jsx(view, region):
    begin = region.begin()
    end = region.end()

    if is_jsx_open_brace(view, begin) and is_comment(view, begin + 1):
        begin += 1

    if is_jsx_close_brace(view, begin) and is_comment(view, begin - 1):
        begin -= 1

    if is_jsx_open_brace(view, end - 1):
        end += 1

    if end < begin:
        end = begin

    return expand_partial_comments(view, Region(begin, end))
Ejemplo n.º 2
0
def generate_jsjsx_comment_punctuation_region(view,
                                              offset,
                                              include_one_whitespace=True):
    result = generate_comment_punctuation_region(view, offset,
                                                 include_one_whitespace)

    begin = result.begin()
    end = result.end()

    if is_jsx_open_brace(view, begin - 1):
        begin -= 1
    if is_jsx_close_brace(view, end + 1):
        end += 1

    return Region(begin, end)
Ejemplo n.º 3
0
def must_comment(view, region):
    non_whitespace_pos = search_non_whitespace(view,
                                               region,
                                               stop_on_line_feed=False)

    # The entire line is blank, a comment must be inserted in the region.
    if is_whitespace(view, non_whitespace_pos):
        return True

    # If it is the JSX open brace, we just need to check the next character.
    if is_jsx_open_brace(view, non_whitespace_pos):
        non_whitespace_pos += 1

    # If the cursor is at a comment, it means that the user wants to remove it.
    return not is_comment(view, non_whitespace_pos)
Ejemplo n.º 4
0
def generate_jsjsx_comment_punctuation_region(
    view, offset, include_one_whitespace=True
):
    result = generate_comment_punctuation_region(
        view, offset, include_one_whitespace
    )

    begin = result.begin()
    end = result.end()

    if is_jsx_open_brace(view, begin - 1):
        begin -= 1
    if is_jsx_close_brace(view, end + 1):
        end += 1

    return Region(begin, end)
Ejemplo n.º 5
0
def must_comment(view, region):
    """ Returns true if the region must be commented or not. """
    non_whitespace_pos = search_non_whitespace(
        view, region, stop_on_line_feed=False
    )

    # The entire line is blank, a comment must be inserted in the region.
    if is_whitespace(view, non_whitespace_pos):
        return True

    # If it is the JSX open brace, we just need to check the next character.
    if is_jsx_open_brace(view, non_whitespace_pos):
        non_whitespace_pos += 1

    # If the cursor is at a comment, it means that the user wants to remove it.
    return not is_comment(view, non_whitespace_pos)
Ejemplo n.º 6
0
def uncomment_region(view, edit, region):
    begin = region.begin()
    end = region.end() - 1

    # We will loop backwards, this means that it will hit the closing
    # punctuation for block comments first.
    i = end + 1
    while i > begin:
        i -= 1
        scopes = view.scope_name(i)

        # Not a punctuation, ignore it.
        if 'punctuation.definition.comment' not in scopes:
            continue

        # Found the second forward slash for the “// ” comment.
        if 'comment.line' in scopes:
            punctuation_region = generate_comment_punctuation_region(view, i)
            view.erase(edit, punctuation_region)
            i = punctuation_region.begin()
            continue

        # We found the beginning of the block comment first, this means that
        # there’s no end to it and we can easily remove it. It can be “/* ”,
        # “/** ”, “{/* ” or “{/** ”.
        if 'punctuation.definition.comment.begin' in scopes:
            punctuation_region = generate_jsjsx_comment_punctuation_region(
                view, i)

            view.erase(edit, punctuation_region)
            i = punctuation_region.begin()
            continue

        # We are looping backwards, so it is expected to find the closing
        # punctuation first which can be “ */” or “ */}”.
        possible_jsx_comment = False
        if i < view.size() and is_jsx_close_brace(view, i + 1):
            possible_jsx_comment = True

        closing_punctuation_region = generate_comment_punctuation_region(
            view, i)

        # Move the cursor 1 character after the beginning punctuation.
        i = scan_reverse(
            view, i,
            not_predicate(
                has_scope_predicate('punctuation.definition.comment.begin')))

        open_punctuation_region = generate_comment_punctuation_region(
            view, i - 1)

        # Correct the regions to include the JSX braces if necessary.
        if possible_jsx_comment:
            if is_jsx_open_brace(view, open_punctuation_region.begin() - 1):
                open_punctuation_region = Region(
                    open_punctuation_region.begin() - 1,
                    open_punctuation_region.end())
                closing_punctuation_region = Region(
                    closing_punctuation_region.begin(),
                    closing_punctuation_region.end() + 1)

        view.erase(edit, closing_punctuation_region)
        view.erase(edit, open_punctuation_region)

        # Move the cursor to the beginning of the block to “consume” it.
        i = open_punctuation_region.begin()
Ejemplo n.º 7
0
def uncomment_region(view, edit, region):
    begin = region.begin()
    end = region.end() - 1

    # We will loop backwards, this means that it will hit the closing
    # punctuation for block comments first.
    i = end + 1
    while i > begin:
        i -= 1
        scopes = view.scope_name(i)

        # Not a punctuation, ignore it.
        if 'punctuation.definition.comment' not in scopes:
            continue

        # Found the second forward slash for the “// ” comment.
        if 'comment.line' in scopes:
            punctuation_region = generate_comment_punctuation_region(view, i)
            view.erase(edit, punctuation_region)
            i = punctuation_region.begin()
            continue

        # We found the beginning of the block comment first, this means that
        # there’s no end to it and we can easily remove it. It can be “/* ”,
        # “/** ”, “{/* ” or “{/** ”.
        if 'punctuation.definition.comment.begin' in scopes:
            punctuation_region = generate_jsjsx_comment_punctuation_region(
                view, i
            )

            view.erase(edit, punctuation_region)
            i = punctuation_region.begin()
            continue

        # We are looping backwards, so it is expected to find the closing
        # punctuation first which can be “ */” or “ */}”.
        possible_jsx_comment = False
        if i < view.size() and is_jsx_close_brace(view, i + 1):
            possible_jsx_comment = True

        closing_punctuation_region = generate_comment_punctuation_region(
            view, i
        )

        # Move the cursor 1 character after the beginning punctuation.
        i = scan_reverse(view, i, not_predicate(has_scope_predicate(
            'punctuation.definition.comment.begin'
        )))

        open_punctuation_region = generate_comment_punctuation_region(
            view, i - 1
        )

        # Correct the regions to include the JSX braces if necessary.
        if possible_jsx_comment:
            if is_jsx_open_brace(view, open_punctuation_region.begin() - 1):
                open_punctuation_region = Region(
                    open_punctuation_region.begin() - 1,
                    open_punctuation_region.end()
                )
                closing_punctuation_region = Region(
                    closing_punctuation_region.begin(),
                    closing_punctuation_region.end() + 1
                )

        view.erase(edit, closing_punctuation_region)
        view.erase(edit, open_punctuation_region)

        # Move the cursor to the beginning of the block to “consume” it.
        i = open_punctuation_region.begin()