Skip to content

Commit 7e38cc6

Browse files
refactor: make node identification simpler
1 parent 376654e commit 7e38cc6

File tree

2 files changed

+78
-168
lines changed

2 files changed

+78
-168
lines changed

crates/pgls_hover/src/hovered_node.rs

Lines changed: 34 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
use pgls_treesitter::WrappingClause;
22

3-
#[derive(Debug)]
4-
pub(crate) enum NodeIdentification {
5-
Name(String),
6-
SchemaAndName((String, String)),
7-
#[allow(unused)]
8-
SchemaAndTableAndName((String, String, String)),
9-
}
3+
type NodeIdentification = (Option<String>, String);
104

115
#[allow(unused)]
126
#[derive(Debug)]
137
pub(crate) enum HoveredNode {
14-
Schema(NodeIdentification),
8+
Schema(String),
159
Table(NodeIdentification),
1610
Function(NodeIdentification),
17-
Column(NodeIdentification),
11+
Column((Option<String>, Option<String>, String)),
1812
Policy(NodeIdentification),
1913
Trigger(NodeIdentification),
20-
Role(NodeIdentification),
14+
Role(String),
2115
PostgresType(NodeIdentification),
2216
}
2317

@@ -39,17 +33,13 @@ impl HoveredNode {
3933
{
4034
let num_sibs = ctx.num_siblings();
4135
if ctx.node_under_cursor_is_nth_child(1) && num_sibs > 0 {
42-
return Some(HoveredNode::Schema(NodeIdentification::Name(node_content)));
36+
return Some(HoveredNode::Schema(node_content));
4337
}
4438

45-
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
46-
Some(HoveredNode::Table(NodeIdentification::SchemaAndName((
47-
schema.clone(),
48-
node_content,
49-
))))
50-
} else {
51-
Some(HoveredNode::Table(NodeIdentification::Name(node_content)))
52-
}
39+
Some(HoveredNode::Table((
40+
ctx.schema_or_alias_name.clone(),
41+
node_content,
42+
)))
5343
}
5444

5545
"any_identifier"
@@ -63,40 +53,25 @@ impl HoveredNode {
6353
)
6454
}) =>
6555
{
66-
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
67-
Some(HoveredNode::Table(NodeIdentification::SchemaAndName((
68-
schema.clone(),
69-
node_content,
70-
))))
71-
} else {
72-
Some(HoveredNode::Table(NodeIdentification::Name(node_content)))
73-
}
56+
Some(HoveredNode::Table((
57+
ctx.schema_or_alias_name.clone(),
58+
node_content,
59+
)))
7460
}
7561

76-
"column_identifier" => {
77-
if let Some(table_or_alias) = ctx.schema_or_alias_name.as_ref() {
78-
Some(HoveredNode::Column(NodeIdentification::SchemaAndName((
79-
table_or_alias.clone(),
80-
node_content,
81-
))))
82-
} else {
83-
Some(HoveredNode::Column(NodeIdentification::Name(node_content)))
84-
}
85-
}
62+
"column_identifier" => Some(HoveredNode::Column((
63+
None,
64+
ctx.schema_or_alias_name.clone(),
65+
node_content,
66+
))),
8667

8768
"any_identifier"
8869
if ctx.matches_ancestor_history(&["invocation", "object_reference"]) =>
8970
{
90-
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
91-
Some(HoveredNode::Function(NodeIdentification::SchemaAndName((
92-
schema.clone(),
93-
node_content,
94-
))))
95-
} else {
96-
Some(HoveredNode::Function(NodeIdentification::Name(
97-
node_content,
98-
)))
99-
}
71+
Some(HoveredNode::Function((
72+
ctx.schema_or_alias_name.clone(),
73+
node_content,
74+
)))
10075
}
10176

10277
"any_identifier"
@@ -106,19 +81,17 @@ impl HoveredNode {
10681
"role_specification",
10782
]) || ctx.before_cursor_matches_kind(&["keyword_revoke"]) =>
10883
{
109-
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
110-
}
111-
"grant_role" | "policy_role" => {
112-
Some(HoveredNode::Role(NodeIdentification::Name(node_content)))
84+
Some(HoveredNode::Role(node_content))
11385
}
86+
"grant_role" | "policy_role" => Some(HoveredNode::Role(node_content)),
11487

11588
"any_identifier"
11689
if (
11790
// hover over custom type in `create table` or `returns`
11891
(ctx.matches_ancestor_history(&["type", "object_reference"])
11992
&& ctx.node_under_cursor_is_within_field_name("custom_type"))
12093

121-
// hover over type in `select` clause etc…
94+
// hover over type in `select` clause etc…
12295
|| (ctx
12396
.matches_ancestor_history(&["field_qualifier", "object_reference"])
12497
&& ctx.before_cursor_matches_kind(&["("])))
@@ -131,32 +104,21 @@ impl HoveredNode {
131104
.is_none() =>
132105
{
133106
let sanitized = node_content.replace(['(', ')'], "");
134-
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
135-
Some(HoveredNode::PostgresType(
136-
NodeIdentification::SchemaAndName((schema.clone(), sanitized)),
137-
))
138-
} else {
139-
Some(HoveredNode::PostgresType(NodeIdentification::Name(
140-
sanitized,
141-
)))
142-
}
107+
Some(HoveredNode::PostgresType((
108+
ctx.schema_or_alias_name.clone(),
109+
sanitized,
110+
)))
143111
}
144112

145113
// quoted columns
146114
"literal" if ctx.matches_ancestor_history(&["select_expression", "term"]) => {
147-
Some(HoveredNode::Column(NodeIdentification::Name(node_content)))
115+
Some(HoveredNode::Column((None, None, node_content)))
148116
}
149117

150-
"grant_table" => {
151-
if let Some(schema) = ctx.schema_or_alias_name.as_ref() {
152-
Some(HoveredNode::Table(NodeIdentification::SchemaAndName((
153-
schema.clone(),
154-
node_content,
155-
))))
156-
} else {
157-
Some(HoveredNode::Table(NodeIdentification::Name(node_content)))
158-
}
159-
}
118+
"grant_table" => Some(HoveredNode::Table((
119+
ctx.schema_or_alias_name.clone(),
120+
node_content,
121+
))),
160122

161123
_ => None,
162124
}

crates/pgls_hover/src/lib.rs

Lines changed: 44 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -33,107 +33,55 @@ pub fn on_hover(params: OnHoverParams) -> Vec<String> {
3333

3434
if let Some(hovered_node) = HoveredNode::get(&ctx) {
3535
let items: Vec<Hoverable> = match hovered_node {
36-
HoveredNode::Table(node_identification) => match node_identification {
37-
hovered_node::NodeIdentification::Name(n) => params
38-
.schema_cache
39-
.find_tables(n.as_str(), None)
40-
.into_iter()
41-
.map(Hoverable::from)
42-
.collect(),
43-
44-
hovered_node::NodeIdentification::SchemaAndName((s, n)) => params
45-
.schema_cache
46-
.find_tables(n.as_str(), Some(&s))
47-
.into_iter()
48-
.map(Hoverable::from)
49-
.collect(),
50-
51-
_ => vec![],
52-
},
53-
54-
HoveredNode::Column(node_identification) => match node_identification {
55-
hovered_node::NodeIdentification::Name(column_name) => params
56-
.schema_cache
57-
.find_cols(&column_name, None, None)
58-
.into_iter()
59-
.map(Hoverable::from)
60-
.collect(),
61-
62-
hovered_node::NodeIdentification::SchemaAndName((table_or_alias, column_name)) => {
63-
// resolve alias to actual table name if needed
64-
let actual_table = ctx
65-
.get_mentioned_table_for_alias(table_or_alias.as_str())
36+
HoveredNode::Table((schema, name)) => params
37+
.schema_cache
38+
.find_tables(name.as_str(), schema.as_deref())
39+
.into_iter()
40+
.map(Hoverable::from)
41+
.collect(),
42+
43+
HoveredNode::Column((schema, table_or_alias, column_name)) => {
44+
let table = table_or_alias.as_ref().and_then(|t| {
45+
ctx.get_mentioned_table_for_alias(t.as_str())
6646
.map(|s| s.as_str())
67-
.unwrap_or(table_or_alias.as_str());
68-
69-
params
70-
.schema_cache
71-
.find_cols(&column_name, Some(actual_table), None)
72-
.into_iter()
73-
.map(Hoverable::from)
74-
.collect()
75-
}
76-
77-
_ => vec![],
78-
},
79-
80-
HoveredNode::Function(node_identification) => match node_identification {
81-
hovered_node::NodeIdentification::Name(function_name) => params
82-
.schema_cache
83-
.find_functions(&function_name, None)
84-
.into_iter()
85-
.map(Hoverable::from)
86-
.collect(),
47+
.or(Some(t.as_str()))
48+
});
8749

88-
hovered_node::NodeIdentification::SchemaAndName((schema, function_name)) => params
50+
params
8951
.schema_cache
90-
.find_functions(&function_name, Some(&schema))
52+
.find_cols(&column_name, table, schema.as_deref())
9153
.into_iter()
9254
.map(Hoverable::from)
93-
.collect(),
94-
95-
_ => vec![],
96-
},
97-
98-
HoveredNode::Role(node_identification) => match node_identification {
99-
hovered_node::NodeIdentification::Name(role_name) => params
100-
.schema_cache
101-
.find_roles(&role_name)
102-
.into_iter()
103-
.map(Hoverable::from)
104-
.collect(),
105-
106-
_ => vec![],
107-
},
108-
109-
HoveredNode::Schema(node_identification) => match node_identification {
110-
hovered_node::NodeIdentification::Name(schema_name) => params
111-
.schema_cache
112-
.find_schema(&schema_name)
113-
.map(Hoverable::from)
114-
.map(|s| vec![s])
115-
.unwrap_or_default(),
116-
117-
_ => vec![],
118-
},
119-
120-
HoveredNode::PostgresType(node_identification) => match node_identification {
121-
hovered_node::NodeIdentification::Name(type_name) => params
122-
.schema_cache
123-
.find_type(&type_name, None)
124-
.map(Hoverable::from)
125-
.map(|s| vec![s])
126-
.unwrap_or_default(),
127-
128-
hovered_node::NodeIdentification::SchemaAndName((schema, type_name)) => params
129-
.schema_cache
130-
.find_type(&type_name, Some(schema.as_str()))
131-
.map(Hoverable::from)
132-
.map(|s| vec![s])
133-
.unwrap_or_default(),
134-
135-
_ => vec![],
136-
},
55+
.collect()
56+
}
57+
58+
HoveredNode::Function((schema, function_name)) => params
59+
.schema_cache
60+
.find_functions(&function_name, schema.as_deref())
61+
.into_iter()
62+
.map(Hoverable::from)
63+
.collect(),
64+
65+
HoveredNode::Role(role_name) => params
66+
.schema_cache
67+
.find_roles(&role_name)
68+
.into_iter()
69+
.map(Hoverable::from)
70+
.collect(),
71+
72+
HoveredNode::Schema(schema_name) => params
73+
.schema_cache
74+
.find_schema(&schema_name)
75+
.map(Hoverable::from)
76+
.map(|s| vec![s])
77+
.unwrap_or_default(),
78+
79+
HoveredNode::PostgresType((schema, type_name)) => params
80+
.schema_cache
81+
.find_type(&type_name, schema.as_deref())
82+
.map(Hoverable::from)
83+
.map(|s| vec![s])
84+
.unwrap_or_default(),
13785

13886
_ => todo!(),
13987
};

0 commit comments

Comments
 (0)