Fixed Buffer edit + save only dirty (not pristine) buffers: the feature works

This commit is contained in:
Noé Heuillet 2022-07-20 19:50:37 +02:00
parent df2c3df125
commit f33dc293e4
6 changed files with 38 additions and 99 deletions

View File

@ -419,6 +419,7 @@ pub enum LapceUICommand {
path: PathBuf,
content: Rope,
locations: Vec<(WidgetId, EditorLocation)>,
edits: Option<Rope>,
},
/// Init buffer content but using lsp positions instead
InitBufferContentLsp {

View File

@ -612,7 +612,7 @@ pub fn new(
let search = Arc::new(SearchData::new());
let file_picker = Arc::new(FilePickerData::new());
let mut unsaved_buffers = match db.get_unsaved_buffers() {
let unsaved_buffers = match db.get_unsaved_buffers() {
Ok(val) => val,
Err(err) => {
log::warn!("Error during unsaved buffer fetching : {:}", err);
@ -620,8 +620,6 @@ pub fn new(
}
};
unsaved_buffers.clear();
let mut main_split = LapceMainSplitData::new(
tab_id,
workspace_info.as_ref(),
@ -634,30 +632,6 @@ pub fn new(
unsaved_buffers,
);
let unsaved_buffers = match db.get_unsaved_buffers() {
Ok(val) => val,
Err(err) => {
log::warn!("Error during unsaved buffer fetching : {:}", err);
im::HashMap::new()
}
};
for (doc_path, doc) in main_split.open_docs.iter_mut() {
let doc_path_string = doc_path.to_str().unwrap().to_string();
// if doc_path_string.contains("main.go") {
// println!("SKIPPING");
// continue;
// }
let val = match unsaved_buffers.get(&doc_path_string) {
Some(val) => val,
None => continue,
};
let new_rope = Rope::from(val);
Arc::make_mut(doc).buffer_mut().init_content(new_rope);
}
main_split.add_editor(
source_control.editor_view_id,
None,
@ -713,33 +687,6 @@ pub fn new(
let focus = (*main_split.active).unwrap_or(*main_split.split_id);
// if unsave_buffers.is_ok() {
// let unsave_buffers = unsave_buffers.unwrap();
// for (path, doc) in main_split.open_docs.iter() {
// for (path_buf, content) in &unsave_buffers {
// let path_buf = PathBuf::from(path_buf);
// if path == &path_buf {
// let doc = main_split.open_docs.get_mut(&path_buf).unwrap();
// let new_rope = Rope::from(content);
// doc.init_content(new_rope);
// // main_split.open_docs.insert(path_buf, Arc::new(doc));
// }
// }
// println!(
// "Current new lapceTabData: {:?} {:?}",
// path,
// doc.buffer().text()
// );
// }
// } else {
// log::warn!(
// "Error during unsaved buffer fetching : {:?}",
// unsave_buffers.unwrap_err()
// );
// }
let mut tab = Self {
id: tab_id,
multiple_tab: false,
@ -1962,7 +1909,12 @@ pub fn document_save(
exit_widget_id: Option<WidgetId>,
) {
let doc = self.open_docs.get(path).unwrap();
println!("DOCUMENT SAVE:\n {:?}", doc.buffer().text());
println!("DOCUMENT SAVE:\n");
println!(
"buffer: {:?}\nrev: {:?}",
doc.buffer().text(),
doc.buffer().rev()
);
let rev = doc.rev();
let buffer_id = doc.id();
let event_sink = ctx.get_external_handle();
@ -2505,7 +2457,7 @@ pub fn go_to_location<P: EditorPosition + Send + 'static>(
Vec2::new(info.scroll_offset.0, info.scroll_offset.1);
doc.cursor_offset = info.cursor_offset;
}
doc.retrieve_file(vec![(editor_view_id, location)]);
doc.retrieve_file(vec![(editor_view_id, location)], None);
self.open_docs.insert(path.clone(), Arc::new(doc));
} else {
let doc = self.open_docs.get_mut(&path).unwrap().clone();
@ -2712,12 +2664,16 @@ pub fn new(
tab_id,
config,
event_sink,
unsaved_buffers,
);
main_split_data.split_id = Arc::new(split_data.widget_id);
for (path, locations) in positions.into_iter() {
let unsaved_buffer =
match unsaved_buffers.get(&path.to_str().unwrap().to_string()) {
Some(val) => Some(Rope::from(val)),
None => None,
};
Arc::make_mut(main_split_data.open_docs.get_mut(&path).unwrap())
.retrieve_file(locations.clone());
.retrieve_file(locations.clone(), unsaved_buffer);
}
} else {
main_split_data.splits.insert(

View File

@ -54,7 +54,6 @@ pub fn to_data(
tab_id: WidgetId,
config: &Config,
event_sink: ExtEventSink,
unsaved_buffers: im::HashMap<String, String>,
) -> SplitContent {
match &self {
SplitContentInfo::EditorTab(tab_info) => {
@ -65,7 +64,6 @@ pub fn to_data(
tab_id,
config,
event_sink,
unsaved_buffers,
);
SplitContent::EditorTab(tab_data.widget_id)
}
@ -77,7 +75,6 @@ pub fn to_data(
tab_id,
config,
event_sink,
unsaved_buffers,
);
SplitContent::Split(split_data.widget_id)
}
@ -101,7 +98,6 @@ pub fn to_data(
tab_id: WidgetId,
config: &Config,
event_sink: ExtEventSink,
unsaved_buffers: im::HashMap<String, String>,
) -> LapceEditorTabData {
let editor_tab_id = WidgetId::next();
let editor_tab_data = LapceEditorTabData {
@ -119,7 +115,6 @@ pub fn to_data(
tab_id,
config,
event_sink.clone(),
unsaved_buffers.clone(),
)
})
.collect(),
@ -153,7 +148,6 @@ pub fn to_data(
tab_id: WidgetId,
config: &Config,
event_sink: ExtEventSink,
unsaved_buffers: im::HashMap<String, String>,
) -> EditorTabChild {
match &self {
EditorTabChildInfo::Editor(editor_info) => {
@ -164,7 +158,6 @@ pub fn to_data(
tab_id,
config,
event_sink,
unsaved_buffers,
);
EditorTabChild::Editor(
editor_data.view_id,
@ -194,7 +187,6 @@ pub fn to_data(
tab_id: WidgetId,
config: &Config,
event_sink: ExtEventSink,
unsaved_buffers: im::HashMap<String, String>,
) -> SplitData {
let split_id = WidgetId::next();
let split_data = SplitData {
@ -212,7 +204,6 @@ pub fn to_data(
tab_id,
config,
event_sink.clone(),
unsaved_buffers.clone(),
)
})
.collect(),
@ -273,7 +264,6 @@ pub fn to_data(
tab_id: WidgetId,
config: &Config,
event_sink: ExtEventSink,
unsaved_buffers: im::HashMap<String, String>,
) -> LapceEditorData {
let editor_data = LapceEditorData::new(
None,
@ -301,25 +291,15 @@ pub fn to_data(
));
if !data.open_docs.contains_key(path) {
let mut doc = Document::new(
BufferContent::File(path.clone()),
tab_id,
event_sink,
data.proxy.clone(),
data.open_docs.insert(
path.clone(),
Arc::new(Document::new(
BufferContent::File(path.clone()),
tab_id,
event_sink,
data.proxy.clone(),
)),
);
let string_path = path.to_str().unwrap().to_string();
if let Some(val) = unsaved_buffers.get(&string_path) {
println!(
"CURRENT SAVED BUFFER FOR PATH {}:\n {}",
string_path, val
);
let new_rope = Rope::from(val);
doc.buffer_mut().init_content(new_rope);
println!("AFTER VAL: {}", doc.buffer_mut().text().to_string());
};
let doc = Arc::new(doc);
data.open_docs.insert(path.clone(), doc);
}
} else if let BufferContent::Scratch(id, _) = &self.content {
if !data.scratch_docs.contains_key(id) {
@ -586,10 +566,14 @@ fn insert_unsaved_buffer(&self, main_split: &LapceMainSplitData) -> Result<()> {
let sled_db = self.get_db()?;
for (path, doc) in &main_split.open_docs {
let path_str = path.to_str().unwrap();
let buf_text = doc.buffer().text().to_string();
sled_db
.insert(format!("unsaved_buffer:{}", path_str), buf_text.as_str())?;
if !doc.buffer().is_pristine() && doc.content().is_file() {
let path_str = path.to_str().unwrap();
let buf_text = doc.buffer().text().to_string();
sled_db.insert(
format!("unsaved_buffer:{}", path_str),
buf_text.as_str(),
)?;
}
}
sled_db.flush()?;

View File

@ -539,6 +539,7 @@ pub fn retrieve_file<P: EditorPosition + Send + 'static>(
&mut self,
locations: Vec<(WidgetId, EditorLocation<P>)>,
) {
unsaved_buffer: Option<Rope>,
if self.loaded || *self.load_started.borrow() {
return;
}
@ -559,6 +560,7 @@ pub fn retrieve_file<P: EditorPosition + Send + 'static>(
path,
Rope::from(resp.content),
locations,
edits: unsaved_buffer,
),
Target::Widget(tab_id),
);

View File

@ -219,20 +219,11 @@ fn window_removed(
_env: &Env,
_ctx: &mut druid::DelegateCtx,
) {
println!("Removing window!");
if let Some(window) = data.windows.remove(&id) {
// let tmp = window.tabs.get(&window.active_id).unwrap();
// let tmp = tmp.main_split.active_editor().unwrap();
// println!("Buffer value: {:?}", tmp.content);
for (_, tab) in window.tabs.iter() {
let split = &tab.main_split;
for (path, open_doc) in &split.open_docs {
println!(
"PATH: {:?}\nContent: {:?}",
path,
open_doc.buffer().text()
);
}
let _ = data.db.save_workspace(tab);
}
data.db.save_last_window(&window);

View File

@ -731,10 +731,15 @@ fn handle_event(
path,
content,
locations,
edits,
} => {
let doc = data.main_split.open_docs.get_mut(path).unwrap();
let doc = Arc::make_mut(doc);
doc.init_content(content.to_owned());
if let Some(rope) = edits {
doc.reload(rope.clone(), false);
}
if let BufferContent::File(path) = doc.content() {
if let Some(d) = data.main_split.diagnostics.get(path) {
doc.set_diagnostics(d);