Skip to content

Commit

Permalink
Merge pull request #27 from TheSecEng/feature/14-16
Browse files Browse the repository at this point in the history
Feature/14 16
  • Loading branch information
TerminalFi authored May 19, 2020
2 parents 8c4c74b + f7fd403 commit 1026030
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 153 deletions.
4 changes: 4 additions & 0 deletions Commands.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"caption": "Colored Comments: Remove Generated Color Scheme",
"command": "colored_comments_theme_revert"
},
{
"caption": "Colored Comments: Clear Colorization",
"command": "colored_comments_clear"
},
{
"caption": "Colored Comments: Settings",
"command": "edit_settings",
Expand Down
39 changes: 21 additions & 18 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
[
{
"id": "preferences",
"children": [
{
"id": "package-settings",
"id": "preferences",
"children": [
{
"caption": "Colored Comments",
"children": [
{
"caption": "Settings",
"command": "edit_settings",
"args":
{
"base_file": "${packages}/Colored Comments/colored_comments.sublime-settings",
"default": "{\n\t$0\n}\n",
},
}]
}]
}]
}]
"id": "package-settings",
"children": [
{
"caption": "Colored Comments",
"children": [
{
"caption": "Settings",
"command": "edit_settings",
"args": {
"base_file": "${packages}/Colored Comments/colored_comments.sublime-settings",
"default": "{\n\t$0\n}\n"
}
}
]
}
]
}
]
}
]
44 changes: 18 additions & 26 deletions colored_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
from .plugin.settings import load_settings, settings, unload_settings

NAME = "Colored Comments"
VERSION = "3.0.1"
VERSION = "3.0.2"

region_keys = list()
color_scheme_manager = ColorManager

comment_selector = "comment - punctuation.definition.comment"


Expand All @@ -28,17 +26,17 @@ def on_modified_async(self, view):

class ColoredCommentsCommand(sublime_plugin.TextCommand):
def run(self, edit):
if self.view.match_selector(0, "text.plain"):
if self.view.settings().get("syntax") in settings.disabled_syntax:
return

self.ClearDecorations()
self.ApplyDecorations()

def ClearDecorations(self):
for region_key in region_keys:
def ClearDecorations(self) -> None:
for region_key in settings.region_keys:
self.view.erase_regions(region_key)

def ApplyDecorations(self):
def ApplyDecorations(self) -> None:
to_decorate = dict()
prev_match = str()
for region in self.view.find_by_selector(comment_selector):
Expand Down Expand Up @@ -72,10 +70,10 @@ def ApplyDecorations(self):
regions=to_decorate.get(key),
scope=_get_scope_for_region(tag),
icon=settings.comment_icon if settings.comment_icon_enabled else "",
flags=self._get_tag_flags(tag),
flags=self._get_flags(tag),
)

def _get_tag_flags(self, tag):
def _get_flags(self, tag: dict) -> int:
options = {
"outline": sublime.DRAW_NO_FILL,
"underline": sublime.DRAW_SOLID_UNDERLINE,
Expand All @@ -84,11 +82,16 @@ def _get_tag_flags(self, tag):
}
flags = sublime.PERSISTENT
for index, option in options.items():
if index in tag.keys() and tag[index] is True:
if tag.get(index) is True:
flags |= option
return flags


class ColoredCommentsClearCommand(ColoredCommentsCommand, sublime_plugin.TextCommand):
def run(self, edit):
self.ClearDecorations()


class ColoredCommentsThemeGeneratorCommand(sublime_plugin.TextCommand):
def run(self, edit):
color_scheme_manager.tags = settings.tags
Expand All @@ -99,35 +102,24 @@ class ColoredCommentsThemeRevertCommand(sublime_plugin.TextCommand):
def run(self, edit):
preferences = sublime.load_settings("Preferences.sublime-settings")
if preferences.get("color_scheme"):
color_scheme_manager.remove_override(
preferences.get("color_scheme"))
color_scheme_manager.remove_override(preferences.get("color_scheme"))


def _get_scope_for_region(tag: dict) -> str:
if tag.get("scope"):
return tag.get("scope")
scope_name = "colored.comments.color.{}".format(
tag.get("color").get("name"))
scope_name = "colored.comments.color.{}".format(tag.get("color").get("name"))
return scope_name.replace(" ", ".").lower()


def _generate_region_keys(region_keys, tag_map):
for key in tag_map:
if key.lower() not in region_keys:
region_keys.append(key.lower())


def plugin_loaded():
def plugin_loaded() -> None:
global region_keys
global color_scheme_manager
load_settings()
_generate_region_keys(region_keys, settings.tags)
log.set_debug_logging(settings.debug)

color_scheme_manager = ColorManager(
tags=settings.tags
)
color_scheme_manager = ColorManager(tags=settings.tags)


def plugin_unloaded():
def plugin_unloaded() -> None:
unload_settings()
52 changes: 20 additions & 32 deletions colored_comments.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
{
// Enable debug logging
"debug": false,

// Enables continued matching of the previous tag
"continued_matching": true,

// Character to continue matching on
"continued_matching_pattern": "-",

// Shows comment icon next to comments
"comment_icon_enabled": true,

// Which comment icon to use
// Valid options: comment, dots
"comment_icon": "dots",

// Ignored Syntax List
"disabled_syntax": [
"Packages/Text/Plain text.tmLanguage",
"Packages/Markdown/MultiMarkdown.sublime-syntax",
"Packages/Markdown/Markdown.sublime-syntax"
],
// This is the list of tags/identifiers you want to highlight
// in your code.
"tags":
{
"tags": {
// Friendly name for you to know what the purpose is
// and used in the OrderedDict
// These should be unique
"Important":
{
"Important": {
// The actual identifier used to highlight the comments
"identifier": "!",
// Enables sublime.DRAW_SOLID_UNDERLINE
Expand All @@ -45,8 +44,7 @@
// Enables ignorecase for the ideentifier
"ignorecase": true,
// Colors used for Scope generation
"color":
{
"color": {
// Scope name this becomes colored.comments.color.important
"name": "important",
// Color you want the text to be
Expand All @@ -57,30 +55,24 @@
"background": "rgba(1,22,38, 0.1)"
},
},
"Deprecated":
{
"Deprecated": {
"identifier": "*",
"color":
{
"color": {
"name": "deprecated",
"foreground": "#98C379",
"background": "rgba(1,22,38, 0.1)"
},
},
"Question":
{
"Question": {
"identifier": "?",
"color":
{
"color": {
"name": "question",
"foreground": "#3498DB",
"background": "rgba(1,22,38, 0.1)"
},
},
"TODO":
{
"color":
{
"TODO": {
"color": {
"background": "rgba(1,22,38, 0.1)",
"foreground": "#FF8C00",
"name": "todo"
Expand All @@ -89,21 +81,17 @@
"is_regex": true,
"ignorecase": true,
},
"FIXME":
{
"color":
{
"FIXME": {
"color": {
"background": "rgba(1,22,38, 0.1)",
"foreground": "#9933FF",
"name": "fixme"
},
"identifier": "FIXME[:]?|fixme[:]?",
"is_regex": true
},
"UNDEFINED":
{
"color":
{
"UNDEFINED": {
"color": {
"background": "rgba(1,22,38, 0.1)",
"foreground": "#474747",
"name": "undefined"
Expand All @@ -112,4 +100,4 @@
"is_regex": true
}
}
}
}
1 change: 1 addition & 0 deletions messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
"install": "messages/install.txt",
"3.0.0": "messages/3.0.0.txt",
"3.0.1": "messages/3.0.1.txt",
"3.0.2": "messages/3.0.2.txt"
}
10 changes: 10 additions & 0 deletions messages/3.0.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Version 3.0.2 (May 18, 2020)
----------------------------

* NEW Setting: disabled_syntax
- Now you can specify if colored comments
should be disabled on certain syntax's

* NEW Command: Colored Comments: Clear Colorization
- Useful when the file was edited outside of
Sublime Text and the color regions were messed up
19 changes: 10 additions & 9 deletions plugin/color_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import sublime


sublime_settings = "Preferences.sublime-settings"
override_path = "Colored Comments Override"
scope_name = "colored.comments.color."
Expand All @@ -12,10 +11,10 @@ class ColorManager:
def __init__(self, tags):
self.tags = tags

def remove_override(self, scheme):
def remove_override(self, scheme) -> None:
self.save_scheme(os.path.basename(scheme), {"rules": [], "variables": {}})

def create_user_custom_theme(self):
def create_user_custom_theme(self) -> None:
if not self.tags:
return

Expand Down Expand Up @@ -44,11 +43,11 @@ def _add_colors_to_scheme(self, scheme_content: dict) -> dict:
scope = "{}{}".format(scope_name, name.lower().replace(" ", "."))
if not any(rule.get("scope") == scope for rule in rules):
entry = {
"name": "[Colored Comments] {}".format(name.title()),
"scope": scope,
"foreground": foreground,
"background": background
}
"name": "[Colored Comments] {}".format(name.title()),
"scope": scope,
"foreground": foreground,
"background": background,
}
rules.append(entry)
scheme_content["rules"] = rules
return scheme_content
Expand All @@ -60,7 +59,9 @@ def _build_scheme_path(scheme: str) -> str:


def _create_override_path() -> None:
return os.makedirs(os.path.join(sublime.packages_path(), override_path), exist_ok=True)
return os.makedirs(
os.path.join(sublime.packages_path(), override_path), exist_ok=True
)


def _get_color_property(property: str, tags: dict) -> str:
Expand Down
Loading

0 comments on commit 1026030

Please sign in to comment.