Skip to content

Commit

Permalink
feat: not rebuilding the entire tree on every render
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed May 6, 2024
1 parent 8b9a699 commit a66efd6
Show file tree
Hide file tree
Showing 12 changed files with 250 additions and 109 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"
reqwest = { version = "0.12", features = ["json"] }
ratatui = { version = "0.26.1", features = ["all-widgets", "crossterm"] }
tree-sitter = "0.22.5"
tree-sitter-json = "0.21"
4 changes: 2 additions & 2 deletions reqtui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ tokio.workspace = true
reqwest.workspace = true
serde_json.workspace = true
ratatui.workspace = true
tree-sitter.workspace = true
tree-sitter-json.workspace = true

tree-sitter = "0.22.5"
tree-sitter-json = "0.21"
ropey = "1.6.1"
lazy_static = "1.4"

Expand Down
5 changes: 2 additions & 3 deletions reqtui/src/net/request_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ pub fn handle_request(

let mut highlighter = HIGHLIGHTER.write().unwrap();
let body = body.to_string();

let tree = highlighter.parse(&pretty_body);
let highlight = highlighter.apply(&pretty_body, tree.as_ref(), &tokens);
let pretty_body = TextObject::from(&pretty_body).with_highlight(highlight);
let _highlight = highlighter.apply(&pretty_body, tree.as_ref(), &tokens);
let pretty_body = TextObject::from(&pretty_body);

response_tx
.send(ReqtuiNetRequest::Response(ReqtuiResponse {
Expand Down
5 changes: 3 additions & 2 deletions reqtui/src/schema/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::{hash::Hash, path::PathBuf};

use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Schema {
Expand Down Expand Up @@ -54,7 +53,9 @@ pub struct Request {
pub method: RequestMethod,
pub name: String,
pub uri: String,
pub body: Option<Value>,
pub body: Option<String>,
#[serde(rename = "bodyType")]
pub body_type: Option<String>,
}

impl Hash for Request {
Expand Down
76 changes: 17 additions & 59 deletions reqtui/src/text_object/text_object.rs
Original file line number Diff line number Diff line change
@@ -1,87 +1,45 @@
use ratatui::{
style::Styled,
text::{Line, Span},
};
use ropey::Rope;

use crate::syntax::highlighter::ColorInfo;

#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub struct Readonly;
#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub struct Write;

#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Clone)]
pub struct TextObject<State = Readonly> {
content: Rope,
state: std::marker::PhantomData<State>,
pub display: Vec<Line<'static>>,
pub longest_line: usize,
}

impl<State> Default for TextObject<State> {
fn default() -> Self {
let content = String::default();

TextObject {
content: Rope::from_str(&content),
state: std::marker::PhantomData,
}
}
}

impl TextObject<Readonly> {
pub fn from(content: &str) -> TextObject<Readonly> {
let content = Rope::from_str(content);
TextObject::<Readonly> {
display: vec![Line::from(content.to_string())],
content: Rope::from_str(content),
content,
state: std::marker::PhantomData::<Readonly>,
longest_line: 0,
}
}

pub fn with_write(self) -> TextObject<Write> {
TextObject::<Write> {
content: self.content,
state: std::marker::PhantomData,
display: self.display,
longest_line: self.longest_line,
}
}
}

fn build_stylized_line(c: char, i: usize, colors: &[ColorInfo]) -> Span<'static> {
c.to_string().set_style(
colors
.iter()
.find(|color| color.start <= i && color.end >= i)
.map(|c| c.style)
.unwrap_or_default(),
)
}

impl TextObject {
pub fn with_highlight(self, colors: Vec<ColorInfo>) -> Self {
let mut display: Vec<Line> = vec![];
let mut current_line: Vec<Span> = vec![];
let mut longest_line = 0;

self.to_string()
.chars()
.enumerate()
.for_each(|(i, c)| match c {
'\n' => {
longest_line = longest_line.max(current_line.len());
current_line.push(build_stylized_line(c, i, &colors));
display.push(current_line.clone().into());
current_line.clear();
}
_ => current_line.push(build_stylized_line(c, i, &colors)),
});

if !self.to_string().ends_with('\n') {
display.push(current_line.clone().into());
}

Self {
content: self.content,
state: std::marker::PhantomData,
display,
longest_line,
}
}
}

impl std::fmt::Display for TextObject {
impl<State> std::fmt::Display for TextObject<State> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.content.to_string())
}
Expand Down
1 change: 1 addition & 0 deletions tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tracing.workspace = true
reqwest.workspace = true
serde_json.workspace = true
ratatui.workspace = true
tree-sitter.workspace = true

futures = "0.3.30"
tui-big-text = { version = "0.4.3" }
Expand Down
Loading

0 comments on commit a66efd6

Please sign in to comment.