Rearrange the terminal code to not have a cyclic dependency with the project

This commit is contained in:
Mikayla Maki 2022-12-08 10:48:28 -08:00
parent 1b8763d0cf
commit 83aefffa38
12 changed files with 270 additions and 300 deletions

View file

@ -1,5 +1,5 @@
pub mod mappings;
mod persistence;
pub use alacritty_terminal;
use alacritty_terminal::{
ansi::{ClearMode, Handler},
@ -30,7 +30,6 @@ use mappings::mouse::{
alt_scroll, grid_point, mouse_button_report, mouse_moved_report, mouse_side, scroll_report,
};
use persistence::TERMINAL_CONNECTION;
use procinfo::LocalProcessInfo;
use settings::{AlternateScroll, Settings, Shell, TerminalBlink};
use util::ResultExt;
@ -53,8 +52,7 @@ use gpui::{
geometry::vector::{vec2f, Vector2F},
keymap::Keystroke,
scene::{MouseDown, MouseDrag, MouseScrollWheel, MouseUp},
AppContext, ClipboardItem, Entity, ModelContext, MouseButton, MouseMovedEvent,
MutableAppContext, Task,
ClipboardItem, Entity, ModelContext, MouseButton, MouseMovedEvent, Task,
};
use crate::mappings::{
@ -63,12 +61,6 @@ use crate::mappings::{
};
use lazy_static::lazy_static;
///Initialize and register all of our action handlers
pub fn init(cx: &mut MutableAppContext) {
terminal_view::init(cx);
terminal_container_view::init(cx);
}
///Scrolling is unbearably sluggish by default. Alacritty supports a configurable
///Scroll multiplier that is set to 3 by default. This will be removed when I
///Implement scroll bars.
@ -124,10 +116,10 @@ impl EventListener for ZedListener {
#[derive(Clone, Copy, Debug)]
pub struct TerminalSize {
cell_width: f32,
line_height: f32,
height: f32,
width: f32,
pub cell_width: f32,
pub line_height: f32,
pub height: f32,
pub width: f32,
}
impl TerminalSize {
@ -281,8 +273,6 @@ impl TerminalBuilder {
blink_settings: Option<TerminalBlink>,
alternate_scroll: &AlternateScroll,
window_id: usize,
item_id: ItemId,
workspace_id: WorkspaceId,
) -> Result<TerminalBuilder> {
let pty_config = {
let alac_shell = shell.clone().and_then(|shell| match shell {
@ -387,8 +377,6 @@ impl TerminalBuilder {
last_mouse_position: None,
next_link_id: 0,
selection_phase: SelectionPhase::Ended,
workspace_id,
item_id,
};
Ok(TerminalBuilder {
@ -460,9 +448,9 @@ impl TerminalBuilder {
}
#[derive(Debug, Clone)]
struct IndexedCell {
point: Point,
cell: Cell,
pub struct IndexedCell {
pub point: Point,
pub cell: Cell,
}
impl Deref for IndexedCell {
@ -474,17 +462,18 @@ impl Deref for IndexedCell {
}
}
// TODO: Un-pub
#[derive(Clone)]
pub struct TerminalContent {
cells: Vec<IndexedCell>,
mode: TermMode,
display_offset: usize,
selection_text: Option<String>,
selection: Option<SelectionRange>,
cursor: RenderableCursor,
cursor_char: char,
size: TerminalSize,
last_hovered_hyperlink: Option<(String, RangeInclusive<Point>, usize)>,
pub cells: Vec<IndexedCell>,
pub mode: TermMode,
pub display_offset: usize,
pub selection_text: Option<String>,
pub selection: Option<SelectionRange>,
pub cursor: RenderableCursor,
pub cursor_char: char,
pub size: TerminalSize,
pub last_hovered_hyperlink: Option<(String, RangeInclusive<Point>, usize)>,
}
impl Default for TerminalContent {
@ -521,19 +510,17 @@ pub struct Terminal {
/// This is only used for terminal hyperlink checking
last_mouse_position: Option<Vector2F>,
pub matches: Vec<RangeInclusive<Point>>,
last_content: TerminalContent,
pub last_content: TerminalContent,
last_synced: Instant,
sync_task: Option<Task<()>>,
selection_head: Option<Point>,
breadcrumb_text: String,
pub selection_head: Option<Point>,
pub breadcrumb_text: String,
shell_pid: u32,
shell_fd: u32,
foreground_process_info: Option<LocalProcessInfo>,
pub foreground_process_info: Option<LocalProcessInfo>,
scroll_px: f32,
next_link_id: usize,
selection_phase: SelectionPhase,
workspace_id: WorkspaceId,
item_id: ItemId,
}
impl Terminal {
@ -574,20 +561,6 @@ impl Terminal {
if self.update_process_info() {
cx.emit(Event::TitleChanged);
if let Some(foreground_info) = &self.foreground_process_info {
let cwd = foreground_info.cwd.clone();
let item_id = self.item_id;
let workspace_id = self.workspace_id;
cx.background()
.spawn(async move {
TERMINAL_CONNECTION
.save_working_directory(item_id, workspace_id, cwd)
.await
.log_err();
})
.detach();
}
}
}
AlacTermEvent::ColorRequest(idx, fun_ptr) => {
@ -1190,42 +1163,13 @@ impl Terminal {
}
}
pub fn set_workspace_id(&mut self, id: WorkspaceId, cx: &AppContext) {
let old_workspace_id = self.workspace_id;
let item_id = self.item_id;
cx.background()
.spawn(async move {
TERMINAL_CONNECTION
.update_workspace_id(id, old_workspace_id, item_id)
.await
.log_err()
})
.detach();
self.workspace_id = id;
}
pub fn find_matches(
&mut self,
query: project::search::SearchQuery,
searcher: RegexSearch,
cx: &mut ModelContext<Self>,
) -> Task<Vec<RangeInclusive<Point>>> {
let term = self.term.clone();
cx.background().spawn(async move {
let searcher = match query {
project::search::SearchQuery::Text { query, .. } => {
RegexSearch::new(query.as_ref())
}
project::search::SearchQuery::Regex { query, .. } => {
RegexSearch::new(query.as_ref())
}
};
if searcher.is_err() {
return Vec::new();
}
let searcher = searcher.unwrap();
let term = term.lock();
all_search_matches(&term, &searcher).collect()
@ -1322,14 +1266,14 @@ fn open_uri(uri: &str) -> Result<(), std::io::Error> {
#[cfg(test)]
mod tests {
use alacritty_terminal::{
index::{Column, Line, Point},
term::cell::Cell,
};
use gpui::geometry::vector::vec2f;
use rand::{thread_rng, Rng};
use rand::{rngs::ThreadRng, thread_rng, Rng};
use crate::content_index_for_mouse;
use self::terminal_test_context::TerminalTestContext;
pub mod terminal_test_context;
use crate::{content_index_for_mouse, IndexedCell, TerminalContent, TerminalSize};
#[test]
fn test_mouse_to_cell() {
@ -1346,7 +1290,7 @@ mod tests {
width: cell_size * (viewport_cells as f32),
};
let (content, cells) = TerminalTestContext::create_terminal_content(size, &mut rng);
let (content, cells) = create_terminal_content(size, &mut rng);
for i in 0..(viewport_cells - 1) {
let i = i as usize;
@ -1382,7 +1326,7 @@ mod tests {
width: 100.,
};
let (content, cells) = TerminalTestContext::create_terminal_content(size, &mut rng);
let (content, cells) = create_terminal_content(size, &mut rng);
assert_eq!(
content.cells[content_index_for_mouse(vec2f(-10., -10.), &content)].c,
@ -1393,4 +1337,37 @@ mod tests {
cells[9][9]
);
}
fn create_terminal_content(
size: TerminalSize,
rng: &mut ThreadRng,
) -> (TerminalContent, Vec<Vec<char>>) {
let mut ic = Vec::new();
let mut cells = Vec::new();
for row in 0..((size.height() / size.line_height()) as usize) {
let mut row_vec = Vec::new();
for col in 0..((size.width() / size.cell_width()) as usize) {
let cell_char = rng.gen();
ic.push(IndexedCell {
point: Point::new(Line(row as i32), Column(col)),
cell: Cell {
c: cell_char,
..Default::default()
},
});
row_vec.push(cell_char)
}
cells.push(row_vec)
}
(
TerminalContent {
cells: ic,
size,
..Default::default()
},
cells,
)
}
}