turbo_static/
identifier.rs1use std::{fs, path::PathBuf};
2
3use lsp_types::{CallHierarchyIncomingCall, CallHierarchyItem, Range};
4use serde::{Deserialize, Serialize};
5
6#[derive(Hash, PartialEq, Eq, Deserialize, Serialize, Clone, Debug)]
8pub struct IdentifierReference {
9 pub identifier: Identifier,
10 pub references: Vec<Range>, }
12
13#[derive(Hash, PartialEq, Eq, Deserialize, Serialize, Clone)]
15pub struct Identifier {
16 pub path: String,
17 pub name: String,
19 pub range: lsp_types::Range,
21}
22
23impl Identifier {
24 pub fn equals_ident(&self, other: &syn::Ident, match_location: bool) -> bool {
29 *other == self.name
30 && (!match_location
31 || (self.range.start.line == other.span().start().line as u32
32 && self.range.start.character == other.span().start().column as u32))
33 }
34
35 fn get_name(item: &CallHierarchyItem) -> String {
38 let file = fs::read_to_string(item.uri.path()).unwrap();
40 let start = item.selection_range.start;
41 let end = item.selection_range.end;
42 file.lines()
43 .nth(start.line as usize)
44 .unwrap()
45 .chars()
46 .skip(start.character as usize)
47 .take(end.character as usize - start.character as usize)
48 .collect()
49 }
50}
51
52impl From<(PathBuf, syn::Ident)> for Identifier {
53 fn from((path, ident): (PathBuf, syn::Ident)) -> Self {
54 Self {
55 path: path.display().to_string(),
56 name: ident.to_string(),
57 range: Range {
59 start: lsp_types::Position {
60 line: ident.span().start().line as u32 - 1,
61 character: ident.span().start().column as u32,
62 },
63 end: lsp_types::Position {
64 line: ident.span().end().line as u32 - 1,
65 character: ident.span().end().column as u32,
66 },
67 },
68 }
69 }
70}
71
72impl From<CallHierarchyIncomingCall> for IdentifierReference {
73 fn from(item: CallHierarchyIncomingCall) -> Self {
74 Self {
75 identifier: Identifier {
76 name: Identifier::get_name(&item.from),
77 path: item.from.uri.path().to_owned(),
79 range: item.from.selection_range,
80 },
81 references: item.from_ranges,
82 }
83 }
84}
85
86impl std::fmt::Debug for Identifier {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88 std::fmt::Display::fmt(self, f)
89 }
90}
91
92impl std::fmt::Display for Identifier {
93 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94 write!(f, "{}:{}#{}", self.path, self.range.start.line, self.name,)
95 }
96}