1524: make Parse fields private r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-07-12 16:42:06 +00:00
commit 8bb81d7418
31 changed files with 109 additions and 99 deletions

View File

@ -71,11 +71,11 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
where where
F: FnOnce(AssistCtx<DB>) -> T, F: FnOnce(AssistCtx<DB>) -> T,
{ {
let source_file = &db.parse(frange.file_id).tree; let parse = db.parse(frange.file_id);
let assist = let assist =
if should_compute_edit { Assist::Resolved(vec![]) } else { Assist::Unresolved(vec![]) }; if should_compute_edit { Assist::Resolved(vec![]) } else { Assist::Unresolved(vec![]) };
let ctx = AssistCtx { db, frange, source_file, should_compute_edit, assist }; let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit, assist };
f(ctx) f(ctx)
} }

View File

@ -278,8 +278,8 @@ impl AstBuilder<ast::NameRef> {
} }
fn ast_node_from_file_text<N: AstNode>(text: &str) -> TreeArc<N> { fn ast_node_from_file_text<N: AstNode>(text: &str) -> TreeArc<N> {
let file = SourceFile::parse(text).tree; let parse = SourceFile::parse(text);
let res = file.syntax().descendants().find_map(N::cast).unwrap().to_owned(); let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap().to_owned();
res res
} }
@ -287,7 +287,8 @@ mod tokens {
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use ra_syntax::{AstNode, SourceFile, SyntaxKind::*, SyntaxToken, TreeArc, T}; use ra_syntax::{AstNode, SourceFile, SyntaxKind::*, SyntaxToken, TreeArc, T};
static SOURCE_FILE: Lazy<TreeArc<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;").tree); static SOURCE_FILE: Lazy<TreeArc<SourceFile>> =
Lazy::new(|| SourceFile::parse(",\n; ;").tree().to_owned());
pub(crate) fn comma() -> SyntaxToken<'static> { pub(crate) fn comma() -> SyntaxToken<'static> {
SOURCE_FILE SOURCE_FILE

View File

@ -102,7 +102,7 @@ fn main() -> Result<()> {
fn file() -> Result<TreeArc<SourceFile>> { fn file() -> Result<TreeArc<SourceFile>> {
let text = read_stdin()?; let text = read_stdin()?;
Ok(SourceFile::parse(&text).tree) Ok(SourceFile::parse(&text).tree().to_owned())
} }
fn read_stdin() -> Result<String> { fn read_stdin() -> Result<String> {

View File

@ -167,7 +167,7 @@ impl ModuleSource {
) -> ModuleSource { ) -> ModuleSource {
match (file_id, decl_id) { match (file_id, decl_id) {
(Some(file_id), _) => { (Some(file_id), _) => {
let source_file = db.parse(file_id).tree; let source_file = db.parse(file_id).tree().to_owned();
ModuleSource::SourceFile(source_file) ModuleSource::SourceFile(source_file)
} }
(None, Some(item_id)) => { (None, Some(item_id)) => {

View File

@ -71,7 +71,8 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
} }
let source_map = self.func.body_source_map(db); let source_map = self.func.body_source_map(db);
let file_id = self.func.source(db).file_id; let file_id = self.func.source(db).file_id;
let source_file = db.parse(file_id.original_file(db)).tree; let parse = db.parse(file_id.original_file(db));
let source_file = parse.tree();
if let Some(field_list_node) = source_map if let Some(field_list_node) = source_map
.expr_syntax(id) .expr_syntax(id)
.map(|ptr| ptr.to_node(source_file.syntax())) .map(|ptr| ptr.to_node(source_file.syntax()))

View File

@ -60,7 +60,7 @@ impl HirFileId {
file_id: HirFileId, file_id: HirFileId,
) -> Option<TreeArc<SyntaxNode>> { ) -> Option<TreeArc<SyntaxNode>> {
match file_id.0 { match file_id.0 {
HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree.syntax().to_owned()), HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree().syntax().to_owned()),
HirFileIdRepr::Macro(macro_file) => db.parse_macro(macro_file), HirFileIdRepr::Macro(macro_file) => db.parse_macro(macro_file),
} }
} }

View File

@ -49,8 +49,8 @@ pub fn module_from_declaration(
/// Locates the module by position in the source code. /// Locates the module by position in the source code.
pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Module> { pub fn module_from_position(db: &impl HirDatabase, position: FilePosition) -> Option<Module> {
let file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) { match find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) {
Some(m) if !m.has_semi() => module_from_inline(db, position.file_id, m), Some(m) if !m.has_semi() => module_from_inline(db, position.file_id, m),
_ => module_from_file_id(db, position.file_id), _ => module_from_file_id(db, position.file_id),
} }

View File

@ -10,8 +10,8 @@ use crate::{db::RootDatabase, CallInfo, FilePosition, FunctionSignature};
/// Computes parameter information for the given call expression. /// Computes parameter information for the given call expression.
pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> { pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
let file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
let syntax = file.syntax(); let syntax = parse.tree().syntax();
// Find the calling expression and it's NameRef // Find the calling expression and it's NameRef
let calling_node = FnCallNode::with_node(syntax, position.offset)?; let calling_node = FnCallNode::with_node(syntax, position.offset)?;

View File

@ -135,8 +135,8 @@ impl LibraryData {
files: Vec<(FileId, RelativePathBuf, Arc<String>)>, files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
) -> LibraryData { ) -> LibraryData {
let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| { let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
let file = SourceFile::parse(text).tree; let parse = SourceFile::parse(text);
(*file_id, file) (*file_id, parse)
})); }));
let mut root_change = RootChange::default(); let mut root_change = RootChange::default();
root_change.added = files root_change.added = files

View File

@ -48,7 +48,7 @@ impl<'a> CompletionContext<'a> {
) -> Option<CompletionContext<'a>> { ) -> Option<CompletionContext<'a>> {
let module = source_binder::module_from_position(db, position); let module = source_binder::module_from_position(db, position);
let token = let token =
find_token_at_offset(original_parse.tree.syntax(), position.offset).left_biased()?; find_token_at_offset(original_parse.tree().syntax(), position.offset).left_biased()?;
let analyzer = let analyzer =
hir::SourceAnalyzer::new(db, position.file_id, token.parent(), Some(position.offset)); hir::SourceAnalyzer::new(db, position.file_id, token.parent(), Some(position.offset));
let mut ctx = CompletionContext { let mut ctx = CompletionContext {
@ -89,7 +89,7 @@ impl<'a> CompletionContext<'a> {
// actual completion. // actual completion.
let file = { let file = {
let edit = AtomTextEdit::insert(offset, "intellijRulezz".to_string()); let edit = AtomTextEdit::insert(offset, "intellijRulezz".to_string());
original_parse.reparse(&edit).tree original_parse.reparse(&edit).tree().to_owned()
}; };
// First, let's try to complete a reference to some declaration. // First, let's try to complete a reference to some declaration.
@ -100,7 +100,7 @@ impl<'a> CompletionContext<'a> {
self.is_param = true; self.is_param = true;
return; return;
} }
self.classify_name_ref(&original_parse.tree, name_ref); self.classify_name_ref(original_parse.tree(), name_ref);
} }
// Otherwise, see if this is a declaration. We can use heuristics to // Otherwise, see if this is a declaration. We can use heuristics to

View File

@ -27,14 +27,14 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
let parse = db.parse(file_id); let parse = db.parse(file_id);
let mut res = Vec::new(); let mut res = Vec::new();
res.extend(parse.errors.iter().map(|err| Diagnostic { res.extend(parse.errors().iter().map(|err| Diagnostic {
range: location_to_range(err.location()), range: location_to_range(err.location()),
message: format!("Syntax Error: {}", err), message: format!("Syntax Error: {}", err),
severity: Severity::Error, severity: Severity::Error,
fix: None, fix: None,
})); }));
for node in parse.tree.syntax().descendants() { for node in parse.tree().syntax().descendants() {
check_unnecessary_braces_in_use_statement(&mut res, file_id, node); check_unnecessary_braces_in_use_statement(&mut res, file_id, node);
check_struct_shorthand_initialization(&mut res, file_id, node); check_struct_shorthand_initialization(&mut res, file_id, node);
} }
@ -181,18 +181,18 @@ mod tests {
type DiagnosticChecker = fn(&mut Vec<Diagnostic>, FileId, &SyntaxNode) -> Option<()>; type DiagnosticChecker = fn(&mut Vec<Diagnostic>, FileId, &SyntaxNode) -> Option<()>;
fn check_not_applicable(code: &str, func: DiagnosticChecker) { fn check_not_applicable(code: &str, func: DiagnosticChecker) {
let file = SourceFile::parse(code).tree; let parse = SourceFile::parse(code);
let mut diagnostics = Vec::new(); let mut diagnostics = Vec::new();
for node in file.syntax().descendants() { for node in parse.tree().syntax().descendants() {
func(&mut diagnostics, FileId(0), node); func(&mut diagnostics, FileId(0), node);
} }
assert!(diagnostics.is_empty()); assert!(diagnostics.is_empty());
} }
fn check_apply(before: &str, after: &str, func: DiagnosticChecker) { fn check_apply(before: &str, after: &str, func: DiagnosticChecker) {
let file = SourceFile::parse(before).tree; let parse = SourceFile::parse(before);
let mut diagnostics = Vec::new(); let mut diagnostics = Vec::new();
for node in file.syntax().descendants() { for node in parse.tree().syntax().descendants() {
func(&mut diagnostics, FileId(0), node); func(&mut diagnostics, FileId(0), node);
} }
let diagnostic = let diagnostic =

View File

@ -93,8 +93,8 @@ impl NavigationTarget {
file_id: FileId, file_id: FileId,
pat: AstPtr<ast::Pat>, pat: AstPtr<ast::Pat>,
) -> NavigationTarget { ) -> NavigationTarget {
let file = db.parse(file_id).tree; let parse = db.parse(file_id);
let (name, full_range) = match pat.to_node(file.syntax()).kind() { let (name, full_range) = match pat.to_node(parse.tree().syntax()).kind() {
ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat), ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat),
_ => ("_".into(), pat.syntax_node_ptr().range()), _ => ("_".into(), pat.syntax_node_ptr().range()),
}; };
@ -315,8 +315,8 @@ impl NavigationTarget {
} }
pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
let file = db.parse(symbol.file_id).tree; let parse = db.parse(symbol.file_id);
let node = symbol.ptr.to_node(file.syntax()).to_owned(); let node = symbol.ptr.to_node(parse.tree().syntax()).to_owned();
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> { fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
node.doc_comment_text() node.doc_comment_text()
@ -341,8 +341,8 @@ pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option
/// ///
/// e.g. `struct Name`, `enum Name`, `fn Name` /// e.g. `struct Name`, `enum Name`, `fn Name`
pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> { pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
let file = db.parse(symbol.file_id).tree; let parse = db.parse(symbol.file_id);
let node = symbol.ptr.to_node(file.syntax()).to_owned(); let node = symbol.ptr.to_node(parse.tree().syntax()).to_owned();
visitor() visitor()
.visit(|node: &ast::FnDef| node.short_label()) .visit(|node: &ast::FnDef| node.short_label())

View File

@ -11,8 +11,8 @@ use crate::{db::RootDatabase, FileRange};
// FIXME: restore macro support // FIXME: restore macro support
pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange { pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange {
let source_file = db.parse(frange.file_id).tree; let parse = db.parse(frange.file_id);
try_extend_selection(source_file.syntax(), frange.range).unwrap_or(frange.range) try_extend_selection(parse.tree().syntax(), frange.range).unwrap_or(frange.range)
} }
fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange> { fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange> {
@ -212,10 +212,10 @@ mod tests {
fn do_check(before: &str, afters: &[&str]) { fn do_check(before: &str, afters: &[&str]) {
let (cursor, before) = extract_offset(before); let (cursor, before) = extract_offset(before);
let file = SourceFile::parse(&before).tree; let parse = SourceFile::parse(&before);
let mut range = TextRange::offset_len(cursor, 0.into()); let mut range = TextRange::offset_len(cursor, 0.into());
for &after in afters { for &after in afters {
range = try_extend_selection(file.syntax(), range).unwrap(); range = try_extend_selection(parse.tree().syntax(), range).unwrap();
let actual = &before[range]; let actual = &before[range];
assert_eq!(after, actual); assert_eq!(after, actual);
} }

View File

@ -192,8 +192,8 @@ mod tests {
fn do_check(text: &str, fold_kinds: &[FoldKind]) { fn do_check(text: &str, fold_kinds: &[FoldKind]) {
let (ranges, text) = extract_ranges(text, "fold"); let (ranges, text) = extract_ranges(text, "fold");
let file = SourceFile::parse(&text).tree; let parse = SourceFile::parse(&text);
let folds = folding_ranges(&file); let folds = folding_ranges(parse.tree());
assert_eq!( assert_eq!(
folds.len(), folds.len(),

View File

@ -19,8 +19,8 @@ pub(crate) fn goto_definition(
db: &RootDatabase, db: &RootDatabase,
position: FilePosition, position: FilePosition,
) -> Option<RangeInfo<Vec<NavigationTarget>>> { ) -> Option<RangeInfo<Vec<NavigationTarget>>> {
let file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
let syntax = file.syntax(); let syntax = parse.tree().syntax();
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) { if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
let navs = reference_definition(db, position.file_id, name_ref).to_vec(); let navs = reference_definition(db, position.file_id, name_ref).to_vec();
return Some(RangeInfo::new(name_ref.syntax().range(), navs.to_vec())); return Some(RangeInfo::new(name_ref.syntax().range(), navs.to_vec()));

View File

@ -7,9 +7,9 @@ pub(crate) fn goto_type_definition(
db: &RootDatabase, db: &RootDatabase,
position: FilePosition, position: FilePosition,
) -> Option<RangeInfo<Vec<NavigationTarget>>> { ) -> Option<RangeInfo<Vec<NavigationTarget>>> {
let file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
let node = find_token_at_offset(file.syntax(), position.offset).find_map(|token| { let node = find_token_at_offset(parse.tree().syntax(), position.offset).find_map(|token| {
token token
.parent() .parent()
.ancestors() .ancestors()

View File

@ -94,7 +94,8 @@ fn hover_text(docs: Option<String>, desc: Option<String>) -> Option<String> {
} }
pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<HoverResult>> { pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<HoverResult>> {
let file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
let file = parse.tree();
let mut res = HoverResult::new(); let mut res = HoverResult::new();
let mut range = None; let mut range = None;
@ -241,8 +242,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
} }
pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> { pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
let file = db.parse(frange.file_id).tree; let parse = db.parse(frange.file_id);
let syntax = file.syntax(); let syntax = parse.tree().syntax();
let leaf_node = find_covering_element(syntax, frange.range); let leaf_node = find_covering_element(syntax, frange.range);
// if we picked identifier, expand to pattern/expression // if we picked identifier, expand to pattern/expression
let node = leaf_node let node = leaf_node

View File

@ -8,8 +8,8 @@ pub(crate) fn goto_implementation(
db: &RootDatabase, db: &RootDatabase,
position: FilePosition, position: FilePosition,
) -> Option<RangeInfo<Vec<NavigationTarget>>> { ) -> Option<RangeInfo<Vec<NavigationTarget>>> {
let file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
let syntax = file.syntax(); let syntax = parse.tree().syntax();
let module = source_binder::module_from_position(db, position)?; let module = source_binder::module_from_position(db, position)?;

View File

@ -503,8 +503,8 @@ fn foo() {
fn check_join_lines_sel(before: &str, after: &str) { fn check_join_lines_sel(before: &str, after: &str) {
let (sel, before) = extract_range(before); let (sel, before) = extract_range(before);
let file = SourceFile::parse(&before).tree; let parse = SourceFile::parse(&before);
let result = join_lines(&file, sel); let result = join_lines(parse.tree(), sel);
let actual = result.apply(&before); let actual = result.apply(&before);
assert_eq_text!(after, &actual); assert_eq_text!(after, &actual);
} }

View File

@ -326,7 +326,7 @@ impl Analysis {
/// Gets the syntax tree of the file. /// Gets the syntax tree of the file.
pub fn parse(&self, file_id: FileId) -> TreeArc<SourceFile> { pub fn parse(&self, file_id: FileId) -> TreeArc<SourceFile> {
self.db.parse(file_id).tree self.db.parse(file_id).tree().to_owned()
} }
/// Gets the file's `LineIndex`: data structure to convert between absolute /// Gets the file's `LineIndex`: data structure to convert between absolute
@ -343,7 +343,8 @@ impl Analysis {
/// Returns position of the matching brace (all types of braces are /// Returns position of the matching brace (all types of braces are
/// supported). /// supported).
pub fn matching_brace(&self, position: FilePosition) -> Option<TextUnit> { pub fn matching_brace(&self, position: FilePosition) -> Option<TextUnit> {
let file = self.db.parse(position.file_id).tree; let parse = self.db.parse(position.file_id);
let file = parse.tree();
matching_brace::matching_brace(&file, position.offset) matching_brace::matching_brace(&file, position.offset)
} }
@ -356,10 +357,10 @@ impl Analysis {
/// Returns an edit to remove all newlines in the range, cleaning up minor /// Returns an edit to remove all newlines in the range, cleaning up minor
/// stuff like trailing commas. /// stuff like trailing commas.
pub fn join_lines(&self, frange: FileRange) -> SourceChange { pub fn join_lines(&self, frange: FileRange) -> SourceChange {
let file = self.db.parse(frange.file_id).tree; let parse = self.db.parse(frange.file_id);
let file_edit = SourceFileEdit { let file_edit = SourceFileEdit {
file_id: frange.file_id, file_id: frange.file_id,
edit: join_lines::join_lines(&file, frange.range), edit: join_lines::join_lines(parse.tree(), frange.range),
}; };
SourceChange::source_file_edit("join lines", file_edit) SourceChange::source_file_edit("join lines", file_edit)
} }
@ -374,7 +375,8 @@ impl Analysis {
/// this works when adding `let =`. /// this works when adding `let =`.
// FIXME: use a snippet completion instead of this hack here. // FIXME: use a snippet completion instead of this hack here.
pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> { pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> {
let file = self.db.parse(position.file_id).tree; let parse = self.db.parse(position.file_id);
let file = parse.tree();
let edit = typing::on_eq_typed(&file, position.offset)?; let edit = typing::on_eq_typed(&file, position.offset)?;
Some(SourceChange::source_file_edit( Some(SourceChange::source_file_edit(
"add semicolon", "add semicolon",
@ -390,14 +392,14 @@ impl Analysis {
/// Returns a tree representation of symbols in the file. Useful to draw a /// Returns a tree representation of symbols in the file. Useful to draw a
/// file outline. /// file outline.
pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> { pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> {
let file = self.db.parse(file_id).tree; let parse = self.db.parse(file_id);
file_structure(&file) file_structure(parse.tree())
} }
/// Returns the set of folding ranges. /// Returns the set of folding ranges.
pub fn folding_ranges(&self, file_id: FileId) -> Vec<Fold> { pub fn folding_ranges(&self, file_id: FileId) -> Vec<Fold> {
let file = self.db.parse(file_id).tree; let parse = self.db.parse(file_id);
folding_ranges::folding_ranges(&file) folding_ranges::folding_ranges(parse.tree())
} }
/// Fuzzy searches for a symbol. /// Fuzzy searches for a symbol.

View File

@ -25,8 +25,8 @@ mod tests {
fn test_matching_brace() { fn test_matching_brace() {
fn do_check(before: &str, after: &str) { fn do_check(before: &str, after: &str) {
let (pos, before) = extract_offset(before); let (pos, before) = extract_offset(before);
let file = SourceFile::parse(&before).tree; let parse = SourceFile::parse(&before);
let new_pos = match matching_brace(&file, pos) { let new_pos = match matching_brace(parse.tree(), pos) {
None => pos, None => pos,
Some(pos) => pos, Some(pos) => pos,
}; };

View File

@ -49,8 +49,8 @@ pub(crate) fn find_all_refs(
db: &RootDatabase, db: &RootDatabase,
position: FilePosition, position: FilePosition,
) -> Option<ReferenceSearchResult> { ) -> Option<ReferenceSearchResult> {
let file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
let (binding, analyzer) = find_binding(db, &file, position)?; let (binding, analyzer) = find_binding(db, parse.tree(), position)?;
let declaration = NavigationTarget::from_bind_pat(position.file_id, binding); let declaration = NavigationTarget::from_bind_pat(position.file_id, binding);
let references = analyzer let references = analyzer
@ -88,8 +88,8 @@ pub(crate) fn rename(
position: FilePosition, position: FilePosition,
new_name: &str, new_name: &str,
) -> Option<SourceChange> { ) -> Option<SourceChange> {
let source_file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
let syntax = source_file.syntax(); let syntax = parse.tree().syntax();
if let Some((ast_name, ast_module)) = find_name_and_module_at_offset(syntax, position) { if let Some((ast_name, ast_module)) = find_name_and_module_at_offset(syntax, position) {
rename_mod(db, ast_name, ast_module, position, new_name) rename_mod(db, ast_name, ast_module, position, new_name)

View File

@ -22,8 +22,8 @@ pub enum RunnableKind {
} }
pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> { pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
let source_file = db.parse(file_id).tree; let parse = db.parse(file_id);
source_file.syntax().descendants().filter_map(|i| runnable(db, file_id, i)).collect() parse.tree().syntax().descendants().filter_map(|i| runnable(db, file_id, i)).collect()
} }
fn runnable(db: &RootDatabase, file_id: FileId, item: &SyntaxNode) -> Option<Runnable> { fn runnable(db: &RootDatabase, file_id: FileId, item: &SyntaxNode) -> Option<Runnable> {

View File

@ -87,7 +87,7 @@ impl FromIterator<TableEntry<FileId, Parse>> for SyntaxTreeStats {
let mut res = SyntaxTreeStats::default(); let mut res = SyntaxTreeStats::default();
for entry in iter { for entry in iter {
res.total += 1; res.total += 1;
if let Some(tree) = entry.value.as_ref().map(|it| &it.tree) { if let Some(tree) = entry.value.as_ref().map(|it| it.tree()) {
res.retained += 1; res.retained += 1;
res.retained_size += tree.syntax().memory_size_of_subtree(); res.retained_size += tree.syntax().memory_size_of_subtree();
} }

View File

@ -34,9 +34,9 @@ use ra_db::{
use ra_syntax::{ use ra_syntax::{
algo::visit::{visitor, Visitor}, algo::visit::{visitor, Visitor},
ast::{self, NameOwner}, ast::{self, NameOwner},
AstNode, SmolStr, SourceFile, AstNode, Parse, SmolStr, SourceFile,
SyntaxKind::{self, *}, SyntaxKind::{self, *},
SyntaxNode, SyntaxNodePtr, TextRange, TreeArc, WalkEvent, SyntaxNode, SyntaxNodePtr, TextRange, WalkEvent,
}; };
use rayon::prelude::*; use rayon::prelude::*;
@ -59,9 +59,9 @@ pub(crate) trait SymbolsDatabase: hir::db::HirDatabase {
fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex> { fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Arc<SymbolIndex> {
db.check_canceled(); db.check_canceled();
let source_file = db.parse(file_id).tree; let parse = db.parse(file_id);
let symbols = source_file_to_file_symbols(&source_file, file_id); let symbols = source_file_to_file_symbols(parse.tree(), file_id);
// FIXME: add macros here // FIXME: add macros here
@ -169,11 +169,9 @@ impl SymbolIndex {
self.map.as_fst().size() + self.symbols.len() * mem::size_of::<FileSymbol>() self.map.as_fst().size() + self.symbols.len() * mem::size_of::<FileSymbol>()
} }
pub(crate) fn for_files( pub(crate) fn for_files(files: impl ParallelIterator<Item = (FileId, Parse)>) -> SymbolIndex {
files: impl ParallelIterator<Item = (FileId, TreeArc<SourceFile>)>,
) -> SymbolIndex {
let symbols = files let symbols = files
.flat_map(|(file_id, file)| source_file_to_file_symbols(&file, file_id)) .flat_map(|(file_id, file)| source_file_to_file_symbols(file.tree(), file_id))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
SymbolIndex::new(symbols) SymbolIndex::new(symbols)
} }

View File

@ -32,7 +32,8 @@ fn is_control_keyword(kind: SyntaxKind) -> bool {
pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> { pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRange> {
let _p = profile("highlight"); let _p = profile("highlight");
let source_file = db.parse(file_id).tree; let parse = db.parse(file_id);
let root = parse.tree().syntax();
fn calc_binding_hash(file_id: FileId, text: &SmolStr, shadow_count: u32) -> u64 { fn calc_binding_hash(file_id: FileId, text: &SmolStr, shadow_count: u32) -> u64 {
fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 { fn hash<T: std::hash::Hash + std::fmt::Debug>(x: T) -> u64 {
@ -51,7 +52,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
let mut bindings_shadow_count: FxHashMap<SmolStr, u32> = FxHashMap::default(); let mut bindings_shadow_count: FxHashMap<SmolStr, u32> = FxHashMap::default();
let mut res = Vec::new(); let mut res = Vec::new();
for node in source_file.syntax().descendants_with_tokens() { for node in root.descendants_with_tokens() {
if highlighted.contains(&node) { if highlighted.contains(&node) {
continue; continue;
} }
@ -89,11 +90,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
Some(SelfType(_)) => "type", Some(SelfType(_)) => "type",
Some(Pat(ptr)) => { Some(Pat(ptr)) => {
binding_hash = Some({ binding_hash = Some({
let text = ptr let text =
.syntax_node_ptr() ptr.syntax_node_ptr().to_node(root).text().to_smol_string();
.to_node(&source_file.syntax())
.text()
.to_smol_string();
let shadow_count = let shadow_count =
bindings_shadow_count.entry(text.clone()).or_default(); bindings_shadow_count.entry(text.clone()).or_default();
calc_binding_hash(file_id, &text, *shadow_count) calc_binding_hash(file_id, &text, *shadow_count)
@ -178,7 +176,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
} }
pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String { pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: bool) -> String {
let source_file = db.parse(file_id).tree; let parse = db.parse(file_id);
fn rainbowify(seed: u64) -> String { fn rainbowify(seed: u64) -> String {
use rand::prelude::*; use rand::prelude::*;
@ -200,7 +198,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo
let mut buf = String::new(); let mut buf = String::new();
buf.push_str(&STYLE); buf.push_str(&STYLE);
buf.push_str("<pre><code>"); buf.push_str("<pre><code>");
let tokens = source_file.syntax().descendants_with_tokens().filter_map(|it| it.as_token()); let tokens = parse.tree().syntax().descendants_with_tokens().filter_map(|it| it.as_token());
for token in tokens { for token in tokens {
could_intersect.retain(|it| token.range().start() <= it.range.end()); could_intersect.retain(|it| token.range().start() <= it.range.end());
while let Some(r) = ranges.get(frontier) { while let Some(r) = ranges.get(frontier) {

View File

@ -13,9 +13,9 @@ pub(crate) fn syntax_tree(
file_id: FileId, file_id: FileId,
text_range: Option<TextRange>, text_range: Option<TextRange>,
) -> String { ) -> String {
let parse = db.parse(file_id);
if let Some(text_range) = text_range { if let Some(text_range) = text_range {
let file = db.parse(file_id).tree; let node = match algo::find_covering_element(parse.tree().syntax(), text_range) {
let node = match algo::find_covering_element(file.syntax(), text_range) {
SyntaxElement::Node(node) => node, SyntaxElement::Node(node) => node,
SyntaxElement::Token(token) => { SyntaxElement::Token(token) => {
if let Some(tree) = syntax_tree_for_string(token, text_range) { if let Some(tree) = syntax_tree_for_string(token, text_range) {
@ -27,7 +27,7 @@ pub(crate) fn syntax_tree(
node.debug_dump() node.debug_dump()
} else { } else {
db.parse(file_id).tree.syntax().debug_dump() parse.tree().syntax().debug_dump()
} }
} }
@ -84,8 +84,8 @@ fn syntax_tree_for_token(node: SyntaxToken, text_range: TextRange) -> Option<Str
// If the "file" parsed without errors, // If the "file" parsed without errors,
// return its syntax // return its syntax
if parsed.errors.is_empty() { if parsed.errors().is_empty() {
return Some(parsed.tree.syntax().debug_dump()); return Some(parsed.tree().syntax().debug_dump());
} }
None None

View File

@ -11,7 +11,8 @@ use ra_syntax::{
use ra_text_edit::{TextEdit, TextEditBuilder}; use ra_text_edit::{TextEdit, TextEditBuilder};
pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> { pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
let file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
let file = parse.tree();
let comment = find_token_at_offset(file.syntax(), position.offset) let comment = find_token_at_offset(file.syntax(), position.offset)
.left_biased() .left_biased()
.and_then(ast::Comment::cast)?; .and_then(ast::Comment::cast)?;
@ -86,10 +87,10 @@ pub fn on_eq_typed(file: &SourceFile, eq_offset: TextUnit) -> Option<TextEdit> {
} }
pub(crate) fn on_dot_typed(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> { pub(crate) fn on_dot_typed(db: &RootDatabase, position: FilePosition) -> Option<SourceChange> {
let file = db.parse(position.file_id).tree; let parse = db.parse(position.file_id);
assert_eq!(file.syntax().text().char_at(position.offset), Some('.')); assert_eq!(parse.tree().syntax().text().char_at(position.offset), Some('.'));
let whitespace = find_token_at_offset(file.syntax(), position.offset) let whitespace = find_token_at_offset(parse.tree().syntax(), position.offset)
.left_biased() .left_biased()
.and_then(ast::Whitespace::cast)?; .and_then(ast::Whitespace::cast)?;
@ -139,8 +140,8 @@ mod tests {
let mut edit = TextEditBuilder::default(); let mut edit = TextEditBuilder::default();
edit.insert(offset, "=".to_string()); edit.insert(offset, "=".to_string());
let before = edit.finish().apply(&before); let before = edit.finish().apply(&before);
let file = SourceFile::parse(&before).tree; let parse = SourceFile::parse(&before);
if let Some(result) = on_eq_typed(&file, offset) { if let Some(result) = on_eq_typed(parse.tree(), offset) {
let actual = result.apply(&before); let actual = result.apply(&before);
assert_eq_text!(after, &actual); assert_eq_text!(after, &actual);
} else { } else {

View File

@ -95,8 +95,8 @@ pub(crate) fn expand_to_expr(
pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree {
// wrap the given text to a macro call // wrap the given text to a macro call
let wrapped = format!("wrap_macro!( {} )", text); let wrapped = format!("wrap_macro!( {} )", text);
let wrapped = ast::SourceFile::parse(&wrapped).tree; let wrapped = ast::SourceFile::parse(&wrapped);
let wrapped = wrapped.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); let wrapped = wrapped.tree().syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
let mut wrapped = ast_to_token_tree(wrapped).unwrap().0; let mut wrapped = ast_to_token_tree(wrapped).unwrap().0;
wrapped.delimiter = tt::Delimiter::None; wrapped.delimiter = tt::Delimiter::None;

View File

@ -59,11 +59,19 @@ pub use rowan::{SmolStr, TextRange, TextUnit};
/// files. /// files.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Parse { pub struct Parse {
pub tree: TreeArc<SourceFile>, tree: TreeArc<SourceFile>,
pub errors: Arc<Vec<SyntaxError>>, errors: Arc<Vec<SyntaxError>>,
} }
impl Parse { impl Parse {
pub fn tree(&self) -> &SourceFile {
&*self.tree
}
pub fn errors(&self) -> &[SyntaxError] {
&*self.errors
}
pub fn ok(self) -> Result<TreeArc<SourceFile>, Arc<Vec<SyntaxError>>> { pub fn ok(self) -> Result<TreeArc<SourceFile>, Arc<Vec<SyntaxError>>> {
if self.errors.is_empty() { if self.errors.is_empty() {
Ok(self.tree) Ok(self.tree)

View File

@ -22,7 +22,7 @@ fn lexer_tests() {
fn parser_tests() { fn parser_tests() {
dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], |text, path| { dir_tests(&test_data_dir(), &["parser/inline/ok", "parser/ok"], |text, path| {
let parse = SourceFile::parse(text); let parse = SourceFile::parse(text);
let errors = parse.errors.as_slice(); let errors = parse.errors();
assert_eq!( assert_eq!(
errors, errors,
&[] as &[ra_syntax::SyntaxError], &[] as &[ra_syntax::SyntaxError],
@ -33,7 +33,7 @@ fn parser_tests() {
}); });
dir_tests(&test_data_dir(), &["parser/err", "parser/inline/err"], |text, path| { dir_tests(&test_data_dir(), &["parser/err", "parser/inline/err"], |text, path| {
let parse = SourceFile::parse(text); let parse = SourceFile::parse(text);
let errors = parse.errors.as_slice(); let errors = parse.errors();
assert!(!errors.is_empty(), "There should be errors in the file {:?}", path.display()); assert!(!errors.is_empty(), "There should be errors in the file {:?}", path.display());
parse.debug_dump() parse.debug_dump()
}); });