From a61595e4624c8f1b35488ae77e5c033134a7a2ee Mon Sep 17 00:00:00 2001 From: Dongdong Zhou Date: Sat, 21 Nov 2020 18:36:03 +0000 Subject: [PATCH] proxy changes --- core/src/palette.rs | 2 -- core/src/proxy.rs | 18 +++++++++--------- core/src/state.rs | 3 +-- proxy/src/buffer.rs | 11 ++++++++++- proxy/src/dispatch.rs | 14 +++++++++++--- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/core/src/palette.rs b/core/src/palette.rs index 7ea5c639..38768791 100644 --- a/core/src/palette.rs +++ b/core/src/palette.rs @@ -1241,8 +1241,6 @@ pub fn select( &PaletteType::Workspace => { let state = LAPCE_APP_STATE.get_tab_state(&self.window_id, &self.tab_id); - state.stop(); - *state.workspace.lock() = self.workspace.clone().unwrap(); *state.ssh_session.lock() = None; state.start_proxy(); diff --git a/core/src/proxy.rs b/core/src/proxy.rs index b3a0e9ed..14e133e8 100644 --- a/core/src/proxy.rs +++ b/core/src/proxy.rs @@ -139,6 +139,7 @@ fn handle_notification( line_changes, rev, } => { + println!("receive update git"); let state = LAPCE_APP_STATE.get_tab_state(&self.window_id, &self.tab_id); let mut editor_split = state.editor_split.lock(); @@ -170,13 +171,7 @@ pub fn start_proxy_process( workspace: LapceWorkspace, ) { thread::spawn(move || { - let workspace_type = LAPCE_APP_STATE - .get_tab_state(&window_id, &tab_id) - .workspace - .lock() - .kind - .clone(); - let mut child = match workspace_type { + let mut child = match workspace.kind { LapceWorkspaceType::Local => { Command::new("/Users/Lulu/lapce/target/debug/lapce-proxy") .stdin(Stdio::piped()) @@ -185,7 +180,7 @@ pub fn start_proxy_process( } LapceWorkspaceType::RemoteSSH(user, host) => Command::new("ssh") .arg(format!("{}@{}", user, host)) - .arg("./lapce/lapce-proxy") + .arg("./.lapce/lapce-proxy") .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn(), @@ -206,7 +201,12 @@ pub fn start_proxy_process( proxy.initialize(workspace.path.clone()); { let state = LAPCE_APP_STATE.get_tab_state(&window_id, &tab_id); - *state.proxy.lock() = Some(proxy); + let mut proxy_state = state.proxy.lock(); + let old_proxy = proxy_state.take(); + *proxy_state = Some(proxy); + if let Some(old_proxy) = old_proxy { + old_proxy.process.lock().kill(); + } } let mut handler = ProxyHandler { window_id, tab_id }; diff --git a/core/src/state.rs b/core/src/state.rs index 8e765105..2573f087 100644 --- a/core/src/state.rs +++ b/core/src/state.rs @@ -352,10 +352,9 @@ pub fn new(window_id: WindowId) -> LapceTabState { ssh_session: Arc::new(Mutex::new(None)), proxy: Arc::new(Mutex::new(None)), }; - start_proxy_process(window_id, tab_id, workspace.clone()); let local_state = state.clone(); thread::spawn(move || { - local_state.start_plugin(); + local_state.start_proxy(); }); state } diff --git a/proxy/src/buffer.rs b/proxy/src/buffer.rs index 4a539209..fc0e35d4 100644 --- a/proxy/src/buffer.rs +++ b/proxy/src/buffer.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use crossbeam_channel::Sender; use std::borrow::Cow; use std::fs::File; use std::io::Read; @@ -20,10 +21,16 @@ pub struct Buffer { pub rope: Rope, pub path: PathBuf, pub rev: u64, + sender: Sender<(BufferId, u64)>, } impl Buffer { - pub fn new(id: BufferId, path: PathBuf) -> Buffer { + pub fn new( + id: BufferId, + path: PathBuf, + + sender: Sender<(BufferId, u64)>, + ) -> Buffer { let rope = if let Ok(rope) = load_file(&path) { rope } else { @@ -36,6 +43,7 @@ pub fn new(id: BufferId, path: PathBuf) -> Buffer { path, language_id, rev: 0, + sender, } } @@ -58,6 +66,7 @@ pub fn update( text: self.get_document(), }, }; + self.sender.send((self.id, self.rev)); Some(content_change) } diff --git a/proxy/src/dispatch.rs b/proxy/src/dispatch.rs index f1becf82..2df0feb0 100644 --- a/proxy/src/dispatch.rs +++ b/proxy/src/dispatch.rs @@ -3,7 +3,7 @@ use crate::lsp::LspCatalog; use crate::plugin::PluginCatalog; use anyhow::{anyhow, Result}; -use crossbeam_channel::{Receiver, Sender}; +use crossbeam_channel::{unbounded, Receiver, Sender}; use git2::Repository; use jsonrpc_lite::{self, JsonRpc}; use lapce_rpc::{self, Call, RequestId, RpcObject}; @@ -24,6 +24,7 @@ #[derive(Clone)] pub struct Dispatcher { pub sender: Arc>, + pub git_sender: Sender<(BufferId, u64)>, pub workspace: Arc>, buffers: Arc>>, plugins: Arc>, @@ -67,8 +68,10 @@ pub struct NewBufferResponse { impl Dispatcher { pub fn new(sender: Sender) -> Dispatcher { let plugins = PluginCatalog::new(); + let (git_sender, git_receiver) = unbounded(); let dispatcher = Dispatcher { sender: Arc::new(sender), + git_sender, workspace: Arc::new(Mutex::new(PathBuf::new())), buffers: Arc::new(Mutex::new(HashMap::new())), plugins: Arc::new(Mutex::new(plugins)), @@ -77,6 +80,10 @@ pub fn new(sender: Sender) -> Dispatcher { dispatcher.lsp.lock().dispatcher = Some(dispatcher.clone()); dispatcher.plugins.lock().reload(); dispatcher.plugins.lock().start_all(dispatcher.clone()); + let local_dispatcher = dispatcher.clone(); + thread::spawn(move || { + local_dispatcher.start_git_process(git_receiver); + }); dispatcher } @@ -103,8 +110,8 @@ pub fn start_git_process( &self, receiver: Receiver<(BufferId, u64)>, ) -> Result<()> { - let workspace = self.workspace.lock().clone(); loop { + let workspace = self.workspace.lock().clone(); let (buffer_id, rev) = receiver.recv()?; let buffers = self.buffers.lock(); let buffer = buffers.get(&buffer_id).unwrap(); @@ -117,6 +124,7 @@ pub fn start_git_process( ) }; + eprintln!("start to get git diff"); if let Some((diff, line_changes)) = get_git_diff(&workspace, &PathBuf::from(path), &content) { @@ -177,7 +185,7 @@ fn handle_notification(&self, rpc: Notification) { fn handle_request(&self, id: RequestId, rpc: Request) { match rpc { Request::NewBuffer { buffer_id, path } => { - let buffer = Buffer::new(buffer_id, path); + let buffer = Buffer::new(buffer_id, path, self.git_sender.clone()); let content = buffer.rope.to_string(); self.buffers.lock().insert(buffer_id, buffer); let resp = NewBufferResponse { content };