def __init__(self, view) -> None: self._scope_styles = {} # type: dict self._view = view for scope in [ "entity.name.function", "variable.parameter", "punctuation" ]: self._scope_styles[scope] = mdpopups.scope2style(view, scope)
def get_markup(self, view, point, region, icon): """Get markup.""" settings = sublime.load_settings('bh_core.sublime-settings') tab_size = view.settings().get('tab_size', 4) # Get highlight colors if icon is not None: color = mdpopups.scope2style(view, icon[1]).get('color') if color is None or bool( settings.get('use_custom_popup_bracket_emphasis', False)): bracket_em = settings.get('popup_bracket_emphasis', '#ff0000') else: bracket_em = color # Get positions of bracket extents on the line row, col = view.rowcol(region[0]) col2 = view.rowcol(region[1])[1] # Calculate how far before and after bracket we can/should grab for context # Format and truncate (if necessary). context = int(settings.get('popup_char_context', 120)) - (col2 - col) - 1 start = region[1] - context col_start = col2 - context overage = 0 line = view.line(region[0]) if start < line.begin(): overage = line.begin() - start start = line.begin() col_start = 0 end = region[1] + overage col_end = col2 + overage if end > line.end(): end = line.end() col_end = view.rowcol(line.end())[1] # Get line of code with bracket and emphasize the bracket content = view.substr(sublime.Region(start, end)) if re.match(r'#([\da-fA-F]{3}){1,2}', bracket_em): highlight_open = '<span class="brackethighlighter" style="color: %s;"><strong>' % bracket_em else: highlight_open = '<span class="brackethighlighter %s"><strong>' % bracket_em content = (self.escape_code(content[:col - col_start], tab_size) + highlight_open + self.escape_code( content[col - col_start:col2 - col_start], tab_size) + '</strong></span>' + self.escape_code(content[col2 - col_start:], tab_size)) # Get additional lines of context (if required) and format text if row != view.rowcol(point)[0]: line_context = int(int(settings.get('popup_line_context', 2)) / 2) content = self.get_multiline_context(view, content, row, col_start, col_end, tab_size, line_context) else: content = content.strip() # Put together the markup to show markup = '<div class="highlight"><pre>%s</pre></div>\n' % content markup += '\n' + '[(jump to bracket - line: %d)](%d)' % (row + 1, region[0]) return markup
def get_markup(self, view, point, region, icon): """Get markup.""" settings = sublime.load_settings('bh_core.sublime-settings') tab_size = view.settings().get('tab_size', 4) # Get highlight colors if icon is not None: color = mdpopups.scope2style(view, icon[1]).get('color') if color is None or bool(settings.get('use_custom_popup_bracket_emphasis', False)): bracket_em = settings.get('popup_bracket_emphasis', '#ff0000') else: bracket_em = color # Get positions of bracket extents on the line row, col = view.rowcol(region[0]) col2 = view.rowcol(region[1])[1] # Calculate how far before and after bracket we can/should grab for context # Format and truncate (if necessary). context = int(settings.get('popup_char_context', 120)) - (col2 - col) - 1 start = region[1] - context col_start = col2 - context overage = 0 line = view.line(region[0]) if start < line.begin(): overage = line.begin() - start start = line.begin() col_start = 0 end = region[1] + overage col_end = col2 + overage if end > line.end(): end = line.end() col_end = view.rowcol(line.end())[1] # Get line of code with bracket and emphasize the bracket content = view.substr(sublime.Region(start, end)) if re.match(r'#([\da-fA-F]{3}){1,2}', bracket_em): highlight_open = '<span class="brackethighlighter" style="color: %s;"><strong>' % bracket_em else: highlight_open = '<span class="brackethighlighter %s"><strong>' % bracket_em content = ( self.escape_code(content[:col - col_start], tab_size) + highlight_open + self.escape_code(content[col - col_start: col2 - col_start], tab_size) + '</strong></span>' + self.escape_code(content[col2 - col_start:], tab_size) ) # Get additional lines of context (if required) and format text if row != view.rowcol(point)[0]: line_context = int(int(settings.get('popup_line_context', 2)) / 2) content = self.get_multiline_context(view, content, row, col_start, col_end, tab_size, line_context) else: content = content.strip() # Put together the markup to show markup = '<div class="highlight"><pre>%s</pre></div>\n' % content markup += '\n' + '[(jump to bracket - line: %d)](%d)' % (row + 1, region[0]) return markup
def get_color(view): try: color = mdpopups.scope2style(view, "").get("color", "#CCCCCC") except: color = "#CCCCCC" return color
def do_search(self, force=False): """ Perform the search for the highlighted word. TODO: This function is a big boy. We should look into breaking it up. With that said, this is low priority. """ # Since the plugin has been reloaded, force update. global reload_flag settings = self.view.settings() colors = [] view_id = self.view.id() # Allow per view scan override option = settings.get("color_helper.scan_override", None) if option in ("Force enable", "Force disable"): override = option == "Force enable" else: override = None # Get the rules and use them to get the needed scopes. # The scopes will be used to get the searchable regions. rules = util.get_rules(self.view) # Bail if this if this view has no valid rule or scanning is disabled. if (rules is None or not rules.get("enabled", False) or (not rules.get("allow_scanning", True) and not override) or override is False): self.erase_phantoms() return if reload_flag: reload_flag = False force = True # Calculate size of preview boxes box_height = self.calculate_box_size() check_size = int((box_height - 2) / 4) if check_size < 2: check_size = 2 # If desired preview boxes are different than current, # we need to reload the boxes. old_box_height = int(settings.get('color_helper.box_height', 0)) current_color_scheme = settings.get('color_scheme') if (force or old_box_height != box_height or current_color_scheme != settings.get( 'color_helper.color_scheme', '') or settings.get('color_helper.refresh')): self.erase_phantoms() settings.set('color_helper.color_scheme', current_color_scheme) settings.set('color_helper.box_height', box_height) force = True # Get viewable bounds so we can constrain both vertically and horizontally. visible_region = self.view.visible_region() position = self.view.viewport_position() dimensions = self.view.viewport_extent() bounds = Dimensions( Extent(position[0], position[0] + dimensions[0] - 1), Extent(position[1], position[1] + dimensions[1] - 1)) # If we don't need to force previews, # quit if visible region is the same as last time if not force and self.previous_region[view_id] == bounds: return self.previous_region[view_id] = bounds # Setup "preview on select" preview_on_select = ch_settings.get("preview_on_select", False) show_preview = True sels = [] if preview_on_select: show_preview = False sels = self.get_selections(bounds) if sels: show_preview = True # Get the scan scopes scanning = rules.get("scanning") classes = rules.get("color_class", "css-level-4") if show_preview and visible_region.size() and scanning and classes: # Get out of gamut related options self.setup_gamut_options() # Get triggers that identify where colors are likely color_trigger = re.compile( rules.get("color_trigger", util.RE_COLOR_START)) # Find source content in the visible region. # We will return consecutive content, but if the lines are too wide # horizontally, they will be clipped and returned as separate chunks. for src_region in self.source_iter(visible_region, bounds): source = self.view.substr(src_region) start = 0 # Find colors in this source chunk. for m in color_trigger.finditer(source): # Test if we have found a valid color start = m.start() src_start = src_region.begin() + start # Check if the first point within the color matches our scope rules # and load up the appropriate color class color_class, filters = self.get_color_class( src_start, classes) if color_class is None: continue # Check if scope matches for scanning try: value = self.view.score_selector(src_start, scanning) if not value: continue except Exception: continue obj = color_class.match(source, start=start, filters=filters) if obj is not None: # Calculate true start and end of the color source src_end = src_region.begin() + obj.end region = sublime.Region(src_start, src_end) # If "preview on select" is enabled, only show preview if within a selection # or if the selection as no width and the color comes right after. if preview_on_select and not self.is_selected( region, sels): continue else: continue # Calculate point at which we which to insert preview position_on_left = preview_is_on_left() pt = src_start if position_on_left else src_end if str(region.begin()) in self.previews[view_id]: # Already exists continue # Calculate a reasonable border color for our image at this location and get color strings hsl = Color(mdpopups.scope2style( self.view, self.view.scope_name(pt))['background'], filters=util.SRGB_SPACES).convert("hsl") hsl.lightness = hsl.lightness + ( 30 if hsl.luminance() < 0.5 else -30) preview_border = hsl.convert( "srgb", fit=True).to_string(**util.HEX) color = Color(obj.color) title = '' if not color.in_gamut("srgb"): title = ' title="Preview out of gamut"' if self.show_out_of_gamut_preview: srgb = color.convert("srgb", fit=True) preview1 = srgb.to_string(**util.HEX_NA) preview2 = srgb.to_string(**util.HEX) else: preview1 = self.out_of_gamut preview2 = self.out_of_gamut preview_border = self.out_of_gamut_border else: srgb = color.convert("srgb") preview1 = srgb.to_string(**util.HEX_NA) preview2 = srgb.to_string(**util.HEX) # Create preview unique_id = str(time()) + str(region) html = PREVIEW_IMG.format( unique_id, title, mdpopups.color_box([preview1, preview2], preview_border, height=box_height, width=box_height, border_size=PREVIEW_BORDER_SIZE, check_size=check_size)) colors.append( (html, pt, region.begin(), region.end(), unique_id)) # Add all previews self.add_phantoms(colors) # The phantoms may have altered the viewable region, # so set previous region to the current viewable region visible_region = self.view.visible_region() position = self.view.viewport_position() dimensions = self.view.viewport_extent() bounds = Dimensions( Extent(position[0], position[0] + dimensions[0] - 1), Extent(position[1], position[1] + dimensions[1] - 1)) self.previous_region[view_id] = bounds