retrieve file from doc

This commit is contained in:
Dongdong Zhou 2022-04-29 21:54:51 +01:00
parent 74f70aee11
commit d819151f18
2 changed files with 104 additions and 16 deletions

View File

@ -2403,13 +2403,8 @@ pub fn go_to_location(
*self.tab_id,
ctx.get_external_handle(),
));
doc.retrieve_file(self.proxy.clone(), vec![(editor_view_id, location)]);
self.open_docs.insert(path.clone(), doc);
buffer.retrieve_file(
*self.tab_id,
self.proxy.clone(),
ctx.get_external_handle(),
vec![(editor_view_id, location)],
);
} else {
let buffer = self.open_files.get_mut(&path).unwrap().clone();
@ -2581,20 +2576,15 @@ pub fn new(
&mut positions,
tab_id,
config,
event_sink.clone(),
event_sink,
);
main_split_data.split_id = Arc::new(split_data.widget_id);
for (path, locations) in positions.into_iter() {
main_split_data
.open_files
.open_docs
.get(&path)
.unwrap()
.retrieve_file(
tab_id,
proxy.clone(),
event_sink.clone(),
locations.clone(),
);
.retrieve_file(proxy.clone(), locations.clone());
}
} else {
main_split_data.splits.insert(

View File

@ -1,10 +1,12 @@
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
path::PathBuf,
rc::Rc,
sync::{atomic, Arc},
};
use anyhow::Result;
use druid::{
piet::{
PietText, PietTextLayout, Text, TextAttribute, TextLayout, TextLayoutBuilder,
@ -24,14 +26,19 @@
syntax::Syntax,
word::WordCursor,
};
use lapce_rpc::style::{LineStyle, LineStyles, Style};
use lapce_rpc::{
buffer::{BufferId, NewBufferResponse},
style::{LineStyle, LineStyles, Style},
};
use xi_rope::{spans::Spans, RopeDelta};
use crate::{
buffer::BufferContent,
buffer::{BufferContent, LocalBufferKind},
command::{LapceUICommand, LAPCE_UI_COMMAND},
config::{Config, LapceTheme},
editor::EditorLocationNew,
find::Find,
proxy::LapceProxy,
};
pub struct SystemClipboard {}
@ -48,6 +55,7 @@ fn put_string(&mut self, s: impl AsRef<str>) {
#[derive(Clone)]
pub struct Document {
id: BufferId,
tab_id: WidgetId,
buffer: Buffer,
content: BufferContent,
@ -56,6 +64,8 @@ pub struct Document {
semantic_styles: Option<Arc<Spans<Style>>>,
text_layouts: Rc<RefCell<HashMap<usize, Arc<PietTextLayout>>>>,
event_sink: ExtEventSink,
load_started: Rc<RefCell<bool>>,
loaded: bool,
}
impl Document {
@ -65,6 +75,7 @@ pub fn new(
event_sink: ExtEventSink,
) -> Self {
Self {
id: BufferId::next(),
tab_id,
buffer: Buffer::new(""),
content,
@ -73,6 +84,8 @@ pub fn new(
text_layouts: Rc::new(RefCell::new(HashMap::new())),
semantic_styles: None,
event_sink,
load_started: Rc::new(RefCell::new(false)),
loaded: false,
}
}
@ -90,9 +103,94 @@ pub fn load_content(&mut self, content: &str) {
self.on_update(None);
}
pub fn retrieve_file(
&self,
proxy: Arc<LapceProxy>,
locations: Vec<(WidgetId, EditorLocationNew)>,
) {
if self.loaded || *self.load_started.borrow() {
return;
}
*self.load_started.borrow_mut() = true;
if let BufferContent::File(path) = &self.content {
let id = self.id;
let tab_id = self.tab_id;
let path = path.clone();
let event_sink = self.event_sink.clone();
std::thread::spawn(move || {
proxy.new_buffer(
id,
path.clone(),
Box::new(move |result| {
if let Ok(res) = result {
if let Ok(resp) =
serde_json::from_value::<NewBufferResponse>(res)
{
let _ = event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::LoadBuffer {
path,
content: resp.content,
locations,
},
Target::Widget(tab_id),
);
}
};
}),
)
});
}
}
fn on_update(&mut self, delta: Option<&RopeDelta>) {
self.clear_text_layout_cache();
self.trigger_syntax_change(delta);
self.notify_special();
}
fn notify_special(&self) {
match &self.content {
BufferContent::File(_) => {}
BufferContent::Local(local) => {
let s = self.buffer.text().to_string();
match local {
LocalBufferKind::Search => {
let _ = self.event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::UpdateSearch(s),
Target::Widget(self.tab_id),
);
}
LocalBufferKind::SourceControl => {}
LocalBufferKind::Empty => {}
LocalBufferKind::FilePicker => {
let pwd = PathBuf::from(s);
let _ = self.event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::UpdatePickerPwd(pwd),
Target::Widget(self.tab_id),
);
}
LocalBufferKind::Keymap => {
let _ = self.event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::UpdateKeymapsFilter(s),
Target::Widget(self.tab_id),
);
}
LocalBufferKind::Settings => {
let _ = self.event_sink.submit_command(
LAPCE_UI_COMMAND,
LapceUICommand::UpdateSettingsFilter(s),
Target::Widget(self.tab_id),
);
}
}
}
BufferContent::Value(_) => {}
}
}
pub fn set_syntax(&mut self, syntax: Option<Syntax>) {