* git mv src/wasm-lib rust Signed-off-by: Jess Frazelle <github@jessfraz.com> * mv wasm-lib to workspace Signed-off-by: Jess Frazelle <github@jessfraz.com> * mv kcl-lib Signed-off-by: Jess Frazelle <github@jessfraz.com> * mv derive docs Signed-off-by: Jess Frazelle <github@jessfraz.com> * resolve file paths Signed-off-by: Jess Frazelle <github@jessfraz.com> * clippy Signed-off-by: Jess Frazelle <github@jessfraz.com> * move more shit Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix more paths Signed-off-by: Jess Frazelle <github@jessfraz.com> * make yarn build:wasm work Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix scripts Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixups Signed-off-by: Jess Frazelle <github@jessfraz.com> * better references Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix cargo ci Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix reference Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix more ci Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix tests Signed-off-by: Jess Frazelle <github@jessfraz.com> * cargo sort Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix script Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix Signed-off-by: Jess Frazelle <github@jessfraz.com> * fmt Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix a dep Signed-off-by: Jess Frazelle <github@jessfraz.com> * sort Signed-off-by: Jess Frazelle <github@jessfraz.com> * remove unused deps Signed-off-by: Jess Frazelle <github@jessfraz.com> * Revert "remove unused deps" This reverts commit fbabdb062e275fd5cbc1476f8480a1afee15d972. * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * deps; Signed-off-by: Jess Frazelle <github@jessfraz.com> * fixes Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> --------- Signed-off-by: Jess Frazelle <github@jessfraz.com>
44 lines
1.4 KiB
Rust
44 lines
1.4 KiB
Rust
//! Utility functions for working with ropes and positions.
|
|
|
|
use ropey::Rope;
|
|
use tower_lsp::lsp_types::{Diagnostic, Position};
|
|
|
|
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())
|
|
}
|
|
|
|
/// Convert an object into a [lsp_types::Diagnostic] given the
|
|
/// [TextDocumentItem]'s `.text` field.
|
|
pub trait IntoDiagnostic {
|
|
/// Convert the traited object to a vector of [lsp_types::Diagnostic].
|
|
fn to_lsp_diagnostics(&self, text: &str) -> Vec<Diagnostic>;
|
|
|
|
/// Get the severity of the diagnostic.
|
|
fn severity(&self) -> tower_lsp::lsp_types::DiagnosticSeverity;
|
|
}
|