Created
November 16, 2018 10:39
-
-
Save gxfxyz/7edefb6b00f27be52ac061b4fd32fec3 to your computer and use it in GitHub Desktop.
Sublime Text 3 commands to turn on/off/toggle Flake8Lint E501 (line to long) check
This file contains hidden or 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Sublime Text 3 commands to turn on/off/toggle Flake8Lint E501 (line to long) check. | |
IF current file (view) is in a project: | |
Change will be saved to the project settings (in .sublime-project file). | |
Else: | |
Change will only temporarily affect current file (view). | |
How to use: | |
Save to your "/Sublime Text 3/Packages/User" directory. | |
""" | |
import sublime | |
import sublime_plugin | |
class E501EnableCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
to_enable = True | |
window = self.view.window() | |
if window.project_file_name(): | |
is_project = True | |
else: | |
is_project = False | |
g_f8l_settings = sublime.load_settings('Flake8Lint.sublime-settings') | |
g_ignores = g_f8l_settings.get('ignore', []) | |
view_settings = self.view.settings() | |
v_f8l_settings = view_settings.get('flake8lint', {}) | |
v_ignores = v_f8l_settings.get('ignore', []) | |
ignores = list(set(g_ignores + v_ignores)) | |
disabled = 'E501' in ignores | |
enabled = not disabled | |
if to_enable: | |
try: | |
ignores.remove('E501') | |
except ValueError: | |
pass | |
message = "E501 Warning: enabled" | |
else: | |
ignores.append('E501') | |
message = "E501 Warning: disabled" | |
v_f8l_settings['ignore'] = ignores | |
if is_project: | |
project_data = window.project_data() | |
project_settings = project_data.get('settings', {}) | |
project_settings['flake8lint'] = v_f8l_settings | |
project_data['settings'] = project_settings | |
window.set_project_data(project_data) | |
else: | |
view_settings.set('flake8lint', v_f8l_settings) | |
old_status = "enabled" if enabled else "disabled" | |
new_status = "enabled" if to_enable else "disabled" | |
message = "E501 Warning: {} -> {}".format(old_status, new_status) | |
sublime.status_message(message) | |
class E501DisableCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
to_enable = False | |
window = self.view.window() | |
if window.project_file_name(): | |
is_project = True | |
else: | |
is_project = False | |
g_f8l_settings = sublime.load_settings('Flake8Lint.sublime-settings') | |
g_ignores = g_f8l_settings.get('ignore', []) | |
view_settings = self.view.settings() | |
v_f8l_settings = view_settings.get('flake8lint', {}) | |
v_ignores = v_f8l_settings.get('ignore', []) | |
ignores = list(set(g_ignores + v_ignores)) | |
disabled = 'E501' in ignores | |
enabled = not disabled | |
if to_enable: | |
try: | |
ignores.remove('E501') | |
except ValueError: | |
pass | |
message = "E501 Warning: enabled" | |
else: | |
ignores.append('E501') | |
message = "E501 Warning: disabled" | |
v_f8l_settings['ignore'] = ignores | |
if is_project: | |
project_data = window.project_data() | |
project_settings = project_data.get('settings', {}) | |
project_settings['flake8lint'] = v_f8l_settings | |
project_data['settings'] = project_settings | |
window.set_project_data(project_data) | |
else: | |
view_settings.set('flake8lint', v_f8l_settings) | |
old_status = "enabled" if enabled else "disabled" | |
new_status = "enabled" if to_enable else "disabled" | |
message = "E501 Warning: {} -> {}".format(old_status, new_status) | |
sublime.status_message(message) | |
class E501ToggleCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
window = self.view.window() | |
if window.project_file_name(): | |
is_project = True | |
else: | |
is_project = False | |
g_f8l_settings = sublime.load_settings('Flake8Lint.sublime-settings') | |
g_ignores = g_f8l_settings.get('ignore', []) | |
view_settings = self.view.settings() | |
v_f8l_settings = view_settings.get('flake8lint', {}) | |
v_ignores = v_f8l_settings.get('ignore', []) | |
ignores = list(set(g_ignores + v_ignores)) | |
disabled = 'E501' in ignores | |
enabled = not disabled | |
to_enable = not enabled | |
if to_enable: | |
try: | |
ignores.remove('E501') | |
except ValueError: | |
pass | |
message = "E501 Warning: enabled" | |
else: | |
ignores.append('E501') | |
message = "E501 Warning: disabled" | |
v_f8l_settings['ignore'] = ignores | |
if is_project: | |
project_data = window.project_data() | |
project_settings = project_data.get('settings', {}) | |
project_settings['flake8lint'] = v_f8l_settings | |
project_data['settings'] = project_settings | |
window.set_project_data(project_data) | |
else: | |
view_settings.set('flake8lint', v_f8l_settings) | |
old_status = "enabled" if enabled else "disabled" | |
new_status = "enabled" if to_enable else "disabled" | |
message = "E501 Warning: {} -> {}".format(old_status, new_status) | |
sublime.status_message(message) |
This file contains hidden or 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
Show hidden characters
[{ | |
"caption": "E501 Warning: Disable", | |
"command": "e501_disable" | |
}, { | |
"caption": "E501 Warning: Enable", | |
"command": "e501_enable" | |
}, { | |
"caption": "E501 Warning: Toggle", | |
"command": "e501_toggle" | |
}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment