mod parsing;
use parsing::{prefix, value, workspace_members};
use mylsp::Position;
use tree_sitter::{Node, TreeCursor};
pub struct TomlCursor<'a> {
text: &'a str,
cursor: TreeCursor<'a>,
}
impl TomlCursor<'_> {
pub fn visit_all<F: FnMut(Node)>(&mut self, mut f: F) {
let c = &mut self.cursor;
let mut visited = vec![];
loop {
let n = c.node();
if !visited.contains(&n.id()) {
visited.push(n.id());
f(n);
if c.goto_first_child() {
continue;
}
}
if !c.goto_next_sibling() && !c.goto_parent() {
break;
}
}
}
pub fn blank_line(&mut self, pos: Position) -> bool {
for (i, l) in self.text.lines().enumerate() {
if i == pos.line && l.trim().is_empty() {
return true;
}
}
false
}
pub fn prefix(&self, pos: Position) -> String {
prefix(self.text, pos)
}
pub fn value(&self, key: &str) -> Option<String> {
value(self.text, key)
}
pub fn workspace_members(&self) -> Vec<String> {
workspace_members(self.text)
}
}
impl<'a> From<(&'a str, TreeCursor<'a>)> for TomlCursor<'a> {
fn from(value: (&'a str, TreeCursor<'a>)) -> Self {
Self {
text: value.0,
cursor: value.1,
}
}
}