Skip to content

Commit

Permalink
Format and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed May 3, 2022
1 parent db82267 commit a6d1db4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
43 changes: 24 additions & 19 deletions helix-core/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand All @@ -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;
Expand All @@ -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);
Expand Down
36 changes: 27 additions & 9 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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(' ');
Expand Down Expand Up @@ -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()
};
Expand Down

0 comments on commit a6d1db4

Please sign in to comment.