From dc609cafb506e9677d16632b6fede3c488d4c4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Wed, 1 Sep 2021 10:46:35 +0900 Subject: [PATCH 1/8] Extract the shell command into a separate function --- helix-term/src/commands.rs | 90 +++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index f479a7a0..797a5e99 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4322,9 +4322,46 @@ fn shell_keep_pipe(cx: &mut Context) { shell(cx, "keep-pipe:".into(), ShellBehavior::Filter); } -fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) { +fn shell_impl( + shell: &[String], + cmd: &str, + input: Option<&[u8]>, +) -> anyhow::Result<(Tendril, bool)> { use std::io::Write; use std::process::{Command, Stdio}; + let mut process = match Command::new(&shell[0]) + .args(&shell[1..]) + .arg(cmd) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + { + Ok(process) => process, + Err(e) => { + log::error!("Failed to start shell: {}", e); + return Err(e.into()); + } + }; + if let Some(input) = input { + let mut stdin = process.stdin.take().unwrap(); + stdin.write_all(input)?; + } + let output = process.wait_with_output()?; + + if !output.status.success() { + if !output.stderr.is_empty() { + log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr)); + } + bail!("Command failed"); + } + + let tendril = Tendril::try_from_byte_slice(&output.stdout) + .map_err(|_| anyhow!("Process did not output valid UTF-8"))?; + Ok((tendril, output.status.success())) +} + +fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) { if cx.editor.config.shell.is_empty() { cx.editor.set_error("No shell set".to_owned()); return; @@ -4338,67 +4375,40 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) { Some('|'), |_input: &str| Vec::new(), move |cx: &mut compositor::Context, input: &str, event: PromptEvent| { + let shell = &cx.editor.config.shell; if event != PromptEvent::Validate { return; } - let shell = &cx.editor.config.shell; let (view, doc) = current!(cx.editor); let selection = doc.selection(view.id); let mut changes = Vec::with_capacity(selection.len()); + let text = doc.text().slice(..); for range in selection.ranges() { - let mut process = match Command::new(&shell[0]) - .args(&shell[1..]) - .arg(input) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn() - { - Ok(process) => process, - Err(e) => { - log::error!("Failed to start shell: {}", e); - cx.editor.set_error("Failed to start shell".to_owned()); - return; - } - }; - if pipe { - let stdin = process.stdin.as_mut().unwrap(); - let fragment = range.fragment(doc.text().slice(..)); - stdin.write_all(fragment.as_bytes()).unwrap(); - } - let output = process.wait_with_output().unwrap(); - - if behavior != ShellBehavior::Filter { - if !output.status.success() { - if !output.stderr.is_empty() { - log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr)); - } - cx.editor.set_error("Command failed".to_owned()); - return; - } - let tendril = match Tendril::try_from_byte_slice(&output.stdout) { - Ok(tendril) => tendril, - Err(_) => { - cx.editor - .set_error("Process did not output valid UTF-8".to_owned()); + let fragment = range.fragment(text); + let (output, success) = + match shell_impl(shell, input, pipe.then(|| fragment.as_bytes())) { + Ok(result) => result, + Err(err) => { + cx.editor.set_error(err.to_string()); return; } }; + + if behavior != ShellBehavior::Filter { let (from, to) = match behavior { ShellBehavior::Replace => (range.from(), range.to()), ShellBehavior::Insert => (range.from(), range.from()), ShellBehavior::Append => (range.to(), range.to()), _ => (range.from(), range.from()), }; - changes.push((from, to, Some(tendril))); + changes.push((from, to, Some(output))); } else { // if the process exits successfully, keep the selection, otherwise delete it. - let keep = output.status.success(); changes.push(( range.from(), - if keep { range.from() } else { range.to() }, + if success { range.from() } else { range.to() }, None, )); } From ce7ad2beb5b763e55cc40933d70ca7c7b325292e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Wed, 1 Sep 2021 11:01:19 +0900 Subject: [PATCH 2/8] Reimplement keep-pipe, it needs to manipulate selections, not text --- book/src/keymap.md | 14 +++--- helix-term/src/commands.rs | 98 +++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 34 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index d85fb936..2c6a9576 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -124,13 +124,13 @@ in reverse, or searching via smartcase. ### Shell -| Key | Description | Command | -| ------ | ----------- | ------- | -| `\|` | Pipe each selection through shell command, replacing with output | `shell_pipe` | -| `A-\|` | Pipe each selection into shell command, ignoring output | `shell_pipe_to` | -| `!` | Run shell command, inserting output before each selection | `shell_insert_output` | -| `A-!` | Run shell command, appending output after each selection | `shell_append_output` | -| `$` | Pipe each selection into shell command, removing if the command exits >0 | `shell_keep_pipe` | +| Key | Description | Command | +| ------ | ----------- | ------- | +| `\|` | Pipe each selection through shell command, replacing with output | `shell_pipe` | +| `A-\|` | Pipe each selection into shell command, ignoring output | `shell_pipe_to` | +| `!` | Run shell command, inserting output before each selection | `shell_insert_output` | +| `A-!` | Run shell command, appending output after each selection | `shell_append_output` | +| `$` | Pipe each selection into shell command, keep selections where command returned 0 | `shell_keep_pipe` | ## Select / extend mode diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 797a5e99..5574afbf 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4299,7 +4299,6 @@ enum ShellBehavior { Ignore, Insert, Append, - Filter, } fn shell_pipe(cx: &mut Context) { @@ -4319,7 +4318,56 @@ fn shell_append_output(cx: &mut Context) { } fn shell_keep_pipe(cx: &mut Context) { - shell(cx, "keep-pipe:".into(), ShellBehavior::Filter); + let prompt = Prompt::new( + "keep-pipe:".into(), + Some('|'), + |_input: &str| Vec::new(), + move |cx: &mut compositor::Context, input: &str, event: PromptEvent| { + let shell = &cx.editor.config.shell; + if event != PromptEvent::Validate { + return; + } + if input.is_empty() { + return; + } + let (view, doc) = current!(cx.editor); + let selection = doc.selection(view.id); + + let mut ranges = SmallVec::with_capacity(selection.len()); + let old_index = selection.primary_index(); + let mut index: Option = None; + let text = doc.text().slice(..); + + for (i, range) in selection.ranges().iter().enumerate() { + let fragment = range.fragment(text); + let (_output, success) = match shell_impl(shell, input, Some(fragment.as_bytes())) { + Ok(result) => result, + Err(err) => { + cx.editor.set_error(err.to_string()); + return; + } + }; + + // if the process exits successfully, keep the selection + if success { + ranges.push(*range); + if i >= old_index && index.is_none() { + index = Some(ranges.len() - 1); + } + } + } + + if ranges.is_empty() { + cx.editor.set_error("No selections remaining".to_string()); + return; + } + + let index = index.unwrap_or_else(|| ranges.len() - 1); + doc.set_selection(view.id, Selection::new(ranges, index)); + }, + ); + + cx.push_layer(Box::new(prompt)); } fn shell_impl( @@ -4329,6 +4377,10 @@ fn shell_impl( ) -> anyhow::Result<(Tendril, bool)> { use std::io::Write; use std::process::{Command, Stdio}; + if shell.is_empty() { + bail!("No shell set"); + } + let mut process = match Command::new(&shell[0]) .args(&shell[1..]) .arg(cmd) @@ -4349,11 +4401,8 @@ fn shell_impl( } let output = process.wait_with_output()?; - if !output.status.success() { - if !output.stderr.is_empty() { - log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr)); - } - bail!("Command failed"); + if !output.stderr.is_empty() { + log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr)); } let tendril = Tendril::try_from_byte_slice(&output.stdout) @@ -4362,12 +4411,8 @@ fn shell_impl( } fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) { - if cx.editor.config.shell.is_empty() { - cx.editor.set_error("No shell set".to_owned()); - return; - } let pipe = match behavior { - ShellBehavior::Replace | ShellBehavior::Ignore | ShellBehavior::Filter => true, + ShellBehavior::Replace | ShellBehavior::Ignore => true, ShellBehavior::Insert | ShellBehavior::Append => false, }; let prompt = Prompt::new( @@ -4379,6 +4424,9 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) { if event != PromptEvent::Validate { return; } + if input.is_empty() { + return; + } let (view, doc) = current!(cx.editor); let selection = doc.selection(view.id); @@ -4396,22 +4444,18 @@ fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) { } }; - if behavior != ShellBehavior::Filter { - let (from, to) = match behavior { - ShellBehavior::Replace => (range.from(), range.to()), - ShellBehavior::Insert => (range.from(), range.from()), - ShellBehavior::Append => (range.to(), range.to()), - _ => (range.from(), range.from()), - }; - changes.push((from, to, Some(output))); - } else { - // if the process exits successfully, keep the selection, otherwise delete it. - changes.push(( - range.from(), - if success { range.from() } else { range.to() }, - None, - )); + if !success { + cx.editor.set_error("Command failed".to_string()); + return; } + + let (from, to) = match behavior { + ShellBehavior::Replace => (range.from(), range.to()), + ShellBehavior::Insert => (range.from(), range.from()), + ShellBehavior::Append => (range.to(), range.to()), + _ => (range.from(), range.from()), + }; + changes.push((from, to, Some(output))); } if behavior != ShellBehavior::Ignore { From 1586b0eec7000fe511d6ed381b35130a38759c40 Mon Sep 17 00:00:00 2001 From: oberblastmeister <61095988+oberblastmeister@users.noreply.github.com> Date: Wed, 1 Sep 2021 11:16:16 -0400 Subject: [PATCH 3/8] YAML support (#667) * added submodule * remove wrong one * added highlights * use property * add indents * shallow --- .gitmodules | 4 +++ helix-syntax/languages/tree-sitter-yaml | 1 + languages.toml | 8 ++++++ runtime/queries/yaml/highlights.scm | 33 +++++++++++++++++++++++++ runtime/queries/yaml/indents.toml | 3 +++ 5 files changed, 49 insertions(+) create mode 160000 helix-syntax/languages/tree-sitter-yaml create mode 100644 runtime/queries/yaml/highlights.scm create mode 100644 runtime/queries/yaml/indents.toml diff --git a/.gitmodules b/.gitmodules index 0e015658..aba8084c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -102,6 +102,10 @@ path = helix-syntax/languages/tree-sitter-protobuf url = https://github.com/yusdacra/tree-sitter-protobuf.git shallow = true +[submodule "helix-syntax/languages/tree-sitter-yaml"] + path = helix-syntax/languages/tree-sitter-yaml + url = https://github.com/ikatyang/tree-sitter-yaml + shallow = true [submodule "helix-syntax/languages/tree-sitter-zig"] path = helix-syntax/languages/tree-sitter-zig url = https://github.com/maxxnino/tree-sitter-zig diff --git a/helix-syntax/languages/tree-sitter-yaml b/helix-syntax/languages/tree-sitter-yaml new file mode 160000 index 00000000..0e36bed1 --- /dev/null +++ b/helix-syntax/languages/tree-sitter-yaml @@ -0,0 +1 @@ +Subproject commit 0e36bed171768908f331ff7dff9d956bae016efb diff --git a/languages.toml b/languages.toml index 1ca40377..7a2ea24d 100644 --- a/languages.toml +++ b/languages.toml @@ -224,6 +224,14 @@ roots = [] comment-token = ";" indent = { tab-width = 4, unit = " " } +[[language]] +name = "yaml" +scope = "source.yaml" +file-types = ["yml", "yaml"] +roots = [] +comment-token = "#" +indent = { tab-width = 2, unit = " " } + # [[language]] # name = "haskell" # scope = "source.haskell" diff --git a/runtime/queries/yaml/highlights.scm b/runtime/queries/yaml/highlights.scm new file mode 100644 index 00000000..4ebb4440 --- /dev/null +++ b/runtime/queries/yaml/highlights.scm @@ -0,0 +1,33 @@ +(block_mapping_pair key: (_) @property) +(flow_mapping (_ key: (_) @property)) +(boolean_scalar) @boolean +(null_scalar) @constant.builtin +(double_quote_scalar) @string +(single_quote_scalar) @string +(escape_sequence) @string.escape +(integer_scalar) @number +(float_scalar) @number +(comment) @comment +(anchor_name) @type +(alias_name) @type +(tag) @type +(yaml_directive) @keyword +(ERROR) @error + +[ +"," +"-" +":" +">" +"?" +"|" +] @punctuation.delimiter + +[ +"[" +"]" +"{" +"}" +] @punctuation.bracket + +["*" "&"] @punctuation.special diff --git a/runtime/queries/yaml/indents.toml b/runtime/queries/yaml/indents.toml new file mode 100644 index 00000000..ddc3578b --- /dev/null +++ b/runtime/queries/yaml/indents.toml @@ -0,0 +1,3 @@ +indent = [ + "block_mapping_pair", +] From 31f1455c71457fbd68b2a618f709b68c8de352b8 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Wed, 1 Sep 2021 08:18:56 -0700 Subject: [PATCH 4/8] Add a "vision" document, to help give people a sense of Helix's direction. (#657) * Add a "vision" document, to help give people a sense of Helix's direction. * Fix typo in vision document. * Fix spelling errors in vision document. Caught in PR review. Thanks! --- docs/vision.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 docs/vision.md diff --git a/docs/vision.md b/docs/vision.md new file mode 100644 index 00000000..d49dd64d --- /dev/null +++ b/docs/vision.md @@ -0,0 +1,24 @@ +The Helix project still has a ways to go before reaching its goals. This document outlines some of those goals and the overall vision for the project. + +# Vision + +An efficient, batteries-included editor you can take anywhere and be productive... if it's your kind of thing. + +* **Cross-platform.** Whether on Linux, Windows, or OSX, you should be able to take your editor with you. +* **Terminal first.** Not all environments have a windowing system, and you shouldn't have to abandon your preferred editor in those cases. +* **Native.** No Electron or HTML DOM here. We want an efficient, native-compiled editor that can run with minimal resources when needed. If you're working on a Raspberry Pi, your editor shouldn't consume half of your RAM. +* **Batteries included.** Both the default configuration and bundled features should be enough to have a good editing experience and be productive. You shouldn't need a massive custom config or external executables and plugins for basic features and functionality. +* **Don't try to be everything for everyone.** There are many great editors out there to choose from. Let's make Helix *one of* those great options, with its own take on things. + +# Goals + +Vision statements are all well and good, but are also vague and subjective. Here is a (non-exhaustive) list of some of Helix's more concrete goals, to help give a clearer idea of the project's direction: + +* **Modal.** Vim is a great idea. +* **Selection -> Action**, not Verb -> Object. Interaction models aren't linguistics, and "selection first" lets you see what you're doing (among other benefits). +* **We aren't playing code golf.** It's more important for the keymap to be consistent and easy to memorize than it is to save a key stroke or two when editing. +* **Built-in tools** for working with code bases efficiently. Most projects aren't a single file, and an editor should handle that as a first-class use case. In Helix's case, this means (among other things) a fuzzy-search file navigator and LSP support. +* **Edit anything** that comes up when coding, within reason. Whether it's a 200 MB XML file, a megabyte of minified javascript on a single line, or Japanese text encoded in ShiftJIS, you should be able to open it and edit it without problems. (Note: this doesn't mean handle every esoteric use case. Sometimes you do just need a specialized tool, and Helix isn't that.) +* **Configurable**, within reason. Although the defaults should be good, not everyone will agree on what "good" is. Within the bounds of Helix's core interaction models, it should be reasonably configurable so that it can be "good" for more people. This means, for example, custom key maps among other things. +* **Extensible**, within reason. Although we want Helix to be productive out-of-the-box, it's not practical or desirable to cram every useful feature and use case into the core editor. The basics should be built-in, but you should be able to extend it with additional functionality as needed. Right now we're thinking Wasm-based plugins. +* **Clean code base.** Sometimes other factors (e.g. significant performance gains, important features, correctness, etc.) will trump strict readability, but we nevertheless want to keep the code base straightforward and easy to understand to the extent we can. From ae3f9366118bae9775b5229b817d1131c84cfc96 Mon Sep 17 00:00:00 2001 From: oberblastmeister <61095988+oberblastmeister@users.noreply.github.com> Date: Wed, 1 Sep 2021 11:54:21 -0400 Subject: [PATCH 5/8] Lua support (#665) * added submodule * small changes * updated some stuff * remove * shallow clone * correct indent * shallow * ok * highlights * proper captures --- .gitmodules | 4 + helix-syntax/languages/tree-sitter-lua | 1 + languages.toml | 8 ++ runtime/queries/lua/highlights.scm | 166 +++++++++++++++++++++++++ runtime/queries/lua/indents.toml | 24 ++++ 5 files changed, 203 insertions(+) create mode 160000 helix-syntax/languages/tree-sitter-lua create mode 100644 runtime/queries/lua/highlights.scm create mode 100644 runtime/queries/lua/indents.toml diff --git a/.gitmodules b/.gitmodules index aba8084c..ae4984da 100644 --- a/.gitmodules +++ b/.gitmodules @@ -102,6 +102,10 @@ path = helix-syntax/languages/tree-sitter-protobuf url = https://github.com/yusdacra/tree-sitter-protobuf.git shallow = true +[submodule "helix-syntax/languages/tree-sitter-lua"] + path = helix-syntax/languages/tree-sitter-lua + url = https://github.com/nvim-treesitter/tree-sitter-lua + shallow = true [submodule "helix-syntax/languages/tree-sitter-yaml"] path = helix-syntax/languages/tree-sitter-yaml url = https://github.com/ikatyang/tree-sitter-yaml diff --git a/helix-syntax/languages/tree-sitter-lua b/helix-syntax/languages/tree-sitter-lua new file mode 160000 index 00000000..6f5d4019 --- /dev/null +++ b/helix-syntax/languages/tree-sitter-lua @@ -0,0 +1 @@ +Subproject commit 6f5d40190ec8a0aa8c8410699353d820f4f7d7a6 diff --git a/languages.toml b/languages.toml index 7a2ea24d..7164c4df 100644 --- a/languages.toml +++ b/languages.toml @@ -224,6 +224,14 @@ roots = [] comment-token = ";" indent = { tab-width = 4, unit = " " } +[[language]] +name = "lua" +scope = "source.lua" +file-types = ["lua"] +roots = [] +comment-token = "--" +indent = { tab-width = 2, unit = " " } + [[language]] name = "yaml" scope = "source.yaml" diff --git a/runtime/queries/lua/highlights.scm b/runtime/queries/lua/highlights.scm new file mode 100644 index 00000000..8e27a39a --- /dev/null +++ b/runtime/queries/lua/highlights.scm @@ -0,0 +1,166 @@ +;;; Highlighting for lua + +;;; Builtins +(self) @variable.builtin + +;; Keywords + +(if_statement +[ + "if" + "then" + "end" +] @keyword.control.conditional) + +[ + "else" + "elseif" + "then" +] @keyword.control.conditional + +(for_statement +[ + "for" + "do" + "end" +] @keyword.control.loop) + +(for_in_statement +[ + "for" + "do" + "end" +] @keyword.control.loop) + +(while_statement +[ + "while" + "do" + "end" +] @keyword.control.loop) + +(repeat_statement +[ + "repeat" + "until" +] @keyword.control.loop) + +(do_statement +[ + "do" + "end" +] @keyword) + +[ + "in" + "local" + (break_statement) + "goto" + "return" +] @keyword + +;; Operators + +[ + "not" + "and" + "or" +] @keyword.operator + +[ +"=" +"~=" +"==" +"<=" +">=" +"<" +">" +"+" +"-" +"%" +"/" +"//" +"*" +"^" +"&" +"~" +"|" +">>" +"<<" +".." +"#" + ] @operator + +;; Punctuation +["," "." ":" ";"] @punctuation.delimiter + +;; Brackets +[ + "(" + ")" + "[" + "]" + "{" + "}" +] @punctuation.bracket + +; ;; Constants +[ +(false) +(true) +] @boolean +(nil) @constant.builtin +(spread) @constant ;; "..." +((identifier) @constant + (#match? @constant "^[A-Z][A-Z_0-9]*$")) + +;; Parameters +(parameters + (identifier) @parameter) + +; ;; Functions +(function [(function_name) (identifier)] @function) +(function ["function" "end"] @keyword.function) + +(function + (function_name + (function_name_field + (property_identifier) @function .))) + +(local_function (identifier) @function) +(local_function ["function" "end"] @keyword.function) + +(variable_declaration + (variable_declarator (identifier) @function) (function_definition)) +(local_variable_declaration + (variable_declarator (identifier) @function) (function_definition)) + +(function_definition ["function" "end"] @keyword.function) + +(function_call + [ + ((identifier) @variable (method) @method) + ((_) (method) @method) + (identifier) @function + (field_expression (property_identifier) @function) + ] + . (arguments)) + +;; Nodes +(table ["{" "}"] @constructor) +(comment) @comment +(string) @string +(number) @number +(label_statement) @label +; A bit of a tricky one, this will only match field names +(field . (identifier) @property (_)) +(shebang) @comment + +;; Property +(property_identifier) @property + +;; Variable +(identifier) @variable + +;; Error +(ERROR) @error diff --git a/runtime/queries/lua/indents.toml b/runtime/queries/lua/indents.toml new file mode 100644 index 00000000..df1a9752 --- /dev/null +++ b/runtime/queries/lua/indents.toml @@ -0,0 +1,24 @@ +indent = [ + "function_definition", + "variable_declaration", + "local_variable_declaration", + "field", + "local_function", + "function", + "if_statement", + "for_statement", + "for_in_statement", + "repeat_statement", + "return_statement", + "while_statement", + "table", + "arguments", + "do_statement", +] + +oudent = [ + "end", + "until", + "}", + ")", +] From 825bceeab68276cdf120bda5d172b854867d8585 Mon Sep 17 00:00:00 2001 From: oberblastmeister <61095988+oberblastmeister@users.noreply.github.com> Date: Wed, 1 Sep 2021 11:55:16 -0400 Subject: [PATCH 6/8] add_newline unimpaired mapping (#653) * added some keymaps * remove * remove wrong mappings * remove * wrong import * use enum * correct line ending * added to book * column --- book/src/keymap.md | 23 ++++++++++++----------- helix-term/src/commands.rs | 36 ++++++++++++++++++++++++++++++++++++ helix-term/src/keymap.rs | 2 ++ 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/book/src/keymap.md b/book/src/keymap.md index 2c6a9576..861e46ac 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -110,17 +110,18 @@ in reverse, or searching via smartcase. | `N` | Add next search match to selection | `extend_search_next` | | `*` | Use current selection as the search pattern | `search_selection` | -### Diagnostics - -> NOTE: `[` and `]` will likely contain more pair mappings in the style of -> [vim-unimpaired](https://github.com/tpope/vim-unimpaired) - -| Key | Description | Command | -| ----- | ----------- | ------- | -| `[d` | Go to previous diagnostic | `goto_prev_diag` | -| `]d` | Go to next diagnostic | `goto_next_diag` | -| `[D` | Go to first diagnostic in document | `goto_first_diag` | -| `]D` | Go to last diagnostic in document | `goto_last_diag` | +### Unimpaired + +Mappings in the style of [vim-unimpaired](https://github.com/tpope/vim-unimpaired) + +| Key | Description | Command | +| ----- | ----------- | ------- | +| `[d` | Go to previous diagnostic | `goto_prev_diag` | +| `]d` | Go to next diagnostic | `goto_next_diag` | +| `[D` | Go to first diagnostic in document | `goto_first_diag` | +| `]D` | Go to last diagnostic in document | `goto_last_diag` | +| `[space` | Add newline above | `add_newline_above` | +| `]space` | Add newline below | `add_newline_below` | ### Shell diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 5574afbf..3bd63ab4 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -227,6 +227,8 @@ impl Command { select_mode, "Enter selection extend mode", exit_select_mode, "Exit selection mode", goto_definition, "Goto definition", + add_newline_above, "Add newline above", + add_newline_below, "Add newline below", goto_type_definition, "Goto type definition", goto_implementation, "Goto implementation", goto_file_start, "Goto file start/line", @@ -4473,3 +4475,37 @@ fn suspend(_cx: &mut Context) { #[cfg(not(windows))] signal_hook::low_level::raise(signal_hook::consts::signal::SIGTSTP).unwrap(); } + +fn add_newline_above(cx: &mut Context) { + add_newline_impl(cx, Open::Above); +} + +fn add_newline_below(cx: &mut Context) { + add_newline_impl(cx, Open::Below) +} + +fn add_newline_impl(cx: &mut Context, open: Open) { + let count = cx.count(); + let (view, doc) = current!(cx.editor); + let selection = doc.selection(view.id); + let text = doc.text(); + let slice = text.slice(..); + + let changes = selection.into_iter().map(|range| { + let (start, end) = range.line_range(slice); + let line = match open { + Open::Above => start, + Open::Below => end + 1, + }; + let pos = text.line_to_char(line); + ( + pos, + pos, + Some(doc.line_ending.as_str().repeat(count).into()), + ) + }); + + let transaction = Transaction::change(text, changes); + doc.apply(&transaction, view.id); + doc.append_changes_to_history(view.id); +} diff --git a/helix-term/src/keymap.rs b/helix-term/src/keymap.rs index f3e160b1..71ac01a9 100644 --- a/helix-term/src/keymap.rs +++ b/helix-term/src/keymap.rs @@ -412,10 +412,12 @@ impl Default for Keymaps { "[" => { "Left bracket" "d" => goto_prev_diag, "D" => goto_first_diag, + "space" => add_newline_above, }, "]" => { "Right bracket" "d" => goto_next_diag, "D" => goto_last_diag, + "space" => add_newline_below, }, "/" => search, From 5766f5da8fc9e98320e7e765c47e701d72108028 Mon Sep 17 00:00:00 2001 From: oberblastmeister <61095988+oberblastmeister@users.noreply.github.com> Date: Wed, 1 Sep 2021 12:08:08 -0400 Subject: [PATCH 7/8] OCaml support (#666) * added some stuff * add interface * indent * highlights and locals * scope * change some stuff * add indents * fix blanket highlight * macro * use inherits --- .gitmodules | 4 + helix-syntax/build.rs | 3 + helix-syntax/languages/tree-sitter-ocaml | 1 + languages.toml | 17 ++ .../queries/ocaml-interface/highlights.scm | 1 + runtime/queries/ocaml/highlights.scm | 160 ++++++++++++++++++ runtime/queries/ocaml/indents.toml | 13 ++ runtime/queries/ocaml/locals.scm | 24 +++ 8 files changed, 223 insertions(+) create mode 160000 helix-syntax/languages/tree-sitter-ocaml create mode 100644 runtime/queries/ocaml-interface/highlights.scm create mode 100644 runtime/queries/ocaml/highlights.scm create mode 100644 runtime/queries/ocaml/indents.toml create mode 100644 runtime/queries/ocaml/locals.scm diff --git a/.gitmodules b/.gitmodules index ae4984da..f905b8c7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -102,6 +102,10 @@ path = helix-syntax/languages/tree-sitter-protobuf url = https://github.com/yusdacra/tree-sitter-protobuf.git shallow = true +[submodule "helix-syntax/languages/tree-sitter-ocaml"] + path = helix-syntax/languages/tree-sitter-ocaml + url = https://github.com/tree-sitter/tree-sitter-ocaml + shallow = true [submodule "helix-syntax/languages/tree-sitter-lua"] path = helix-syntax/languages/tree-sitter-lua url = https://github.com/nvim-treesitter/tree-sitter-lua diff --git a/helix-syntax/build.rs b/helix-syntax/build.rs index 75f8c970..473646fd 100644 --- a/helix-syntax/build.rs +++ b/helix-syntax/build.rs @@ -177,6 +177,7 @@ fn main() { let ignore = vec![ "tree-sitter-typescript".to_string(), "tree-sitter-haskell".to_string(), // aarch64 failures: https://github.com/tree-sitter/tree-sitter-haskell/issues/34 + "tree-sitter-ocaml".to_string(), ]; let dirs = collect_tree_sitter_dirs(&ignore).unwrap(); @@ -202,4 +203,6 @@ fn main() { build_dir("tree-sitter-typescript/tsx", "tsx"); build_dir("tree-sitter-typescript/typescript", "typescript"); + build_dir("tree-sitter-ocaml/ocaml", "ocaml"); + build_dir("tree-sitter-ocaml/interface", "ocaml-interface") } diff --git a/helix-syntax/languages/tree-sitter-ocaml b/helix-syntax/languages/tree-sitter-ocaml new file mode 160000 index 00000000..23d419ba --- /dev/null +++ b/helix-syntax/languages/tree-sitter-ocaml @@ -0,0 +1 @@ +Subproject commit 23d419ba45789c5a47d31448061557716b02750a diff --git a/languages.toml b/languages.toml index 7164c4df..c04435fe 100644 --- a/languages.toml +++ b/languages.toml @@ -224,6 +224,23 @@ roots = [] comment-token = ";" indent = { tab-width = 4, unit = " " } +[[language]] +name = "ocaml" +scope = "source.ocaml" +injection-regex = "ocaml" +file-types = ["ml"] +roots = [] +comment-token = "(**)" +indent = { tab-width = 2, unit = " " } + +[[language]] +name = "ocaml-interface" +scope = "source.ocaml.interface" +file-types = ["mli"] +roots = [] +comment-token = "(**)" +indent = { tab-width = 2, unit = " "} + [[language]] name = "lua" scope = "source.lua" diff --git a/runtime/queries/ocaml-interface/highlights.scm b/runtime/queries/ocaml-interface/highlights.scm new file mode 100644 index 00000000..6d3dfbcf --- /dev/null +++ b/runtime/queries/ocaml-interface/highlights.scm @@ -0,0 +1 @@ +; inherits: ocaml diff --git a/runtime/queries/ocaml/highlights.scm b/runtime/queries/ocaml/highlights.scm new file mode 100644 index 00000000..093b3cce --- /dev/null +++ b/runtime/queries/ocaml/highlights.scm @@ -0,0 +1,160 @@ +; Modules +;-------- + +[(module_name) (module_type_name)] @namespace + +; Types +;------ + +[(class_name) (class_type_name) (type_constructor)] @type + +[(constructor_name) (tag)] @constructor + +; Functions +;---------- + +(let_binding + pattern: (value_name) @function + (parameter)) + +(let_binding + pattern: (value_name) @function + body: [(fun_expression) (function_expression)]) + +(value_specification (value_name) @function) + +(external (value_name) @function) + +(method_name) @method + +; Variables +;---------- + +(value_pattern) @parameter + +; Application +;------------ + +(infix_expression + left: (value_path (value_name) @function) + (infix_operator) @operator + (#eq? @operator "@@")) + +(infix_expression + (infix_operator) @operator + right: (value_path (value_name) @function) + (#eq? @operator "|>")) + +(application_expression + function: (value_path (value_name) @function)) + +; Properties +;----------- + +[(label_name) (field_name) (instance_variable_name)] @property + +; Constants +;---------- + +[(boolean) (unit)] @constant + +[(number) (signed_number)] @number + +(character) @character + +(string) @string + +(quoted_string "{" @string "}" @string) @string + +(escape_sequence) @string.escape + +[ + (conversion_specification) + (pretty_printing_indication) +] @punctuation.special + +; Keywords +;--------- + +[ + "and" "as" "assert" "begin" "class" "constraint" + "end" "external" "in" + "inherit" "initializer" "lazy" "let" "match" "method" "module" + "mutable" "new" "nonrec" "object" "of" "private" "rec" "sig" "struct" + "type" "val" "virtual" "when" "with" +] @keyword + +["fun" "function" "functor"] @keyword.function + +["if" "then" "else"] @keyword.control.conditional + +["exception" "try"] @keyword.control.exception + +["include" "open"] @include + +["for" "to" "downto" "while" "do" "done"] @keyword.control.loop + +; Macros +;------- + +(attribute ["[@" "]"] @attribute) +(item_attribute ["[@@" "]"] @attribute) +(floating_attribute ["[@@@" "]"] @attribute) +(extension ["[%" "]"] @function.macro) +(item_extension ["[%%" "]"] @function.macro) +(quoted_extension ["{%" "}"] @function.macro) +(quoted_item_extension ["{%%" "}"] @function.macro) +"%" @function.macro + +["(" ")" "[" "]" "{" "}" "[|" "|]" "[<" "[>"] @punctuation.bracket + +(object_type ["<" ">"] @punctuation.bracket) + +[ + "," "." ";" ":" "=" "|" "~" "?" "+" "-" "!" ">" "&" + "->" ";;" ":>" "+=" ":=" ".." +] @punctuation.delimiter + +; Operators +;---------- + +[ + (prefix_operator) + (sign_operator) + (infix_operator) + (hash_operator) + (indexing_operator) + (let_operator) + (and_operator) + (match_operator) +] @operator + +(match_expression (match_operator) @keyword) + +(value_definition [(let_operator) (and_operator)] @keyword) + +;; TODO: this is an error now +;(prefix_operator "!" @operator) + +(infix_operator ["&" "+" "-" "=" ">" "|" "%"] @operator) + +(signed_number ["+" "-"] @operator) + +["*" "#" "::" "<-"] @operator + +; Attributes +;----------- + +(attribute_id) @property + +; Comments +;--------- + +[(comment) (line_number_directive) (directive) (shebang)] @comment + +(ERROR) @error + +; Blanket highlights +; ------------------ + +[(value_name) (type_variable)] @variable diff --git a/runtime/queries/ocaml/indents.toml b/runtime/queries/ocaml/indents.toml new file mode 100644 index 00000000..9b6462d8 --- /dev/null +++ b/runtime/queries/ocaml/indents.toml @@ -0,0 +1,13 @@ +indent = [ + "let_binding", + "type_binding", + "structure", + "signature", + "record_declaration", + "function_expression", + "match_case", +] + +oudent = [ + "}", +] diff --git a/runtime/queries/ocaml/locals.scm b/runtime/queries/ocaml/locals.scm new file mode 100644 index 00000000..8f3f3fdf --- /dev/null +++ b/runtime/queries/ocaml/locals.scm @@ -0,0 +1,24 @@ +; Scopes +;------- + +[ + (let_binding) + (class_binding) + (class_function) + (method_definition) + (fun_expression) + (object_expression) + (for_expression) + (match_case) + (attribute_payload) +] @local.scope + +; Definitions +;------------ + +(value_pattern) @local.definition + +; References +;----------- + +(value_path . (value_name) @local.reference) From 7e1123680f474bff5113db189f63ca7f948781a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20K=C4=99pka?= Date: Thu, 2 Sep 2021 04:03:42 +0200 Subject: [PATCH 8/8] Expand `~` in `change-current-directory` command (#692) --- helix-term/src/commands.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 3bd63ab4..116f39bd 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1838,7 +1838,11 @@ mod cmd { args: &[&str], _event: PromptEvent, ) -> anyhow::Result<()> { - let dir = args.first().context("target directory not provided")?; + let dir = helix_core::path::expand_tilde( + args.first() + .context("target directory not provided")? + .as_ref(), + ); if let Err(e) = std::env::set_current_dir(dir) { bail!("Couldn't change the current working directory: {}", e);