Skip to content

Commit 5a61122

Browse files
committed
fixed linting colors, added settings for linting colors
1 parent f4f405e commit 5a61122

4 files changed

Lines changed: 171 additions & 10 deletions

File tree

SublimePython.sublime-settings

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@
2727
"python_linter_mark_style": "outline", // "none" or "outline"
2828
"python_linter_gutter_marks": true,
2929
"python_linter_gutter_marks_theme": "simple", // see folder gutter_mark_themes
30+
31+
// linting colors
32+
"warning_color":"EDBA00",
33+
"error_color":"DA2000",
34+
3035
"pep8": true,
36+
// disable certain pep8 errors
37+
// overview of possible error codes at http://pep8.readthedocs.org/en/latest/intro.html#error-codes
3138
"pep8_ignore": [],
3239
"pep8_max_line_length": 80,
3340
"pyflakes_ignore": [],

sublime_python.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from functools import wraps
1212
from inspect import getargspec
1313

14+
from SublimePythonIDE import sublime_python_colors
15+
1416
# contains root paths for each view, see root_folder_for()
1517
ROOT_PATHS = {}
1618
# contains proxy objects for external Python processes, by interpreter used
@@ -44,6 +46,13 @@
4446
DEFAULT_VENV_DIR_NAME = "venv"
4547

4648

49+
def plugin_loaded():
50+
_update_color_scheme()
51+
52+
s = sublime.load_settings('SublimePython.sublime-settings')
53+
s.add_on_change('sublimepython-pref-settings', _update_color_scheme)
54+
55+
4756
def get_setting(key, view=None, default_value=None):
4857
if view is None:
4958
view = get_current_active_view()
@@ -395,6 +404,17 @@ def wrapperN(self, view, *args):
395404
return wrapperN
396405

397406

407+
def _update_color_scheme():
408+
'''Updates the current color scheme to include error and warning
409+
scopes used by the linting features'''
410+
411+
colors = {
412+
"warning": get_setting("warning_color"),
413+
"error": get_setting("error_color")
414+
}
415+
sublime_python_colors.update_color_scheme(colors)
416+
417+
398418
class SimpleClearAndInsertCommand(sublime_plugin.TextCommand):
399419

400420
'''utility command class for writing into the documentation view'''

sublime_python_colors.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import os
2+
from xml.etree import ElementTree
3+
import sublime
4+
5+
# color-related constants
6+
7+
DEFAULT_MARK_COLORS = {'warning': 'EDBA00', 'error': 'DA2000', 'gutter': 'FFFFFF'}
8+
9+
COLOR_SCHEME_PREAMBLE = '''<?xml version="1.0" encoding="UTF-8"?>
10+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
11+
'''
12+
13+
COLOR_SCHEME_STYLES = {
14+
'warning': '''
15+
<dict>
16+
<key>name</key>
17+
<string>SublimePythonIDE Warning</string>
18+
<key>scope</key>
19+
<string>sublimepythonide.mark.warning</string>
20+
<key>settings</key>
21+
<dict>
22+
<key>foreground</key>
23+
<string>#{}</string>
24+
</dict>
25+
</dict>
26+
''',
27+
28+
'error': '''
29+
<dict>
30+
<key>name</key>
31+
<string>SublimePythonIDE Error</string>
32+
<key>scope</key>
33+
<string>sublimepythonide.mark.error</string>
34+
<key>settings</key>
35+
<dict>
36+
<key>foreground</key>
37+
<string>#{}</string>
38+
</dict>
39+
</dict>
40+
''',
41+
42+
'gutter': '''
43+
<dict>
44+
<key>name</key>
45+
<string>SublimePythonIDE Gutter Mark</string>
46+
<key>scope</key>
47+
<string>sublimepythonide.gutter-mark</string>
48+
<key>settings</key>
49+
<dict>
50+
<key>foreground</key>
51+
<string>#FFFFFF</string>
52+
</dict>
53+
</dict>
54+
'''
55+
}
56+
57+
# maps scopes to style names
58+
style_map = {
59+
"sublimepythonide.gutter-mark": "gutter",
60+
"sublimepythonide.mark.warning": "warning",
61+
"sublimepythonide.mark.error": "error"
62+
}
63+
64+
65+
def update_color_scheme(colors):
66+
"""
67+
Adapted from SublimeLinter
68+
Asynchronously call generate_color_scheme_async.
69+
Modify the current color scheme to contain SublimePythonIDE color entries as
70+
set in SublimePython.sublime-settings
71+
"""
72+
73+
def generate_color_scheme_async():
74+
# find and parse current theme
75+
prefs = sublime.load_settings("Preferences.sublime-settings")
76+
scheme = prefs.get('color_scheme')
77+
78+
if scheme is None:
79+
return
80+
81+
scheme_text = sublime.load_resource(scheme)
82+
plist = ElementTree.XML(scheme_text)
83+
dicts = plist.find('./dict/array')
84+
85+
# find all SublimePythonIDE style infos in the theme and update if necessary
86+
change = False
87+
found_styles = {"gutter": False, "warning": False, "error": False}
88+
for d in dicts.findall("./dict"):
89+
for c in d.getchildren():
90+
if "sublimepythonide" in c.text:
91+
style = style_map.get(c.text)
92+
color_elem = d.find("./dict/string")
93+
found_color = color_elem.text.upper().lstrip("#")
94+
target_color = colors.get(style, DEFAULT_MARK_COLORS[style]).upper().lstrip("#")
95+
if found_color != target_color:
96+
change = True
97+
color_elem.text = "#" + target_color
98+
found_styles[style] = True
99+
break
100+
101+
# add defaults for all styles that were not found
102+
for style in [s for s, found in found_styles.items() if not found]:
103+
color = colors.get(style, DEFAULT_MARK_COLORS[style])
104+
color = color.lstrip('#')
105+
dicts.append(
106+
ElementTree.XML(COLOR_SCHEME_STYLES[style].format(color)))
107+
change = True
108+
109+
# only write new theme if necessary
110+
if not change:
111+
return
112+
113+
# write new theme
114+
scheme_path = os.path.join(os.path.split(sublime.packages_path())[0], scheme)
115+
116+
with open(scheme_path, 'w', encoding='utf8') as f:
117+
f.write(COLOR_SCHEME_PREAMBLE)
118+
f.write(ElementTree.tostring(plist, encoding='unicode'))
119+
120+
prefs.set('color_scheme', scheme)
121+
sublime.save_settings("Preferences.sublime-settings")
122+
123+
# run async
124+
sublime.set_timeout_async(generate_color_scheme_async, 0)

sublime_python_linting.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ def _update_lint_marks(view, lines):
159159

160160
_erase_lint_marks(view)
161161

162-
for name, underlines in _get_types(view).items():
163-
if len(underlines) > 0:
164-
view.add_regions(
165-
'lint-underline-{name}'.format(name=name),
166-
underlines,
167-
'python_linter.underline.{name}'.format(name=name),
168-
flags=sublime.DRAW_EMPTY_AS_OVERWRITE
169-
)
162+
# for name, underlines in _get_types(view).items():
163+
# if len(underlines) > 0:
164+
# view.add_regions(
165+
# 'lint-underline-{name}'.format(name=name),
166+
# underlines,
167+
# scope_name(name),
168+
# flags=sublime.DRAW_EMPTY_AS_OVERWRITE
169+
# )
170170

171171
if len(lines) > 0:
172172
outlines = _get_outlines(view)
@@ -175,14 +175,23 @@ def _update_lint_marks(view, lines):
175175
args = [
176176
'lint-outlines-{0}'.format(lint_type),
177177
outlines[lint_type],
178-
'python_linter.outline.{0}'.format(lint_type),
178+
scope_name(lint_type),
179179
_get_gutter_mark_theme(view, lint_type),
180180
outline_style.get(style, sublime.DRAW_OUTLINED)
181181
]
182182

183183
view.add_regions(*args)
184184

185185

186+
def scope_name(error_name):
187+
result = {
188+
"violation": "sublimepythonide.mark.error",
189+
"illegal": "sublimepythonide.mark.error",
190+
"warning": "sublimepythonide.mark.warning",
191+
}.get(error_name)
192+
return result
193+
194+
186195
def add_message(lineno, lines, message, messages):
187196
# Assume lineno is one-based, ST2 wants zero-based line numbers
188197
lineno -= 1
@@ -308,7 +317,8 @@ def underline_duplicate_argument(lineno, word, underlines):
308317
pyflakes.messages.UndefinedExport,
309318
pyflakes.messages.UndefinedLocal,
310319
pyflakes.messages.Redefined,
311-
pyflakes.messages.UnusedVariable)):
320+
pyflakes.messages.UnusedVariable,
321+
pyflakes.messages.RedefinedInListComp)):
312322
underline_word(error.lineno, error.message_args[0], underlines)
313323
elif isinstance(error, pyflakes.messages.ImportShadowedByLoopVar):
314324
underline_for_var(

0 commit comments

Comments
 (0)