|
| 1 | +use std::fs::read_to_string; |
| 2 | + |
| 3 | +use anyhow::Result; |
| 4 | +use rust_lsp::lsp_types::{Location, Position, Range}; |
| 5 | +use slog_scope::{info, debug}; |
| 6 | +use tree_sitter::{Node, Parser, Point, Tree, Query, QueryCursor}; |
| 7 | +use url::Url; |
| 8 | + |
| 9 | +macro_rules! find_function_str { |
| 10 | + () => { |
| 11 | + r#" |
| 12 | + ( |
| 13 | + (function_declarator |
| 14 | + (identifier) @function) |
| 15 | + (#match? @function "{}") |
| 16 | + ) |
| 17 | + "# |
| 18 | + }; |
| 19 | +} |
| 20 | +pub struct ParserContext<'a> { |
| 21 | + source: String, |
| 22 | + tree: Tree, |
| 23 | + parser: &'a mut Parser, |
| 24 | +} |
| 25 | + |
| 26 | +impl<'a> ParserContext<'a> { |
| 27 | + pub fn new(parser: &'a mut Parser, document_uri: Url) -> Result<Self> { |
| 28 | + let source = read_to_string(document_uri.path())?; |
| 29 | + |
| 30 | + let tree = parser.parse(&source, None).unwrap(); |
| 31 | + |
| 32 | + Ok(ParserContext { source, tree, parser }) |
| 33 | + } |
| 34 | + |
| 35 | + pub fn find_definitions(&self, document_uri: Url, point: Position) -> Result<Vec<Location>> { |
| 36 | + let current_node = match self.find_node_at_point(point) { |
| 37 | + Some(node) => node, |
| 38 | + None => return Ok(vec![]), |
| 39 | + }; |
| 40 | + |
| 41 | + let parent = match current_node.parent() { |
| 42 | + Some(parent) => parent, |
| 43 | + None => return Ok(vec![]), |
| 44 | + }; |
| 45 | + |
| 46 | + let query = match (current_node.kind(), parent.kind()) { |
| 47 | + (_, "call_expression") => { |
| 48 | + format!(find_function_str!(), current_node.utf8_text(self.source.as_bytes())?) |
| 49 | + } |
| 50 | + _ => return Ok(vec![]), |
| 51 | + }; |
| 52 | + |
| 53 | + let ts_query = Query::new(tree_sitter_glsl::language(), query.as_str())?; |
| 54 | + |
| 55 | + let mut query_cursor = QueryCursor::new(); |
| 56 | + |
| 57 | + let mut locations = vec![]; |
| 58 | + |
| 59 | + for m in query_cursor.matches(&ts_query, self.tree.root_node(), self.source.as_bytes()) { |
| 60 | + for capture in m.captures { |
| 61 | + let start = capture.node.start_position(); |
| 62 | + let end = capture.node.end_position(); |
| 63 | + |
| 64 | + locations.push(Location { |
| 65 | + uri: document_uri.clone(), |
| 66 | + range: Range { |
| 67 | + start: Position { |
| 68 | + line: start.row as u32, |
| 69 | + character: start.column as u32, |
| 70 | + }, |
| 71 | + end: Position { |
| 72 | + line: end.row as u32, |
| 73 | + character: end.column as u32, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + info!("finished searching for definitions"; "definitions" => format!("{:?}", locations)); |
| 81 | + |
| 82 | + Ok(locations) |
| 83 | + } |
| 84 | + |
| 85 | + pub fn find_references(&self) -> Result<Vec<Location>> { |
| 86 | + Ok(vec![]) |
| 87 | + } |
| 88 | + |
| 89 | + fn root_node(&self) -> Node { |
| 90 | + self.tree.root_node() |
| 91 | + } |
| 92 | + |
| 93 | + fn find_node_at_point(&self, point: Position) -> Option<Node> { |
| 94 | + match self.root_node().named_descendant_for_point_range( |
| 95 | + Point { |
| 96 | + row: point.line as usize, |
| 97 | + column: (point.character - 1) as usize, |
| 98 | + }, |
| 99 | + Point { |
| 100 | + row: point.line as usize, |
| 101 | + column: point.character as usize, |
| 102 | + }, |
| 103 | + ) { |
| 104 | + Some(node) => { |
| 105 | + debug!("found a node"; "node" => format!("{:?}", node)); |
| 106 | + Some(node) |
| 107 | + }, |
| 108 | + None => None, |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments