Skip to content

Commit

Permalink
Identify root nodes by other means
Browse files Browse the repository at this point in the history
Can't believe I was building a list of root node names - haskell,
  program, module, these kinds of things, each language having its own
  - when we can already easily tell when a node is a root node,
    l0lz. So now we just do that.
  • Loading branch information
aaronik committed Dec 17, 2024
1 parent 4e12c48 commit 12d8426
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
26 changes: 16 additions & 10 deletions lua/treewalker/nodes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ local TARGET_BLACKLIST_TYPE_MATCHERS = {
}

local HIGHLIGHT_BLACKLIST_TYPE_MATCHERS = {
"module", -- python
"chunk", -- lua
"body", -- ruby
"block", -- ruby
"program", -- ruby
"haskell", -- guess which language starts their module tree with this node
"translation_unit", -- c module
"source_file", -- rust
"body",
"block",
}


Expand All @@ -32,16 +26,28 @@ local function is_matched_in(node, matchers)
return false
end

---@param node TSNode
---@return boolean
local function is_root_node(node)
return node:parent() == nil
end

---@param node TSNode
---@return boolean
function M.is_jump_target(node)
return not is_matched_in(node, TARGET_BLACKLIST_TYPE_MATCHERS)
return
true
and not is_matched_in(node, TARGET_BLACKLIST_TYPE_MATCHERS)
and not is_root_node(node)
end

---@param node TSNode
---@return boolean
function M.is_highlight_target(node)
return not is_matched_in(node, HIGHLIGHT_BLACKLIST_TYPE_MATCHERS)
return
true
and not is_matched_in(node, HIGHLIGHT_BLACKLIST_TYPE_MATCHERS)
and not is_root_node(node)
end

---Do the nodes have the same starting point
Expand Down
13 changes: 13 additions & 0 deletions tests/fixtures/python.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from functools import wraps

class Person:
def __init__(self, name):
self.name = name
Expand All @@ -22,6 +24,17 @@ def __init__(self, make, model, year):
def display_info(self):
print(f"Make: {self.make}, Model: {self.model}, Year: {self.year}")


def random_annotation(func):
@wraps(func)
def wrapper(*args, **kwargs):
# Perform some task related to randomness
print("Random thing happening")
return func(*args, **kwargs)
return wrapper


@random_annotation
def main():
"""
This function demonstrates a nested structure.
Expand Down

0 comments on commit 12d8426

Please sign in to comment.