-
Notifications
You must be signed in to change notification settings - Fork 0
Index multiline comments #632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
soutaro
wants to merge
3
commits into
main
Choose a base branch
from
comment-lines
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,14 +147,49 @@ impl<'a> RBSIndexer<'a> { | |
| } | ||
| } | ||
|
|
||
| fn collect_comments(comment_node: Option<CommentNode>) -> Vec<Comment> { | ||
| comment_node | ||
| .into_iter() | ||
| .map(|comment| { | ||
| let text = Self::bytes_to_string(comment.string().as_bytes()); | ||
| Comment::new(Offset::from_rbs_location(&comment.location()), text) | ||
| }) | ||
| .collect() | ||
| #[allow(clippy::cast_possible_truncation)] | ||
| fn collect_comments(&self, comment_node: Option<CommentNode>) -> Vec<Comment> { | ||
| let Some(comment_node) = comment_node else { | ||
| return Vec::new(); | ||
| }; | ||
|
|
||
| let location = comment_node.location(); | ||
| let start = location.start().cast_unsigned() as usize; | ||
| let end = location.end().cast_unsigned() as usize; | ||
|
|
||
| let source_bytes = self.source.as_bytes(); | ||
|
|
||
| // Find the indentation level by scanning backwards from start to the line beginning | ||
| let mut indent_start = start; | ||
| while indent_start > 0 && source_bytes[indent_start - 1] == b' ' { | ||
| indent_start -= 1; | ||
| } | ||
| let indent_len = start - indent_start; | ||
|
|
||
| let comment_block = &self.source[start..end]; | ||
| let lines: Vec<&str> = comment_block.split('\n').collect(); | ||
|
|
||
| let mut comments = Vec::with_capacity(lines.len()); | ||
| let mut current_offset = start as u32; | ||
|
|
||
| for (i, line) in lines.iter().enumerate() { | ||
| let line_text = if i == 0 { | ||
| line.to_string() | ||
| } else { | ||
| line[indent_len..].to_string() | ||
| }; | ||
|
|
||
| let line_bytes = line_text.len() as u32; | ||
| let offset = Offset::new(current_offset, current_offset + line_bytes); | ||
| comments.push(Comment::new(offset, line_text)); | ||
|
|
||
| // Advance past current line + newline + indentation for the next line | ||
| if i < lines.len() - 1 { | ||
| current_offset += line_bytes + 1 + indent_len as u32; | ||
| } | ||
| } | ||
|
|
||
| comments | ||
| } | ||
|
|
||
| fn register_definition( | ||
|
|
@@ -201,7 +236,7 @@ impl Visit for RBSIndexer<'_> { | |
| let offset = Offset::from_rbs_location(&class_node.location()); | ||
| let name_offset = Offset::from_rbs_location(&type_name.name().location()); | ||
|
|
||
| let comments = Self::collect_comments(class_node.comment()); | ||
| let comments = self.collect_comments(class_node.comment()); | ||
|
|
||
| let superclass_ref = class_node.super_class().as_ref().map(|super_node| { | ||
| let type_name = super_node.name(); | ||
|
|
@@ -241,7 +276,7 @@ impl Visit for RBSIndexer<'_> { | |
| let offset = Offset::from_rbs_location(&module_node.location()); | ||
| let name_offset = Offset::from_rbs_location(&type_name.name().location()); | ||
|
|
||
| let comments = Self::collect_comments(module_node.comment()); | ||
| let comments = self.collect_comments(module_node.comment()); | ||
|
|
||
| let definition = Definition::Module(Box::new(ModuleDefinition::new( | ||
| name_id, | ||
|
|
@@ -271,7 +306,7 @@ impl Visit for RBSIndexer<'_> { | |
| let name_id = self.index_type_name(&type_name, nesting_name_id); | ||
| let offset = Offset::from_rbs_location(&constant_node.location()); | ||
|
|
||
| let comments = Self::collect_comments(constant_node.comment()); | ||
| let comments = self.collect_comments(constant_node.comment()); | ||
|
|
||
| let definition = Definition::Constant(Box::new(ConstantDefinition::new( | ||
| name_id, | ||
|
|
@@ -293,7 +328,7 @@ impl Visit for RBSIndexer<'_> { | |
| .intern_string(Self::bytes_to_string(global_node.name().name())); | ||
| let offset = Offset::from_rbs_location(&global_node.location()); | ||
|
|
||
| let comments = Self::collect_comments(global_node.comment()); | ||
| let comments = self.collect_comments(global_node.comment()); | ||
|
|
||
| let definition = Definition::GlobalVariable(Box::new(GlobalVariableDefinition::new( | ||
| str_id, | ||
|
|
@@ -526,7 +561,7 @@ mod tests { | |
|
|
||
| assert_definition_at!(&context, "2:1-2:12", Constant, |def| { | ||
| assert_def_name_eq!(&context, def, "FOO"); | ||
| assert_def_comments_eq!(&context, def, ["Some documentation\n"]); | ||
| assert_def_comments_eq!(&context, def, ["# Some documentation"]); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -551,7 +586,7 @@ mod tests { | |
|
|
||
| assert_definition_at!(&context, "4:1-4:14", GlobalVariable, |def| { | ||
| assert_def_str_eq!(&context, def, "$bar"); | ||
| assert_def_comments_eq!(&context, def, ["A global variable\n"]); | ||
| assert_def_comments_eq!(&context, def, ["# A global variable"]); | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -714,4 +749,41 @@ mod tests { | |
| assert!(def.flags().contains(DefinitionFlags::DEPRECATED)); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn split_multiline_comments() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a test with an escaped line break as part of the comment's content. It's easy for this to get confused. # splits strings at the \\n char
def foo; end |
||
| let context = index_source({ | ||
| " | ||
| # First line | ||
| # Second line | ||
| # Third line | ||
| class Foo | ||
| # A comment for Bar | ||
| # Another line for Bar | ||
| module Bar | ||
| end | ||
| end | ||
|
|
||
| # splits strings at the \\n char | ||
| BAZ: Integer | ||
| " | ||
| }); | ||
|
|
||
| assert_no_local_diagnostics!(&context); | ||
|
|
||
| assert_definition_at!(&context, "4:1-9:4", Class, |def| { | ||
| assert_def_name_eq!(&context, def, "Foo"); | ||
| assert_def_comments_eq!(&context, def, ["# First line", "# Second line", "# Third line"]); | ||
| }); | ||
|
|
||
| assert_definition_at!(&context, "7:3-8:6", Module, |def| { | ||
| assert_def_name_eq!(&context, def, "Bar"); | ||
| assert_def_comments_eq!(&context, def, ["# A comment for Bar", "# Another line for Bar"]); | ||
| }); | ||
|
|
||
| assert_definition_at!(&context, "12:1-12:13", Constant, |def| { | ||
| assert_def_name_eq!(&context, def, "BAZ"); | ||
| assert_def_comments_eq!(&context, def, ["# splits strings at the \\n char"]); | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this assuming that all comment lines are indented the same? What happens in this case? Let's add a test to document the behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. 🙇♂️
I was thinking the RBS parser rejects the differently indented comments, but it actually accepts. I need to fix the implementation.