diff --git a/src/config/element.rs b/src/config/element.rs index ba18cc0..42bc2db 100644 --- a/src/config/element.rs +++ b/src/config/element.rs @@ -42,7 +42,7 @@ pub struct WidgetUse { pub name: String, pub children: Vec, pub attrs: HashMap, - pub text_pos: Option, + pub text_pos: Option, } #[derive(Debug, Clone)] @@ -92,7 +92,7 @@ impl WidgetUse { } } - pub fn at_pos(mut self, text_pos: roxmltree::TextPos) -> Self { + pub fn at_pos(mut self, text_pos: TextPos) -> Self { self.text_pos = Some(text_pos); self } diff --git a/src/config/xml_ext.rs b/src/config/xml_ext.rs index 710e733..738a0da 100644 --- a/src/config/xml_ext.rs +++ b/src/config/xml_ext.rs @@ -28,9 +28,28 @@ impl<'a, 'b> fmt::Display for XmlNode<'a, 'b> { } } +#[derive(PartialEq, Eq, Clone, Copy, derive_more::Display)] +#[display(fmt = "{}:{}", row, col)] +pub struct TextPos { + pub row: u32, + pub col: u32, +} + +impl std::fmt::Debug for TextPos { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self) + } +} + +impl From for TextPos { + fn from(x: roxmltree::TextPos) -> Self { + TextPos { row: x.row, col: x.col } + } +} + /// Get the part of a string that is selected by the start and end TextPos. /// Will panic if the range is out of bounds in any way. -fn get_text_from_text_range(s: &str, (start_pos, end_pos): (roxmltree::TextPos, roxmltree::TextPos)) -> String { +fn get_text_from_text_range(s: &str, (start_pos, end_pos): (TextPos, TextPos)) -> String { let mut code_text = s.lines().dropping(start_pos.row as usize - 1).take(end_pos.row as usize - (start_pos.row as usize - 1)).collect_vec(); if let Some(first_line) = code_text.first_mut() { @@ -46,8 +65,8 @@ impl<'a, 'b> XmlNode<'a, 'b> { pub fn get_sourcecode(&self) -> String { let input_text = self.node().document().input_text(); let range = self.node().range(); - let start_pos = self.node().document().text_pos_at(range.start); - let end_pos = self.node().document().text_pos_at(range.end); + let start_pos = self.node().document().text_pos_at(range.start).into(); + let end_pos = self.node().document().text_pos_at(range.end).into(); get_text_from_text_range(input_text, (start_pos, end_pos)) } @@ -73,10 +92,10 @@ impl<'a, 'b> XmlNode<'a, 'b> { self.node().range() } - pub fn text_pos(&self) -> roxmltree::TextPos { + pub fn text_pos(&self) -> TextPos { let document = self.node().document(); let range = self.node().range(); - document.text_pos_at(range.start) + document.text_pos_at(range.start).into() } fn node(&self) -> roxmltree::Node<'a, 'b> { @@ -102,10 +121,10 @@ impl<'a, 'b> XmlText<'a, 'b> { self.0.text().unwrap_or_default().trim_lines().trim_matches('\n').to_owned() } - pub fn text_pos(&self) -> roxmltree::TextPos { + pub fn text_pos(&self) -> TextPos { let document = self.0.document(); let range = self.0.range(); - document.text_pos_at(range.start) + document.text_pos_at(range.start).into() } } @@ -209,10 +228,10 @@ impl<'a, 'b> XmlElement<'a, 'b> { } } - pub fn text_pos(&self) -> roxmltree::TextPos { + pub fn text_pos(&self) -> TextPos { let document = self.0.document(); let range = self.0.range(); - document.text_pos_at(range.start) + document.text_pos_at(range.start).into() } } diff --git a/src/widgets/widget_node.rs b/src/widgets/widget_node.rs index d428228..e9bf5dd 100644 --- a/src/widgets/widget_node.rs +++ b/src/widgets/widget_node.rs @@ -1,6 +1,7 @@ use crate::{ config::{ element::{WidgetDefinition, WidgetUse}, + xml_ext::TextPos, WindowName, }, eww_state::EwwState, @@ -11,7 +12,7 @@ use dyn_clone; use std::collections::HashMap; pub trait WidgetNode: std::fmt::Debug + dyn_clone::DynClone + Send + Sync { fn get_name(&self) -> &str; - fn get_text_pos(&self) -> Option<&roxmltree::TextPos>; + fn get_text_pos(&self) -> Option; /// Generate a [gtk::Widget] from a [element::WidgetUse]. /// @@ -33,7 +34,7 @@ dyn_clone::clone_trait_object!(WidgetNode); #[derive(Debug, Clone)] pub struct UserDefined { name: String, - text_pos: Option, + text_pos: Option, content: Box, } @@ -42,8 +43,8 @@ impl WidgetNode for UserDefined { &self.name } - fn get_text_pos(&self) -> Option<&roxmltree::TextPos> { - self.text_pos.as_ref() + fn get_text_pos(&self) -> Option { + self.text_pos } fn render( @@ -59,7 +60,7 @@ impl WidgetNode for UserDefined { #[derive(Debug, Clone)] pub struct Generic { pub name: String, - pub text_pos: Option, + pub text_pos: Option, pub children: Vec>, pub attrs: HashMap, } @@ -80,8 +81,8 @@ impl WidgetNode for Generic { &self.name } - fn get_text_pos(&self) -> Option<&roxmltree::TextPos> { - self.text_pos.as_ref() + fn get_text_pos(&self) -> Option { + self.text_pos } fn render(