proxy changes

This commit is contained in:
Dongdong Zhou 2020-11-21 18:36:03 +00:00
parent b2daa3e0ae
commit a61595e462
5 changed files with 31 additions and 17 deletions

View File

@ -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();

View File

@ -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 };

View File

@ -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
}

View File

@ -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)
}

View File

@ -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<Sender<Value>>,
pub git_sender: Sender<(BufferId, u64)>,
pub workspace: Arc<Mutex<PathBuf>>,
buffers: Arc<Mutex<HashMap<BufferId, Buffer>>>,
plugins: Arc<Mutex<PluginCatalog>>,
@ -67,8 +68,10 @@ pub struct NewBufferResponse {
impl Dispatcher {
pub fn new(sender: Sender<Value>) -> 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<Value>) -> 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 };