Last active
January 16, 2021 07:49
-
-
Save UltraInstinct05/2e6b87aa9d8cdf0d34d24e6008803796 to your computer and use it in GitHub Desktop.
A ST plugin that colorizes selected code blocks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
import random | |
import string | |
def plugin_loaded(): | |
""" When the plugin is loaded, this function is executed. We load the Code | |
Block colorer settings here. The settings file is mainly to store data | |
regarding the colored code block state. | |
Args: | |
None | |
Returns: | |
None | |
""" | |
code_block_colorer.obj = sublime.load_settings("code_block_colorer.sublime-settings") | |
code_block_colorer.default = { | |
"data": [] | |
} | |
def code_block_colorer(key): | |
""" Given a key, get's the value of that setting from the user settings file. | |
Args: | |
key (str): The key. | |
Returns: | |
value (any): The value of that key as per user's settings file. | |
""" | |
default = code_block_colorer.default.get(key, None) | |
return code_block_colorer.obj.get(key, default) | |
def save_colored_region_info(colored_data): | |
""" Saves the colored state in the settings file. | |
Args: | |
colored_data (dict|list(dict)): The data that needs to be stored in the | |
user settings. | |
Returns: | |
None | |
""" | |
current_data = code_block_colorer("data") | |
if isinstance(colored_data, dict): | |
current_data.append({ | |
"key": colored_data["key"], | |
"region": colored_data["region"], | |
"file_name": colored_data["file_name"], | |
"color": colored_data["color"] | |
}) | |
code_block_colorer.obj.set("data", current_data) | |
sublime.save_settings("code_block_colorer.sublime-settings") | |
else: | |
code_block_colorer.obj.set("data",colored_data) | |
sublime.save_settings("code_block_colorer.sublime-settings") | |
class CodeBlockColorerCommand(sublime_plugin.TextCommand): | |
""" Implements a code_block_colorer command that is used to apply a | |
background color to the selected code block using the generated 'ish' colors. | |
""" | |
def run(self, edit, color = "region.pinkish"): | |
selection = self.view.sel() | |
file_name = self.view.file_name() | |
letters = string.ascii_lowercase | |
for s in selection: | |
rand = ''.join(random.choice(letters) for i in range(10)) | |
key = "code_block_colorer_{}".format(rand) | |
save_colored_region_info({ | |
"key": key, | |
"region": [s.begin(), s.end()], | |
"file_name": file_name, | |
"color": color | |
}) | |
self.view.add_regions( | |
key = key, | |
regions = [s for s in selection], | |
scope = color, | |
flags = sublime.PERSISTENT | |
) | |
def is_enabled(self): | |
""" If there are empty selections in the buffer or the file doesn't | |
exist on disk, don't allow the user to pick a color. | |
""" | |
return ( | |
self.view.file_name() and | |
self.view.has_non_empty_selection_region() | |
) | |
class CodeBlockColorerClearCommand(sublime_plugin.TextCommand): | |
""" Implements a code_block_colorer_clear command that clears off the | |
applied background color on a given code block. | |
The user can either clear of individual colored code blocks or clear them | |
all in a single swoop. | |
""" | |
def __init__(self, view): | |
super().__init__(view) | |
self.view = view | |
self.file_name = self.view.file_name() | |
def run(self, edit, clear_all = False): | |
cur = self.view.sel()[0].begin() | |
colored_regions = code_block_colorer("data") | |
for i, r in enumerate(colored_regions): | |
if not clear_all: | |
if ( | |
self.file_name == r["file_name"] and | |
sublime.Region(r["region"][0], r["region"][1]).contains(cur) | |
): | |
self.view.erase_regions(r["key"]) | |
colored_regions.pop(i) | |
save_colored_region_info(colored_regions) | |
else: | |
if ( | |
self.file_name == r["file_name"] | |
): | |
colored_regions.pop(i) | |
self.view.erase_regions(r["key"]) | |
save_colored_region_info(colored_regions) | |
def is_enabled(self): | |
""" If there are no colored region data in settings for the active file, | |
why bother enabling this command ? | |
""" | |
return ( | |
len(code_block_colorer("data")) > 0 and | |
any([d for d in code_block_colorer("data") if d["file_name"] == self.file_name]) | |
) | |
class CodeBlockColorerListener(sublime_plugin.ViewEventListener): | |
""" A Listener to listen when files are loaded. By default, when a region | |
is 'marked', they aren't persistent. There is the sublime.PERSISTENT flag, | |
but it only apparently works for open files and doesn't persist regions when | |
a file is closed. Hence, we get the data stored in the settings file to | |
color back the regions appropriately when the right file is loaded. | |
""" | |
def __init__(self, view): | |
self.view = view | |
def on_load_async(self): | |
file_name = self.view.file_name() | |
colored_regions = code_block_colorer("data") | |
for i, r in enumerate(colored_regions): | |
if r["file_name"] == file_name: | |
self.view.add_regions( | |
key = r["key"], | |
regions = [sublime.Region(r["region"][0], r["region"][1])], | |
scope = r["color"], | |
flags = sublime.PERSISTENT | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": [ | |
], | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code Block Colorer
This plugin allows a user to color blocks of code or text.
Features
The plugin allows the user to
How to use it.
Copy all of the files in this gist to a folder called
CodeBlockColorer
(create the folder) inPackages
(from the main menu, go toPreferences -> Browse Packages ...
). You should be good to go.This plugin usage is mainly driven through the context menu. Select any block of text and use the context menu to colorise it.
Current shortcomings
I am not sure why the
Code Block Colorer (Clear all)
doesn't work properly. Currently, it clears the colored text blocks one by one.