initial commit

This commit is contained in:
longfangsong
2021-09-28 10:20:29 +08:00
parent 533ca584c3
commit 0049b5b0bc
7 changed files with 184 additions and 34 deletions

View File

@@ -14,11 +14,11 @@ use syntax::{
self,
edit::{self, AstNodeEdit},
edit_in_place::AttrsOwnerEdit,
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds,
make, HasArgList, HasAttrs, HasGenericParams, HasName, HasTypeBounds, Whitespace,
},
ted, AstNode, Direction, SmolStr,
ted, AstNode, AstToken, Direction, SmolStr, SourceFile,
SyntaxKind::*,
SyntaxNode, TextSize, T,
SyntaxNode, TextRange, TextSize, T,
};
use crate::assist_context::{AssistBuilder, AssistContext};
@@ -500,3 +500,29 @@ pub(crate) fn get_methods(items: &ast::AssocItemList) -> Vec<ast::Fn> {
.filter(|f| f.name().is_some())
.collect()
}
/// Trim(remove leading and trailing whitespace) `initial_range` in `source_file`, return the trimmed range.
pub(crate) fn trimmed_text_range(source_file: &SourceFile, initial_range: TextRange) -> TextRange {
let mut trimmed_range = initial_range;
while source_file
.syntax()
.token_at_offset(trimmed_range.start())
.find_map(Whitespace::cast)
.is_some()
&& trimmed_range.start() < trimmed_range.end()
{
let start = trimmed_range.start() + TextSize::from(1);
trimmed_range = TextRange::new(start, trimmed_range.end());
}
while source_file
.syntax()
.token_at_offset(trimmed_range.end())
.find_map(Whitespace::cast)
.is_some()
&& trimmed_range.start() < trimmed_range.end()
{
let end = trimmed_range.end() - TextSize::from(1);
trimmed_range = TextRange::new(trimmed_range.start(), end);
}
trimmed_range
}