fix: Indentation used different default on `hx` vs `hx new_file.txt`

pull/902/head
Blaž Hrastnik 3 years ago
parent 4b4e972af0
commit c913bade0a

@ -23,6 +23,8 @@ use crate::{DocumentId, Theme, ViewId};
/// 8kB of buffer space for encoding and decoding `Rope`s. /// 8kB of buffer space for encoding and decoding `Rope`s.
const BUF_SIZE: usize = 8192; const BUF_SIZE: usize = 8192;
const DEFAULT_INDENT: IndentStyle = IndentStyle::Spaces(4);
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Mode { pub enum Mode {
Normal, Normal,
@ -325,7 +327,7 @@ impl Document {
encoding, encoding,
text, text,
selections: HashMap::default(), selections: HashMap::default(),
indent_style: IndentStyle::Spaces(4), indent_style: DEFAULT_INDENT,
mode: Mode::Normal, mode: Mode::Normal,
restore_cursor: false, restore_cursor: false,
syntax: None, syntax: None,
@ -495,17 +497,15 @@ impl Document {
} }
/// Detect the indentation used in the file, or otherwise defaults to the language indentation /// Detect the indentation used in the file, or otherwise defaults to the language indentation
/// configured in `languages.toml`, with a fallback back to 2 space indentation if it isn't /// configured in `languages.toml`, with a fallback to 4 space indentation if it isn't
/// specified. Line ending is likewise auto-detected, and will fallback to the default OS /// specified. Line ending is likewise auto-detected, and will fallback to the default OS
/// line ending. /// line ending.
pub fn detect_indent_and_line_ending(&mut self) { pub fn detect_indent_and_line_ending(&mut self) {
self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| { self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| {
IndentStyle::from_str( self.language
self.language .as_ref()
.as_ref() .and_then(|config| config.indent.as_ref())
.and_then(|config| config.indent.as_ref()) .map_or(DEFAULT_INDENT, |config| IndentStyle::from_str(&config.unit))
.map_or(" ", |config| config.unit.as_str()), // Fallback to 2 spaces.
)
}); });
self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING); self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING);
} }

Loading…
Cancel
Save