Rename File -> SourceFileNode

This commit is contained in:
Aleksey Kladov 2018-11-07 18:32:33 +03:00
parent 2119fe2143
commit f8b36bbc3b
23 changed files with 117 additions and 102 deletions

View file

@ -2,7 +2,7 @@ use ra_editor::find_node_at_offset;
use ra_syntax::{
algo::visit::{visitor, visitor_ctx, Visitor, VisitorCtx},
ast::{self, AstChildren, LoopBodyOwner, ModuleItemOwner},
AstNode, AtomEdit, File,
AstNode, AtomEdit, SourceFileNode,
SyntaxKind::*,
SyntaxNodeRef, TextUnit,
};
@ -63,7 +63,7 @@ pub(crate) fn resolve_based_completion(
pub(crate) fn find_target_module(
module_tree: &ModuleTree,
module_id: ModuleId,
file: &File,
file: &SourceFileNode,
offset: TextUnit,
) -> Option<ModuleId> {
let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), offset)?;
@ -142,7 +142,7 @@ pub(crate) fn scope_completion(
}
fn complete_module_items(
file: &File,
file: &SourceFileNode,
items: AstChildren<ast::ModuleItem>,
this_item: Option<ast::NameRef>,
acc: &mut Vec<CompletionItem>,
@ -164,7 +164,7 @@ fn complete_module_items(
);
}
fn complete_name_ref(file: &File, name_ref: ast::NameRef, acc: &mut Vec<CompletionItem>) {
fn complete_name_ref(file: &SourceFileNode, name_ref: ast::NameRef, acc: &mut Vec<CompletionItem>) {
if !is_node::<ast::Path>(name_ref.syntax()) {
return;
}
@ -239,7 +239,7 @@ fn is_node<'a, N: AstNode<'a>>(node: SyntaxNodeRef<'a>) -> bool {
}
fn complete_expr_keywords(
file: &File,
file: &SourceFileNode,
fn_def: ast::FnDef,
name_ref: ast::NameRef,
acc: &mut Vec<CompletionItem>,

View file

@ -1,7 +1,7 @@
use std::sync::Arc;
use ra_editor::LineIndex;
use ra_syntax::{File, SyntaxNode};
use ra_syntax::{SourceFileNode, SyntaxNode};
use salsa::{self, Database};
use crate::{
@ -85,7 +85,7 @@ salsa::database_storage! {
salsa::query_group! {
pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase {
fn file_syntax(file_id: FileId) -> File {
fn file_syntax(file_id: FileId) -> SourceFileNode {
type FileSyntaxQuery;
}
fn file_lines(file_id: FileId) -> Arc<LineIndex> {
@ -103,9 +103,9 @@ salsa::query_group! {
}
}
fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> File {
fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode {
let text = db.file_text(file_id);
File::parse(&*text)
SourceFileNode::parse(&*text)
}
fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
let text = db.file_text(file_id);

View file

@ -272,7 +272,7 @@ pub fn resolve_local_name<'a>(
#[cfg(test)]
mod tests {
use ra_editor::find_node_at_offset;
use ra_syntax::File;
use ra_syntax::SourceFileNode;
use test_utils::extract_offset;
use super::*;
@ -287,7 +287,7 @@ mod tests {
buf.push_str(&code[off..]);
buf
};
let file = File::parse(&code);
let file = SourceFileNode::parse(&code);
let marker: ast::PathExpr = find_node_at_offset(file.syntax(), off).unwrap();
let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap();
let scopes = FnScopes::new(fn_def);
@ -376,7 +376,7 @@ mod tests {
fn do_check_local_name(code: &str, expected_offset: u32) {
let (off, code) = extract_offset(code);
let file = File::parse(&code);
let file = SourceFileNode::parse(&code);
let fn_def: ast::FnDef = find_node_at_offset(file.syntax(), off).unwrap();
let name_ref: ast::NameRef = find_node_at_offset(file.syntax(), off).unwrap();

View file

@ -95,10 +95,10 @@ fn collect_imports(tree: ast::UseTree, acc: &mut Vec<Entry>) {
#[cfg(test)]
mod tests {
use super::*;
use ra_syntax::{ast::ModuleItemOwner, File};
use ra_syntax::{ast::ModuleItemOwner, SourceFileNode};
fn do_check(code: &str, expected: &[&str]) {
let file = File::parse(&code);
let file = SourceFileNode::parse(&code);
let scope = ModuleScope::new(file.ast().items());
let actual = scope.entries.iter().map(|it| it.name()).collect::<Vec<_>>();
assert_eq!(expected, actual.as_slice());

View file

@ -7,7 +7,7 @@ use std::{
use ra_editor::{self, find_node_at_offset, FileSymbol, LineIndex, LocalEdit};
use ra_syntax::{
ast::{self, ArgListOwner, Expr, NameOwner},
AstNode, File, SmolStr,
AstNode, SourceFileNode, SmolStr,
SyntaxKind::*,
SyntaxNodeRef, TextRange, TextUnit,
};
@ -27,7 +27,7 @@ use crate::{
input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE},
symbol_index::SymbolIndex,
AnalysisChange, Cancelable, CrateGraph, CrateId, Diagnostic, FileId, FileResolver,
FileSystemEdit, FilePosition, Query, SourceChange, SourceFileEdit,
FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit,
};
#[derive(Clone, Debug)]
@ -180,7 +180,7 @@ impl fmt::Debug for AnalysisImpl {
}
impl AnalysisImpl {
pub fn file_syntax(&self, file_id: FileId) -> File {
pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode {
self.db.file_syntax(file_id)
}
pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
@ -562,7 +562,7 @@ impl AnalysisImpl {
impl SourceChange {
pub(crate) fn from_local_edit(file_id: FileId, label: &str, edit: LocalEdit) -> SourceChange {
let file_edit = SourceFileEdit {
let file_edit = SourceFileNodeEdit {
file_id,
edits: edit.edit.into_atoms(),
};

View file

@ -20,7 +20,7 @@ pub mod mock_analysis;
use std::{fmt, sync::Arc};
use ra_syntax::{AtomEdit, File, TextRange, TextUnit};
use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit};
use rayon::prelude::*;
use relative_path::RelativePathBuf;
@ -128,13 +128,13 @@ pub struct FilePosition {
#[derive(Debug)]
pub struct SourceChange {
pub label: String,
pub source_file_edits: Vec<SourceFileEdit>,
pub source_file_edits: Vec<SourceFileNodeEdit>,
pub file_system_edits: Vec<FileSystemEdit>,
pub cursor_position: Option<FilePosition>,
}
#[derive(Debug)]
pub struct SourceFileEdit {
pub struct SourceFileNodeEdit {
pub file_id: FileId,
pub edits: Vec<AtomEdit>,
}
@ -204,16 +204,16 @@ pub struct Analysis {
}
impl Analysis {
pub fn file_syntax(&self, file_id: FileId) -> File {
pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode {
self.imp.file_syntax(file_id).clone()
}
pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
self.imp.file_line_index(file_id)
}
pub fn extend_selection(&self, file: &File, range: TextRange) -> TextRange {
pub fn extend_selection(&self, file: &SourceFileNode, range: TextRange) -> TextRange {
ra_editor::extend_selection(file, range).unwrap_or(range)
}
pub fn matching_brace(&self, file: &File, offset: TextUnit) -> Option<TextUnit> {
pub fn matching_brace(&self, file: &SourceFileNode, offset: TextUnit) -> Option<TextUnit> {
ra_editor::matching_brace(file, offset)
}
pub fn syntax_tree(&self, file_id: FileId) -> String {
@ -309,7 +309,7 @@ pub struct LibraryData {
impl LibraryData {
pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData {
let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, text)| {
let file = File::parse(text);
let file = SourceFileNode::parse(text);
(*file_id, file)
}));
LibraryData {

View file

@ -6,7 +6,7 @@ use std::{
use fst::{self, Streamer};
use ra_editor::{file_symbols, FileSymbol};
use ra_syntax::{
File,
SourceFileNode,
SyntaxKind::{self, *},
};
use rayon::prelude::*;
@ -34,7 +34,9 @@ impl Hash for SymbolIndex {
}
impl SymbolIndex {
pub(crate) fn for_files(files: impl ParallelIterator<Item = (FileId, File)>) -> SymbolIndex {
pub(crate) fn for_files(
files: impl ParallelIterator<Item = (FileId, SourceFileNode)>,
) -> SymbolIndex {
let mut symbols = files
.flat_map(|(file_id, file)| {
file_symbols(&file)
@ -51,7 +53,7 @@ impl SymbolIndex {
SymbolIndex { symbols, map }
}
pub(crate) fn for_file(file_id: FileId, file: File) -> SymbolIndex {
pub(crate) fn for_file(file_id: FileId, file: SourceFileNode) -> SymbolIndex {
SymbolIndex::for_files(rayon::iter::once((file_id, file)))
}
}

View file

@ -1,4 +1,4 @@
use ra_syntax::{File, SyntaxKind, SyntaxNode, SyntaxNodeRef, TextRange};
use ra_syntax::{SourceFileNode, SyntaxKind, SyntaxNode, SyntaxNodeRef, TextRange};
use crate::db::SyntaxDatabase;
use crate::FileId;
@ -43,7 +43,7 @@ impl LocalSyntaxPtr {
}
}
pub(crate) fn resolve(self, file: &File) -> SyntaxNode {
pub(crate) fn resolve(self, file: &SourceFileNode) -> SyntaxNode {
let mut curr = file.syntax();
loop {
if curr.range() == self.range && curr.kind() == self.kind {
@ -67,7 +67,7 @@ impl LocalSyntaxPtr {
#[test]
fn test_local_syntax_ptr() {
use ra_syntax::{ast, AstNode};
let file = File::parse("struct Foo { f: u32, }");
let file = SourceFileNode::parse("struct Foo { f: u32, }");
let field = file
.syntax()
.descendants()

View file

@ -11,7 +11,7 @@ use std::{fs, io::Read, path::Path, time::Instant};
use clap::{App, Arg, SubCommand};
use join_to_string::join;
use ra_editor::{extend_selection, file_structure, syntax_tree};
use ra_syntax::{File, TextRange};
use ra_syntax::{SourceFileNode, TextRange};
use tools::collect_tests;
type Result<T> = ::std::result::Result<T, failure::Error>;
@ -79,9 +79,9 @@ fn main() -> Result<()> {
Ok(())
}
fn file() -> Result<File> {
fn file() -> Result<SourceFileNode> {
let text = read_stdin()?;
Ok(File::parse(&text))
Ok(SourceFileNode::parse(&text))
}
fn read_stdin() -> Result<String> {
@ -100,12 +100,12 @@ fn render_test(file: &Path, line: usize) -> Result<(String, String)> {
None => bail!("No test found at line {} at {}", line, file.display()),
Some((_start_line, test)) => test,
};
let file = File::parse(&test.text);
let file = SourceFileNode::parse(&test.text);
let tree = syntax_tree(&file);
Ok((test.text, tree))
}
fn selections(file: &File, start: u32, end: u32) -> String {
fn selections(file: &SourceFileNode, start: u32, end: u32) -> String {
let mut ranges = Vec::new();
let mut cur = Some(TextRange::from_to((start - 1).into(), (end - 1).into()));
while let Some(r) = cur {

View file

@ -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);

View file

@ -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();

View file

@ -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!(

View file

@ -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,

View file

@ -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

View file

@ -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 {

View file

@ -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());

View file

@ -2,7 +2,7 @@ use languageserver_types::{
Location, Position, Range, SymbolKind, TextDocumentEdit, TextDocumentIdentifier,
TextDocumentItem, TextDocumentPositionParams, TextEdit, Url, VersionedTextDocumentIdentifier,
};
use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileEdit, FilePosition};
use ra_analysis::{FileId, FileSystemEdit, SourceChange, SourceFileNodeEdit, FilePosition};
use ra_editor::{AtomEdit, Edit, LineCol, LineIndex};
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
@ -257,7 +257,7 @@ fn translate_offset_with_edit(
}
}
impl TryConvWith for SourceFileEdit {
impl TryConvWith for SourceFileNodeEdit {
type Ctx = ServerWorld;
type Output = TextDocumentEdit;
fn try_conv_with(self, world: &ServerWorld) -> Result<TextDocumentEdit> {

View file

@ -61,12 +61,12 @@ pub use crate::{
use crate::yellow::GreenNode;
// TODO: pick a single name for everything. SourceFile maybe?
// TODO: pick a single name for everything. SourceFileNode maybe?
/// File represents a parse tree for a single Rust file.
pub type File = ast::RootNode;
pub type SourceFileNode = ast::RootNode;
impl File {
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> File {
impl SourceFileNode {
fn new(green: GreenNode, errors: Vec<SyntaxError>) -> SourceFileNode {
let root = SyntaxNode::new(green, errors);
if cfg!(debug_assertions) {
utils::validate_block_structure(root.borrowed());
@ -74,24 +74,24 @@ impl File {
assert_eq!(root.kind(), SyntaxKind::ROOT);
ast::RootNode { syntax: root }
}
pub fn parse(text: &str) -> File {
pub fn parse(text: &str) -> SourceFileNode {
let tokens = tokenize(&text);
let (green, errors) =
parser_impl::parse_with(yellow::GreenBuilder::new(), text, &tokens, grammar::root);
File::new(green, errors)
SourceFileNode::new(green, errors)
}
pub fn reparse(&self, edit: &AtomEdit) -> File {
pub fn reparse(&self, edit: &AtomEdit) -> SourceFileNode {
self.incremental_reparse(edit)
.unwrap_or_else(|| self.full_reparse(edit))
}
pub fn incremental_reparse(&self, edit: &AtomEdit) -> Option<File> {
pub fn incremental_reparse(&self, edit: &AtomEdit) -> Option<SourceFileNode> {
reparsing::incremental_reparse(self.syntax(), edit, self.errors())
.map(|(green_node, errors)| File::new(green_node, errors))
.map(|(green_node, errors)| SourceFileNode::new(green_node, errors))
}
fn full_reparse(&self, edit: &AtomEdit) -> File {
fn full_reparse(&self, edit: &AtomEdit) -> SourceFileNode {
let text =
text_utils::replace_range(self.syntax().text().to_string(), edit.delete, &edit.insert);
File::parse(&text)
SourceFileNode::parse(&text)
}
/// Typed AST representation of the parse tree.
pub fn ast(&self) -> ast::Root {

View file

@ -180,7 +180,7 @@ fn merge_errors(
#[cfg(test)]
mod tests {
use super::{
super::{test_utils::extract_range, text_utils::replace_range, utils::dump_tree, File},
super::{test_utils::extract_range, text_utils::replace_range, utils::dump_tree, SourceFileNode},
reparse_block, reparse_leaf, AtomEdit, GreenNode, SyntaxError, SyntaxNodeRef,
};
@ -192,9 +192,9 @@ mod tests {
let (range, before) = extract_range(before);
let after = replace_range(before.clone(), range, replace_with);
let fully_reparsed = File::parse(&after);
let fully_reparsed = SourceFileNode::parse(&after);
let incrementally_reparsed = {
let f = File::parse(&before);
let f = SourceFileNode::parse(&before);
let edit = AtomEdit {
delete: range,
insert: replace_with.to_string(),
@ -203,7 +203,7 @@ mod tests {
reparser(f.syntax(), &edit).expect("cannot incrementally reparse");
let green_root = node.replace_with(green);
let errors = super::merge_errors(f.errors(), new_errors, node, &edit);
File::new(green_root, errors)
SourceFileNode::new(green_root, errors)
};
assert_eq_text!(

View file

@ -1,4 +1,4 @@
use crate::{File, SyntaxKind, SyntaxNodeRef, WalkEvent};
use crate::{SourceFileNode, SyntaxKind, SyntaxNodeRef, WalkEvent};
use std::fmt::Write;
use std::str;
@ -45,7 +45,7 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
}
pub fn check_fuzz_invariants(text: &str) {
let file = File::parse(text);
let file = SourceFileNode::parse(text);
let root = file.syntax();
validate_block_structure(root);
let _ = file.ast();

View file

@ -5,7 +5,7 @@ use arrayvec::ArrayString;
use crate::{
algo::visit::{visitor_ctx, VisitorCtx},
ast::{self, AstNode},
File,
SourceFileNode,
string_lexing::{self, CharComponentKind},
yellow::{
SyntaxError,
@ -13,7 +13,7 @@ use crate::{
},
};
pub(crate) fn validate(file: &File) -> Vec<SyntaxError> {
pub(crate) fn validate(file: &SourceFileNode) -> Vec<SyntaxError> {
let mut errors = Vec::new();
for node in file.syntax().descendants() {
let _ = visitor_ctx(&mut errors)
@ -155,11 +155,11 @@ fn is_ascii_escape(code: char) -> bool {
#[cfg(test)]
mod test {
use crate::File;
use crate::SourceFileNode;
fn build_file(literal: &str) -> File {
fn build_file(literal: &str) -> SourceFileNode {
let src = format!("const C: char = '{}';", literal);
File::parse(&src)
SourceFileNode::parse(&src)
}
fn assert_valid_char(literal: &str) {

View file

@ -11,7 +11,7 @@ use std::{
use ra_syntax::{
utils::{check_fuzz_invariants, dump_tree},
File,
SourceFileNode,
};
#[test]
@ -25,7 +25,7 @@ fn lexer_tests() {
#[test]
fn parser_tests() {
dir_tests(&["parser/inline", "parser/ok", "parser/err"], |text| {
let file = File::parse(text);
let file = SourceFileNode::parse(text);
dump_tree(file.syntax())
})
}

View file

@ -12,14 +12,14 @@ interface FileSystemEdit {
export interface SourceChange {
label: string;
sourceFileEdits: lc.TextDocumentEdit[];
SourceFileNodeEdits: lc.TextDocumentEdit[];
fileSystemEdits: FileSystemEdit[];
cursorPosition?: lc.TextDocumentPositionParams;
}
export async function handle(change: SourceChange) {
const wsEdit = new vscode.WorkspaceEdit();
for (const sourceEdit of change.sourceFileEdits) {
for (const sourceEdit of change.SourceFileNodeEdits) {
const uri = Server.client.protocol2CodeConverter.asUri(
sourceEdit.textDocument.uri
);