Skip to content

Commit

Permalink
feat(config): add option to enforce EOF character
Browse files Browse the repository at this point in the history
Add an option (`eof`) to the config which should determine if files
should end with EOF newline character or not. Update documentation of
the book to detail the new flag as well.

Closes: helix-editor#4274
Signed-off-by: Filip Dutescu <filip.dutescu@gmail.com>
  • Loading branch information
filipdutescu committed Jan 14, 2023
1 parent bdeefbf commit 3b375f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ on unix operating systems.
| `rulers` | List of column positions at which to display the rulers. Can be overridden by language specific `rulers` in `languages.toml` file. | `[]` |
| `bufferline` | Renders a line at the top of the editor displaying open buffers. Can be `always`, `never` or `multiple` (only shown if more than one buffer is in use) | `never` |
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
| `eol` | Whether to enforce files to end with an end-of-line character (ie newline) upon writing, if not present. | `false` |

### `[editor.statusline]` Section

Expand Down
23 changes: 18 additions & 5 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ use anyhow::{anyhow, bail, Error};

pub use helix_core::diagnostic::Severity;
pub use helix_core::register::Registers;
use helix_core::Position;
use helix_core::{
auto_pairs::AutoPairs,
syntax::{self, AutoPairConfig},
Change,
Change, Selection, Tendril,
};
use helix_core::{Position, Transaction};
use helix_dap as dap;
use helix_lsp::lsp;

Expand Down Expand Up @@ -178,6 +178,8 @@ pub struct Config {
pub indent_guides: IndentGuidesConfig,
/// Whether to color modes with different colors. Defaults to `false`.
pub color_modes: bool,
/// Whether to always end files with a newline. Defaults to `false`.
pub eol: bool,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -630,6 +632,7 @@ impl Default for Config {
bufferline: BufferLine::default(),
indent_guides: IndentGuidesConfig::default(),
color_modes: false,
eol: false,
}
}
}
Expand Down Expand Up @@ -1239,7 +1242,19 @@ impl Editor {
// via stream.then() ? then push into main future

let path = path.map(|path| path.into());
let eol_enabled = self.config().eol;
let doc = doc_mut!(self, &doc_id);
if eol_enabled {
let current_text = doc.text();
let add_extra_eol = Transaction::insert(
current_text,
&Selection::point(current_text.len_chars()),
Tendril::from(doc.line_ending.as_str()),
);
let view = view_mut!(self);
doc.apply(&add_extra_eol, view.id);
doc.append_changes_to_history(view);
}
let future = doc.save(path, force)?;

use futures_util::stream;
Expand Down Expand Up @@ -1463,9 +1478,7 @@ impl Editor {
}

fn try_restore_indent(doc: &mut Document, view: &mut View) {
use helix_core::{
chars::char_is_whitespace, line_ending::line_end_char_index, Operation, Transaction,
};
use helix_core::{chars::char_is_whitespace, line_ending::line_end_char_index, Operation};

fn inserted_a_new_blank_line(changes: &[Operation], pos: usize, line_end_pos: usize) -> bool {
if let [Operation::Retain(move_pos), Operation::Insert(ref inserted_str), Operation::Retain(_)] =
Expand Down

0 comments on commit 3b375f9

Please sign in to comment.