mirror of
https://github.com/helix-editor/helix.git
synced 2025-04-03 10:57:48 +03:00
Merge b9f60bdfaa
into 7ebf650029
This commit is contained in:
commit
3d4643c405
2 changed files with 102 additions and 0 deletions
|
@ -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 |
|
||||
| `: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 |
|
||||
|
|
|
@ -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,
|
||||
|
@ -3552,6 +3618,39 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
|
|||
..Signature::DEFAULT
|
||||
},
|
||||
},
|
||||
TypableCommand {
|
||||
name: "left",
|
||||
aliases: &[],
|
||||
doc: "Align text to the left",
|
||||
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>> =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue