use tower_lsp::lsp_types::{Position, Url};
use crate::ast::ParsedDoc;
pub fn cursor(src: &str) -> (String, Position) {
const MARKER: &str = "$0";
let marker_byte = src.find(MARKER).expect("no `$0` cursor marker in source");
let before = &src[..marker_byte];
let line = before.chars().filter(|&c| c == '\n').count() as u32;
let last_nl_end = before.rfind('\n').map(|i| i + 1).unwrap_or(0);
let character = before[last_nl_end..].encode_utf16().count() as u32;
let cleaned = format!("{}{}", before, &src[marker_byte + MARKER.len()..]);
(cleaned, Position { line, character })
}
pub fn file_url(path: &str) -> Url {
Url::parse(&format!("file://{path}")).unwrap()
}
pub fn parse_doc(src: &str) -> ParsedDoc {
ParsedDoc::parse(src.to_string())
}
pub fn position(line: u32, character: u32) -> Position {
Position { line, character }
}