Skip to content

Commit

Permalink
Handle single-line comment prefixes in :reflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rose Hogenson committed Jan 8, 2025
1 parent bc8c898 commit 31facba
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
15 changes: 14 additions & 1 deletion helix-core/src/doc_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::graphemes::{Grapheme, GraphemeStr};
use crate::syntax::Highlight;
use crate::text_annotations::TextAnnotations;
use crate::{movement, Change, LineEnding, Position, Rope, RopeGraphemes, RopeSlice, Tendril};
use helix_stdx::rope::RopeSliceExt;

/// TODO make Highlight a u32 to reduce the size of this enum to a single word.
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -150,6 +151,7 @@ pub struct TextFormat {
pub wrap_indicator_highlight: Option<Highlight>,
pub viewport_width: u16,
pub soft_wrap_at_text_width: bool,
pub continue_comments: Vec<String>,
}

// test implementation is basically only used for testing or when softwrap is always disabled
Expand All @@ -164,6 +166,7 @@ impl Default for TextFormat {
viewport_width: 17,
wrap_indicator_highlight: None,
soft_wrap_at_text_width: false,
continue_comments: Vec::new(),
}
}
}
Expand Down Expand Up @@ -434,8 +437,18 @@ impl<'t> DocumentFormatter<'t> {

fn find_indent<'a>(&self, line: usize, doc: RopeSlice<'a>) -> RopeSlice<'a> {
let line_start = doc.line_to_char(line);
let indent_end = movement::skip_while(doc, line_start, |ch| matches!(ch, ' ' | '\t'))
let mut indent_end = movement::skip_while(doc, line_start, |ch| matches!(ch, ' ' | '\t'))
.unwrap_or(line_start);
let slice = doc.slice(indent_end..);
if let Some(token) = self
.text_fmt
.continue_comments
.iter()
.filter(|token| slice.starts_with(token))
.max_by_key(|x| x.len())
{
indent_end += token.chars().count();
}
let indent_end = movement::skip_while(doc, indent_end, |ch| matches!(ch, ' ' | '\t'))
.unwrap_or(indent_end);
return doc.slice(line_start..indent_end);
Expand Down
1 change: 1 addition & 0 deletions helix-core/src/doc_formatter/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl TextFormat {
// use a prime number to allow lining up too often with repeat
viewport_width: 17,
soft_wrap_at_text_width: false,
continue_comments: Vec::new(),
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2142,6 +2142,11 @@ fn reflow(
wrap_indicator_highlight: None,
viewport_width: u16::try_from(text_width).unwrap_or(u16::MAX),
soft_wrap_at_text_width: true,
continue_comments: Vec::from(
doc.language_config()
.and_then(|config| config.comment_tokens.as_deref())
.unwrap_or(&[]),
),
};
let annotations = TextAnnotations::default();

Expand Down
55 changes: 55 additions & 0 deletions helix-term/tests/test/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,61 @@ bla]#",
))
.await?;

test((
"// #[|This is a really long comment that we want to break onto multiple lines.]#",
":lang rust<ret>:reflow 13<ret>",
"// #[|This is a
// really long
// comment that
// we want to
// break onto
// multiple
// lines.]#",
))
.await?;

test((
"#[\t// Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
\t// tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
\t// veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
\t// commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
\t// velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
\t// occaecat cupidatat non proident, sunt in culpa qui officia deserunt
\t// mollit anim id est laborum.
|]#",
":lang go<ret>:reflow 50<ret>",
"#[\t// Lorem ipsum dolor sit amet,
\t// consectetur adipiscing elit, sed do
\t// eiusmod
\t// tempor incididunt ut labore et dolore
\t// magna aliqua. Ut enim ad minim
\t// veniam, quis nostrud exercitation
\t// ullamco laboris nisi ut aliquip ex ea
\t// commodo consequat. Duis aute irure
\t// dolor in reprehenderit in voluptate
\t// velit esse cillum dolore eu fugiat
\t// nulla pariatur. Excepteur sint
\t// occaecat cupidatat non proident, sunt
\t// in culpa qui officia deserunt
\t// mollit anim id est laborum.
|]#",
))
.await?;

test((
" // #[|This document has multiple lines that each need wrapping
/// currently we wrap each line completely separately in order to preserve existing newlines.]#",
":lang rust<ret>:reflow 40<ret>",
" // #[|This document has multiple lines
// that each need wrapping
/// currently we wrap each line
/// completely separately in order to
/// preserve existing newlines.]#"
))
.await?;

test((
"#[|Very_long_words_should_not_be_broken_by_hard_wrap]#",
":reflow 2<ret>",
Expand Down
1 change: 1 addition & 0 deletions helix-view/src/annotations/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl InlineDiagnosticsConfig {
wrap_indicator_highlight: None,
viewport_width: width,
soft_wrap_at_text_width: true,
continue_comments: Vec::new(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,7 @@ impl Document {
.and_then(|theme| theme.find_scope_index("ui.virtual.wrap"))
.map(Highlight),
soft_wrap_at_text_width,
continue_comments: Vec::new(),
}
}

Expand Down

0 comments on commit 31facba

Please sign in to comment.