Mercurial > public > mercurial-scm > hg
comparison mercurial/templater.py @ 38355:d4fae9a0ab1f
templater: add function to look up symbols used in template
Formatter can use this information to enable slow paths such as loading
ctx object only when necessary.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Thu, 03 May 2018 11:53:56 +0900 |
parents | e637dc0b3b1f |
children | f79237942dec |
comparison
equal
deleted
inserted
replaced
38354:e637dc0b3b1f | 38355:d4fae9a0ab1f |
---|---|
840 x = parse(tmpl) | 840 x = parse(tmpl) |
841 if self._aliasmap: | 841 if self._aliasmap: |
842 x = _aliasrules.expand(self._aliasmap, x) | 842 x = _aliasrules.expand(self._aliasmap, x) |
843 return x | 843 return x |
844 | 844 |
845 def _findsymbolsused(self, tree, syms): | |
846 if not tree: | |
847 return | |
848 op = tree[0] | |
849 if op == 'symbol': | |
850 s = tree[1] | |
851 if s in syms[0]: | |
852 return # avoid recursion: s -> cache[s] -> s | |
853 syms[0].add(s) | |
854 if s in self.cache or s in self._map: | |
855 # s may be a reference for named template | |
856 self._findsymbolsused(self.load(s), syms) | |
857 return | |
858 if op in {'integer', 'string'}: | |
859 return | |
860 # '{arg|func}' == '{func(arg)}' | |
861 if op == '|': | |
862 syms[1].add(getsymbol(tree[2])) | |
863 self._findsymbolsused(tree[1], syms) | |
864 return | |
865 if op == 'func': | |
866 syms[1].add(getsymbol(tree[1])) | |
867 self._findsymbolsused(tree[2], syms) | |
868 return | |
869 for x in tree[1:]: | |
870 self._findsymbolsused(x, syms) | |
871 | |
872 def symbolsuseddefault(self): | |
873 """Look up (keywords, filters/functions) referenced from the default | |
874 unnamed template | |
875 | |
876 This may load additional templates from the map file. | |
877 """ | |
878 return self.symbolsused('') | |
879 | |
880 def symbolsused(self, t): | |
881 """Look up (keywords, filters/functions) referenced from the name | |
882 template 't' | |
883 | |
884 This may load additional templates from the map file. | |
885 """ | |
886 syms = (set(), set()) | |
887 self._findsymbolsused(self.load(t), syms) | |
888 return syms | |
889 | |
845 def renderdefault(self, mapping): | 890 def renderdefault(self, mapping): |
846 """Render the default unnamed template and return result as string""" | 891 """Render the default unnamed template and return result as string""" |
847 return self.render('', mapping) | 892 return self.render('', mapping) |
848 | 893 |
849 def render(self, t, mapping): | 894 def render(self, t, mapping): |