use lsp_types::{GotoDefinitionResponse, Location, LocationLink, Uri};
use crate::App;
pub trait DefinitionProvider {
fn definition(
&self,
text: &ropey::Rope,
offset: usize,
window: &mut crate::Window,
cx: &mut App,
) -> crate::Task<anyhow::Result<Vec<DefinitionLocation>>>;
fn references(
&self,
text: &ropey::Rope,
offset: usize,
include_declarations: bool,
window: &mut crate::Window,
cx: &mut App,
) -> crate::Task<anyhow::Result<Vec<DefinitionLocation>>>;
fn document_highlights(
&self,
text: &ropey::Rope,
offset: usize,
window: &mut crate::Window,
_cx: &mut App,
) -> crate::Task<anyhow::Result<Vec<DocumentHighlight>>> {
let _ = (text, offset, window);
crate::Task::ready(Ok(vec![]))
}
}
#[derive(Debug, Clone)]
pub struct DefinitionLocation {
pub uri: Uri,
pub range: lsp_types::Range,
pub display_name: Option<String>,
}
impl DefinitionLocation {
pub fn from_location(location: Location) -> Self {
Self {
uri: location.uri,
range: location.range,
display_name: None,
}
}
pub fn from_location_link(link: LocationLink) -> Self {
Self {
uri: link.target_uri,
range: link.target_selection_range,
display_name: None,
}
}
pub fn from_response(response: GotoDefinitionResponse) -> Vec<Self> {
match response {
GotoDefinitionResponse::Scalar(location) => vec![Self::from_location(location)],
GotoDefinitionResponse::Array(locations) => {
locations.into_iter().map(Self::from_location).collect()
}
GotoDefinitionResponse::Link(links) => {
links.into_iter().map(Self::from_location_link).collect()
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DocumentHighlightKind {
Text,
Read,
Write,
}
#[derive(Debug, Clone)]
pub struct DocumentHighlight {
pub range: lsp_types::Range,
pub kind: Option<DocumentHighlightKind>,
}
impl DocumentHighlight {
pub fn from_lsp(highlight: lsp_types::DocumentHighlight) -> Self {
Self {
range: highlight.range,
kind: highlight.kind.map(|k| match k {
lsp_types::DocumentHighlightKind::TEXT => DocumentHighlightKind::Text,
lsp_types::DocumentHighlightKind::READ => DocumentHighlightKind::Read,
lsp_types::DocumentHighlightKind::WRITE => DocumentHighlightKind::Write,
_ => DocumentHighlightKind::Text,
}),
}
}
}