in colorSchemeTool.py [0:0]
def find_by_scope(settings, scope):
# compound scope
less_specific = None
less_specific_weight = 0
less_specific_selector_size = 0
# simple scope (without whitespaces and '-')
ss_less_specific = None
ss_less_specific_weight = 0
for setting in settings:
scope_of_setting = setting.get('scope', None)
if scope_of_setting is None:
if scope is None: return setting
else:
if not isinstance(scope_of_setting, list):
scopes_of_setting = scope_of_setting.split(",")
else:
scopes_of_setting = scope_of_setting
for aScope in scopes_of_setting:
aScope = aScope.strip()
# ignore excludes in scopes selectors,
# more accurate parsing/matching required!
chain_without_excludes = aScope.split(' -')[0]
aScope_selectors = chain_without_excludes.split(' ')
# We need:
# 1. "Match the element deepest down in the scope e.g. string wins over source.php when the scope is source.php string.quoted."
# it is very simple implementation of above rule
matchingScope = aScope_selectors[-1]
if matchingScope is None:
continue
# Consider scope size as scope size until first not excluded element
aScopeSelectorSize = 0
for i in range(0, len(aScope_selectors) - 1):
aScopeSelectorSize = len(aScope_selectors[i].strip())
isSimpleScope = (aScopeSelectorSize == 0)
if matchingScope == scope:
if isSimpleScope:
return setting
less_specific = setting
less_specific_weight = len(matchingScope)
less_specific_selector_size = aScopeSelectorSize
if scope is not None and scope.startswith(matchingScope):
# We need:
# 2. "Match most of the deepest element e.g. string.quoted wins over string."
# so let's consider matched symbols count as weight
new_match_weight = len(matchingScope)
weight = ss_less_specific_weight if isSimpleScope else less_specific_weight
if new_match_weight > weight:
if isSimpleScope:
ss_less_specific = setting
ss_less_specific_weight = new_match_weight
else:
less_specific = setting
less_specific_weight = new_match_weight
less_specific_selector_size = aScopeSelectorSize
else:
# if matched part is equal and scope isn't simple - let's choose the shortest scope
# in general case should work better, because some where particular complicated
# scope won't override similar but more general scope
if not isSimpleScope and (new_match_weight == weight):
if less_specific_selector_size > aScopeSelectorSize:
less_specific = setting
less_specific_weight = new_match_weight
less_specific_selector_size = aScopeSelectorSize
return ss_less_specific if (ss_less_specific is not None) else less_specific