Ejemplo n.º 1
0
    def get_selection_from_region(region: sublime.Region, regions_length: int,
                                  view: sublime.View) -> sublime.Region:
        entire_file = False
        if region.empty() and regions_length > 1:
            return None, None
        elif region.empty() and s.get("use_entire_file_if_no_selection", True):
            region = sublime.Region(0, view.size())
            entire_file = True

        return region, entire_file
Ejemplo n.º 2
0
def get_block(view: sublime.View, s: sublime.Region) -> (str, sublime.Region):
    """Get the code block under the cursor.

    The code block is the lines satisfying the following conditions:

      - Includes the line under the cursor.
      - Includes no blank line.
      - More indented than that of the line under the cursor.

    If `s` is a selected region, the code block is it.
    """
    if not s.empty():
        return (view.substr(s), s)
    view_end_row = view.rowcol(view.size())[0]
    current_row = view.rowcol(s.begin())[0]
    current_indent = get_indent(view, current_row)
    start_point = 0
    for first_row in range(current_row, -1, -1):
        indent = get_indent(view, first_row)
        if (not indent.startswith(current_indent)
                or get_line(view, first_row).strip() == ''):
            start_point = view.text_point(first_row + 1, 0)
            break
    end_point = view.size()
    for last_row in range(current_row, view_end_row + 1):
        indent = get_indent(view, last_row)
        if (not indent.startswith(current_indent)
                or get_line(view, last_row).strip() == ''):
            end_point = view.text_point(last_row, 0) - 1
            break
    block_region = sublime.Region(start_point, end_point)
    return (view.substr(block_region), block_region)
Ejemplo n.º 3
0
    def run(self, edit, forward=None):
        assert forward in (True, False), 'forward must be set to True or False'

        count = self.view.settings().get('dired_project_count', 0)
        files = Region(self.view.text_point(3, 0),
                       self.view.text_point(count + 3 - 1, 0))

        if files.empty():
            return

        pt = self.view.sel()[0].a

        if files.contains(pt):
            # Try moving by one line.
            line = self.view.line(pt)
            pt = forward and (line.b + 1) or (line.a - 1)

        if not files.contains(pt):
            # Not (or no longer) in the list of files, so move to the closest edge.
            pt = (pt > files.b) and files.b or files.a

        # print(pt)
        line = self.view.line(pt)
        self.view.sel().clear()
        self.view.sel().add(Region(line.a, line.a))
Ejemplo n.º 4
0
def _get_text_object_tag(view, s: Region, inclusive: bool, count: int) -> Region:
    # When the active cursor position is on leading whitespace before a tag on
    # the same line then the start point of the text object is the tag.
    line = view.line(get_insertion_point_at_b(s))
    tag_in_line = view_find_in_range(view, '^\\s*<[^>]+>', line.begin(), line.end())
    if tag_in_line:
        if s.b >= s.a and s.b < tag_in_line.end():
            if s.empty():
                s.a = s.b = tag_in_line.end()
            else:
                s.a = tag_in_line.end()
                s.b = tag_in_line.end() + 1

    begin_tag, end_tag, _ = find_containing_tag(view, s.begin())
    if not (begin_tag and end_tag):
        return s

    # The normal method is to select a <tag> until the matching </tag>. For "at"
    # the tags are included, for "it" they are excluded. But when "it" is
    # repeated the tags will be included (otherwise nothing would change).
    if not inclusive:
        if s == Region(begin_tag.end(), end_tag.begin()):
            inclusive = True

    if inclusive:
        return Region(begin_tag.a, end_tag.b)
    else:
        return Region(begin_tag.b, end_tag.a)
def get_wrap_region(view: sublime.View, sel: sublime.Region,
                    config: Config) -> sublime.Region:
    "Returns region to wrap with abbreviation"
    if sel.empty():
        # No selection means user wants to wrap current tag container
        pt = sel.begin()
        ctx = emmet.get_tag_context(view, pt)
        if ctx:
            # Check how given point relates to matched tag:
            # if it's in either open or close tag, we should wrap tag itself,
            # otherwise we should wrap its contents
            open_tag = ctx.get('open')
            close_tag = ctx.get('close')

            if in_range(open_tag, pt) or (close_tag
                                          and in_range(close_tag, pt)):
                return sublime.Region(
                    open_tag.begin(), close_tag and close_tag.end()
                    or open_tag.end())

            if close_tag:
                r = sublime.Region(open_tag.end(), close_tag.begin())
                return utils.narrow_to_non_space(view, r)

    return utils.narrow_to_non_space(view, sel)
Ejemplo n.º 6
0
 def _validate_region(self, region: sublime.Region):
     "Validates abbreviation found in given region in current view"
     if region and not region.empty():
         prefix = self.options.get('prefix', '')
         abbr = self.view.substr(region)[len(prefix):]
         return emmet.validate(abbr, self.options)
     return None
Ejemplo n.º 7
0
def get_wrap_region(view: sublime.View, sel: sublime.Region,
                    config: Config) -> sublime.Region:
    "Returns region to wrap with abbreviation"
    if sel.empty():
        # No selection means user wants to wrap current tag container
        pt = sel.begin()
        ctx = emmet.get_tag_context(view, pt)
        if ctx:
            # Check how given point relates to matched tag:
            # if it's in either open or close tag, we should wrap tag itself,
            # otherwise we should wrap its contents
            open_tag = ctx.get('open')
            close_tag = ctx.get('close')

            if in_range(open_tag, pt) or (close_tag
                                          and in_range(close_tag, pt)):
                return sublime.Region(
                    open_tag.begin(), close_tag and close_tag.end()
                    or open_tag.end())

            if close_tag:
                r = sublime.Region(open_tag.end(), close_tag.begin())
                next_region = utils.narrow_to_non_space(view, r)

                # On the right side of tag contents, we should skip new lines only
                # and trim spaces at the end of line
                padding = view.substr(
                    sublime.Region(next_region.end(), r.end()))
                ix = padding.find('\n')
                end = next_region.end() + ix if ix != -1 else r.end()
                next_region = sublime.Region(next_region.begin(), end)

                return next_region

    return utils.narrow_to_non_space(view, sel, utils.NON_SPACE_LEFT)
Ejemplo n.º 8
0
 def apply_change(self, region: sublime.Region, replacement: str, edit: Any) -> None:
     if region.empty():
         self.view.insert(edit, region.a, replacement)
     else:
         if len(replacement) > 0:
             self.view.replace(edit, region, replacement)
         else:
             self.view.erase(edit, region)
Ejemplo n.º 9
0
    def apply_change(self, region: sublime.Region, replacement: str,
                     edit: Any) -> None:
        if (self.view.settings().get('default_line_ending') == 'unix'):
            replacement = re.sub(r'\r\n?', '\n', replacement)

        if region.empty():
            self.view.insert(edit, region.a, replacement)
        else:
            if len(replacement) > 0:
                self.view.replace(edit, region, replacement)
            else:
                self.view.erase(edit, region)
Ejemplo n.º 10
0
    def run(self, edit, forward=None):
        assert forward in (True, False), 'forward must be set to True or False'

        count = self.view.settings().get('dired_project_count', 0)
        files = Region(self.view.text_point(3, 0), self.view.text_point(count+3-1, 0))

        if files.empty():
            return

        pt = self.view.sel()[0].a
        line = self.next_line(forward, pt, files)

        self.view.sel().clear()
        self.view.sel().add(Region(line.a, line.a))
Ejemplo n.º 11
0
    def run(self, edit, forward=None):
        assert forward in (True, False), 'forward must be set to True or False'

        count = self.view.settings().get('dired_project_count', 0)
        files = Region(self.view.text_point(3, 0), self.view.text_point(count+3-1, 0))

        if files.empty():
            return

        pt = self.view.sel()[0].a
        line = self.next_line(forward, pt, files)

        self.view.sel().clear()
        self.view.sel().add(Region(line.a, line.a))
Ejemplo n.º 12
0
    def validate(self, region: sublime.Region=None):
        "Validates currently marked abbreviation"
        if region is None:
            region = get_region(self.view)

        if region and not region.empty():
            prefix = self.options.get('prefix', '')
            abbr = self.view.substr(region)[len(prefix):]
            self._data = emmet.validate(abbr, self.options)
            self.abbr_data.abbreviation = abbr
            self.region = region
            self.mark()
        else:
            self.reset()

        return self.valid
Ejemplo n.º 13
0
def get_cell(view: sublime.View,
             region: sublime.Region,
             *,
             logger=HERMES_LOGGER) -> (str, sublime.Region):
    """Get the code cell under the cursor.

    Cells are separated by markers defined in `cell_delimiter_pattern` in the config file.

    If `s` is a selected region, the code cell is it.
    """
    if not region.empty():
        return (view.substr(region), region)
    cell_delimiter_pattern = (sublime.load_settings(
        "Hermes.sublime-settings").get("cell_delimiter_pattern"))
    separators = view.find_all(cell_delimiter_pattern)
    r = sublime.Region(region.begin() + 1, region.begin() + 1)
    start_point = separators[bisect.bisect(separators, r) - 1].end() + 1
    end_point = separators[bisect.bisect(separators, r)].begin() - 1
    cell_region = sublime.Region(start_point, end_point)
    return (view.substr(cell_region), cell_region)
Ejemplo n.º 14
0
    def run(self, edit, forward=None):
        assert forward in (True, False), 'forward must be set to True or False'

        count = self.view.settings().get('dired_project_count', 0)
        files = Region(self.view.text_point(3, 0), self.view.text_point(count+3-1, 0))

        if files.empty():
            return

        pt = self.view.sel()[0].a

        if files.contains(pt):
            # Try moving by one line.
            line = self.view.line(pt)
            pt = forward and (line.b + 1) or (line.a - 1)

        if not files.contains(pt):
            # Not (or no longer) in the list of files, so move to the closest edge.
            pt = (pt > files.b) and files.b or files.a

        # print(pt)
        line = self.view.line(pt)
        self.view.sel().clear()
        self.view.sel().add(Region(line.a, line.a))
def get_wrap_region(view: sublime.View, sel: sublime.Region,
                    options: dict) -> sublime.Region:
    "Returns region to wrap with abbreviation"
    if sel.empty() and options.get('context'):
        # If there’s no selection than user wants to wrap current tag container
        ctx = options['context']
        pt = sel.begin()

        # Check how given point relates to matched tag:
        # if it's in either open or close tag, we should wrap tag itself,
        # otherwise we should wrap its contents
        open_tag = ctx.get('open')
        close_tag = ctx.get('close')

        if in_range(open_tag, pt) or (close_tag and in_range(close_tag, pt)):
            return sublime.Region(
                open_tag.begin(), close_tag and close_tag.end()
                or open_tag.end())

        if close_tag:
            r = sublime.Region(open_tag.end(), close_tag.begin())
            return utils.narrow_to_non_space(view, r)

    return sel