mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-06-22 23:19:25 -04:00
Use typelib instead of XML
For normal compilation, use .typelib files rather than .gir XML files. This is much faster. Rather than using libgirepository, which would try to actually load the libraries, we use a custom parser. The language server will still read XML because it needs to access documentation, which is not in the typelib, but that's generally fine because it's a long lived process and only has to do that once.
This commit is contained in:
parent
e78fae4f12
commit
06f54c8ff8
10 changed files with 647 additions and 170 deletions
|
@ -36,7 +36,7 @@ class Element:
|
|||
def __init__(self, tag, attrs: T.Dict[str, str]):
|
||||
self.tag = tag
|
||||
self.attrs = attrs
|
||||
self.children: T.Dict[str, T.List["Element"]] = defaultdict(list)
|
||||
self.children: T.List["Element"] = []
|
||||
self.cdata_chunks: T.List[str] = []
|
||||
|
||||
@cached_property
|
||||
|
@ -44,50 +44,42 @@ class Element:
|
|||
return ''.join(self.cdata_chunks)
|
||||
|
||||
def get_elements(self, name) -> T.List["Element"]:
|
||||
return self.children.get(name, [])
|
||||
return [
|
||||
child
|
||||
for child in self.children
|
||||
if child.tag == name
|
||||
]
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.attrs.get(key)
|
||||
|
||||
|
||||
class Handler(sax.handler.ContentHandler):
|
||||
def __init__(self, parse_type):
|
||||
def __init__(self):
|
||||
self.root = None
|
||||
self.stack = []
|
||||
self.skipping = 0
|
||||
self._interesting_elements = parse_type
|
||||
|
||||
def startElement(self, name, attrs):
|
||||
if self._interesting_elements is not None and name not in self._interesting_elements:
|
||||
self.skipping += 1
|
||||
if self.skipping > 0:
|
||||
return
|
||||
|
||||
element = Element(name, attrs.copy())
|
||||
|
||||
if len(self.stack):
|
||||
last = self.stack[-1]
|
||||
last.children[name].append(element)
|
||||
last.children.append(element)
|
||||
else:
|
||||
self.root = element
|
||||
|
||||
self.stack.append(element)
|
||||
|
||||
|
||||
def endElement(self, name):
|
||||
if self.skipping == 0:
|
||||
self.stack.pop()
|
||||
if self._interesting_elements is not None and name not in self._interesting_elements:
|
||||
self.skipping -= 1
|
||||
self.stack.pop()
|
||||
|
||||
def characters(self, content):
|
||||
if not self.skipping:
|
||||
self.stack[-1].cdata_chunks.append(content)
|
||||
self.stack[-1].cdata_chunks.append(content)
|
||||
|
||||
|
||||
def parse(filename, parse_type=None):
|
||||
def parse(filename):
|
||||
parser = sax.make_parser()
|
||||
handler = Handler(parse_type)
|
||||
handler = Handler()
|
||||
parser.setContentHandler(handler)
|
||||
parser.parse(filename)
|
||||
return handler.root
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue