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
def parse_scope_definition(self, language, loaded_modules): 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 entry.enabled: log( SCOPE_ERROR % ( str(params.get('name', '?')), "\\A" + params.get("open", ""), params.get("close", "") + "\\Z" ) ) continue 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 if not self.highlighting and entry.highlighting is not None: self.highlighting = 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) ) except Exception as e: log(e)
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
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 if not self.highlighting and entry.highlighting is not None: self.highlighting = 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)
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)
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
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: 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 except Exception: log("Plugin Post Match Error:\n%s" % str(traceback.format_exc())) return left, right
def on_navigate(self, href): """Navigate to code position.""" if HOVER_SUPPORT: try: pt = int(href) self.popup_view.sel().clear() self.popup_view.sel().add(sublime.Region(pt)) self.popup_view.show(pt) mdpopups.hide_popup(self.popup_view) except Exception: log("Problem handling popup event:\n%s" % str(traceback.format_exc()))
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 if not self.highlighting and entry.highlighting is not None: self.highlighting = 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)
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 if not self.highlighting and entry.highlighting is not None: self.highlighting = 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)
def validate(self, b, bracket_type, scope_bracket=False): 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
def load_modules(obj, loaded): """Load bracket plugin modules.""" plib = obj.get("plugin_library") if plib is None: return try: module = _import_module(plib, loaded) obj["compare"] = getattr(module, "compare", None) obj["post_match"] = getattr(module, "post_match", None) obj["validate"] = getattr(module, "validate", None) obj["highlighting"] = getattr(module, "highlighting", None) loaded.add(plib) except Exception: log("Could not load module %s\n%s" % (plib, str(traceback.format_exc()))) raise
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
def parse_scope_definition(self, language, loaded_modules): """Parse the scope definition.""" 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 entry.enabled: log( SCOPE_ERROR % ( str(params.get('name', '?')), "\\A" + params.get("open", ""), params.get("close", "") + "\\Z" ) ) continue 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 if not self.highlighting and entry.highlighting is not None: self.highlighting = 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)
def plugin_loaded(): """ General plugin initialization. Load up uniocode table, initialize settings and match object, and start event loop. Restart event loop if already loaded. """ global HIGH_VISIBILITY global bh_thread settings = sublime.load_settings("bh_core.sublime-settings") # Try and ensure key dependencies are at the latest known good version. # This is only done because Package Control does not do this on package upgrade at the present. try: from package_control import events if bh_popup.HOVER_SUPPORT and events.post_upgrade(support.__pc_name__): if not bh_popup.LATEST_SUPPORTED_MDPOPUPS and settings.get( 'upgrade_dependencies', True): window = sublime.active_window() if window: window.run_command('satisfy_dependencies') except ImportError: log('Could not import Package Control') init_bh_match() global HIGH_VISIBILITY if sublime.load_settings("bh_core.sublime-settings").get( 'high_visibility_enabled_by_default', False): HIGH_VISIBILITY = True if bh_thread is not None: bh_thread.kill() bh_thread = BhThread() bh_thread.start()
def plugin_loaded(): """ General plugin initialization. Load up uniocode table, initialize settings and match object, and start event loop. Restart event loop if already loaded. """ global HIGH_VISIBILITY global bh_thread settings = sublime.load_settings("bh_core.sublime-settings") # Try and ensure key dependencies are at the latest known good version. # This is only done because Package Control does not do this on package upgrade at the present. try: from package_control import events if bh_popup.HOVER_SUPPORT and events.post_upgrade(support.__pc_name__): if not bh_popup.LATEST_SUPPORTED_MDPOPUPS and settings.get('upgrade_dependencies', True): window = sublime.active_window() if window: window.run_command('satisfy_dependencies') except ImportError: log('Could not import Package Control') init_bh_match() global HIGH_VISIBILITY if sublime.load_settings("bh_core.sublime-settings").get('high_visibility_enabled_by_default', False): HIGH_VISIBILITY = True if bh_thread is not None: bh_thread.kill() bh_thread = BhThread() bh_thread.start()
def parse_bracket_definition(self, language, loaded_modules): names = [] subnames = [] find_regex = [] sub_find_regex = [] self.sub_pattern = None self.pattern = None 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 if not self.highlighting and entry.highlighting is not None: self.highlighting = 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) self.sub_pattern = bre.compile_search("(?:%s)" % '|'.join(sub_find_regex), bre.MULTILINE | bre.IGNORECASE) self.pattern = bre.compile_search("(?:%s)" % '|'.join(find_regex), bre.MULTILINE | bre.IGNORECASE) if ( self.sub_pattern.groups != len(sub_find_regex) or self.pattern.groups != len(find_regex) ): if self.sub_pattern.groups != len(sub_find_regex): log( BRACKET_ERROR % ( len(sub_find_regex), 'sub-pattern', self.sub_pattern.groups, "(?:%s)" % '|'.join(sub_find_regex) ) ) if self.pattern.groups != len(find_regex): log( BRACKET_ERROR % ( len(find_regex), 'pattern', self.pattern.groups, "(?:%s)" % '|'.join(find_regex) ) ) self.brackets = [] self.sub_pattern = None self.pattern = None