cleanup TextPos in eww debug

This commit is contained in:
elkowar 2021-04-14 12:20:33 +02:00
parent 11f595be2c
commit b9c9e67c2e
3 changed files with 38 additions and 18 deletions

View file

@ -42,7 +42,7 @@ pub struct WidgetUse {
pub name: String, pub name: String,
pub children: Vec<WidgetUse>, pub children: Vec<WidgetUse>,
pub attrs: HashMap<AttrName, AttrValue>, pub attrs: HashMap<AttrName, AttrValue>,
pub text_pos: Option<roxmltree::TextPos>, pub text_pos: Option<TextPos>,
} }
#[derive(Debug, Clone)] #[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.text_pos = Some(text_pos);
self self
} }

View file

@ -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<roxmltree::TextPos> 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. /// 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. /// 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 = 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(); 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() { 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 { pub fn get_sourcecode(&self) -> String {
let input_text = self.node().document().input_text(); let input_text = self.node().document().input_text();
let range = self.node().range(); let range = self.node().range();
let start_pos = self.node().document().text_pos_at(range.start); let start_pos = self.node().document().text_pos_at(range.start).into();
let end_pos = self.node().document().text_pos_at(range.end); let end_pos = self.node().document().text_pos_at(range.end).into();
get_text_from_text_range(input_text, (start_pos, end_pos)) get_text_from_text_range(input_text, (start_pos, end_pos))
} }
@ -73,10 +92,10 @@ impl<'a, 'b> XmlNode<'a, 'b> {
self.node().range() self.node().range()
} }
pub fn text_pos(&self) -> roxmltree::TextPos { pub fn text_pos(&self) -> TextPos {
let document = self.node().document(); let document = self.node().document();
let range = self.node().range(); 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> { 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() 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 document = self.0.document();
let range = self.0.range(); 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 document = self.0.document();
let range = self.0.range(); let range = self.0.range();
document.text_pos_at(range.start) document.text_pos_at(range.start).into()
} }
} }

View file

@ -1,6 +1,7 @@
use crate::{ use crate::{
config::{ config::{
element::{WidgetDefinition, WidgetUse}, element::{WidgetDefinition, WidgetUse},
xml_ext::TextPos,
WindowName, WindowName,
}, },
eww_state::EwwState, eww_state::EwwState,
@ -11,7 +12,7 @@ use dyn_clone;
use std::collections::HashMap; use std::collections::HashMap;
pub trait WidgetNode: std::fmt::Debug + dyn_clone::DynClone + Send + Sync { pub trait WidgetNode: std::fmt::Debug + dyn_clone::DynClone + Send + Sync {
fn get_name(&self) -> &str; fn get_name(&self) -> &str;
fn get_text_pos(&self) -> Option<&roxmltree::TextPos>; fn get_text_pos(&self) -> Option<TextPos>;
/// Generate a [gtk::Widget] from a [element::WidgetUse]. /// Generate a [gtk::Widget] from a [element::WidgetUse].
/// ///
@ -33,7 +34,7 @@ dyn_clone::clone_trait_object!(WidgetNode);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct UserDefined { pub struct UserDefined {
name: String, name: String,
text_pos: Option<roxmltree::TextPos>, text_pos: Option<TextPos>,
content: Box<dyn WidgetNode>, content: Box<dyn WidgetNode>,
} }
@ -42,8 +43,8 @@ impl WidgetNode for UserDefined {
&self.name &self.name
} }
fn get_text_pos(&self) -> Option<&roxmltree::TextPos> { fn get_text_pos(&self) -> Option<TextPos> {
self.text_pos.as_ref() self.text_pos
} }
fn render( fn render(
@ -59,7 +60,7 @@ impl WidgetNode for UserDefined {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Generic { pub struct Generic {
pub name: String, pub name: String,
pub text_pos: Option<roxmltree::TextPos>, pub text_pos: Option<TextPos>,
pub children: Vec<Box<dyn WidgetNode>>, pub children: Vec<Box<dyn WidgetNode>>,
pub attrs: HashMap<AttrName, AttrValue>, pub attrs: HashMap<AttrName, AttrValue>,
} }
@ -80,8 +81,8 @@ impl WidgetNode for Generic {
&self.name &self.name
} }
fn get_text_pos(&self) -> Option<&roxmltree::TextPos> { fn get_text_pos(&self) -> Option<TextPos> {
self.text_pos.as_ref() self.text_pos
} }
fn render( fn render(