From a3a3b0b517d0e690f3efc66b17ac7b9f769dba9d Mon Sep 17 00:00:00 2001 From: Martin Junghanns Date: Sat, 20 Nov 2021 06:17:25 -0800 Subject: [PATCH 01/36] Jump to end char of surrounding pair from any cursor pos (#1121) * Jump to end char of surrounding pair from any cursor pos * Separate bracket matching into exact and fuzzy search * Add constants for bracket chars * Abort early if char under cursor is not a bracket * Simplify bracket char validation * Refactor node search and unify find methods * Remove bracket constants --- helix-core/src/match_brackets.rs | 103 +++++++++++++++++++++---------- helix-term/src/commands.rs | 4 +- helix-term/src/ui/editor.rs | 2 +- 3 files changed, 74 insertions(+), 35 deletions(-) diff --git a/helix-core/src/match_brackets.rs b/helix-core/src/match_brackets.rs index 136ce320..cd554005 100644 --- a/helix-core/src/match_brackets.rs +++ b/helix-core/src/match_brackets.rs @@ -1,3 +1,5 @@ +use tree_sitter::Node; + use crate::{Rope, Syntax}; const PAIRS: &[(char, char)] = &[ @@ -6,50 +8,85 @@ ('[', ']'), ('<', '>'), ('\'', '\''), - ('"', '"'), + ('\"', '\"'), ]; + // limit matching pairs to only ( ) { } [ ] < > +// Returns the position of the matching bracket under cursor. +// +// If the cursor is one the opening bracket, the position of +// the closing bracket is returned. If the cursor in the closing +// bracket, the position of the opening bracket is returned. +// +// If the cursor is not on a bracket, `None` is returned. #[must_use] -pub fn find(syntax: &Syntax, doc: &Rope, pos: usize) -> Option { - let tree = syntax.tree(); - - let byte_pos = doc.char_to_byte(pos); - - // most naive implementation: find the innermost syntax node, if we're at the edge of a node, - // return the other edge. - - let node = match tree - .root_node() - .named_descendant_for_byte_range(byte_pos, byte_pos) - { - Some(node) => node, - None => return None, - }; - - if node.is_error() { +pub fn find_matching_bracket(syntax: &Syntax, doc: &Rope, pos: usize) -> Option { + if pos >= doc.len_chars() || !is_valid_bracket(doc.char(pos)) { return None; } + find_pair(syntax, doc, pos, false) +} +// Returns the position of the bracket that is closing the current scope. +// +// If the cursor is on an opening or closing bracket, the function +// behaves equivalent to [`find_matching_bracket`]. +// +// If the cursor position is within a scope, the function searches +// for the surrounding scope that is surrounded by brackets and +// returns the position of the closing bracket for that scope. +// +// If no surrounding scope is found, the function returns `None`. +#[must_use] +pub fn find_matching_bracket_fuzzy(syntax: &Syntax, doc: &Rope, pos: usize) -> Option { + find_pair(syntax, doc, pos, true) +} + +fn find_pair(syntax: &Syntax, doc: &Rope, pos: usize, traverse_parents: bool) -> Option { + let tree = syntax.tree(); + let pos = doc.char_to_byte(pos); + + let mut node = tree.root_node().named_descendant_for_byte_range(pos, pos)?; + + loop { + let (start_byte, end_byte) = surrounding_bytes(doc, &node)?; + let (start_char, end_char) = (doc.byte_to_char(start_byte), doc.byte_to_char(end_byte)); + + if is_valid_pair(doc, start_char, end_char) { + if end_byte == pos { + return Some(start_char); + } + // We return the end char if the cursor is either on the start char + // or at some arbitrary position between start and end char. + return Some(end_char); + } + + if traverse_parents { + node = node.parent()?; + } else { + return None; + } + } +} + +fn is_valid_bracket(c: char) -> bool { + PAIRS.iter().any(|(l, r)| *l == c || *r == c) +} + +fn is_valid_pair(doc: &Rope, start_char: usize, end_char: usize) -> bool { + PAIRS.contains(&(doc.char(start_char), doc.char(end_char))) +} + +fn surrounding_bytes(doc: &Rope, node: &Node) -> Option<(usize, usize)> { let len = doc.len_bytes(); + let start_byte = node.start_byte(); - let end_byte = node.end_byte().saturating_sub(1); // it's end exclusive + let end_byte = node.end_byte().saturating_sub(1); + if start_byte >= len || end_byte >= len { return None; } - let start_char = doc.byte_to_char(start_byte); - let end_char = doc.byte_to_char(end_byte); - - if PAIRS.contains(&(doc.char(start_char), doc.char(end_char))) { - if start_byte == byte_pos { - return Some(end_char); - } - - if end_byte == byte_pos { - return Some(start_char); - } - } - - None + Some((start_byte, end_byte)) } diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 431265cd..e70773eb 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4954,7 +4954,9 @@ fn match_brackets(cx: &mut Context) { if let Some(syntax) = doc.syntax() { let text = doc.text().slice(..); let selection = doc.selection(view.id).clone().transform(|range| { - if let Some(pos) = match_brackets::find(syntax, doc.text(), range.anchor) { + if let Some(pos) = + match_brackets::find_matching_bracket_fuzzy(syntax, doc.text(), range.anchor) + { range.put_cursor(text, pos, doc.mode == Mode::Select) } else { range diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 03cd0474..27d33d22 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -377,7 +377,7 @@ pub fn render_focused_view_elements( use helix_core::match_brackets; let pos = doc.selection(view.id).primary().cursor(text); - let pos = match_brackets::find(syntax, doc.text(), pos) + let pos = match_brackets::find_matching_bracket(syntax, doc.text(), pos) .and_then(|pos| view.screen_coords_at_pos(doc, text, pos)); if let Some(pos) = pos { From 05c6cb1d0b576547c14b204e0df543650c93892f Mon Sep 17 00:00:00 2001 From: Skyler Hawthorne Date: Sat, 20 Nov 2021 09:17:38 -0500 Subject: [PATCH 02/36] Solarized theme: fix popup colors, adjust menu (#1124) * fix popup colors, adjust menu * fix hardcoded horizontal rule color --- helix-term/src/ui/markdown.rs | 6 ++++-- runtime/themes/solarized_dark.toml | 12 ++++++------ runtime/themes/solarized_light.toml | 12 ++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/helix-term/src/ui/markdown.rs b/helix-term/src/ui/markdown.rs index 61630d55..649703b5 100644 --- a/helix-term/src/ui/markdown.rs +++ b/helix-term/src/ui/markdown.rs @@ -55,7 +55,7 @@ fn parse<'a>( fn to_span(text: pulldown_cmark::CowStr) -> Span { use std::ops::Deref; Span::raw::>(match text { - CowStr::Borrowed(s) => s.to_string().into(), // could retain borrow + CowStr::Borrowed(s) => s.into(), CowStr::Boxed(s) => s.to_string().into(), CowStr::Inlined(s) => s.deref().to_owned().into(), }) @@ -179,7 +179,9 @@ fn to_span(text: pulldown_cmark::CowStr) -> Span { spans.push(Span::raw(" ")); } Event::Rule => { - lines.push(Spans::from("---")); + let mut span = Span::raw("---"); + span.style = code_style; + lines.push(Spans::from(span)); lines.push(Spans::default()); } // TaskListMarker(bool) true if checked diff --git a/runtime/themes/solarized_dark.toml b/runtime/themes/solarized_dark.toml index afcafd54..984c86ee 100644 --- a/runtime/themes/solarized_dark.toml +++ b/runtime/themes/solarized_dark.toml @@ -28,18 +28,18 @@ # 行号栏 "ui.linenr" = { fg = "base0", bg = "base02" } # 当前行号栏 -"ui.linenr.selected" = { fg = "red", modifiers = ["bold"] } +"ui.linenr.selected" = { fg = "blue", modifiers = ["bold"] } # 状态栏 -"ui.statusline" = { fg = "base02", bg = "base1" } +"ui.statusline" = { fg = "base03", bg = "base0" } # 非活动状态栏 -"ui.statusline.inactive" = { fg = "base02", bg = "base00" } +"ui.statusline.inactive" = { fg = "base1", bg = "base01" } # 补全窗口, preview窗口 -"ui.popup" = { bg = "base1" } +"ui.popup" = { bg = "base02" } # 影响 补全选中 cmd弹出信息选中 -"ui.menu.selected" = { fg = "base02", bg = "violet"} -"ui.menu" = { fg = "base02" } +"ui.menu.selected" = { fg = "base02", bg = "base2"} +"ui.menu" = { fg = "base1" } # ?? "ui.window" = { fg = "base3" } # 命令行 补全的帮助信息 diff --git a/runtime/themes/solarized_light.toml b/runtime/themes/solarized_light.toml index aec5bf48..0ab1b962 100644 --- a/runtime/themes/solarized_light.toml +++ b/runtime/themes/solarized_light.toml @@ -28,18 +28,18 @@ # 行号栏 "ui.linenr" = { fg = "base0", bg = "base02" } # 当前行号栏 -"ui.linenr.selected" = { fg = "red", modifiers = ["bold"] } +"ui.linenr.selected" = { fg = "blue", modifiers = ["bold"] } # 状态栏 -"ui.statusline" = { fg = "base02", bg = "base1" } +"ui.statusline" = { fg = "base03", bg = "base0" } # 非活动状态栏 -"ui.statusline.inactive" = { fg = "base02", bg = "base00" } +"ui.statusline.inactive" = { fg = "base1", bg = "base01" } # 补全窗口, preview窗口 -"ui.popup" = { bg = "base1" } +"ui.popup" = { bg = "base02" } # 影响 补全选中 cmd弹出信息选中 -"ui.menu.selected" = { fg = "base02", bg = "violet"} -"ui.menu" = { fg = "base02" } +"ui.menu.selected" = { fg = "base02", bg = "base2"} +"ui.menu" = { fg = "base1" } # ?? "ui.window" = { fg = "base3" } # 命令行 补全的帮助信息 From 6a4d9693ba12feed5b6d6b1b34a4ff56cb9f9fd7 Mon Sep 17 00:00:00 2001 From: Dan Nases Sha <70554613+dannasessha@users.noreply.github.com> Date: Sat, 20 Nov 2021 14:23:36 +0000 Subject: [PATCH 03/36] File picker config (#988) * squashed WIP commits * hide_gitignore working with config * pass reference to new config parameter of file_picker() * update config option name to match name on walk builder * add comments to config and documentation of option to book * add git_ignore option to WalkBuilder within prompt in commands.rs * WIP: add FilePickerConfig struct * WIP: cleanup * WIP: add more options including max_depth * WIP: changed defaults to match ignore crate defaults * WIP: change WalkBuilder in global_search() to use config options * WIP: removed follow_links, changed max_depth to follow config setting * WIP: update book with file-picker inline table notation * update documentation for file-picker config in book * adjusted to [editor.file-picker] in book configuration.md * adjust comments in editor.rs to be doc comments, cleanup * adjust comments * adjust book --- book/src/configuration.md | 12 ++++++ helix-term/src/application.rs | 2 +- helix-term/src/commands.rs | 77 +++++++++++++++++++++-------------- helix-term/src/ui/mod.rs | 11 ++++- helix-view/src/editor.rs | 42 +++++++++++++++++++ 5 files changed, 111 insertions(+), 33 deletions(-) diff --git a/book/src/configuration.md b/book/src/configuration.md index be25441f..2ed48d51 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -24,6 +24,18 @@ ## Editor | `completion-trigger-len` | The min-length of word under cursor to trigger autocompletion | `2` | | `auto-info` | Whether to display infoboxes | `true` | +`[editor.filepicker]` section of the config. Sets options for file picker and global search. All but the last key listed in the default file-picker configuration below are IgnoreOptions: whether hidden files and files listed within ignore files are ignored by (not visible in) the helix file picker and global search. There is also one other key, `max-depth` available, which is not defined by default. + +| Key | Description | Default | +|--|--|---------| +|`hidden` | Enables ignoring hidden files. | true +|`parents` | Enables reading ignore files from parent directories. | true +|`ignore` | Enables reading `.ignore` files. | true +|`git-ignore` | Enables reading `.gitignore` files. | true +|`git-global` | Enables reading global .gitignore, whose path is specified in git's config: `core.excludefile` option. | true +|`git-exclude` | Enables reading `.git/info/exclude` files. | true +|`max-depth` | Set with an integer value for maximum depth to recurse. | Defaults to `None`. + ## LSP To display all language server messages in the status line add the following to your `config.toml`: diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index 78b93cd9..a795a56e 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -120,7 +120,7 @@ pub fn new(args: Args, mut config: Config) -> Result { if first.is_dir() { std::env::set_current_dir(&first)?; editor.new_file(Action::VerticalSplit); - compositor.push(Box::new(ui::file_picker(".".into()))); + compositor.push(Box::new(ui::file_picker(".".into(), &config.editor))); } else { let nr_of_files = args.files.len(); editor.open(first.to_path_buf(), Action::VerticalSplit)?; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index e70773eb..fde505fd 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1440,6 +1440,7 @@ fn global_search(cx: &mut Context) { let (all_matches_sx, all_matches_rx) = tokio::sync::mpsc::unbounded_channel::<(usize, PathBuf)>(); let smart_case = cx.editor.config.smart_case; + let file_picker_config = cx.editor.config.file_picker.clone(); let completions = search_completions(cx, None); let prompt = ui::regex_prompt( @@ -1468,41 +1469,55 @@ fn global_search(cx: &mut Context) { let search_root = std::env::current_dir() .expect("Global search error: Failed to get current dir"); - WalkBuilder::new(search_root).build_parallel().run(|| { - let mut searcher_cl = searcher.clone(); - let matcher_cl = matcher.clone(); - let all_matches_sx_cl = all_matches_sx.clone(); - Box::new(move |dent: Result| -> WalkState { - let dent = match dent { - Ok(dent) => dent, - Err(_) => return WalkState::Continue, - }; + WalkBuilder::new(search_root) + .hidden(file_picker_config.hidden) + .parents(file_picker_config.parents) + .ignore(file_picker_config.ignore) + .git_ignore(file_picker_config.git_ignore) + .git_global(file_picker_config.git_global) + .git_exclude(file_picker_config.git_exclude) + .max_depth(file_picker_config.max_depth) + .build_parallel() + .run(|| { + let mut searcher_cl = searcher.clone(); + let matcher_cl = matcher.clone(); + let all_matches_sx_cl = all_matches_sx.clone(); + Box::new(move |dent: Result| -> WalkState { + let dent = match dent { + Ok(dent) => dent, + Err(_) => return WalkState::Continue, + }; - match dent.file_type() { - Some(fi) => { - if !fi.is_file() { - return WalkState::Continue; + match dent.file_type() { + Some(fi) => { + if !fi.is_file() { + return WalkState::Continue; + } } + None => return WalkState::Continue, } - None => return WalkState::Continue, - } - let result_sink = sinks::UTF8(|line_num, _| { - match all_matches_sx_cl - .send((line_num as usize - 1, dent.path().to_path_buf())) - { - Ok(_) => Ok(true), - Err(_) => Ok(false), + let result_sink = sinks::UTF8(|line_num, _| { + match all_matches_sx_cl + .send((line_num as usize - 1, dent.path().to_path_buf())) + { + Ok(_) => Ok(true), + Err(_) => Ok(false), + } + }); + let result = + searcher_cl.search_path(&matcher_cl, dent.path(), result_sink); + + if let Err(err) = result { + log::error!( + "Global search error: {}, {}", + dent.path().display(), + err + ); } - }); - let result = searcher_cl.search_path(&matcher_cl, dent.path(), result_sink); - - if let Err(err) = result { - log::error!("Global search error: {}, {}", dent.path().display(), err); - } - WalkState::Continue - }) - }); + WalkState::Continue + }) + }); } else { // Otherwise do nothing // log::warn!("Global Search Invalid Pattern") @@ -2742,7 +2757,7 @@ fn command_mode(cx: &mut Context) { fn file_picker(cx: &mut Context) { let root = find_root(None).unwrap_or_else(|| PathBuf::from("./")); - let picker = ui::file_picker(root); + let picker = ui::file_picker(root, &cx.editor.config); cx.push_layer(Box::new(picker)); } diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index 62da0dce..cdf42311 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -93,13 +93,22 @@ pub fn regex_prompt( ) } -pub fn file_picker(root: PathBuf) -> FilePicker { +pub fn file_picker(root: PathBuf, config: &helix_view::editor::Config) -> FilePicker { use ignore::{types::TypesBuilder, WalkBuilder}; use std::time; // We want to exclude files that the editor can't handle yet let mut type_builder = TypesBuilder::new(); let mut walk_builder = WalkBuilder::new(&root); + walk_builder + .hidden(config.file_picker.hidden) + .parents(config.file_picker.parents) + .ignore(config.file_picker.ignore) + .git_ignore(config.file_picker.git_ignore) + .git_global(config.file_picker.git_global) + .git_exclude(config.file_picker.git_exclude) + .max_depth(config.file_picker.max_depth); + let walk_builder = match type_builder.add( "compressed", "*.{zip,gz,bz2,zst,lzo,sz,tgz,tbz2,lz,lz4,lzma,lzo,z,Z,xz,7z,rar,cab}", diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 364865d9..1ce33760 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -35,6 +35,46 @@ fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result