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 children: Vec<WidgetUse>,
pub attrs: HashMap<AttrName, AttrValue>,
pub text_pos: Option<roxmltree::TextPos>,
pub text_pos: Option<TextPos>,
}
#[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
}

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.
/// 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()
}
}

View file

@ -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<TextPos>;
/// 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<roxmltree::TextPos>,
text_pos: Option<TextPos>,
content: Box<dyn WidgetNode>,
}
@ -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<TextPos> {
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<roxmltree::TextPos>,
pub text_pos: Option<TextPos>,
pub children: Vec<Box<dyn WidgetNode>>,
pub attrs: HashMap<AttrName, AttrValue>,
}
@ -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<TextPos> {
self.text_pos
}
fn render(