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

feat: Commands for aligning text. :left, :right and :center #12944

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@
| `:read`, `:r` | Load a file into buffer |
| `:echo` | Prints the given arguments to the statusline. |
| `:noop` | Does nothing. |
| `:left` | Align text to the left, optionally pass a number overriding the current document's text width |
| `:center` | Center-align text, optionally pass a number overriding the current document's text width |
| `:right` | Align text to the right, optionally pass a number overriding the current document's text width |
99 changes: 99 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,72 @@ fn buffer_gather_paths_impl(editor: &mut Editor, args: Args) -> Vec<DocumentId>
document_ids
}

fn align_text_impl(
cx: &mut compositor::Context,
args: Args,
event: PromptEvent,
format: fn(usize, &str) -> String,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}
let editor_text_width = cx.editor.config().text_width;
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);

let custom_text_width = args.first().map(|arg| {
arg.parse::<usize>()
.map_err(|_| anyhow!("Could not parse argument as a number: {arg}"))
});

let text_width = custom_text_width.unwrap_or(Ok(doc
.language_config()
.and_then(|c| c.text_width)
.unwrap_or(editor_text_width)))?;

let lines = get_lines(doc, view.id);
let mut changes = Vec::with_capacity(lines.len());

for line_idx in lines {
let line = doc.text().line(line_idx);
let old_line = line.to_string();

let aligned_line = format(text_width, old_line.trim());
let aligned_line = aligned_line.trim_end();
let tendril = Tendril::from(aligned_line);

changes.push((
text.line_to_char(line_idx),
text.line_to_char(line_idx + 1) - doc.line_ending.len_chars(),
Some(tendril),
))
}

let transaction = Transaction::change(doc.text(), changes.into_iter());

doc.apply(&transaction, view.id);

Ok(())
}

fn left(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> {
align_text_impl(cx, args, event, |text_width, text| {
format!("{:<text_width$}", text)
})
}

fn center(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> {
align_text_impl(cx, args, event, |text_width, text| {
format!("{:^text_width$}", text)
})
}

fn right(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> {
align_text_impl(cx, args, event, |text_width, text| {
format!("{:>text_width$}", text)
})
}

fn buffer_close(
cx: &mut compositor::Context,
args: Args,
Expand Down Expand Up @@ -3493,6 +3559,39 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
..Signature::DEFAULT
},
},
TypableCommand {
name: "left",
aliases: &[],
doc: "Align text to the left, optionally pass a number overriding the current document's text width",
fun: left,
completer: CommandCompleter::none(),
signature: Signature {
positionals: (0, Some(0)),
..Signature::DEFAULT
},
},
TypableCommand {
name: "center",
aliases: &[],
doc: "Center-align text, optionally pass a number overriding the current document's text width",
fun: center,
completer: CommandCompleter::none(),
signature: Signature {
positionals: (0, Some(1)),
..Signature::DEFAULT
},
},
TypableCommand {
name: "right",
aliases: &[],
doc: "Align text to the right, optionally pass a number overriding the current document's text width",
fun: right,
completer: CommandCompleter::none(),
signature: Signature {
positionals: (0, Some(1)),
..Signature::DEFAULT
},
},
];

pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
Expand Down