on master help sign in
fix: Indentation used different default on `hx` vs `hx new_file.txt`
c913badBlaž Hrastnik
helix-view/src/document.rs+8−8
@@ -23,6 +23,8 @@ use crate::{DocumentId, Theme, ViewId};
23/// 8kB of buffer space for encoding and decoding `Rope`s.
23/// 8kB of buffer space for encoding and decoding `Rope`s.
24const BUF_SIZE: usize = 8192;
24const BUF_SIZE: usize = 8192;
25
25
26+const DEFAULT_INDENT: IndentStyle = IndentStyle::Spaces(4);
27+
26#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
28#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
27pub enum Mode {
29pub enum Mode {
28 Normal,
30 Normal,
@@ -325,7 +327,7 @@ impl Document {
325 encoding,
327 encoding,
326 text,
328 text,
327 selections: HashMap::default(),
329 selections: HashMap::default(),
328− indent_style: IndentStyle::Spaces(4),
330+ indent_style: DEFAULT_INDENT,
329 mode: Mode::Normal,
331 mode: Mode::Normal,
330 restore_cursor: false,
332 restore_cursor: false,
331 syntax: None,
333 syntax: None,
@@ -495,17 +497,15 @@ impl Document {
495 }
497 }
496
498
497 /// Detect the indentation used in the file, or otherwise defaults to the language indentation
499 /// Detect the indentation used in the file, or otherwise defaults to the language indentation
498− /// configured in `languages.toml`, with a fallback back to 2 space indentation if it isn't
500+ /// configured in `languages.toml`, with a fallback to 4 space indentation if it isn't
499 /// specified. Line ending is likewise auto-detected, and will fallback to the default OS
501 /// specified. Line ending is likewise auto-detected, and will fallback to the default OS
500 /// line ending.
502 /// line ending.
501 pub fn detect_indent_and_line_ending(&mut self) {
503 pub fn detect_indent_and_line_ending(&mut self) {
502 self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| {
504 self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| {
503− IndentStyle::from_str(
504− self.language
505+ self.language
505− .as_ref()
506+ .as_ref()
506− .and_then(|config| config.indent.as_ref())
507+ .and_then(|config| config.indent.as_ref())
507− .map_or(" ", |config| config.unit.as_str()), // Fallback to 2 spaces.
508+ .map_or(DEFAULT_INDENT, |config| IndentStyle::from_str(&config.unit))
508− )
509 });
509 });
510 self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING);
510 self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING);
511 }
511 }