2024-02-15 13:56:31 -08:00
|
|
|
//! Utility functions for working with ropes and positions.
|
|
|
|
|
|
|
|
use ropey::Rope;
|
2024-06-11 19:23:35 -04:00
|
|
|
use tower_lsp::lsp_types::{Diagnostic, Position};
|
2024-02-15 13:56:31 -08:00
|
|
|
|
|
|
|
pub fn position_to_offset(position: Position, rope: &Rope) -> Option<usize> {
|
|
|
|
Some(rope.try_line_to_char(position.line as usize).ok()? + position.character as usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_text_before(offset: usize, rope: &Rope) -> Option<String> {
|
|
|
|
if offset == 0 {
|
|
|
|
return Some("".to_string());
|
|
|
|
}
|
|
|
|
Some(rope.slice(0..offset).to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_text_after(offset: usize, rope: &Rope) -> Option<String> {
|
|
|
|
let end_idx = rope.len_chars();
|
|
|
|
if offset == end_idx {
|
|
|
|
return Some("".to_string());
|
|
|
|
}
|
|
|
|
Some(rope.slice(offset..end_idx).to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_line_before(pos: Position, rope: &Rope) -> Option<String> {
|
|
|
|
let char_offset = pos.character as usize;
|
|
|
|
let offset = position_to_offset(pos, rope).unwrap();
|
|
|
|
if char_offset == 0 {
|
|
|
|
return Some("".to_string());
|
|
|
|
}
|
|
|
|
let line_start = offset - char_offset;
|
|
|
|
Some(rope.slice(line_start..offset).to_string())
|
|
|
|
}
|
2024-06-11 19:23:35 -04:00
|
|
|
|
|
|
|
/// Convert an object into a [lsp_types::Diagnostic] given the
|
|
|
|
/// [TextDocumentItem]'s `.text` field.
|
|
|
|
pub trait IntoDiagnostic {
|
2025-02-26 19:29:59 -08:00
|
|
|
/// Convert the traited object to a vector of [lsp_types::Diagnostic].
|
|
|
|
fn to_lsp_diagnostics(&self, text: &str) -> Vec<Diagnostic>;
|
2024-06-27 15:43:49 -07:00
|
|
|
|
|
|
|
/// Get the severity of the diagnostic.
|
|
|
|
fn severity(&self) -> tower_lsp::lsp_types::DiagnosticSeverity;
|
2024-06-11 19:23:35 -04:00
|
|
|
}
|