Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle binary data properly, implement different buffer types #2541

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion lapce-app/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ fn completion_lens_text(
.unwrap_or(InsertTextFormat::PLAIN_TEXT);

// We don't display insert and replace
let CompletionTextEdit::Edit(edit) = edit else { return None };
let CompletionTextEdit::Edit(edit) = edit else {
return None;
};
// The completion offset can be different from the current cursor offset.
let completion_offset = completion.offset;

Expand Down
4 changes: 3 additions & 1 deletion lapce-app/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,9 @@ impl Document {

/// Update the completion lens position after an edit so that it appears in the correct place.
pub fn update_completion_lens(&mut self, delta: &RopeDelta) {
let Some(completion) = self.completion_lens.as_ref() else { return };
let Some(completion) = self.completion_lens.as_ref() else {
return;
};

let (line, col) = self.completion_pos;
let offset = self.buffer().offset_of_line_col(line, col);
Expand Down
9 changes: 8 additions & 1 deletion lapce-app/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,14 @@ impl PaletteData {
}

fn preselect_matching(&self, matching: &str) {
let Some((idx, _)) = self.items.get_untracked().iter().find_position(|item| item.filter_text == matching) else { return };
let Some((idx, _)) = self
.items
.get_untracked()
.iter()
.find_position(|item| item.filter_text == matching)
else {
return;
};

self.index.set(idx);
}
Expand Down
33 changes: 22 additions & 11 deletions lapce-app/src/window_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,17 +540,20 @@ impl WindowTabData {
let mut paths = HashSet::new();
for (_, editor_data) in editors.iter() {
editor_data.with_untracked(|editor_data| {
let should_save = editor_data.view.doc.with_untracked(|doc| {
let DocContent::File(path) = &doc.content else { return false };
let should_save =
editor_data.view.doc.with_untracked(|doc| {
let DocContent::File(path) = &doc.content else {
return false;
};

if paths.contains(path) {
return false;
}
if paths.contains(path) {
return false;
}

paths.insert(path.clone());
paths.insert(path.clone());

true
});
true
});

if should_save {
editor_data.save(true, || {});
Expand Down Expand Up @@ -681,12 +684,17 @@ impl WindowTabData {
self.main_split.active_editor_tab.get_untracked()
{
self.main_split.editor_tabs.with_untracked(|editor_tabs| {
let Some(editor_tab) = editor_tabs.get(&editor_tab_id) else { return };
let Some(editor_tab) = editor_tabs.get(&editor_tab_id)
else {
return;
};

let new_index = editor_tab.with_untracked(|editor_tab| {
if editor_tab.children.is_empty() {
None
} else if editor_tab.active == editor_tab.children.len() - 1 {
} else if editor_tab.active
== editor_tab.children.len() - 1
{
Some(0)
} else {
Some(editor_tab.active + 1)
Expand All @@ -706,7 +714,10 @@ impl WindowTabData {
self.main_split.active_editor_tab.get_untracked()
{
self.main_split.editor_tabs.with_untracked(|editor_tabs| {
let Some(editor_tab) = editor_tabs.get(&editor_tab_id) else { return };
let Some(editor_tab) = editor_tabs.get(&editor_tab_id)
else {
return;
};

let new_index = editor_tab.with_untracked(|editor_tab| {
if editor_tab.children.is_empty() {
Expand Down
69 changes: 55 additions & 14 deletions lapce-proxy/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ use lapce_rpc::buffer::BufferId;
use lapce_xi_rope::{interval::IntervalBounds, rope::Rope, RopeDelta};
use lsp_types::*;

#[derive(Clone)]
pub enum BufferType {
Text(Buffer),
File,
Error(String),
}

impl BufferType {
pub fn new(id: BufferId, path: PathBuf) -> BufferType {
return match load_file(&path) {
Ok(content) => {
let rope = Rope::from(content);
let rev = u64::from(!rope.is_empty());
let language_id = language_id_from_path(&path).unwrap_or("");
let mod_time = get_mod_time(&path);
BufferType::Text(Buffer {
id,
rope,
path,
language_id,
rev,
mod_time,
})
}
Err(e) => BufferType::Error(e.to_string()),
};
}
}

#[derive(Clone)]
pub struct Buffer {
pub language_id: &'static str,
Expand All @@ -27,20 +56,25 @@ pub struct Buffer {
}

impl Buffer {
pub fn new(id: BufferId, path: PathBuf) -> Buffer {
let rope = Rope::from(load_file(&path).unwrap_or_default());
let rev = u64::from(!rope.is_empty());
let language_id = language_id_from_path(&path).unwrap_or("");
let mod_time = get_mod_time(&path);
Buffer {
id,
rope,
path,
language_id,
rev,
mod_time,
}
}
// pub fn new(id: BufferId, path: PathBuf) -> BufferType {
// return match load_file(&path) {
// Ok(content) => {
// let rope = Rope::from(content);
// let rev = u64::from(!rope.is_empty());
// let language_id = language_id_from_path(&path).unwrap_or("");
// let mod_time = get_mod_time(&path);
// BufferType::Text(Buffer {
// id,
// rope,
// path,
// language_id,
// rev,
// mod_time,
// })
// }
// Err(e) => BufferType::Error(e.to_string()),
// };
// }

pub fn save(&mut self, rev: u64) -> Result<()> {
if self.rev != rev {
Expand Down Expand Up @@ -165,6 +199,13 @@ pub fn read_path_to_string_lossy<P: AsRef<Path>>(
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;

if buffer[..8] == [137, 80, 78, 71, 13, 10, 26, 10] {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Not a text file",
));
}

// Parse the file contents as utf8, replacing non-utf8 data with the
// replacement character
let contents = String::from_utf8_lossy(&buffer);
Expand Down