Don't insert a final newline in editlines (#320)

This commit is contained in:
Leonard Hecker 2025-05-27 17:08:51 +02:00 committed by GitHub
parent 0a36f82bb4
commit 41eb396ef7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 23 deletions

View file

@ -114,13 +114,7 @@ impl DocumentManager {
}
pub fn add_untitled(&mut self) -> apperr::Result<&mut Document> {
let buffer = TextBuffer::new_rc(false)?;
{
let mut tb = buffer.borrow_mut();
tb.set_margin_enabled(true);
tb.set_line_highlight_enabled(true);
}
let buffer = Self::create_buffer()?;
let mut doc = Document {
buffer,
path: None,
@ -167,13 +161,10 @@ impl DocumentManager {
return Ok(doc);
}
let buffer = TextBuffer::new_rc(false)?;
let buffer = Self::create_buffer()?;
{
let mut tb = buffer.borrow_mut();
tb.set_margin_enabled(true);
tb.set_line_highlight_enabled(true);
if let Some(file) = &mut file {
let mut tb = buffer.borrow_mut();
tb.read_file(file, None)?;
if let Some(goto) = goto
@ -216,6 +207,17 @@ impl DocumentManager {
File::create(path).map_err(apperr::Error::from)
}
fn create_buffer() -> apperr::Result<RcTextBuffer> {
let buffer = TextBuffer::new_rc(false)?;
{
let mut tb = buffer.borrow_mut();
tb.set_insert_final_newline(!cfg!(windows)); // As mandated by POSIX.
tb.set_margin_enabled(true);
tb.set_line_highlight_enabled(true);
}
Ok(buffer)
}
// Parse a filename in the form of "filename:line:char".
// Returns the position of the first colon and the line/char coordinates.
fn parse_filename_goto(path: &Path) -> (&Path, Option<Point>) {

View file

@ -6,7 +6,6 @@ use std::ffi::{OsStr, OsString};
use std::mem;
use std::path::{Path, PathBuf};
use edit::buffer::TextBuffer;
use edit::framebuffer::IndexedColor;
use edit::helpers::*;
use edit::tui::*;
@ -164,13 +163,6 @@ pub struct State {
impl State {
pub fn new() -> apperr::Result<Self> {
let buffer = TextBuffer::new_rc(false)?;
{
let mut tb = buffer.borrow_mut();
tb.set_margin_enabled(true);
tb.set_line_highlight_enabled(true);
}
Ok(Self {
menubar_color_bg: 0,
menubar_color_fg: 0,

View file

@ -250,9 +250,8 @@ impl TextBuffer {
line_highlight_enabled: false,
ruler: 0,
encoding: "UTF-8",
// Windows users want CRLF and no final newline.
newlines_are_crlf: cfg!(windows),
insert_final_newline: !cfg!(windows),
newlines_are_crlf: cfg!(windows), // Windows users want CRLF
insert_final_newline: false,
overtype: false,
wants_cursor_visibility: false,
@ -384,6 +383,12 @@ impl TextBuffer {
self.newlines_are_crlf = crlf;
}
/// If enabled, automatically insert a final newline
/// when typing at the end of the file.
pub fn set_insert_final_newline(&mut self, enabled: bool) {
self.insert_final_newline = enabled;
}
/// Whether to insert or overtype text when writing.
pub fn is_overtype(&self) -> bool {
self.overtype