Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any indent size from 1 to 16 #7429

Merged
merged 3 commits into from
Jun 23, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions helix-core/src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub enum IndentStyle {
Spaces(u8),
}

// 16 spaces
const INDENTS: &str = " ";

impl IndentStyle {
/// Creates an `IndentStyle` from an indentation string.
///
Expand All @@ -27,7 +30,7 @@ impl IndentStyle {
#[inline]
pub fn from_str(indent: &str) -> Self {
// XXX: do we care about validating the input more than this? Probably not...?
debug_assert!(!indent.is_empty() && indent.len() <= 8);
debug_assert!(!indent.is_empty() && indent.len() <= 256);

if indent.starts_with(' ') {
IndentStyle::Spaces(indent.len() as u8)
Expand All @@ -40,20 +43,13 @@ impl IndentStyle {
pub fn as_str(&self) -> &'static str {
match *self {
IndentStyle::Tabs => "\t",
IndentStyle::Spaces(1) => " ",
IndentStyle::Spaces(2) => " ",
IndentStyle::Spaces(3) => " ",
IndentStyle::Spaces(4) => " ",
IndentStyle::Spaces(5) => " ",
IndentStyle::Spaces(6) => " ",
IndentStyle::Spaces(7) => " ",
IndentStyle::Spaces(8) => " ",

IndentStyle::Spaces(n) if n <= 16 && n >= 1 => &INDENTS[0..n as usize],
msdrigg marked this conversation as resolved.
Show resolved Hide resolved
// Unsupported indentation style. This should never happen,
// but just in case fall back to two spaces.
// but just in case fall back to 16 spaces (the largest we support).
IndentStyle::Spaces(n) => {
debug_assert!(n > 0 && n <= 8); // Always triggers. `debug_panic!()` wanted.
" "
debug_assert!(n > 0 && n <= 16); // Always triggers. `debug_panic!()` wanted.
let closest_n = n.clamp(1, 16);
&INDENTS[0..closest_n as usize]
}
}
}
Expand All @@ -75,9 +71,9 @@ 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];
// Index 0 is for tabs, the rest are 1-16 spaces.
let histogram: [usize; 17] = {
let mut histogram = [0; 17];
let mut prev_line_is_tabs = false;
let mut prev_line_leading_count = 0usize;

Expand Down Expand Up @@ -136,7 +132,7 @@ pub fn auto_detect_indent_style(document_text: &Rope) -> Option<IndentStyle> {
histogram[0] += 1;
} else {
let amount = leading_count - prev_line_leading_count;
if amount <= 8 {
if amount <= 16 {
histogram[amount] += 1;
}
}
Expand Down Expand Up @@ -797,4 +793,20 @@ mod test {
3
);
}

#[test]
fn test_large_indent_level() {
let tab_width = 16;
let indent_width = 16;
let line = Rope::from(" fn new"); // 16 spaces
assert_eq!(
indent_level_for_line(line.slice(..), tab_width, indent_width),
1
);
let line = Rope::from(" fn new"); // 32 spaces
assert_eq!(
indent_level_for_line(line.slice(..), tab_width, indent_width),
2
);
}
}
Loading