Exemple #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:
            if scope_bracket:
                bracket = self.rules.scopes[first.scope]["brackets"][
                    first.type]
            else:
                bracket = 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 Exception:
                log("Plugin Compare Error:\n%s" % str(traceback.format_exc()))
        return match
Exemple #2
0
    def highlighting(self, left, right, scope_bracket=False):
        """
        Adjust region to highlight.

        This is done after all methods that need
        to know actual bracket region.  At this point, we no longer care about
        the actual bracket's region, and we change the highlight region to something
        completely different.
        """

        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

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

        if bracket.highlighting is not None:
            lbracket, rbracket = bracket.highlighting(
                self.view,
                bracket.name,
                self.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
            )

            if scope_bracket:
                if lbracket is not None:
                    left = bh_search.ScopeEntry(lbracket.begin, lbracket.end, bracket_scope, bracket_type)
                else:
                    left = None
                if rbracket is not None:
                    right = bh_search.ScopeEntry(rbracket.begin, rbracket.end, bracket_scope, bracket_type)
                else:
                    right = None
            else:
                if lbracket is not None:
                    left = bh_search.BracketEntry(lbracket.begin, lbracket.end, bracket_type)
                else:
                    left = None
                if rbracket is not None:
                    right = bh_search.BracketEntry(rbracket.begin, rbracket.end, bracket_type)
                else:
                    right = None
        return left, right
Exemple #3
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
Exemple #4
0
    def run_plugin(self, name, left, right, regions):
        """Run a bracket plugin."""

        lbracket = bh_plugin.BracketRegion(left.begin, left.end)
        rbracket = bh_plugin.BracketRegion(right.begin, right.end)
        nobracket = False

        if (("__all__" in self.plugin_targets or name in self.plugin_targets)
                and self.plugin is not None and self.plugin.is_enabled()):
            lbracket, rbracket, regions, nobracket = self.plugin.run_command(
                self.view, name, lbracket, rbracket, regions)
            left = left.move(lbracket.begin,
                             lbracket.end) if lbracket is not None else None
            right = right.move(rbracket.begin,
                               rbracket.end) if rbracket is not None else None
        return left, right, regions, nobracket
Exemple #5
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 Exception:
                log("Plugin Bracket Find Error:\n%s" %
                    str(traceback.format_exc()))
        return match