Code cleanup.

Full tested and does not break and old tests.
This commit is contained in:
cwahbong 2014-07-16 22:57:24 +08:00
parent 5c87806d7b
commit 20f3832ffa
2 changed files with 58 additions and 67 deletions

View File

@ -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 priority is None or snippets._clear_priority > priority: if pri is None or snippets._clear_priority > pri:
priority = snippets._clear_priority pri = snippets._clear_priority
todo_parent_fts = [] return pri
for ft in filetypes: priority = __inner(filetypes)
extends = self._snippets[ft].extends deep_clear_priority = __inner(self._extends_all(filetypes))
havnt_seen = filter(lambda ft: ft not in seen, extends) if deep_clear_priority is None:
seen.update(havnt_seen)
todo_parent_fts.extend(havnt_seen)
if todo_parent_fts:
return max(priority, self.get_clear_priority(todo_parent_fts, seen))
return priority return priority
elif priority is None:
return deep_clear_priority
else:
return max(priority, deep_clear_priority)
def get_cleared(self, filetypes, seen=None): def get_cleared(self, filetypes):
def __inner(fts):
cleared = {} cleared = {}
if not seen: existing_fts = filter(lambda ft: ft in self._snippets, fts)
seen = set() for ft in existing_fts:
for ft in filetypes:
seen.add(ft)
snippets = self._snippets[ft] snippets = self._snippets[ft]
for key, value in snippets._cleared.items(): for key, value in snippets._cleared.items():
if key not in cleared or value > cleared[key]: if key not in cleared or value > cleared[key]:
cleared[key] = value 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 return cleared
return dict(__inner(filetypes).items() + __inner(self._extends_all(filetypes)).items())
def _find_snippets(self, ft, trigger, potentially=False, seen=None): def _extends_all(self, filetypes, seen=None):
"""Find snippets matching 'trigger' for 'ft'. If 'potentially' is True, """Return deep extends dependency, excluding the filetype itself
partial matches are enough.""" """
snips = self._snippets.get(ft, None) if seen is None:
if not snips: seen = set(filetypes)
return []
if not seen: shallow_extends = set()
seen = set() for filetype in filetypes:
seen.add(ft) ft_extends = self._snippets[filetype].extends
parent_results = [] havnt_seen = set(filter(lambda ft: ft not in seen, ft_extends))
# TODO(sirver): extends information is not bound to one seen.update(havnt_seen)
# source. It should be tracked further up. shallow_extends.update(havnt_seen)
for parent_ft in snips.extends: if not shallow_extends:
if parent_ft not in seen: return shallow_extends
seen.add(parent_ft) return shallow_extends | self._extends_all(shallow_extends, seen)
parent_results += self._find_snippets(parent_ft, trigger,
potentially, seen)
return parent_results + snips.get_matching_snippets(
trigger, potentially)