Add full support for conditionals.
This commit is contained in:
Родитель
57c76b5172
Коммит
dc11c7e796
19
DevSkim.py
19
DevSkim.py
|
@ -25,6 +25,8 @@ except:
|
|||
import sys
|
||||
sys.exit(1)
|
||||
|
||||
from . import DevSkimConditionals
|
||||
|
||||
# Non-Configurable Settings
|
||||
MIN_ST_VERSION = 3114
|
||||
MARK_FLAGS = sublime.DRAW_NO_FILL | sublime.HIDE_ON_MINIMAP
|
||||
|
@ -93,6 +95,9 @@ stylesheet_content = ""
|
|||
# Cache suppress days
|
||||
suppress_days = []
|
||||
|
||||
# Conditional Functions
|
||||
conditional_func_map = {}
|
||||
|
||||
class DevSkimEventListener(sublime_plugin.EventListener):
|
||||
"""Handles events from Sublime Text."""
|
||||
|
||||
|
@ -162,6 +167,12 @@ class DevSkimEventListener(sublime_plugin.EventListener):
|
|||
logger.handlers = []
|
||||
logger.addHandler(console)
|
||||
|
||||
# Initialize the conditionals
|
||||
for func in dir(DevSkimConditionals):
|
||||
if func.startswith('condition__'):
|
||||
func_short = func.replace('condition__', '')
|
||||
conditional_func_map[func_short] = getattr(DevSkimConditionals, func)
|
||||
|
||||
def clear_regions(self, view):
|
||||
"""Clear all regions."""
|
||||
global marked_regions, finding_list
|
||||
|
@ -390,10 +401,10 @@ class DevSkimEventListener(sublime_plugin.EventListener):
|
|||
return
|
||||
|
||||
window = view.window()
|
||||
|
||||
|
||||
if not single_line:
|
||||
self.clear_regions(view)
|
||||
|
||||
|
||||
# TRY
|
||||
_v = window.extract_variables()
|
||||
filename = _v.get('file', '').replace('\\', '/')
|
||||
|
@ -402,7 +413,7 @@ class DevSkimEventListener(sublime_plugin.EventListener):
|
|||
logger.info("File is ignored.")
|
||||
return
|
||||
# DONE
|
||||
|
||||
|
||||
self.lazy_initialize()
|
||||
|
||||
logger.debug("analyze_current_view()")
|
||||
|
@ -1111,7 +1122,7 @@ class DevSkimReloadRulesCommand(sublime_plugin.TextCommand):
|
|||
def plugin_loaded():
|
||||
"""Handle the plugin_loaded event from ST3."""
|
||||
logger.info('DevSkim plugin_loaded(), Sublime Text v%s' % sublime.version())
|
||||
|
||||
|
||||
|
||||
|
||||
def plugin_unloaded():
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
"""
|
||||
Copyright (c) 2017 Microsoft. All rights reserved.
|
||||
Licensed under the MIT License. See LICENSE.txt in the project root for license information.
|
||||
|
||||
DevSkim Sublime Text Plugin
|
||||
https://github.com/Microsoft/DevSkim-Sublime-Plugin
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
try:
|
||||
import sublime
|
||||
except Exception:
|
||||
print("Unable to import Sublime Text modules. DevSkim must be run within Sublime Text.")
|
||||
import sys
|
||||
sys.exit(1)
|
||||
|
||||
logger = logging.getLogger()
|
||||
|
||||
def condition__line_match_all(**kwargs):
|
||||
"""Are all elements of targets substrings of line?"""
|
||||
line = kwargs.get('line')
|
||||
targets = kwargs.get('value')
|
||||
logger.debug('condition__line_match_all({%s}, {%s})', line, targets)
|
||||
|
||||
line = line.lower()
|
||||
return all([t.lower() in line for t in targets])
|
||||
|
||||
def condition__line_match_any(**kwargs):
|
||||
"""Are any elements of value substrings of line?"""
|
||||
line = kwargs.get('line')
|
||||
targets = kwargs.get('value')
|
||||
logger.debug('condition__line_match_any({%s}, {%s})', line, targets)
|
||||
|
||||
line = line.lower()
|
||||
return any([t.lower() in line for t in targets])
|
||||
|
||||
def condition__line_match_none(**kwargs):
|
||||
"""Are none of the elements of targets substrings of line?"""
|
||||
line = kwargs.get('line')
|
||||
targets = kwargs.get('value')
|
||||
logger.debug('condition__line_match_any({%s}, {%s})', line, targets)
|
||||
|
||||
return not condition__line_match_any(**kwargs)
|
||||
|
||||
def condition__match_prefix_any(**kwargs):
|
||||
"""Are any of the targets a prefix to the matched region?"""
|
||||
match_start = kwargs.get('match_start')
|
||||
targets = kwargs.get('value')
|
||||
view = kwargs.get('view')
|
||||
match_region = kwargs.get('match_region')
|
||||
|
||||
for target in targets:
|
||||
region = sublime.Region(match_region.a - len(target), match_start)
|
||||
if view.substr(region).lower() == target.lower():
|
||||
return True
|
||||
return False
|
Загрузка…
Ссылка в новой задаче