Implement 'new N' names for scratch buffers.

This commit is contained in:
Philip Daniels 2022-05-24 21:52:25 +01:00
parent aa67b7c274
commit 48fec1d862
9 changed files with 57 additions and 34 deletions

View File

@ -701,7 +701,7 @@ pub fn editor_view_content(
BufferContent::File(path) => {
self.main_split.open_docs.get(path).unwrap().clone()
}
BufferContent::Scratch(id) => {
BufferContent::Scratch(id, _) => {
self.main_split.scratch_docs.get(id).unwrap().clone()
}
BufferContent::Local(kind) => {
@ -742,7 +742,7 @@ pub fn code_action_size(&self, text: &mut PietText, _env: &Env) -> Size {
let offset = editor.new_cursor.offset();
doc.code_action_size(text, offset, &self.config)
}
BufferContent::Scratch(id) => {
BufferContent::Scratch(id, _) => {
let doc = self.main_split.scratch_docs.get(id).unwrap();
let offset = editor.new_cursor.offset();
doc.code_action_size(text, offset, &self.config)
@ -781,7 +781,7 @@ pub fn update_from_editor_buffer_data(
.open_docs
.insert(path.clone(), editor_buffer_data.doc);
}
BufferContent::Scratch(id) => {
BufferContent::Scratch(id, _) => {
self.main_split
.scratch_docs
.insert(*id, editor_buffer_data.doc);
@ -823,7 +823,7 @@ pub fn completion_origin(
*editor.window_origin.borrow()
- self.window_origin.borrow().to_vec2()
}
BufferContent::File(_) | BufferContent::Scratch(_) => {
BufferContent::File(_) | BufferContent::Scratch(_, _) => {
let doc = self.main_split.editor_doc(editor.view_id);
let offset = self.completion.offset;
let (line, col) = doc.buffer().offset_to_line_col(offset);
@ -879,7 +879,7 @@ pub fn hover_origin(
*editor.window_origin.borrow()
- self.window_origin.borrow().to_vec2()
}
BufferContent::File(_) | BufferContent::Scratch(_) => {
BufferContent::File(_) | BufferContent::Scratch(_, _) => {
let doc = self.main_split.editor_doc(editor.view_id);
let offset = self.hover.offset;
let (line, col) = doc.buffer().offset_to_line_col(offset);
@ -1764,7 +1764,7 @@ pub fn content_doc(&self, content: &BufferContent) -> Arc<Document> {
BufferContent::File(path) => self.open_docs.get(path).unwrap().clone(),
BufferContent::Local(kind) => self.local_docs.get(kind).unwrap().clone(),
BufferContent::Value(name) => self.value_docs.get(name).unwrap().clone(),
BufferContent::Scratch(id) => self.scratch_docs.get(id).unwrap().clone(),
BufferContent::Scratch(id, _) => self.scratch_docs.get(id).unwrap().clone(),
}
}
@ -2244,11 +2244,34 @@ pub fn jump_to_location(
editor_view_id
}
fn get_name_for_new_file(&self) -> String {
let mut i = 0;
loop {
i += 1;
let potential_name = format!("new {}", i);
// Checking just the current scratch_docs rather than all the different document
// collections seems to be the right thing to do. The user may have genuine 'new N'
// files tucked away somewhere in their workspace.
if self.scratch_docs.values().any(|doc| match doc.content() {
BufferContent::Scratch(_, existing_name) => {
*existing_name == potential_name
}
_ => false,
}) {
continue;
}
return potential_name;
}
}
pub fn new_file(&mut self, ctx: &mut EventCtx, config: &Config) {
let tab_id = *self.tab_id;
let proxy = self.proxy.clone();
let buffer_id = BufferId::next();
let content = BufferContent::Scratch(buffer_id);
let content =
BufferContent::Scratch(buffer_id, self.get_name_for_new_file());
let doc =
Document::new(content.clone(), tab_id, ctx.get_external_handle(), proxy);
self.scratch_docs.insert(buffer_id, Arc::new(doc));
@ -2283,7 +2306,7 @@ pub fn go_to_location(
BufferContent::File(path) => path != &location.path,
BufferContent::Local(_) => true,
BufferContent::Value(_) => true,
BufferContent::Scratch(_) => true,
BufferContent::Scratch(_, _) => true,
};
if new_buffer {
self.db.save_doc_position(&self.workspace, &doc);
@ -2589,12 +2612,12 @@ pub fn save_as_success(
exit: bool,
) {
match content {
BufferContent::Scratch(id) => {
BufferContent::Scratch(id, scratch_doc_name) => {
let doc = self.scratch_docs.get(id).unwrap();
if doc.rev() == rev {
let new_content = BufferContent::File(path.to_path_buf());
for (_, editor) in self.editors.iter_mut() {
if editor.content == BufferContent::Scratch(*id) {
if editor.content== BufferContent::Scratch(*id, scratch_doc_name.to_string()) {
Arc::make_mut(editor).content = new_content.clone();
}
}
@ -2630,7 +2653,7 @@ pub fn save_as(
exit: bool,
) {
match content {
BufferContent::Scratch(id) => {
BufferContent::Scratch(id, _) => {
let event_sink = ctx.get_external_handle();
let doc = self.scratch_docs.get(id).unwrap();
let rev = doc.rev();
@ -2686,7 +2709,9 @@ pub fn editor_close(
force: bool,
) {
let editor = self.editors.get(&view_id).unwrap();
if let BufferContent::File(_) | BufferContent::Scratch(_) = &editor.content {
if let BufferContent::File(_) | BufferContent::Scratch(_, _) =
&editor.content
{
let doc = self.editor_doc(view_id);
if !force && !doc.buffer().is_pristine() {
let exits = self.editors.iter().any(|(_, e)| {
@ -3229,7 +3254,7 @@ pub fn save_jump_location(&mut self, doc: &Document) {
}
pub fn editor_info(&self, data: &LapceTabData) -> EditorInfo {
let unsaved = if let BufferContent::Scratch(id) = &self.content {
let unsaved = if let BufferContent::Scratch(id, _) = &self.content {
let doc = data.main_split.scratch_docs.get(id).unwrap();
Some(doc.buffer().text().to_string())
} else {

View File

@ -296,7 +296,7 @@ pub fn to_data(
));
data.open_docs.insert(path.clone(), doc);
}
} else if let BufferContent::Scratch(id) = &self.content {
} else if let BufferContent::Scratch(id, _) = &self.content {
if !data.scratch_docs.contains_key(id) {
let mut doc = Document::new(
self.content.clone(),

View File

@ -99,7 +99,7 @@ pub enum BufferContent {
File(PathBuf),
Local(LocalBufferKind),
Value(String),
Scratch(BufferId),
Scratch(BufferId, String),
}
impl BufferContent {
@ -120,7 +120,7 @@ pub fn is_special(&self) -> bool {
LocalBufferKind::Empty => false,
},
BufferContent::Value(_) => true,
BufferContent::Scratch(_) => false,
BufferContent::Scratch(_, _) => false,
}
}
@ -136,7 +136,7 @@ pub fn is_input(&self) -> bool {
LocalBufferKind::Empty | LocalBufferKind::SourceControl => false,
},
BufferContent::Value(_) => true,
BufferContent::Scratch(_) => false,
BufferContent::Scratch(_, _) => false,
}
}
@ -144,7 +144,7 @@ pub fn is_search(&self) -> bool {
match &self {
BufferContent::File(_) => false,
BufferContent::Value(_) => false,
BufferContent::Scratch(_) => false,
BufferContent::Scratch(_, _) => false,
BufferContent::Local(local) => matches!(local, LocalBufferKind::Search),
}
}
@ -154,7 +154,7 @@ pub fn is_settings(&self) -> bool {
BufferContent::File(_) => false,
BufferContent::Value(_) => true,
BufferContent::Local(_) => false,
BufferContent::Scratch(_) => false,
BufferContent::Scratch(_, _) => false,
}
}
@ -163,7 +163,7 @@ pub fn file_name(&self) -> &str {
BufferContent::File(p) => {
p.file_name().and_then(|f| f.to_str()).unwrap_or("")
}
BufferContent::Scratch(_) => "[Untitled]",
BufferContent::Scratch(_, scratch_doc_name) => scratch_doc_name,
_ => "",
}
}
@ -202,10 +202,10 @@ pub fn new(
BufferContent::File(path) => Syntax::init(path),
BufferContent::Local(_) => None,
BufferContent::Value(_) => None,
BufferContent::Scratch(_) => None,
BufferContent::Scratch(_, _) => None,
};
let id = match &content {
BufferContent::Scratch(id) => *id,
BufferContent::Scratch(id, _) => *id,
_ => BufferId::next(),
};
@ -245,7 +245,7 @@ pub fn set_content(&mut self, content: BufferContent) {
BufferContent::File(path) => Syntax::init(path),
BufferContent::Local(_) => None,
BufferContent::Value(_) => None,
BufferContent::Scratch(_) => None,
BufferContent::Scratch(_, _) => None,
};
self.on_update(None);
}
@ -473,7 +473,7 @@ fn on_update(&mut self, delta: Option<&RopeDelta>) {
fn notify_special(&self) {
match &self.content {
BufferContent::File(_) => {}
BufferContent::Scratch(_) => {}
BufferContent::Scratch(_, _) => {}
BufferContent::Local(local) => {
let s = self.buffer.text().to_string();
match local {

View File

@ -1175,7 +1175,7 @@ fn save(&mut self, ctx: &mut EventCtx, exit: bool) {
Target::Auto,
);
});
} else if let BufferContent::Scratch(_) = self.doc.content() {
} else if let BufferContent::Scratch(_, _) = self.doc.content() {
let content = self.doc.content().clone();
let view_id = self.editor.view_id;
self.main_split.current_save_as =
@ -1945,7 +1945,7 @@ fn check_condition(&self, condition: &str) -> bool {
"input_focus" => self.editor.content.is_input(),
"editor_focus" => match self.editor.content {
BufferContent::File(_) => true,
BufferContent::Scratch(_) => true,
BufferContent::Scratch(_, _) => true,
BufferContent::Local(_) => false,
BufferContent::Value(_) => false,
},

View File

@ -285,7 +285,7 @@ pub fn get_size(
let width = data.config.editor_char_width(text);
match &data.editor.content {
BufferContent::File(_)
| BufferContent::Scratch(_)
| BufferContent::Scratch(_, _)
| BufferContent::Local(LocalBufferKind::Empty) => {
if data.editor.code_lens {
if let Some(syntax) = data.doc.syntax() {

View File

@ -135,14 +135,12 @@ pub fn paint_buffer(
clip_rect.x1 = icon.rect.x0;
}
}
if let BufferContent::File(_) | BufferContent::Scratch(_) =
if let BufferContent::File(_) | BufferContent::Scratch(_, _) =
data.doc.content()
{
let mut path = match data.doc.content() {
BufferContent::File(path) => path.to_path_buf(),
BufferContent::Scratch(_) => {
PathBuf::from(data.doc.content().file_name())
}
BufferContent::Scratch(_, scratch_doc_name) => scratch_doc_name.into(),
_ => PathBuf::from(""),
};

View File

@ -116,7 +116,7 @@ fn paint_header(&self, ctx: &mut PaintCtx, data: &LapceTabData) {
.and_then(|s| s.to_str())
.unwrap_or("")
.to_string();
} else if let BufferContent::Scratch(_) =
} else if let BufferContent::Scratch(..) =
&editor_buffer.editor.content
{
text = editor_buffer.editor.content.file_name().to_string();

View File

@ -364,7 +364,7 @@ fn layout(
text = s.to_string();
}
}
} else if let BufferContent::Scratch(_) = &editor.content {
} else if let BufferContent::Scratch(_, _) = &editor.content {
text = editor.content.file_name().to_string();
}
}

View File

@ -120,7 +120,7 @@ pub fn request_focus(
));
}
match &editor.content {
BufferContent::File(_) | BufferContent::Scratch(_) => {
BufferContent::File(_) | BufferContent::Scratch(_, _) => {
data.focus_area = FocusArea::Editor;
data.main_split.active = Arc::new(Some(self.view_id));
data.main_split.active_tab = Arc::new(editor.tab_id);