bash.rs

  1use project::ContextProviderWithTasks;
  2use task::{TaskTemplate, TaskTemplates, VariableName};
  3
  4pub(super) fn bash_task_context() -> ContextProviderWithTasks {
  5    ContextProviderWithTasks::new(TaskTemplates(vec![
  6        TaskTemplate {
  7            label: "execute selection".to_owned(),
  8            command: VariableName::SelectedText.template_value(),
  9            ..TaskTemplate::default()
 10        },
 11        TaskTemplate {
 12            label: format!("run '{}'", VariableName::File.template_value()),
 13            command: VariableName::File.template_value(),
 14            ..TaskTemplate::default()
 15        },
 16    ]))
 17}
 18
 19#[cfg(test)]
 20mod tests {
 21    use gpui::{AppContext as _, BorrowAppContext, Context, TestAppContext};
 22    use language::{language_settings::AllLanguageSettings, AutoindentMode, Buffer};
 23    use settings::SettingsStore;
 24    use std::num::NonZeroU32;
 25
 26    #[gpui::test]
 27    async fn test_bash_autoindent(cx: &mut TestAppContext) {
 28        cx.executor().set_block_on_ticks(usize::MAX..=usize::MAX);
 29        let language = crate::language("bash", tree_sitter_bash::LANGUAGE.into());
 30        cx.update(|cx| {
 31            let test_settings = SettingsStore::test(cx);
 32            cx.set_global(test_settings);
 33            language::init(cx);
 34            cx.update_global::<SettingsStore, _>(|store, cx| {
 35                store.update_user_settings::<AllLanguageSettings>(cx, |s| {
 36                    s.defaults.tab_size = NonZeroU32::new(2)
 37                });
 38            });
 39        });
 40
 41        cx.new(|cx| {
 42            let mut buffer = Buffer::local("", cx).with_language(language, cx);
 43
 44            let expect_indents_to =
 45                |buffer: &mut Buffer, cx: &mut Context<Buffer>, input: &str, expected: &str| {
 46                    buffer.edit( [(0..buffer.len(), input)], Some(AutoindentMode::EachLine), cx, );
 47                    assert_eq!(buffer.text(), expected);
 48                };
 49
 50            // indent function correctly
 51            expect_indents_to(
 52                &mut buffer,
 53                cx,
 54                "function name() {\necho \"Hello, World!\"\n}",
 55                "function name() {\n  echo \"Hello, World!\"\n}",
 56            );
 57
 58            // indent if-else correctly
 59            expect_indents_to(
 60                &mut buffer,
 61                cx,
 62                "if true;then\nfoo\nelse\nbar\nfi",
 63                "if true;then\n  foo\nelse\n  bar\nfi",
 64            );
 65
 66            // indent if-elif-else correctly
 67            expect_indents_to(
 68                &mut buffer,
 69                cx,
 70                "if true;then\nfoo\nelif true;then\nbar\nelse\nbar\nfi",
 71                "if true;then\n  foo\nelif true;then\n  bar\nelse\n  bar\nfi",
 72            );
 73
 74            // indent case-when-else correctly
 75            expect_indents_to(
 76                &mut buffer,
 77                cx,
 78                "case $1 in\nfoo) echo \"Hello, World!\";;\n*) echo \"Unknown argument\";;\nesac",
 79                "case $1 in\n  foo) echo \"Hello, World!\";;\n  *) echo \"Unknown argument\";;\nesac",
 80            );
 81
 82            // indent for-loop correctly
 83            expect_indents_to(
 84                &mut buffer,
 85                cx,
 86                "for i in {1..10};do\nfoo\ndone",
 87                "for i in {1..10};do\n  foo\ndone",
 88            );
 89
 90            // indent while-loop correctly
 91            expect_indents_to(
 92                &mut buffer,
 93                cx,
 94                "while true; do\nfoo\ndone",
 95                "while true; do\n  foo\ndone",
 96            );
 97
 98            // indent array correctly
 99            expect_indents_to(
100                &mut buffer,
101                cx,
102                "array=(\n1\n2\n3\n)",
103                "array=(\n  1\n  2\n  3\n)",
104            );
105
106            // indents non-"function" function correctly
107            expect_indents_to(
108                &mut buffer,
109                cx,
110                "foo() {\necho \"Hello, World!\"\n}",
111                "foo() {\n  echo \"Hello, World!\"\n}",
112            );
113
114            buffer
115        });
116    }
117}