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

Add a mappable version of existing :reflow command #7098

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ impl MappableCommand {
paste_primary_clipboard_before, "Paste primary clipboard before selections",
indent, "Indent selection",
unindent, "Unindent selection",
reflow, "Hard-wrap selection to given width",
format_selections, "Format selection",
join_selections, "Join lines inside selection",
join_selections_space, "Join lines inside selection and select spaces",
Expand Down Expand Up @@ -4044,6 +4045,10 @@ fn unindent(cx: &mut Context) {
doc.apply(&transaction, view.id);
}

fn reflow(cx: &mut Context) {
reflow_impl(cx.editor, cx.count);
}

fn format_selections(cx: &mut Context) {
use helix_lsp::{lsp, util::range_to_lsp_range};

Expand Down
40 changes: 23 additions & 17 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1911,27 +1911,17 @@ fn sort_impl(
Ok(())
}

fn reflow(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let scrolloff = cx.editor.config().scrolloff;
let cfg_text_width: usize = cx.editor.config().text_width;
let (view, doc) = current!(cx.editor);
pub(super) fn reflow_impl(editor: &mut Editor, text_width: Option<NonZeroUsize>) {
let scrolloff = editor.config().scrolloff;
let cfg_text_width = editor.config().text_width;
let (view, doc) = current!(editor);

// Find the text_width by checking the following sources in order:
// - The passed argument in `args`
// - The passed argument
// - The configured text-width for this language in languages.toml
// - The configured text-width in the config.toml
let text_width: usize = args
.get(0)
.map(|num| num.parse::<usize>())
.transpose()?
let text_width: usize = text_width
.map(Into::into)
.or_else(|| doc.language_config().and_then(|config| config.text_width))
.unwrap_or(cfg_text_width);

Expand All @@ -1948,6 +1938,22 @@ fn reflow(
doc.apply(&transaction, view.id);
doc.append_changes_to_history(view);
view.ensure_cursor_in_view(doc, scrolloff);
}

fn reflow(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let text_width = args
.get(0)
.map(|num| num.parse())
.transpose()?;
reflow_impl(cx.editor, text_width);

Ok(())
}
Expand Down