mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
Rename File -> SourceFileNode
This commit is contained in:
parent
2119fe2143
commit
f8b36bbc3b
23 changed files with 117 additions and 102 deletions
|
@ -3,7 +3,7 @@ use join_to_string::join;
|
|||
use ra_syntax::{
|
||||
algo::{find_covering_node, find_leaf_at_offset},
|
||||
ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
|
||||
Direction, File,
|
||||
Direction, SourceFileNode,
|
||||
SyntaxKind::{COMMA, WHITESPACE},
|
||||
SyntaxNodeRef, TextRange, TextUnit,
|
||||
};
|
||||
|
@ -16,7 +16,10 @@ pub struct LocalEdit {
|
|||
pub cursor_position: Option<TextUnit>,
|
||||
}
|
||||
|
||||
pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
||||
pub fn flip_comma<'a>(
|
||||
file: &'a SourceFileNode,
|
||||
offset: TextUnit,
|
||||
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
||||
let syntax = file.syntax();
|
||||
|
||||
let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
|
||||
|
@ -33,7 +36,10 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce()
|
|||
})
|
||||
}
|
||||
|
||||
pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
||||
pub fn add_derive<'a>(
|
||||
file: &'a SourceFileNode,
|
||||
offset: TextUnit,
|
||||
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
||||
let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
|
||||
Some(move || {
|
||||
let derive_attr = nominal
|
||||
|
@ -58,7 +64,10 @@ pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce()
|
|||
})
|
||||
}
|
||||
|
||||
pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
||||
pub fn add_impl<'a>(
|
||||
file: &'a SourceFileNode,
|
||||
offset: TextUnit,
|
||||
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
||||
let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
|
||||
let name = nominal.name()?;
|
||||
|
||||
|
@ -98,7 +107,7 @@ pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() ->
|
|||
}
|
||||
|
||||
pub fn introduce_variable<'a>(
|
||||
file: &'a File,
|
||||
file: &'a SourceFileNode,
|
||||
range: TextRange,
|
||||
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
||||
let node = find_covering_node(file.syntax(), range);
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
use ra_syntax::{
|
||||
algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset},
|
||||
Direction, File,
|
||||
Direction, SourceFileNode,
|
||||
SyntaxKind::*,
|
||||
SyntaxNodeRef, TextRange, TextUnit,
|
||||
};
|
||||
|
||||
pub fn extend_selection(file: &File, range: TextRange) -> Option<TextRange> {
|
||||
pub fn extend_selection(file: &SourceFileNode, range: TextRange) -> Option<TextRange> {
|
||||
let syntax = file.syntax();
|
||||
extend(syntax.borrowed(), range)
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ mod tests {
|
|||
|
||||
fn do_check(before: &str, afters: &[&str]) {
|
||||
let (cursor, before) = extract_offset(before);
|
||||
let file = File::parse(&before);
|
||||
let file = SourceFileNode::parse(&before);
|
||||
let mut range = TextRange::offset_len(cursor, 0.into());
|
||||
for &after in afters {
|
||||
range = extend_selection(&file, range).unwrap();
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use rustc_hash::FxHashSet;
|
||||
|
||||
use ra_syntax::{
|
||||
ast, AstNode, Direction, File,
|
||||
ast, AstNode, Direction, SourceFileNode,
|
||||
SyntaxKind::{self, *},
|
||||
SyntaxNodeRef, TextRange,
|
||||
};
|
||||
|
@ -18,7 +18,7 @@ pub struct Fold {
|
|||
pub kind: FoldKind,
|
||||
}
|
||||
|
||||
pub fn folding_ranges(file: &File) -> Vec<Fold> {
|
||||
pub fn folding_ranges(file: &SourceFileNode) -> Vec<Fold> {
|
||||
let mut res = vec![];
|
||||
let mut visited_comments = FxHashSet::default();
|
||||
let mut visited_imports = FxHashSet::default();
|
||||
|
@ -171,7 +171,7 @@ mod tests {
|
|||
|
||||
fn do_check(text: &str, fold_kinds: &[FoldKind]) {
|
||||
let (ranges, text) = extract_ranges(text);
|
||||
let file = File::parse(&text);
|
||||
let file = SourceFileNode::parse(&text);
|
||||
let folds = folding_ranges(&file);
|
||||
|
||||
assert_eq!(
|
||||
|
|
|
@ -30,7 +30,7 @@ pub use ra_syntax::AtomEdit;
|
|||
use ra_syntax::{
|
||||
algo::find_leaf_at_offset,
|
||||
ast::{self, AstNode, NameOwner},
|
||||
File,
|
||||
SourceFileNode,
|
||||
Location,
|
||||
SyntaxKind::{self, *},
|
||||
SyntaxNodeRef, TextRange, TextUnit,
|
||||
|
@ -60,7 +60,7 @@ pub enum RunnableKind {
|
|||
Bin,
|
||||
}
|
||||
|
||||
pub fn matching_brace(file: &File, offset: TextUnit) -> Option<TextUnit> {
|
||||
pub fn matching_brace(file: &SourceFileNode, offset: TextUnit) -> Option<TextUnit> {
|
||||
const BRACES: &[SyntaxKind] = &[
|
||||
L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE,
|
||||
];
|
||||
|
@ -78,7 +78,7 @@ pub fn matching_brace(file: &File, offset: TextUnit) -> Option<TextUnit> {
|
|||
Some(matching_node.range().start())
|
||||
}
|
||||
|
||||
pub fn highlight(file: &File) -> Vec<HighlightedRange> {
|
||||
pub fn highlight(file: &SourceFileNode) -> Vec<HighlightedRange> {
|
||||
let mut res = Vec::new();
|
||||
for node in file.syntax().descendants() {
|
||||
let tag = match node.kind() {
|
||||
|
@ -100,7 +100,7 @@ pub fn highlight(file: &File) -> Vec<HighlightedRange> {
|
|||
res
|
||||
}
|
||||
|
||||
pub fn diagnostics(file: &File) -> Vec<Diagnostic> {
|
||||
pub fn diagnostics(file: &SourceFileNode) -> Vec<Diagnostic> {
|
||||
fn location_to_range(location: Location) -> TextRange {
|
||||
match location {
|
||||
Location::Offset(offset) => TextRange::offset_len(offset, 1.into()),
|
||||
|
@ -117,11 +117,11 @@ pub fn diagnostics(file: &File) -> Vec<Diagnostic> {
|
|||
.collect()
|
||||
}
|
||||
|
||||
pub fn syntax_tree(file: &File) -> String {
|
||||
pub fn syntax_tree(file: &SourceFileNode) -> String {
|
||||
::ra_syntax::utils::dump_tree(file.syntax())
|
||||
}
|
||||
|
||||
pub fn runnables(file: &File) -> Vec<Runnable> {
|
||||
pub fn runnables(file: &SourceFileNode) -> Vec<Runnable> {
|
||||
file.syntax()
|
||||
.descendants()
|
||||
.filter_map(ast::FnDef::cast)
|
||||
|
@ -163,7 +163,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_highlighting() {
|
||||
let file = File::parse(
|
||||
let file = SourceFileNode::parse(
|
||||
r#"
|
||||
// comment
|
||||
fn main() {}
|
||||
|
@ -184,7 +184,7 @@ fn main() {}
|
|||
|
||||
#[test]
|
||||
fn test_runnables() {
|
||||
let file = File::parse(
|
||||
let file = SourceFileNode::parse(
|
||||
r#"
|
||||
fn main() {}
|
||||
|
||||
|
@ -209,7 +209,7 @@ fn test_foo() {}
|
|||
fn test_matching_brace() {
|
||||
fn do_check(before: &str, after: &str) {
|
||||
let (pos, before) = extract_offset(before);
|
||||
let file = File::parse(&before);
|
||||
let file = SourceFileNode::parse(&before);
|
||||
let new_pos = match matching_brace(&file, pos) {
|
||||
None => pos,
|
||||
Some(pos) => pos,
|
||||
|
|
|
@ -3,7 +3,7 @@ use crate::TextRange;
|
|||
use ra_syntax::{
|
||||
algo::visit::{visitor, Visitor},
|
||||
ast::{self, DocCommentsOwner, NameOwner},
|
||||
AstNode, File, SmolStr, SyntaxKind, SyntaxNodeRef, WalkEvent,
|
||||
AstNode, SourceFileNode, SmolStr, SyntaxKind, SyntaxNodeRef, WalkEvent,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -23,7 +23,7 @@ pub struct FileSymbol {
|
|||
}
|
||||
|
||||
impl FileSymbol {
|
||||
pub fn docs(&self, file: &File) -> Option<String> {
|
||||
pub fn docs(&self, file: &SourceFileNode) -> Option<String> {
|
||||
file.syntax()
|
||||
.descendants()
|
||||
.filter(|node| node.kind() == self.kind && node.range() == self.node_range)
|
||||
|
@ -52,7 +52,7 @@ impl FileSymbol {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn file_symbols(file: &File) -> Vec<FileSymbol> {
|
||||
pub fn file_symbols(file: &SourceFileNode) -> Vec<FileSymbol> {
|
||||
file.syntax().descendants().filter_map(to_symbol).collect()
|
||||
}
|
||||
|
||||
|
@ -77,7 +77,7 @@ fn to_symbol(node: SyntaxNodeRef) -> Option<FileSymbol> {
|
|||
.accept(node)?
|
||||
}
|
||||
|
||||
pub fn file_structure(file: &File) -> Vec<StructureNode> {
|
||||
pub fn file_structure(file: &SourceFileNode) -> Vec<StructureNode> {
|
||||
let mut res = Vec::new();
|
||||
let mut stack = Vec::new();
|
||||
|
||||
|
@ -153,7 +153,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_file_structure() {
|
||||
let file = File::parse(
|
||||
let file = SourceFileNode::parse(
|
||||
r#"
|
||||
struct Foo {
|
||||
x: i32
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
use crate::LocalEdit;
|
||||
pub use crate::_test_utils::*;
|
||||
use ra_syntax::{File, TextRange, TextUnit};
|
||||
use ra_syntax::{SourceFileNode, TextRange, TextUnit};
|
||||
|
||||
pub fn check_action<F: Fn(&File, TextUnit) -> Option<LocalEdit>>(before: &str, after: &str, f: F) {
|
||||
pub fn check_action<F: Fn(&SourceFileNode, TextUnit) -> Option<LocalEdit>>(
|
||||
before: &str,
|
||||
after: &str,
|
||||
f: F,
|
||||
) {
|
||||
let (before_cursor_pos, before) = extract_offset(before);
|
||||
let file = File::parse(&before);
|
||||
let file = SourceFileNode::parse(&before);
|
||||
let result = f(&file, before_cursor_pos).expect("code action is not applicable");
|
||||
let actual = result.edit.apply(&before);
|
||||
let actual_cursor_pos = match result.cursor_position {
|
||||
|
@ -15,13 +19,13 @@ pub fn check_action<F: Fn(&File, TextUnit) -> Option<LocalEdit>>(before: &str, a
|
|||
assert_eq_text!(after, &actual);
|
||||
}
|
||||
|
||||
pub fn check_action_range<F: Fn(&File, TextRange) -> Option<LocalEdit>>(
|
||||
pub fn check_action_range<F: Fn(&SourceFileNode, TextRange) -> Option<LocalEdit>>(
|
||||
before: &str,
|
||||
after: &str,
|
||||
f: F,
|
||||
) {
|
||||
let (range, before) = extract_range(before);
|
||||
let file = File::parse(&before);
|
||||
let file = SourceFileNode::parse(&before);
|
||||
let result = f(&file, range).expect("code action is not applicable");
|
||||
let actual = result.edit.apply(&before);
|
||||
let actual_cursor_pos = match result.cursor_position {
|
||||
|
|
|
@ -4,14 +4,14 @@ use ra_syntax::{
|
|||
algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset},
|
||||
ast,
|
||||
text_utils::{contains_offset_nonstrict, intersect},
|
||||
AstNode, File, SyntaxKind,
|
||||
AstNode, SourceFileNode, SyntaxKind,
|
||||
SyntaxKind::*,
|
||||
SyntaxNodeRef, TextRange, TextUnit,
|
||||
};
|
||||
|
||||
use crate::{find_node_at_offset, EditBuilder, LocalEdit};
|
||||
|
||||
pub fn join_lines(file: &File, range: TextRange) -> LocalEdit {
|
||||
pub fn join_lines(file: &SourceFileNode, range: TextRange) -> LocalEdit {
|
||||
let range = if range.is_empty() {
|
||||
let syntax = file.syntax();
|
||||
let text = syntax.text().slice(range.start()..);
|
||||
|
@ -55,7 +55,7 @@ pub fn join_lines(file: &File, range: TextRange) -> LocalEdit {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn on_enter(file: &File, offset: TextUnit) -> Option<LocalEdit> {
|
||||
pub fn on_enter(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> {
|
||||
let comment = find_leaf_at_offset(file.syntax(), offset)
|
||||
.left_biased()
|
||||
.and_then(ast::Comment::cast)?;
|
||||
|
@ -80,7 +80,7 @@ pub fn on_enter(file: &File, offset: TextUnit) -> Option<LocalEdit> {
|
|||
})
|
||||
}
|
||||
|
||||
fn node_indent<'a>(file: &'a File, node: SyntaxNodeRef) -> Option<&'a str> {
|
||||
fn node_indent<'a>(file: &'a SourceFileNode, node: SyntaxNodeRef) -> Option<&'a str> {
|
||||
let ws = match find_leaf_at_offset(file.syntax(), node.range().start()) {
|
||||
LeafAtOffset::Between(l, r) => {
|
||||
assert!(r == node);
|
||||
|
@ -100,7 +100,7 @@ fn node_indent<'a>(file: &'a File, node: SyntaxNodeRef) -> Option<&'a str> {
|
|||
Some(&text[pos..])
|
||||
}
|
||||
|
||||
pub fn on_eq_typed(file: &File, offset: TextUnit) -> Option<LocalEdit> {
|
||||
pub fn on_eq_typed(file: &SourceFileNode, offset: TextUnit) -> Option<LocalEdit> {
|
||||
let let_stmt: ast::LetStmt = find_node_at_offset(file.syntax(), offset)?;
|
||||
if let_stmt.has_semi() {
|
||||
return None;
|
||||
|
@ -390,7 +390,7 @@ fn foo() {
|
|||
|
||||
fn check_join_lines_sel(before: &str, after: &str) {
|
||||
let (sel, before) = extract_range(before);
|
||||
let file = File::parse(&before);
|
||||
let file = SourceFileNode::parse(&before);
|
||||
let result = join_lines(&file, sel);
|
||||
let actual = result.edit.apply(&before);
|
||||
assert_eq_text!(after, &actual);
|
||||
|
@ -469,7 +469,7 @@ pub fn handle_find_matching_brace() {
|
|||
fn test_on_eq_typed() {
|
||||
fn do_check(before: &str, after: &str) {
|
||||
let (offset, before) = extract_offset(before);
|
||||
let file = File::parse(&before);
|
||||
let file = SourceFileNode::parse(&before);
|
||||
let result = on_eq_typed(&file, offset).unwrap();
|
||||
let actual = result.edit.apply(&before);
|
||||
assert_eq_text!(after, &actual);
|
||||
|
@ -513,7 +513,7 @@ fn foo() {
|
|||
fn test_on_enter() {
|
||||
fn apply_on_enter(before: &str) -> Option<String> {
|
||||
let (offset, before) = extract_offset(before);
|
||||
let file = File::parse(&before);
|
||||
let file = SourceFileNode::parse(&before);
|
||||
let result = on_enter(&file, offset)?;
|
||||
let actual = result.edit.apply(&before);
|
||||
let actual = add_cursor(&actual, result.cursor_position.unwrap());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue