on master help sign in
Add config for default line ending (#5621)
commit3fb9fafb2a366d004a04aa3d45d52e3ecfb57241 authorAlex <3957610+CptPotato@users.noreply.github.com> dateFri 16 Jun 2023, 19:02 (3 years ago) parentdf09490
7 files changed+69−16
book/src/configuration.md+1 helix-core/src/lib.rs+1−1 helix-core/src/line_ending.rs+2−2 helix-term/tests/test/auto_pairs.rs+1−1 helix-term/tests/test/helpers.rs+1−1 helix-view/src/document.rs+13−9 helix-view/src/editor.rs+50−2
book/src/configuration.md+1
@@ -63,6 +63,7 @@ Its settings will be merged with the configuration directory `config.toml` and t
6363| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
6464| `text-width` | Maximum line length. Used for the `:reflow` command and soft-wrapping if `soft-wrap.wrap-at-text-width` is set | `80` |
6565| `workspace-lsp-roots` | Directories relative to the workspace root that are treated as LSP roots. Should only be set in `.helix/config.toml` | `[]` |
66+| `default-line-ending` | The line ending to use for new documents. Can be `native`, `lf`, `crlf`, `ff`, `cr` or `nel`. `native` uses the platform's native line ending (`crlf` on Windows, otherwise `lf`). | `native` |
6667
6768### `[editor.statusline]` Section
6869
helix-core/src/lib.rs+1−1
@@ -66,5 +66,5 @@ pub use syntax::Syntax;
6666
6767pub use diagnostic::Diagnostic;
6868
69−pub use line_ending::{LineEnding, DEFAULT_LINE_ENDING};
69+pub use line_ending::{LineEnding, NATIVE_LINE_ENDING};
7070pub use transaction::{Assoc, Change, ChangeSet, Deletion, Operation, Transaction};
helix-core/src/line_ending.rs+2−2
@@ -1,9 +1,9 @@
11use crate::{Rope, RopeSlice};
22
33#[cfg(target_os = "windows")]
4−pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::Crlf;
4+pub const NATIVE_LINE_ENDING: LineEnding = LineEnding::Crlf;
55#[cfg(not(target_os = "windows"))]
6−pub const DEFAULT_LINE_ENDING: LineEnding = LineEnding::LF;
6+pub const NATIVE_LINE_ENDING: LineEnding = LineEnding::LF;
77
88/// Represents one of the valid Unicode line endings.
99#[derive(PartialEq, Eq, Copy, Clone, Debug)]
helix-term/tests/test/auto_pairs.rs+1−1
@@ -2,7 +2,7 @@ use helix_core::{auto_pairs::DEFAULT_PAIRS, hashmap};
22
33use super::*;
44
5−const LINE_END: &str = helix_core::DEFAULT_LINE_ENDING.as_str();
5+const LINE_END: &str = helix_core::NATIVE_LINE_ENDING.as_str();
66
77fn differing_pairs() -> impl Iterator<Item = &'static (char, char)> {
88 DEFAULT_PAIRS.iter().filter(|(open, close)| open != close)
helix-term/tests/test/helpers.rs+1−1
@@ -244,7 +244,7 @@ pub fn test_editor_config() -> helix_view::editor::Config {
244244/// character, and if one doesn't exist already, appends the system's
245245/// appropriate line ending to the end of a string.
246246pub fn platform_line(input: &str) -> String {
247− let line_end = helix_core::DEFAULT_LINE_ENDING.as_str();
247+ let line_end = helix_core::NATIVE_LINE_ENDING.as_str();
248248
249249 // we can assume that the source files in this code base will always
250250 // be LF, so indoc strings will always insert LF
helix-view/src/document.rs+13−9
@@ -8,7 +8,6 @@ use helix_core::doc_formatter::TextFormat;
88use helix_core::encoding::Encoding;
99use helix_core::syntax::{Highlight, LanguageServerFeature};
1010use helix_core::text_annotations::{InlineAnnotation, TextAnnotations};
11−use helix_core::Range;
1211use helix_vcs::{DiffHandle, DiffProviderRegistry};
1312
1413use ::parking_lot::Mutex;
@@ -31,8 +30,8 @@ use helix_core::{
3130 indent::{auto_detect_indent_style, IndentStyle},
3231 line_ending::auto_detect_line_ending,
3332 syntax::{self, LanguageConfiguration},
34− ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, Syntax, Transaction,
35− DEFAULT_LINE_ENDING,
33+ ChangeSet, Diagnostic, LineEnding, Range, Rope, RopeBuilder, Selection, Syntax, Transaction,
34+ NATIVE_LINE_ENDING,
3635};
3736
3837use crate::editor::{Config, RedrawHandle};
@@ -590,6 +589,7 @@ impl Document {
590589 config: Arc<dyn DynAccess<Config>>,
591590 ) -> Self {
592591 let (encoding, has_bom) = encoding_with_bom_info.unwrap_or((encoding::UTF_8, false));
592+ let line_ending = config.load().default_line_ending.into();
593593 let changes = ChangeSet::new(&text);
594594 let old_state = None;
595595
@@ -603,7 +603,7 @@ impl Document {
603603 inlay_hints: HashMap::default(),
604604 inlay_hints_oudated: false,
605605 indent_style: DEFAULT_INDENT,
606− line_ending: DEFAULT_LINE_ENDING,
606+ line_ending,
607607 restore_cursor: false,
608608 syntax: None,
609609 language: None,
@@ -623,10 +623,12 @@ impl Document {
623623 focused_at: std::time::Instant::now(),
624624 }
625625 }
626+
626627 pub fn default(config: Arc<dyn DynAccess<Config>>) -> Self {
627− let text = Rope::from(DEFAULT_LINE_ENDING.as_str());
628+ let text = Rope::from(NATIVE_LINE_ENDING.as_str());
628629 Self::from(text, None, config)
629630 }
631+
630632 // TODO: async fn?
631633 /// Create a new document from `path`. Encoding is auto-detected, but it can be manually
632634 /// overwritten with the `encoding` parameter.
@@ -643,7 +645,7 @@ impl Document {
643645 from_reader(&mut file, encoding)?
644646 } else {
645647 let encoding = encoding.unwrap_or(encoding::UTF_8);
646− (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding, false)
648+ (Rope::from(NATIVE_LINE_ENDING.as_str()), encoding, false)
647649 };
648650
649651 let mut doc = Self::from(rope, Some((encoding, has_bom)), config);
@@ -887,14 +889,16 @@ impl Document {
887889
888890 /// Detect the indentation used in the file, or otherwise defaults to the language indentation
889891 /// configured in `languages.toml`, with a fallback to tabs if it isn't specified. Line ending
890− /// is likewise auto-detected, and will fallback to the default OS line ending.
892+ /// is likewise auto-detected, and will remain unchanged if no line endings were detected.
891893 pub fn detect_indent_and_line_ending(&mut self) {
892894 self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| {
893895 self.language_config()
894896 .and_then(|config| config.indent.as_ref())
895897 .map_or(DEFAULT_INDENT, |config| IndentStyle::from_str(&config.unit))
896898 });
897− self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING);
899+ if let Some(line_ending) = auto_detect_line_ending(&self.text) {
900+ self.line_ending = line_ending;
901+ }
898902 }
899903
900904 /// Reload the document from its path.
@@ -1921,7 +1925,7 @@ mod test {
19211925 Document::default(Arc::new(ArcSwap::new(Arc::new(Config::default()))))
19221926 .text()
19231927 .to_string(),
1924− DEFAULT_LINE_ENDING.as_str()
1928+ NATIVE_LINE_ENDING.as_str()
19251929 );
19261930 }
19271931
helix-view/src/editor.rs+50−2
@@ -44,7 +44,7 @@ pub use helix_core::register::Registers;
4444use helix_core::{
4545 auto_pairs::AutoPairs,
4646 syntax::{self, AutoPairConfig, SoftWrap},
47− Change,
47+ Change, LineEnding, NATIVE_LINE_ENDING,
4848};
4949use helix_core::{Position, Selection};
5050use helix_dap as dap;
@@ -273,7 +273,7 @@ pub struct Config {
273273 pub search: SearchConfig,
274274 pub lsp: LspConfig,
275275 pub terminal: Option<TerminalConfig>,
276− /// Column numbers at which to draw the rulers. Default to `[]`, meaning no rulers.
276+ /// Column numbers at which to draw the rulers. Defaults to `[]`, meaning no rulers.
277277 pub rulers: Vec<u16>,
278278 #[serde(default)]
279279 pub whitespace: WhitespaceConfig,
@@ -286,6 +286,8 @@ pub struct Config {
286286 pub soft_wrap: SoftWrap,
287287 /// Workspace specific lsp ceiling dirs
288288 pub workspace_lsp_roots: Vec<PathBuf>,
289+ /// Which line ending to choose for new documents. Defaults to `native`. i.e. `crlf` on Windows, otherwise `lf`.
290+ pub default_line_ending: LineEndingConfig,
289291}
290292
291293#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
@@ -727,6 +729,51 @@ impl Default for IndentGuidesConfig {
727729 }
728730}
729731
732+/// Line ending configuration.
733+#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
734+#[serde(rename_all = "lowercase")]
735+pub enum LineEndingConfig {
736+ /// The platform's native line ending.
737+ ///
738+ /// `crlf` on Windows, otherwise `lf`.
739+ Native,
740+ /// Line feed.
741+ LF,
742+ /// Carriage return followed by line feed.
743+ Crlf,
744+ /// Form feed.
745+ #[cfg(feature = "unicode-lines")]
746+ FF,
747+ /// Carriage return.
748+ #[cfg(feature = "unicode-lines")]
749+ CR,
750+ /// Next line.
751+ #[cfg(feature = "unicode-lines")]
752+ Nel,
753+}
754+
755+impl Default for LineEndingConfig {
756+ fn default() -> Self {
757+ LineEndingConfig::Native
758+ }
759+}
760+
761+impl From<LineEndingConfig> for LineEnding {
762+ fn from(line_ending: LineEndingConfig) -> Self {
763+ match line_ending {
764+ LineEndingConfig::Native => NATIVE_LINE_ENDING,
765+ LineEndingConfig::LF => LineEnding::LF,
766+ LineEndingConfig::Crlf => LineEnding::Crlf,
767+ #[cfg(feature = "unicode-lines")]
768+ LineEndingConfig::FF => LineEnding::FF,
769+ #[cfg(feature = "unicode-lines")]
770+ LineEndingConfig::CR => LineEnding::CR,
771+ #[cfg(feature = "unicode-lines")]
772+ LineEndingConfig::Nel => LineEnding::Nel,
773+ }
774+ }
775+}
776+
730777impl Default for Config {
731778 fn default() -> Self {
732779 Self {
@@ -771,6 +818,7 @@ impl Default for Config {
771818 text_width: 80,
772819 completion_replace: false,
773820 workspace_lsp_roots: Vec::new(),
821+ default_line_ending: LineEndingConfig::default(),
774822 }
775823 }
776824}