|
|
use std::{fs, path::PathBuf}; |
|
|
|
|
|
use lsp_types::{CallHierarchyIncomingCall, CallHierarchyItem, Range}; |
|
|
|
|
|
|
|
|
#[derive(Hash, PartialEq, Eq, serde::Deserialize, serde::Serialize, Clone, Debug)] |
|
|
pub struct IdentifierReference { |
|
|
pub identifier: Identifier, |
|
|
pub references: Vec<Range>, |
|
|
} |
|
|
|
|
|
|
|
|
#[derive(Hash, PartialEq, Eq, serde::Deserialize, serde::Serialize, Clone)] |
|
|
pub struct Identifier { |
|
|
pub path: String, |
|
|
|
|
|
pub name: String, |
|
|
|
|
|
pub range: lsp_types::Range, |
|
|
} |
|
|
|
|
|
impl Identifier { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn equals_ident(&self, other: &syn::Ident, match_location: bool) -> bool { |
|
|
*other == self.name |
|
|
&& (!match_location |
|
|
|| (self.range.start.line == other.span().start().line as u32 |
|
|
&& self.range.start.character == other.span().start().column as u32)) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn get_name(item: &CallHierarchyItem) -> String { |
|
|
|
|
|
let file = fs::read_to_string(item.uri.path()).unwrap(); |
|
|
let start = item.selection_range.start; |
|
|
let end = item.selection_range.end; |
|
|
file.lines() |
|
|
.nth(start.line as usize) |
|
|
.unwrap() |
|
|
.chars() |
|
|
.skip(start.character as usize) |
|
|
.take(end.character as usize - start.character as usize) |
|
|
.collect() |
|
|
} |
|
|
} |
|
|
|
|
|
impl From<(PathBuf, syn::Ident)> for Identifier { |
|
|
fn from((path, ident): (PathBuf, syn::Ident)) -> Self { |
|
|
Self { |
|
|
path: path.display().to_string(), |
|
|
name: ident.to_string(), |
|
|
|
|
|
range: Range { |
|
|
start: lsp_types::Position { |
|
|
line: ident.span().start().line as u32 - 1, |
|
|
character: ident.span().start().column as u32, |
|
|
}, |
|
|
end: lsp_types::Position { |
|
|
line: ident.span().end().line as u32 - 1, |
|
|
character: ident.span().end().column as u32, |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl From<CallHierarchyIncomingCall> for IdentifierReference { |
|
|
fn from(item: CallHierarchyIncomingCall) -> Self { |
|
|
Self { |
|
|
identifier: Identifier { |
|
|
name: Identifier::get_name(&item.from), |
|
|
|
|
|
path: item.from.uri.path().to_owned(), |
|
|
range: item.from.selection_range, |
|
|
}, |
|
|
references: item.from_ranges, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
impl std::fmt::Debug for Identifier { |
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
std::fmt::Display::fmt(self, f) |
|
|
} |
|
|
} |
|
|
|
|
|
impl std::fmt::Display for Identifier { |
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
write!(f, "{}:{}#{}", self.path, self.range.start.line, self.name,) |
|
|
} |
|
|
} |
|
|
|