Skip to content

Commit

Permalink
perf: Avoid reconstructing the TS query
Browse files Browse the repository at this point in the history
  • Loading branch information
caksoylar committed Nov 16, 2024
1 parent 20fd314 commit 4c3d554
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions keymap_drawer/dts.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,15 @@ def include_handler(*args): # type: ignore

def get_compatible_nodes(self, compatible_value: str) -> list[DTNode]:
"""Return a list of nodes that have the given compatible value."""
nodes = chain.from_iterable(
TS_LANG.query(
rf"""
(node
(property name: (identifier) @prop value: (string_literal) @propval)
(#eq? @prop "compatible") (#eq? @propval "\"{compatible_value}\"")
) @node
"""
)
.captures(node)
.get("node", [])
for node in self.root_nodes
query = TS_LANG.query(
rf"""
(node
(property name: (identifier) @prop value: (string_literal) @propval)
(#eq? @prop "compatible") (#eq? @propval "\"{compatible_value}\"")
) @node
"""
)
nodes = chain.from_iterable(query.captures(node).get("node", []) for node in self.root_nodes)
return sorted(
(DTNode(node, self.ts_buffer, self.override_nodes) for node in nodes), key=lambda x: x.node.start_byte
)
Expand Down

0 comments on commit 4c3d554

Please sign in to comment.