Ejemplo n.º 1
0
    def compare(self, first, second, scope_bracket=False):
        """
        Compare brackets.  This function allows bracket plugins to add aditional logic.
        """

        if scope_bracket:
            match = first is not None and second is not None
        else:
            match = first.type == second.type

        if not self.rules.check_compare:
            return match

        if match:
            bracket = self.rules.scopes[first.scope]["brackets"][
                first.type] if scope_bracket else self.rules.brackets[
                    first.type]
            try:
                if bracket.compare is not None and match:
                    match = bracket.compare(
                        bracket.name,
                        bh_plugin.BracketRegion(first.begin, first.end),
                        bh_plugin.BracketRegion(second.begin, second.end),
                        self.search.get_buffer())
            except:
                log("Plugin Compare Error:\n%s" % str(traceback.format_exc()))
        return match
Ejemplo n.º 2
0
    def parse_scope_definition(self, language, loaded_modules):
        """
        Parse the scope defintion
        """

        scopes = {}
        scope_count = 0
        for params in self.scope_rules:
            if is_valid_definition(params, language):
                try:
                    bh_plugin.load_modules(params, loaded_modules)
                    entry = ScopeDefinition(params)
                    if not self.check_compare and entry.compare is not None:
                        self.check_compare = True
                    if not self.check_validate and entry.validate is not None:
                        self.check_validate = True
                    if not self.check_post_match and entry.post_match is not None:
                        self.check_post_match = True
                    for x in entry.scopes:
                        if x not in scopes:
                            scopes[x] = scope_count
                            scope_count += 1
                            self.scopes.append({"name": x, "brackets": [entry]})
                        else:
                            self.scopes[scopes[x]]["brackets"].append(entry)
                    debug("Scope Regex (%s)\n    Opening: %s\n    Closing: %s\n" % (entry.name, entry.open.pattern, entry.close.pattern))
                except Exception as e:
                    log(e)
Ejemplo n.º 3
0
    def compare(self, first, second, scope_bracket=False):
        """
        Compare brackets.  This function allows bracket plugins to add aditional logic.
        """

        if scope_bracket:
            match = first is not None and second is not None
        else:
            match = first.type == second.type

        if not self.rules.check_compare:
            return match

        if match:
            bracket = self.rules.scopes[first.scope]["brackets"][first.type] if scope_bracket else self.rules.brackets[first.type]
            try:
                if bracket.compare is not None and match:
                    match = bracket.compare(
                        bracket.name,
                        bh_plugin.BracketRegion(first.begin, first.end),
                        bh_plugin.BracketRegion(second.begin, second.end),
                        self.search.get_buffer()
                    )
            except:
                log("Plugin Compare Error:\n%s" % str(traceback.format_exc()))
        return match
Ejemplo n.º 4
0
    def post_match(self, left, right, center, scope_bracket=False):
        """
        Peform special logic after a match has been made.
        This function allows bracket plugins to add aditional logic.
        """

        if left is not None:
            if scope_bracket:
                bracket = self.rules.scopes[left.scope]["brackets"][left.type]
                bracket_scope = left.scope
            else:
                bracket = self.rules.brackets[left.type]
            bracket_type = left.type
        elif right is not None:
            if scope_bracket:
                bracket = self.rules.scopes[right.scope]["brackets"][
                    right.type]
                bracket_scope = right.scope
            else:
                bracket = self.rules.brackets[right.type]
            bracket_type = right.type
        else:
            return left, right

        self.bracket_style = bracket.style

        if not self.rules.check_post_match:
            return left, right

        if bracket.post_match is not None:
            try:
                lbracket, rbracket, self.bracket_style = bracket.post_match(
                    self.view, bracket.name, bracket.style,
                    bh_plugin.BracketRegion(left.begin, left.end)
                    if left is not None else None,
                    bh_plugin.BracketRegion(right.begin, right.end)
                    if right is not None else None, center,
                    self.search.get_buffer(), self.search.search_window)

                if scope_bracket:
                    left = bh_search.ScopeEntry(
                        lbracket.begin, lbracket.end, bracket_scope,
                        bracket_type) if lbracket is not None else None
                    right = bh_search.ScopeEntry(
                        rbracket.begin, rbracket.end, bracket_scope,
                        bracket_type) if rbracket is not None else None
                else:
                    left = bh_search.BracketEntry(
                        lbracket.begin, lbracket.end,
                        bracket_type) if lbracket is not None else None
                    right = bh_search.BracketEntry(
                        rbracket.begin, rbracket.end,
                        bracket_type) if rbracket is not None else None
            except:
                log("Plugin Post Match Error:\n%s" %
                    str(traceback.format_exc()))

        return left, right
Ejemplo n.º 5
0
    def parse_bracket_definition(self, language, loaded_modules):
        """
        Parse the bracket defintion
        """

        names = []
        subnames = []
        find_regex = []
        sub_find_regex = []

        for params in self.bracket_rules:
            if is_valid_definition(params, language):
                try:
                    bh_plugin.load_modules(params, loaded_modules)
                    entry = BracketDefinition(params)
                    if not self.check_compare and entry.compare is not None:
                        self.check_compare = True
                    if not self.check_validate and entry.validate is not None:
                        self.check_validate = True
                    if not self.check_post_match and entry.post_match is not None:
                        self.check_post_match = True
                    self.brackets.append(entry)
                    if not entry.find_in_sub_search_only:
                        find_regex.append(params["open"])
                        find_regex.append(params["close"])
                        names.append(params["name"])
                    else:
                        find_regex.append(r"([^\s\S])")
                        find_regex.append(r"([^\s\S])")

                    if entry.find_in_sub_search:
                        sub_find_regex.append(params["open"])
                        sub_find_regex.append(params["close"])
                        subnames.append(params["name"])
                    else:
                        sub_find_regex.append(r"([^\s\S])")
                        sub_find_regex.append(r"([^\s\S])")
                except Exception as e:
                    log(e)

        if len(self.brackets):
            self.brackets = tuple(self.brackets)
            debug(
                "Bracket Pattern: (%s)\n" % ','.join(names) +
                "    (Opening|Closing):     (?:%s)\n" % '|'.join(find_regex)
            )
            debug(
                "SubBracket Pattern: (%s)\n" % ','.join(subnames) +
                "    (Opening|Closing): (?:%s)\n" % '|'.join(sub_find_regex)
            )
            self.sub_pattern = ure.compile("(?:%s)" % '|'.join(sub_find_regex), ure.MULTILINE | ure.IGNORECASE)
            self.pattern = ure.compile("(?:%s)" % '|'.join(find_regex), ure.MULTILINE | ure.IGNORECASE)
Ejemplo n.º 6
0
    def post_match(self, left, right, center, scope_bracket=False):
        """
        Peform special logic after a match has been made.
        This function allows bracket plugins to add aditional logic.
        """

        if left is not None:
            if scope_bracket:
                bracket = self.rules.scopes[left.scope]["brackets"][left.type]
                bracket_scope = left.scope
            else:
                bracket = self.rules.brackets[left.type]
            bracket_type = left.type
        elif right is not None:
            if scope_bracket:
                bracket = self.rules.scopes[right.scope]["brackets"][right.type]
                bracket_scope = right.scope
            else:
                bracket = self.rules.brackets[right.type]
            bracket_type = right.type
        else:
            return left, right

        self.bracket_style = bracket.style

        if not self.rules.check_post_match:
            return left, right

        if bracket.post_match is not None:
            try:
                lbracket, rbracket, self.bracket_style = bracket.post_match(
                    self.view,
                    bracket.name,
                    bracket.style,
                    bh_plugin.BracketRegion(left.begin, left.end) if left is not None else None,
                    bh_plugin.BracketRegion(right.begin, right.end) if right is not None else None,
                    center,
                    self.search.get_buffer(),
                    self.search.search_window
                )

                if scope_bracket:
                    left = bh_search.ScopeEntry(lbracket.begin, lbracket.end, bracket_scope, bracket_type) if lbracket is not None else None
                    right = bh_search.ScopeEntry(rbracket.begin, rbracket.end, bracket_scope, bracket_type) if rbracket is not None else None
                else:
                    left = bh_search.BracketEntry(lbracket.begin, lbracket.end, bracket_type) if lbracket is not None else None
                    right = bh_search.BracketEntry(rbracket.begin, rbracket.end, bracket_type) if rbracket is not None else None
            except:
                log("Plugin Post Match Error:\n%s" % str(traceback.format_exc()))

        return left, right
Ejemplo n.º 7
0
    def __init__(self, plugin, loaded):
        """
        Load plugin module
        """

        self.enabled = False
        self.args = plugin['args'] if ("args" in plugin) else {}
        self.plugin = None
        if 'command' in plugin:
            plib = plugin['command']
            try:
                module = ImportModule.import_module(plib, loaded)
                self.plugin = getattr(module, 'plugin')()
                loaded.add(plib)
                self.enabled = True
            except Exception:
                log('BracketHighlighter: Load Plugin Error: %s\n%s' % (plugin['command'], traceback.format_exc()))
Ejemplo n.º 8
0
def load_modules(obj, loaded):
    """
    Load bracket plugin modules
    """

    plib = obj.get("plugin_library")
    if plib is None:
        return

    try:
        module = ImportModule.import_module(plib, loaded)
        obj["compare"] = getattr(module, "compare", None)
        obj["post_match"] = getattr(module, "post_match", None)
        obj["validate"] = getattr(module, "validate", None)
        loaded.add(plib)
    except:
        log("Could not load module %s\n%s" % (plib, str(traceback.format_exc())))
        raise
Ejemplo n.º 9
0
    def validate(self, b, bracket_type, scope_bracket=False):
        """
        Validate bracket.
        """

        match = True

        if not self.rules.check_validate:
            return match

        bracket = self.rules.scopes[b.scope]["brackets"][
            b.type] if scope_bracket else self.rules.brackets[b.type]
        if bracket.validate is not None:
            try:
                match = bracket.validate(
                    bracket.name, bh_plugin.BracketRegion(b.begin, b.end),
                    bracket_type, self.search.get_buffer())
            except:
                log("Plugin Bracket Find Error:\n%s" %
                    str(traceback.format_exc()))
        return match
Ejemplo n.º 10
0
    def run_command(self, view, name, left, right, selection):
        """
        Load arguments into plugin and run
        """

        plugin = self.plugin()
        setattr(plugin, "left", left)
        setattr(plugin, "right", right)
        setattr(plugin, "view", view)
        setattr(plugin, "selection", selection)
        setattr(plugin, "nobracket", False)
        edit = view.begin_edit()
        self.args["edit"] = edit
        self.args["name"] = name
        try:
            nobracket = False
            plugin.run(**self.args)
            left, right, selection, nobracket = plugin.left, plugin.right, plugin.selection, plugin.nobracket
        except Exception:
            log("BracketHighlighter: Plugin Run Error:\n%s" % str(traceback.format_exc()))
        view.end_edit(edit)
        return left, right, selection, nobracket
Ejemplo n.º 11
0
    def validate(self, b, bracket_type, scope_bracket=False):
        """
        Validate bracket.
        """

        match = True

        if not self.rules.check_validate:
            return match

        bracket = self.rules.scopes[b.scope]["brackets"][b.type] if scope_bracket else self.rules.brackets[b.type]
        if bracket.validate is not None:
            try:
                match = bracket.validate(
                    bracket.name,
                    bh_plugin.BracketRegion(b.begin, b.end),
                    bracket_type,
                    self.search.get_buffer()
                )
            except:
                log("Plugin Bracket Find Error:\n%s" % str(traceback.format_exc()))
        return match