Skip to content

Commit f0b2965

Browse files
simplify context tracking
1 parent 725e7cf commit f0b2965

File tree

2 files changed

+3
-126
lines changed

2 files changed

+3
-126
lines changed

crates/pgls_hover/src/hovered_node.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,9 @@ impl HoveredNode {
4747
"schema_identifier" => Some(HoveredNode::Schema(node_content)),
4848
"role_identifier" => Some(HoveredNode::Role(node_content)),
4949

50-
"any_identifier"
51-
if ctx.matches_ancestor_history(&["table_reference"])
52-
|| ctx
53-
.matches_ancestor_history(&["grantable_on_table", "object_reference"]) =>
54-
{
55-
let num_sibs = ctx.num_siblings();
56-
if ctx.node_under_cursor_is_nth_child(1) && num_sibs > 0 {
57-
return Some(HoveredNode::Schema(node_content));
58-
}
59-
60-
Some(HoveredNode::Table((
61-
ctx.tail_qualifier_sanitized(),
62-
node_content,
63-
)))
64-
}
50+
"any_identifier" if ctx.matches_ancestor_history(&["table_reference"]) => Some(
51+
HoveredNode::Table((ctx.tail_qualifier_sanitized(), node_content)),
52+
),
6553

6654
"any_identifier"
6755
if ctx.matches_ancestor_history(&["object_reference"])
@@ -100,17 +88,6 @@ impl HoveredNode {
10088
)))
10189
}
10290

103-
"any_identifier"
104-
if ctx.matches_one_of_ancestors(&[
105-
"alter_role",
106-
"policy_to_role",
107-
"role_specification",
108-
]) || ctx.before_cursor_matches_kind(&["keyword_revoke"]) =>
109-
{
110-
Some(HoveredNode::Role(node_content))
111-
}
112-
"grant_role" | "policy_role" => Some(HoveredNode::Role(node_content)),
113-
11491
"any_identifier"
11592
if (
11693
// hover over custom type in `create table` or `returns`

crates/pgls_treesitter/src/context/mod.rs

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -631,75 +631,6 @@ impl<'a> TreesitterContext<'a> {
631631
.current()
632632
.ancestors
633633
.matches_history(expected_ancestors)
634-
635-
// self.node_under_cursor.as_ref().is_some_and(|node| {
636-
// let mut current = Some(*node);
637-
638-
// for &expected_kind in expected_ancestors.iter().rev() {
639-
// current = current.and_then(|n| n.parent());
640-
641-
// match current {
642-
// Some(ancestor) if ancestor.kind() == expected_kind => continue,
643-
// _ => return false,
644-
// }
645-
// }
646-
647-
// true
648-
// })
649-
}
650-
651-
/// Verifies if the node has one of the named direct ancestors.
652-
pub fn matches_one_of_ancestors(&self, expected_ancestors: &[&'static str]) -> bool {
653-
self.node_under_cursor.as_ref().is_some_and(|node| {
654-
node.parent()
655-
.is_some_and(|p| expected_ancestors.contains(&p.kind()))
656-
})
657-
}
658-
659-
/// Checks whether the Node under the cursor is the nth child of the parent.
660-
///
661-
/// ```
662-
/// /*
663-
/// * Given `select * from "a|uth"."users";`
664-
/// * The node under the cursor is "auth".
665-
/// *
666-
/// * [...] redacted
667-
/// * from [9..28] 'from "auth"."users"'
668-
/// * keyword_from [9..13] 'from'
669-
/// * relation [14..28] '"auth"."users"'
670-
/// * object_reference [14..28] '"auth"."users"'
671-
/// * any_identifier [14..20] '"auth"'
672-
/// * . [20..21] '.'
673-
/// * any_identifier [21..28] '"users"'
674-
/// */
675-
///
676-
/// if node_under_cursor_is_nth_child(1) {
677-
/// node_type = "schema";
678-
/// } else if node_under_cursor_is_nth_child(3) {
679-
/// node_type = "table";
680-
/// }
681-
/// ```
682-
pub fn node_under_cursor_is_nth_child(&self, nth: usize) -> bool {
683-
self.node_under_cursor.as_ref().is_some_and(|node| {
684-
let mut cursor = node.walk();
685-
node.parent().is_some_and(|p| {
686-
p.children(&mut cursor)
687-
.nth(nth - 1)
688-
.is_some_and(|n| n.id() == node.id())
689-
})
690-
})
691-
}
692-
693-
/// Returns the number of siblings of the node under the cursor.
694-
pub fn num_siblings(&self) -> usize {
695-
self.node_under_cursor
696-
.as_ref()
697-
.map(|node| {
698-
// if there's no parent, we're on the top of the tree,
699-
// where we have 0 siblings.
700-
node.parent().map(|p| p.child_count() - 1).unwrap_or(0)
701-
})
702-
.unwrap_or(0)
703634
}
704635

705636
/// Returns true if the node under the cursor matches the field_name OR has a parent that matches the field_name.
@@ -708,37 +639,6 @@ impl<'a> TreesitterContext<'a> {
708639
.current()
709640
.ancestors
710641
.is_within_one_of_fields(names)
711-
// self.node_under_cursor
712-
// .as_ref()
713-
// .map(|node| {
714-
// // It might seem weird that we have to check for the field_name from the parent,
715-
// // but TreeSitter wants it this way, since nodes often can only be named in
716-
// // the context of their parents.
717-
// let root_node = self.tree.root_node();
718-
// let mut cursor = node.walk();
719-
// let mut parent = node.parent();
720-
721-
// while let Some(p) = parent {
722-
// if p == root_node {
723-
// break;
724-
// }
725-
726-
// for name in names {
727-
// if p.children_by_field_name(name, &mut cursor).any(|c| {
728-
// let r = c.range();
729-
// // if the parent range contains the node range, the node is of the field_name.
730-
// r.start_byte <= node.start_byte() && r.end_byte >= node.end_byte()
731-
// }) {
732-
// return true;
733-
// }
734-
// }
735-
736-
// parent = p.parent();
737-
// }
738-
739-
// false
740-
// })
741-
// .unwrap_or(false)
742642
}
743643

744644
pub fn get_mentioned_relations(&self, key: &Option<String>) -> Option<&HashSet<String>> {

0 commit comments

Comments
 (0)