From a6d1db4c04eea36208e8bc73e50616c1a0d2b24d Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Mon, 2 May 2022 22:54:05 -0400 Subject: [PATCH] Format and clippy --- helix-core/src/comment.rs | 43 +++++++++++++++++++++----------------- helix-term/src/commands.rs | 36 +++++++++++++++++++++++-------- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index a07436957f23..f9b57d65596c 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -6,7 +6,9 @@ use tree_sitter::QueryCursor; use crate::{ - find_first_non_whitespace_char, Change, Rope, RopeSlice, Selection, Tendril, Transaction, Syntax, syntax::{LanguageConfiguration, CapturedNode}, Range, + find_first_non_whitespace_char, + syntax::{CapturedNode, LanguageConfiguration}, + Change, Range, Rope, RopeSlice, Selection, Syntax, Tendril, Transaction, }; use std::borrow::Cow; @@ -124,8 +126,16 @@ pub fn continue_comment<'a>(doc: &Rope, line: usize, tokens: &'a [String]) -> Op result } -pub fn continue_block_comment<'a>(doc: &Rope, syntax: Option<&Syntax>, lang_config: &'a LanguageConfiguration, range: &Range, open_below: bool) -> Option<&'a str> { - if let Some((doc_syntax, block_comment_tokens)) = syntax.zip(lang_config.block_comment_tokens.as_ref()) { +pub fn continue_block_comment<'a>( + doc: &Rope, + syntax: Option<&Syntax>, + lang_config: &'a LanguageConfiguration, + range: &Range, + open_below: bool, +) -> Option<&'a str> { + if let Some((doc_syntax, block_comment_tokens)) = + syntax.zip(lang_config.block_comment_tokens.as_ref()) + { let slice_tree = doc_syntax.tree().root_node(); let slice = doc.slice(..); let line_pos = slice.char_to_line(range.cursor(slice)); @@ -143,13 +153,9 @@ pub fn continue_block_comment<'a>(doc: &Rope, syntax: Option<&Syntax>, lang_conf }; { - let nodes = lang_config.textobject_query() - .and_then(|query| query.capture_nodes_any( - &["comment.block.around"], - slice_tree, - slice, - &mut cursor, - )); + let nodes = lang_config.textobject_query().and_then(|query| { + query.capture_nodes_any(&["comment.block.around"], slice_tree, slice, &mut cursor) + }); if let Some(nodes) = nodes { for node in nodes { found_block_comments = true; @@ -165,23 +171,22 @@ pub fn continue_block_comment<'a>(doc: &Rope, syntax: Option<&Syntax>, lang_conf // FIXME: this doesn't take into account that a line comment followed by a block comment // counts as only one comment object. if !found_block_comments { - let nodes = lang_config.textobject_query() - .and_then(|query| query.capture_nodes_any( - &["comment.around"], - slice_tree, - slice, - &mut cursor, - )); + let nodes = lang_config.textobject_query().and_then(|query| { + query.capture_nodes_any(&["comment.around"], slice_tree, slice, &mut cursor) + }); if let Some(nodes) = nodes { for node in nodes { let node_start = doc.byte_to_char(node.start_byte()); let node_end = doc.byte_to_char(node.end_byte()); let start_token_len = block_comment_tokens.start.len(); let end_token_len = block_comment_tokens.end.len(); - if doc.len_chars() > node_start + start_token_len && doc.len_chars() > node_end { + if doc.len_chars() > node_start + start_token_len && doc.len_chars() > node_end + { let comment_start = doc.slice(node_start..node_start + start_token_len); let comment_end = doc.slice(node_end - end_token_len..node_end); - if comment_start == block_comment_tokens.start && comment_end == block_comment_tokens.end { + if comment_start == block_comment_tokens.start + && comment_end == block_comment_tokens.end + { // We're in a block comment. if should_insert_comment_middle(node) { return Some(&block_comment_tokens.middle); diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index b576a4523ae9..33081181d6ac 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -2295,7 +2295,7 @@ fn open(cx: &mut Context, open: Open) { text.push_str(doc.line_ending.as_str()); text.push_str(&indent); - handle_comment_continue(&doc, range, &mut text, cursor_line, open == Open::Below); + handle_comment_continue(doc, range, &mut text, cursor_line, open == Open::Below); let text = text.repeat(count); @@ -2321,22 +2321,40 @@ fn open(cx: &mut Context, open: Open) { goto_line_end_newline(cx); } -fn handle_comment_continue(doc: &Document, range: &Range, text: &mut String, cursor_line: usize, open_below: bool) { +fn handle_comment_continue( + doc: &Document, + range: &Range, + text: &mut String, + cursor_line: usize, + open_below: bool, +) { if let Some(lang_config) = doc.language_config() { let line_comment_tokens = &lang_config.comment_tokens; - if let Some(token) = comment::continue_block_comment(doc.text(), doc.syntax(), lang_config, range, open_below) { + if let Some(token) = comment::continue_block_comment( + doc.text(), + doc.syntax(), + lang_config, + range, + open_below, + ) { text.push(' '); text.push_str(token); text.push(' '); - } - else if let Some(token) = comment::continue_comment(doc.text(), cursor_line, line_comment_tokens) { + } else if let Some(token) = + comment::continue_comment(doc.text(), cursor_line, line_comment_tokens) + { text.push_str(token); text.push(' '); - } - else if let Some(ref block_comment_tokens) = lang_config.block_comment_tokens { + } else if let Some(ref block_comment_tokens) = lang_config.block_comment_tokens { // FIXME: this doesn't work for the following lines of a comment start. // FIXME: this is too indented in some cases. - if comment::continue_comment(doc.text(), cursor_line, &[block_comment_tokens.start.clone()]).is_some() { + if comment::continue_comment( + doc.text(), + cursor_line, + &[block_comment_tokens.start.clone()], + ) + .is_some() + { text.push(' '); text.push_str(&block_comment_tokens.middle); text.push(' '); @@ -2806,7 +2824,7 @@ pub mod insert { text.push_str(doc.line_ending.as_str()); text.push_str(&indent); - handle_comment_continue(&doc, range, &mut text, current_line, false); + handle_comment_continue(doc, range, &mut text, current_line, false); pos + offs + text.chars().count() };