예제 #1
0
    def regex_findall(self, find, flags, replace, extractions, literal=False, sel=None):
        """Findall with regex."""

        regions = []
        offset = 0
        if sel is not None:
            offset = sel.begin()
            bfr = self.view.substr(sublime.Region(offset, sel.end()))
        else:
            bfr = self.view.substr(sublime.Region(0, self.view.size()))
        flags |= re.MULTILINE
        if literal:
            find = re.escape(find)
        if self.extend and not literal:
            pattern = bre.compile_search(find, flags)
            self.template = bre.compile_replace(pattern, replace)
        else:
            pattern = re.compile(find, flags)
        for m in pattern.finditer(bfr):
            regions.append(sublime.Region(offset + m.start(0), offset + m.end(0)))
            if self.plugin is not None:
                extractions.append(self.on_replace(m))
            else:
                extractions.append(self.expand(m, replace))
        return regions
예제 #2
0
    def apply_scope_regex(self, string, re_find, replace, greedy_replace, multi):
        """Apply regex on a scope."""

        replaced = 0
        extraction = string

        scope_repl = ScopeRepl(self.plugin, replace, self.expand, self.on_replace)
        if self.extend:
            pattern = bre.compile_search(re_find)
            self.template = bre.compile_replace(pattern, replace)
        else:
            pattern = re.compile(re_find)
        if multi and not self.find_only and self.action is None:
            extraction, replaced = self.apply_multi_pass_scope_regex(
                pattern, extraction, scope_repl.repl, greedy_replace
            )
        else:
            if greedy_replace:
                extraction, replaced = pattern.subn(scope_repl.repl, string)
            else:
                extraction, replaced = pattern.subn(scope_repl.repl, string, 1)
        return extraction, replaced
예제 #3
0
    def scope_apply(self, pattern):
        """Find and replace based on scope."""

        # Initialize replacement variables
        replaced = 0
        regions = []

        # Grab pattern definitions
        scope = pattern['scope']
        find = pattern['find'] if 'find' in pattern else None
        replace = pattern['replace'] if 'replace' in pattern else '\\0'
        greedy_scope = bool(pattern['greedy_scope']) if 'greedy_scope' in pattern else True
        greedy_replace = bool(pattern['greedy_replace']) if 'greedy_replace' in pattern else True
        case = bool(pattern['case']) if 'case' in pattern else True
        multi = bool(pattern['multi_pass_regex']) if 'multi_pass_regex' in pattern else False
        literal = bool(pattern['literal']) if 'literal' in pattern else False
        dotall = bool(pattern['dotall']) if 'dotall' in pattern else False
        self.plugin = pattern.get("plugin", None)
        self.plugin_args = pattern.get("args", {})

        if scope is None or scope == '':
            return replace

        if self.selection_only:
            sels = self.view.sel()
            sel_start = []
            sel_size = []
            for s in sels:
                sel_start.append(s.begin())
                sel_size.append(s.size())

        regions = self.view.find_by_selector(scope)

        if self.selection_only:
            regions = self.filter_by_selection(regions)[0]

        # Find supplied?
        if find is not None:
            # Compile regex: Ignore case flag?
            if not literal:
                try:
                    flags = 0
                    if not case:
                        flags |= re.IGNORECASE
                    if dotall:
                        flags |= re.DOTALL
                    if self.extend:
                        re_find = bre.compile_search(find, flags)
                    else:
                        re_find = re.compile(find, flags)
                except Exception as err:
                    print(str(traceback.format_exc()))
                    error('REGEX ERROR: %s' % str(err))
                    return replaced

                # Greedy Scope?
                if greedy_scope:
                    replaced = self.greedy_scope_replace(regions, re_find, replace, greedy_replace, multi)
                else:
                    replaced = self.non_greedy_scope_replace(regions, re_find, replace, greedy_replace, multi)
            else:
                if greedy_scope:
                    replaced = self.greedy_scope_literal_replace(regions, find, replace, greedy_replace)
                else:
                    replaced = self.non_greedy_scope_literal_replace(regions, find, replace, greedy_replace)
        else:
            replaced = self.select_scope_regions(regions, greedy_scope)

        if self.selection_only:
            new_sels = []
            count = 0
            offset = 0
            for s in sels:
                r = sublime.Region(sel_start[count] + offset, s.end())
                new_sels.append(r)
                offset += r.size() - sel_size[count]
                count += 1
            sels.clear()
            sels.add_all(new_sels)

        return replaced