Repository: lapce/lapce
File: lapce-core/src/indent.rs (Rust)

Code before:
pub const DEFAULT_INDENT: IndentStyle = IndentStyle::Spaces(4);

impl IndentStyle {
    pub const LONGEST_INDENT: &'static str = "        "; // 8 spaces

    /// Creates an `IndentStyle` from an indentation string.
    ///
    /// For example, passing `"    "` (four spaces) will create `IndentStyle::Spaces(4)`.
    #[allow(clippy::should_implement_trait)]
    #[inline]
    pub fn from_str(indent: &str) -> Self {
        if indent.starts_with(' ') {
            IndentStyle::Spaces((indent.len() % Self::LONGEST_INDENT.len()) as u8)
        } else {
            IndentStyle::Tabs
        }
    }

    #[inline]
    pub fn as_str(&self) -> &'static str {
        match *self {
            IndentStyle::Tabs => "\t",
            IndentStyle::Spaces(x) if x <= Self::LONGEST_INDENT.len() as u8 => Self::LONGEST_INDENT.split_at(x.into()).0,
            // Unsupported indentation style.  This should never happen,
            // but just in case fall back to the default of 4 spaces
            _ => "    "
        }
    }
}

/// Attempts to detect the indentation style used in a document.
///
/// Returns the indentation style if the auto-detect confidence is
/// reasonably high, otherwise returns `None`.
pub fn auto_detect_indent_style(document_text: &Rope) -> Option<IndentStyle> {
    // Build a histogram of the indentation *increases* between
    // subsequent lines, ignoring lines that are all whitespace.
    //
    // Index 0 is for tabs, the rest are 1-8 spaces.
    let histogram: [usize; 9] = {
        let mut histogram = [0; 9];
        let mut prev_line_is_tabs = false;
        let mut prev_line_leading_count = 0usize;

        // Loop through the lines, checking for and recording indentation
        // increases as we go.
        let offset = document_text.offset_of_line(
            document_text.line_of_offset(document_text.len()).min(1000),
        );
        'outer: for line in document_text.lines(..offset) {
            let mut c_iter = line.chars();

Review comment (bug):
Any reason why this was changed? Panicking in debug builds is a good way to catch unintended behaviour. Also, since this branch is supposed to be unreachable, why change from 2 spaces to 4?

Code after:
impl IndentStyle {
    pub const LONGEST_INDENT: &'static str = "        "; // 8 spaces
    pub const DEFAULT_INDENT: IndentStyle = IndentStyle::Spaces(4);


    /// Creates an `IndentStyle` from an indentation string.
    ///
    /// For example, passing `"    "` (four spaces) will create `IndentStyle::Spaces(4)`.
    #[allow(clippy::should_implement_trait)]
    #[inline]
    pub fn from_str(indent: &str) -> Self {
        debug_assert!(!indent.is_empty() && indent.len() <= Self::LONGEST_INDENT.len());
        if indent.starts_with(' ') {
            IndentStyle::Spaces(indent.len() as u8)
        } else {
            IndentStyle::Tabs
        }
    }

    #[inline]
    pub fn as_str(&self) -> &'static str {
        match *self {
            IndentStyle::Tabs => "\t",
            IndentStyle::Spaces(x) if x <= Self::LONGEST_INDENT.len() as u8 => Self::LONGEST_INDENT.split_at(x.into()).0,
            // Unsupported indentation style.  This should never happen,
            // but just in case fall back to the default of 4 spaces
            IndentStyle::Spaces(n) => {
                debug_assert!(n > 0 && n <= Self::LONGEST_INDENT.len() as u8);
                "    "
            }
        }
    }
}

/// Attempts to detect the indentation style used in a document.
///
/// Returns the indentation style if the auto-detect confidence is
/// reasonably high, otherwise returns `None`.
pub fn auto_detect_indent_style(document_text: &Rope) -> Option<IndentStyle> {
    // Build a histogram of the indentation *increases* between
    // subsequent lines, ignoring lines that are all whitespace.
    //
    // Index 0 is for tabs, the rest are 1-8 spaces.
    let histogram: [usize; 9] = {
        let mut histogram = [0; 9];
        let mut prev_line_is_tabs = false;
        let mut prev_line_leading_count = 0usize;

        // Loop through the lines, checking for and recording indentation
        // increases as we go.
        let offset = document_text.offset_of_line(