@@ -9,8 +9,8 @@ use std::str::FromStr;
99use std::sync::Arc;
1010
1111use helix_core::{
12− chars::{char_is_line_ending, char_is_whitespace},
1312 history::History,
13+ indent::{auto_detect_indent_style, IndentStyle},
1414 line_ending::auto_detect_line_ending,
1515 syntax::{self, LanguageConfiguration},
1616 ChangeSet, Diagnostic, LineEnding, Rope, RopeBuilder, Selection, State, Syntax, Transaction,
@@ -63,12 +63,6 @@ impl<'de> Deserialize<'de> for Mode {
6363 }
6464}
6565
66−#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
67−pub enum IndentStyle {
68− Tabs,
69− Spaces(u8),
70−}
71−
7266pub struct Document {
7367 pub(crate) id: DocumentId,
7468 text: Rope,
@@ -460,9 +454,7 @@ impl Document {
460454 doc.detect_language(theme, loader);
461455 }
462456
463− // Detect indentation style and line ending.
464− doc.detect_indent_style();
465− doc.line_ending = auto_detect_line_ending(&doc.text).unwrap_or(DEFAULT_LINE_ENDING);
457+ doc.detect_indent_and_line_ending();
466458
467459 Ok(doc)
468460 }
@@ -580,6 +572,18 @@ impl Document {
580572 }
581573 }
582574
575+ pub fn detect_indent_and_line_ending(&mut self) {
576+ self.indent_style = auto_detect_indent_style(&self.text).unwrap_or_else(|| {
577+ IndentStyle::from_str(
578+ self.language
579+ .as_ref()
580+ .and_then(|config| config.indent.as_ref())
581+ .map_or(" ", |config| config.unit.as_str()), // Fallback to 2 spaces.
582+ )
583+ });
584+ self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING);
585+ }
586+
583587 /// Reload the document from its path.
584588 pub fn reload(&mut self, view_id: ViewId) -> Result<(), Error> {
585589 let encoding = &self.encoding;
@@ -598,9 +602,7 @@ impl Document {
598602 self.append_changes_to_history(view_id);
599603 self.reset_modified();
600604
601− // Detect indentation style and line ending.
602− self.detect_indent_style();
603− self.line_ending = auto_detect_line_ending(&self.text).unwrap_or(DEFAULT_LINE_ENDING);
605+ self.detect_indent_and_line_ending();
604606
605607 Ok(())
606608 }
@@ -619,132 +621,6 @@ impl Document {
619621 self.encoding
620622 }
621623
622− fn detect_indent_style(&mut self) {
623− // Build a histogram of the indentation *increases* between
624− // subsequent lines, ignoring lines that are all whitespace.
625− //
626− // Index 0 is for tabs, the rest are 1-8 spaces.
627− let histogram: [usize; 9] = {
628− let mut histogram = [0; 9];
629− let mut prev_line_is_tabs = false;
630− let mut prev_line_leading_count = 0usize;
631−
632− // Loop through the lines, checking for and recording indentation
633− // increases as we go.
634− 'outer: for line in self.text.lines().take(1000) {
635− let mut c_iter = line.chars();
636−
637− // Is first character a tab or space?
638− let is_tabs = match c_iter.next() {
639− Some('\t') => true,
640− Some(' ') => false,
641−
642− // Ignore blank lines.
643− Some(c) if char_is_line_ending(c) => continue,
644−
645− _ => {
646− prev_line_is_tabs = false;
647− prev_line_leading_count = 0;
648− continue;
649− }
650− };
651−
652− // Count the line's total leading tab/space characters.
653− let mut leading_count = 1;
654− let mut count_is_done = false;
655− for c in c_iter {
656− match c {
657− '\t' if is_tabs && !count_is_done => leading_count += 1,
658− ' ' if !is_tabs && !count_is_done => leading_count += 1,
659−
660− // We stop counting if we hit whitespace that doesn't
661− // qualify as indent or doesn't match the leading
662− // whitespace, but we don't exit the loop yet because
663− // we still want to determine if the line is blank.
664− c if char_is_whitespace(c) => count_is_done = true,
665−
666− // Ignore blank lines.
667− c if char_is_line_ending(c) => continue 'outer,
668−
669− _ => break,
670− }
671−
672− // Bound the worst-case execution time for weird text files.
673− if leading_count > 256 {
674− continue 'outer;
675− }
676− }
677−
678− // If there was an increase in indentation over the previous
679− // line, update the histogram with that increase.
680− if (prev_line_is_tabs == is_tabs || prev_line_leading_count == 0)
681− && prev_line_leading_count < leading_count
682− {
683− if is_tabs {
684− histogram[0] += 1;
685− } else {
686− let amount = leading_count - prev_line_leading_count;
687− if amount <= 8 {
688− histogram[amount] += 1;
689− }
690− }
691− }
692−
693− // Store this line's leading whitespace info for use with
694− // the next line.
695− prev_line_is_tabs = is_tabs;
696− prev_line_leading_count = leading_count;
697− }
698−
699− // Give more weight to tabs, because their presence is a very
700− // strong indicator.
701− histogram[0] *= 2;
702−
703− histogram
704− };
705−
706− // Find the most frequent indent, its frequency, and the frequency of
707− // the next-most frequent indent.
708− let indent = histogram
709− .iter()
710− .enumerate()
711− .max_by_key(|kv| kv.1)
712− .unwrap()
713− .0;
714− let indent_freq = histogram[indent];
715− let indent_freq_2 = *histogram
716− .iter()
717− .enumerate()
718− .filter(|kv| kv.0 != indent)
719− .map(|kv| kv.1)
720− .max()
721− .unwrap();
722−
723− // Use the auto-detected result if we're confident enough in its
724− // accuracy, based on some heuristics. Otherwise fall back to
725− // the language-based setting.
726− if indent_freq >= 1 && (indent_freq_2 as f64 / indent_freq as f64) < 0.66 {
727− // Use the auto-detected setting.
728− self.indent_style = match indent {
729− 0 => IndentStyle::Tabs,
730− _ => IndentStyle::Spaces(indent as u8),
731− };
732− } else {
733− // Fall back to language-based setting.
734− let indent = self
735− .language
736− .as_ref()
737− .and_then(|config| config.indent.as_ref())
738− .map_or(" ", |config| config.unit.as_str()); // fallback to 2 spaces
739−
740− self.indent_style = if indent.starts_with(' ') {
741− IndentStyle::Spaces(indent.len() as u8)
742− } else {
743− IndentStyle::Tabs
744− };
745− }
746− }
747−
748624 pub fn set_path(&mut self, path: &Path) -> Result<(), std::io::Error> {
749625 let path = canonicalize_path(path)?;
750626
@@ -1002,21 +878,7 @@ impl Document {
1002878 /// TODO: we might not need this function anymore, since the information
1003879 /// is conveniently available in `Document::indent_style` now.
1004880 pub fn indent_unit(&self) -> &'static str {
1005− match self.indent_style {
1006− IndentStyle::Tabs => "\t",
1007− IndentStyle::Spaces(1) => " ",
1008− IndentStyle::Spaces(2) => " ",
1009− IndentStyle::Spaces(3) => " ",
1010− IndentStyle::Spaces(4) => " ",
1011− IndentStyle::Spaces(5) => " ",
1012− IndentStyle::Spaces(6) => " ",
1013− IndentStyle::Spaces(7) => " ",
1014− IndentStyle::Spaces(8) => " ",
1015−
1016− // Unsupported indentation style. This should never happen,
1017− // but just in case fall back to two spaces.
1018− _ => " ",
1019− }
881+ self.indent_style.as_str()
1020882 }
1021883
1022884 #[inline]