Implement command to change the indent-style setting of a document.

This commit is contained in:
Nathan Vegdahl 2021-06-13 18:09:22 -07:00
parent 8648e483f7
commit 358ea6a37c
2 changed files with 31 additions and 2 deletions

View File

@ -9,7 +9,7 @@
}; };
use helix_view::{ use helix_view::{
document::Mode, document::{IndentStyle, Mode},
view::{View, PADDING}, view::{View, PADDING},
Document, DocumentId, Editor, ViewId, Document, DocumentId, Editor, ViewId,
}; };
@ -979,6 +979,28 @@ fn format(editor: &mut Editor, args: &[&str], event: PromptEvent) {
doc.format(view.id) doc.format(view.id)
} }
fn set_indent_style(editor: &mut Editor, args: &[&str], event: PromptEvent) {
use IndentStyle::*;
let style = match args.get(0) {
Some(arg) if "tabs".starts_with(&arg.to_lowercase()) => Some(Tabs),
Some(arg) if arg.len() == 1 => {
let ch = arg.chars().next().unwrap();
if ('1'..='8').contains(&ch) {
Some(Spaces(ch.to_digit(10).unwrap() as u8))
} else {
None
}
}
_ => None,
};
if let Some(s) = style {
let (_, doc) = editor.current();
doc.indent_style = s;
}
}
fn earlier(editor: &mut Editor, args: &[&str], event: PromptEvent) { fn earlier(editor: &mut Editor, args: &[&str], event: PromptEvent) {
let uk = match args.join(" ").parse::<helix_core::history::UndoKind>() { let uk = match args.join(" ").parse::<helix_core::history::UndoKind>() {
Ok(uk) => uk, Ok(uk) => uk,
@ -1143,6 +1165,13 @@ fn force_quit_all(editor: &mut Editor, args: &[&str], event: PromptEvent) {
fun: format, fun: format,
completer: None, completer: None,
}, },
Command {
name: "indent_style",
alias: None,
doc: "Set the indentation style for editing. ('t' for tabs or 1-8 for number of spaces.)",
fun: set_indent_style,
completer: None,
},
Command { Command {
name: "earlier", name: "earlier",
alias: Some("ear"), alias: Some("ear"),

View File

@ -501,7 +501,7 @@ pub fn render_statusline(
// Compute the individual info strings. // Compute the individual info strings.
let diag_count = format!("{}", doc.diagnostics().len()); let diag_count = format!("{}", doc.diagnostics().len());
let indent_info = match doc.indent_style { let indent_info = match doc.indent_style {
IndentStyle::Tabs => "tab", IndentStyle::Tabs => "tabs",
IndentStyle::Spaces(1) => "spaces:1", IndentStyle::Spaces(1) => "spaces:1",
IndentStyle::Spaces(2) => "spaces:2", IndentStyle::Spaces(2) => "spaces:2",
IndentStyle::Spaces(3) => "spaces:3", IndentStyle::Spaces(3) => "spaces:3",