From d4c45332167cabee5600a8f935cf8091cd93838d Mon Sep 17 00:00:00 2001 From: Triton171 Date: Mon, 24 Jan 2022 18:46:19 +0100 Subject: [PATCH] Add comments & replace Option> with Vec<_> --- helix-core/src/indent.rs | 19 +++++++++++++------ helix-core/src/syntax.rs | 16 +++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 1111e4a14e9f..d0f7736d8d89 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -188,6 +188,8 @@ pub fn indent_level_for_line(line: RopeSlice, tab_width: usize) -> usize { len / tab_width } +/// The indent that is added for a single tree-sitter node/a single line. +/// This is different from the total indent ([IndentResult]) because multiple indents/outdents on the same line don't stack. struct AddedIndent { indent: bool, outdent: bool, @@ -205,7 +207,11 @@ impl AddedIndent { } } +/// The total indent for some line of code. +/// This is usually constructed by successively adding instances of [AddedIndent] struct IndentResult { + /// The total indent (the number of indent levels). + /// The string that this results in depends on the indent style (spaces or tabs, etc.) indent: i32, } impl IndentResult { @@ -277,13 +283,13 @@ fn matches<'a>(query_node: &IndentQueryNode, node: Node<'a>, cursor: &mut TreeCu parent_kind_in, field_name_in, } => { - if let Some(kind_not_in) = kind_not_in { + if !kind_not_in.is_empty() { let kind = node.kind(); if kind_not_in.iter().any(|k| k == kind) { return false; } } - if let Some(parent_kind_in) = parent_kind_in { + if !parent_kind_in.is_empty() { let parent_matches = node.parent().map_or(false, |p| { let parent_kind = p.kind(); parent_kind_in @@ -294,7 +300,7 @@ fn matches<'a>(query_node: &IndentQueryNode, node: Node<'a>, cursor: &mut TreeCu return false; } } - if let Some(field_name_in) = field_name_in { + if !field_name_in.is_empty() { let parent = match node.parent() { None => { return false; @@ -345,6 +351,7 @@ fn contains_match<'a>( false } +/// Returns whether the given scopes contain a match for this line and/or for the next fn scopes_contain_match<'a>( scopes: &IndentQueryScopes, node: Node<'a>, @@ -355,7 +362,7 @@ fn scopes_contain_match<'a>( (match_for_line, match_for_next) } -// The added indent for the line of the node and the next line +/// The added indent for the line of the node and the next line fn added_indent<'a>( query: &IndentQuery, node: Node<'a>, @@ -441,8 +448,8 @@ fn treesitter_indent_for_pos( Some(result.as_string(indent_style)) } -// Returns the indentation for a new line. -// This is done either using treesitter, or if that's not available by copying the indentation from the current line +/// Returns the indentation for a new line. +/// This is done either using treesitter, or if that's not available by copying the indentation from the current line #[allow(clippy::too_many_arguments)] pub fn indent_for_newline( language_config: Option<&LanguageConfiguration>, diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 42a2832c763c..fd22a5859120 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -114,9 +114,15 @@ pub enum IndentQueryNode { // A node given by a list of characteristics which must all be fulfilled in order to match ComplexNode { kind: Option, - kind_not_in: Option>, - parent_kind_in: Option>, - field_name_in: Option>, + #[serde(default)] + #[serde(skip_serializing_if = "Vec::is_empty")] + kind_not_in: Vec, + #[serde(default)] + #[serde(skip_serializing_if = "Vec::is_empty")] + parent_kind_in: Vec, + #[serde(default)] + #[serde(skip_serializing_if = "Vec::is_empty")] + field_name_in: Vec, }, } impl IndentQueryNode { @@ -154,10 +160,6 @@ pub struct IndentQueryScopes { pub tail: Vec, } -impl IndentQueryScopes { - pub fn sort_nodes(&mut self) {} -} - impl<'de> Deserialize<'de> for IndentQueryScopes { fn deserialize(deserializer: D) -> Result where