Code cleanup.
Full tested and does not break and old tests.
This commit is contained in:
parent
5c87806d7b
commit
20f3832ffa
@ -24,66 +24,57 @@ class SnippetSource(object):
|
|||||||
|
|
||||||
Returns a list of SnippetDefinition s.
|
Returns a list of SnippetDefinition s.
|
||||||
"""
|
"""
|
||||||
found_snippets = []
|
def __inner(fts):
|
||||||
for ft in filetypes:
|
result = []
|
||||||
found_snippets += self._find_snippets(ft, before, possible)
|
existing_fts = filter(lambda ft: ft in self._snippets, fts)
|
||||||
return found_snippets
|
for ft in existing_fts:
|
||||||
|
snips = self._snippets[ft]
|
||||||
|
result.extend(snips.get_matching_snippets(before, possible))
|
||||||
|
return result
|
||||||
|
return __inner(filetypes) + __inner(self._extends_all(filetypes))
|
||||||
|
|
||||||
def get_clear_priority(self, filetypes, seen=None):
|
def get_clear_priority(self, filetypes):
|
||||||
priority = None
|
def __inner(fts):
|
||||||
if not seen:
|
pri = None
|
||||||
seen = set()
|
existing_fts = filter(lambda ft: ft in self._snippets, fts)
|
||||||
for ft in filetypes:
|
for ft in existing_fts:
|
||||||
seen.add(ft)
|
snippets = self._snippets[ft]
|
||||||
snippets = self._snippets[ft]
|
if pri is None or snippets._clear_priority > pri:
|
||||||
if priority is None or snippets._clear_priority > priority:
|
pri = snippets._clear_priority
|
||||||
priority = snippets._clear_priority
|
return pri
|
||||||
todo_parent_fts = []
|
priority = __inner(filetypes)
|
||||||
for ft in filetypes:
|
deep_clear_priority = __inner(self._extends_all(filetypes))
|
||||||
extends = self._snippets[ft].extends
|
if deep_clear_priority is None:
|
||||||
havnt_seen = filter(lambda ft: ft not in seen, extends)
|
return priority
|
||||||
|
elif priority is None:
|
||||||
|
return deep_clear_priority
|
||||||
|
else:
|
||||||
|
return max(priority, deep_clear_priority)
|
||||||
|
|
||||||
|
def get_cleared(self, filetypes):
|
||||||
|
def __inner(fts):
|
||||||
|
cleared = {}
|
||||||
|
existing_fts = filter(lambda ft: ft in self._snippets, fts)
|
||||||
|
for ft in existing_fts:
|
||||||
|
snippets = self._snippets[ft]
|
||||||
|
for key, value in snippets._cleared.items():
|
||||||
|
if key not in cleared or value > cleared[key]:
|
||||||
|
cleared[key] = value
|
||||||
|
return cleared
|
||||||
|
return dict(__inner(filetypes).items() + __inner(self._extends_all(filetypes)).items())
|
||||||
|
|
||||||
|
def _extends_all(self, filetypes, seen=None):
|
||||||
|
"""Return deep extends dependency, excluding the filetype itself
|
||||||
|
"""
|
||||||
|
if seen is None:
|
||||||
|
seen = set(filetypes)
|
||||||
|
|
||||||
|
shallow_extends = set()
|
||||||
|
for filetype in filetypes:
|
||||||
|
ft_extends = self._snippets[filetype].extends
|
||||||
|
havnt_seen = set(filter(lambda ft: ft not in seen, ft_extends))
|
||||||
seen.update(havnt_seen)
|
seen.update(havnt_seen)
|
||||||
todo_parent_fts.extend(havnt_seen)
|
shallow_extends.update(havnt_seen)
|
||||||
if todo_parent_fts:
|
if not shallow_extends:
|
||||||
return max(priority, self.get_clear_priority(todo_parent_fts, seen))
|
return shallow_extends
|
||||||
return priority
|
return shallow_extends | self._extends_all(shallow_extends, seen)
|
||||||
|
|
||||||
def get_cleared(self, filetypes, seen=None):
|
|
||||||
cleared = {}
|
|
||||||
if not seen:
|
|
||||||
seen = set()
|
|
||||||
for ft in filetypes:
|
|
||||||
seen.add(ft)
|
|
||||||
snippets = self._snippets[ft]
|
|
||||||
for key, value in snippets._cleared.items():
|
|
||||||
if key not in cleared or value > cleared[key]:
|
|
||||||
cleared[key] = value
|
|
||||||
todo_parent_fts = []
|
|
||||||
for ft in filetypes:
|
|
||||||
extends = self._snippets[ft].extends
|
|
||||||
havnt_seen = filter(lambda ft: ft not in seen, extends)
|
|
||||||
seen.update(havnt_seen)
|
|
||||||
todo_parent_fts.extend(havnt_seen)
|
|
||||||
if todo_parent_fts:
|
|
||||||
cleared.update(self.get_cleared(todo_parent_fts, seen))
|
|
||||||
return cleared
|
|
||||||
|
|
||||||
def _find_snippets(self, ft, trigger, potentially=False, seen=None):
|
|
||||||
"""Find snippets matching 'trigger' for 'ft'. If 'potentially' is True,
|
|
||||||
partial matches are enough."""
|
|
||||||
snips = self._snippets.get(ft, None)
|
|
||||||
if not snips:
|
|
||||||
return []
|
|
||||||
if not seen:
|
|
||||||
seen = set()
|
|
||||||
seen.add(ft)
|
|
||||||
parent_results = []
|
|
||||||
# TODO(sirver): extends information is not bound to one
|
|
||||||
# source. It should be tracked further up.
|
|
||||||
for parent_ft in snips.extends:
|
|
||||||
if parent_ft not in seen:
|
|
||||||
seen.add(parent_ft)
|
|
||||||
parent_results += self._find_snippets(parent_ft, trigger,
|
|
||||||
potentially, seen)
|
|
||||||
return parent_results + snips.get_matching_snippets(
|
|
||||||
trigger, potentially)
|
|
||||||
|
@ -30,13 +30,13 @@ class SnippetDictionary(object):
|
|||||||
"""Mark snippets as cleared with priority. When 'triggers' is
|
"""Mark snippets as cleared with priority. When 'triggers' is
|
||||||
None, then it updated the clear_priority."""
|
None, then it updated the clear_priority."""
|
||||||
if not triggers:
|
if not triggers:
|
||||||
if self._clear_priority is None or priority > self._clear_priority:
|
if self._clear_priority is None or priority > self._clear_priority:
|
||||||
self._clear_priority = priority
|
self._clear_priority = priority
|
||||||
else:
|
else:
|
||||||
for trigger in triggers:
|
for trigger in triggers:
|
||||||
if (trigger not in self._cleared or
|
if (trigger not in self._cleared or
|
||||||
priority > self._cleared[trigger]):
|
priority > self._cleared[trigger]):
|
||||||
self._cleared[trigger] = priority
|
self._cleared[trigger] = priority
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extends(self):
|
def extends(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user