Skip to content

Commit 8bb81d7

Browse files
bors[bot]matklad
andcommitted
Merge #1524
1524: make Parse fields private r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 2e466bb + deab4ca commit 8bb81d7

31 files changed

+109
-99
lines changed

crates/ra_assists/src/assist_ctx.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
7171
where
7272
F: FnOnce(AssistCtx<DB>) -> T,
7373
{
74-
let source_file = &db.parse(frange.file_id).tree;
74+
let parse = db.parse(frange.file_id);
7575
let assist =
7676
if should_compute_edit { Assist::Resolved(vec![]) } else { Assist::Unresolved(vec![]) };
7777

78-
let ctx = AssistCtx { db, frange, source_file, should_compute_edit, assist };
78+
let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit, assist };
7979
f(ctx)
8080
}
8181

crates/ra_assists/src/ast_editor.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -278,16 +278,17 @@ impl AstBuilder<ast::NameRef> {
278278
}
279279

280280
fn ast_node_from_file_text<N: AstNode>(text: &str) -> TreeArc<N> {
281-
let file = SourceFile::parse(text).tree;
282-
let res = file.syntax().descendants().find_map(N::cast).unwrap().to_owned();
281+
let parse = SourceFile::parse(text);
282+
let res = parse.tree().syntax().descendants().find_map(N::cast).unwrap().to_owned();
283283
res
284284
}
285285

286286
mod tokens {
287287
use once_cell::sync::Lazy;
288288
use ra_syntax::{AstNode, SourceFile, SyntaxKind::*, SyntaxToken, TreeArc, T};
289289

290-
static SOURCE_FILE: Lazy<TreeArc<SourceFile>> = Lazy::new(|| SourceFile::parse(",\n; ;").tree);
290+
static SOURCE_FILE: Lazy<TreeArc<SourceFile>> =
291+
Lazy::new(|| SourceFile::parse(",\n; ;").tree().to_owned());
291292

292293
pub(crate) fn comma() -> SyntaxToken<'static> {
293294
SOURCE_FILE

crates/ra_cli/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn main() -> Result<()> {
102102

103103
fn file() -> Result<TreeArc<SourceFile>> {
104104
let text = read_stdin()?;
105-
Ok(SourceFile::parse(&text).tree)
105+
Ok(SourceFile::parse(&text).tree().to_owned())
106106
}
107107

108108
fn read_stdin() -> Result<String> {

crates/ra_hir/src/code_model.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl ModuleSource {
167167
) -> ModuleSource {
168168
match (file_id, decl_id) {
169169
(Some(file_id), _) => {
170-
let source_file = db.parse(file_id).tree;
170+
let source_file = db.parse(file_id).tree().to_owned();
171171
ModuleSource::SourceFile(source_file)
172172
}
173173
(None, Some(item_id)) => {

crates/ra_hir/src/expr/validation.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
7171
}
7272
let source_map = self.func.body_source_map(db);
7373
let file_id = self.func.source(db).file_id;
74-
let source_file = db.parse(file_id.original_file(db)).tree;
74+
let parse = db.parse(file_id.original_file(db));
75+
let source_file = parse.tree();
7576
if let Some(field_list_node) = source_map
7677
.expr_syntax(id)
7778
.map(|ptr| ptr.to_node(source_file.syntax()))

crates/ra_hir/src/ids.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl HirFileId {
6060
file_id: HirFileId,
6161
) -> Option<TreeArc<SyntaxNode>> {
6262
match file_id.0 {
63-
HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree.syntax().to_owned()),
63+
HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree().syntax().to_owned()),
6464
HirFileIdRepr::Macro(macro_file) => db.parse_macro(macro_file),
6565
}
6666
}

crates/ra_hir/src/source_binder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ pub fn module_from_declaration(
4949

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

crates/ra_ide_api/src/call_info.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::{db::RootDatabase, CallInfo, FilePosition, FunctionSignature};
1010

1111
/// Computes parameter information for the given call expression.
1212
pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
13-
let file = db.parse(position.file_id).tree;
14-
let syntax = file.syntax();
13+
let parse = db.parse(position.file_id);
14+
let syntax = parse.tree().syntax();
1515

1616
// Find the calling expression and it's NameRef
1717
let calling_node = FnCallNode::with_node(syntax, position.offset)?;

crates/ra_ide_api/src/change.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ impl LibraryData {
135135
files: Vec<(FileId, RelativePathBuf, Arc<String>)>,
136136
) -> LibraryData {
137137
let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| {
138-
let file = SourceFile::parse(text).tree;
139-
(*file_id, file)
138+
let parse = SourceFile::parse(text);
139+
(*file_id, parse)
140140
}));
141141
let mut root_change = RootChange::default();
142142
root_change.added = files

crates/ra_ide_api/src/completion/completion_context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'a> CompletionContext<'a> {
4848
) -> Option<CompletionContext<'a>> {
4949
let module = source_binder::module_from_position(db, position);
5050
let token =
51-
find_token_at_offset(original_parse.tree.syntax(), position.offset).left_biased()?;
51+
find_token_at_offset(original_parse.tree().syntax(), position.offset).left_biased()?;
5252
let analyzer =
5353
hir::SourceAnalyzer::new(db, position.file_id, token.parent(), Some(position.offset));
5454
let mut ctx = CompletionContext {
@@ -89,7 +89,7 @@ impl<'a> CompletionContext<'a> {
8989
// actual completion.
9090
let file = {
9191
let edit = AtomTextEdit::insert(offset, "intellijRulezz".to_string());
92-
original_parse.reparse(&edit).tree
92+
original_parse.reparse(&edit).tree().to_owned()
9393
};
9494

9595
// First, let's try to complete a reference to some declaration.
@@ -100,7 +100,7 @@ impl<'a> CompletionContext<'a> {
100100
self.is_param = true;
101101
return;
102102
}
103-
self.classify_name_ref(&original_parse.tree, name_ref);
103+
self.classify_name_ref(original_parse.tree(), name_ref);
104104
}
105105

106106
// Otherwise, see if this is a declaration. We can use heuristics to

crates/ra_ide_api/src/diagnostics.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
2727
let parse = db.parse(file_id);
2828
let mut res = Vec::new();
2929

30-
res.extend(parse.errors.iter().map(|err| Diagnostic {
30+
res.extend(parse.errors().iter().map(|err| Diagnostic {
3131
range: location_to_range(err.location()),
3232
message: format!("Syntax Error: {}", err),
3333
severity: Severity::Error,
3434
fix: None,
3535
}));
3636

37-
for node in parse.tree.syntax().descendants() {
37+
for node in parse.tree().syntax().descendants() {
3838
check_unnecessary_braces_in_use_statement(&mut res, file_id, node);
3939
check_struct_shorthand_initialization(&mut res, file_id, node);
4040
}
@@ -181,18 +181,18 @@ mod tests {
181181
type DiagnosticChecker = fn(&mut Vec<Diagnostic>, FileId, &SyntaxNode) -> Option<()>;
182182

183183
fn check_not_applicable(code: &str, func: DiagnosticChecker) {
184-
let file = SourceFile::parse(code).tree;
184+
let parse = SourceFile::parse(code);
185185
let mut diagnostics = Vec::new();
186-
for node in file.syntax().descendants() {
186+
for node in parse.tree().syntax().descendants() {
187187
func(&mut diagnostics, FileId(0), node);
188188
}
189189
assert!(diagnostics.is_empty());
190190
}
191191

192192
fn check_apply(before: &str, after: &str, func: DiagnosticChecker) {
193-
let file = SourceFile::parse(before).tree;
193+
let parse = SourceFile::parse(before);
194194
let mut diagnostics = Vec::new();
195-
for node in file.syntax().descendants() {
195+
for node in parse.tree().syntax().descendants() {
196196
func(&mut diagnostics, FileId(0), node);
197197
}
198198
let diagnostic =

crates/ra_ide_api/src/display/navigation_target.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ impl NavigationTarget {
9393
file_id: FileId,
9494
pat: AstPtr<ast::Pat>,
9595
) -> NavigationTarget {
96-
let file = db.parse(file_id).tree;
97-
let (name, full_range) = match pat.to_node(file.syntax()).kind() {
96+
let parse = db.parse(file_id);
97+
let (name, full_range) = match pat.to_node(parse.tree().syntax()).kind() {
9898
ast::PatKind::BindPat(pat) => return NavigationTarget::from_bind_pat(file_id, &pat),
9999
_ => ("_".into(), pat.syntax_node_ptr().range()),
100100
};
@@ -315,8 +315,8 @@ impl NavigationTarget {
315315
}
316316

317317
pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
318-
let file = db.parse(symbol.file_id).tree;
319-
let node = symbol.ptr.to_node(file.syntax()).to_owned();
318+
let parse = db.parse(symbol.file_id);
319+
let node = symbol.ptr.to_node(parse.tree().syntax()).to_owned();
320320

321321
fn doc_comments<N: ast::DocCommentsOwner>(node: &N) -> Option<String> {
322322
node.doc_comment_text()
@@ -341,8 +341,8 @@ pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option
341341
///
342342
/// e.g. `struct Name`, `enum Name`, `fn Name`
343343
pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option<String> {
344-
let file = db.parse(symbol.file_id).tree;
345-
let node = symbol.ptr.to_node(file.syntax()).to_owned();
344+
let parse = db.parse(symbol.file_id);
345+
let node = symbol.ptr.to_node(parse.tree().syntax()).to_owned();
346346

347347
visitor()
348348
.visit(|node: &ast::FnDef| node.short_label())

crates/ra_ide_api/src/extend_selection.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::{db::RootDatabase, FileRange};
1111

1212
// FIXME: restore macro support
1313
pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange {
14-
let source_file = db.parse(frange.file_id).tree;
15-
try_extend_selection(source_file.syntax(), frange.range).unwrap_or(frange.range)
14+
let parse = db.parse(frange.file_id);
15+
try_extend_selection(parse.tree().syntax(), frange.range).unwrap_or(frange.range)
1616
}
1717

1818
fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange> {
@@ -212,10 +212,10 @@ mod tests {
212212

213213
fn do_check(before: &str, afters: &[&str]) {
214214
let (cursor, before) = extract_offset(before);
215-
let file = SourceFile::parse(&before).tree;
215+
let parse = SourceFile::parse(&before);
216216
let mut range = TextRange::offset_len(cursor, 0.into());
217217
for &after in afters {
218-
range = try_extend_selection(file.syntax(), range).unwrap();
218+
range = try_extend_selection(parse.tree().syntax(), range).unwrap();
219219
let actual = &before[range];
220220
assert_eq!(after, actual);
221221
}

crates/ra_ide_api/src/folding_ranges.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ mod tests {
192192

193193
fn do_check(text: &str, fold_kinds: &[FoldKind]) {
194194
let (ranges, text) = extract_ranges(text, "fold");
195-
let file = SourceFile::parse(&text).tree;
196-
let folds = folding_ranges(&file);
195+
let parse = SourceFile::parse(&text);
196+
let folds = folding_ranges(parse.tree());
197197

198198
assert_eq!(
199199
folds.len(),

crates/ra_ide_api/src/goto_definition.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pub(crate) fn goto_definition(
1919
db: &RootDatabase,
2020
position: FilePosition,
2121
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
22-
let file = db.parse(position.file_id).tree;
23-
let syntax = file.syntax();
22+
let parse = db.parse(position.file_id);
23+
let syntax = parse.tree().syntax();
2424
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
2525
let navs = reference_definition(db, position.file_id, name_ref).to_vec();
2626
return Some(RangeInfo::new(name_ref.syntax().range(), navs.to_vec()));

crates/ra_ide_api/src/goto_type_definition.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ pub(crate) fn goto_type_definition(
77
db: &RootDatabase,
88
position: FilePosition,
99
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
10-
let file = db.parse(position.file_id).tree;
10+
let parse = db.parse(position.file_id);
1111

12-
let node = find_token_at_offset(file.syntax(), position.offset).find_map(|token| {
12+
let node = find_token_at_offset(parse.tree().syntax(), position.offset).find_map(|token| {
1313
token
1414
.parent()
1515
.ancestors()

crates/ra_ide_api/src/hover.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ fn hover_text(docs: Option<String>, desc: Option<String>) -> Option<String> {
9494
}
9595

9696
pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<HoverResult>> {
97-
let file = db.parse(position.file_id).tree;
97+
let parse = db.parse(position.file_id);
98+
let file = parse.tree();
9899
let mut res = HoverResult::new();
99100

100101
let mut range = None;
@@ -241,8 +242,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
241242
}
242243

243244
pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
244-
let file = db.parse(frange.file_id).tree;
245-
let syntax = file.syntax();
245+
let parse = db.parse(frange.file_id);
246+
let syntax = parse.tree().syntax();
246247
let leaf_node = find_covering_element(syntax, frange.range);
247248
// if we picked identifier, expand to pattern/expression
248249
let node = leaf_node

crates/ra_ide_api/src/impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub(crate) fn goto_implementation(
88
db: &RootDatabase,
99
position: FilePosition,
1010
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
11-
let file = db.parse(position.file_id).tree;
12-
let syntax = file.syntax();
11+
let parse = db.parse(position.file_id);
12+
let syntax = parse.tree().syntax();
1313

1414
let module = source_binder::module_from_position(db, position)?;
1515

crates/ra_ide_api/src/join_lines.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,8 @@ fn foo() {
503503

504504
fn check_join_lines_sel(before: &str, after: &str) {
505505
let (sel, before) = extract_range(before);
506-
let file = SourceFile::parse(&before).tree;
507-
let result = join_lines(&file, sel);
506+
let parse = SourceFile::parse(&before);
507+
let result = join_lines(parse.tree(), sel);
508508
let actual = result.apply(&before);
509509
assert_eq_text!(after, &actual);
510510
}

crates/ra_ide_api/src/lib.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ impl Analysis {
326326

327327
/// Gets the syntax tree of the file.
328328
pub fn parse(&self, file_id: FileId) -> TreeArc<SourceFile> {
329-
self.db.parse(file_id).tree
329+
self.db.parse(file_id).tree().to_owned()
330330
}
331331

332332
/// Gets the file's `LineIndex`: data structure to convert between absolute
@@ -343,7 +343,8 @@ impl Analysis {
343343
/// Returns position of the matching brace (all types of braces are
344344
/// supported).
345345
pub fn matching_brace(&self, position: FilePosition) -> Option<TextUnit> {
346-
let file = self.db.parse(position.file_id).tree;
346+
let parse = self.db.parse(position.file_id);
347+
let file = parse.tree();
347348
matching_brace::matching_brace(&file, position.offset)
348349
}
349350

@@ -356,10 +357,10 @@ impl Analysis {
356357
/// Returns an edit to remove all newlines in the range, cleaning up minor
357358
/// stuff like trailing commas.
358359
pub fn join_lines(&self, frange: FileRange) -> SourceChange {
359-
let file = self.db.parse(frange.file_id).tree;
360+
let parse = self.db.parse(frange.file_id);
360361
let file_edit = SourceFileEdit {
361362
file_id: frange.file_id,
362-
edit: join_lines::join_lines(&file, frange.range),
363+
edit: join_lines::join_lines(parse.tree(), frange.range),
363364
};
364365
SourceChange::source_file_edit("join lines", file_edit)
365366
}
@@ -374,7 +375,8 @@ impl Analysis {
374375
/// this works when adding `let =`.
375376
// FIXME: use a snippet completion instead of this hack here.
376377
pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> {
377-
let file = self.db.parse(position.file_id).tree;
378+
let parse = self.db.parse(position.file_id);
379+
let file = parse.tree();
378380
let edit = typing::on_eq_typed(&file, position.offset)?;
379381
Some(SourceChange::source_file_edit(
380382
"add semicolon",
@@ -390,14 +392,14 @@ impl Analysis {
390392
/// Returns a tree representation of symbols in the file. Useful to draw a
391393
/// file outline.
392394
pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> {
393-
let file = self.db.parse(file_id).tree;
394-
file_structure(&file)
395+
let parse = self.db.parse(file_id);
396+
file_structure(parse.tree())
395397
}
396398

397399
/// Returns the set of folding ranges.
398400
pub fn folding_ranges(&self, file_id: FileId) -> Vec<Fold> {
399-
let file = self.db.parse(file_id).tree;
400-
folding_ranges::folding_ranges(&file)
401+
let parse = self.db.parse(file_id);
402+
folding_ranges::folding_ranges(parse.tree())
401403
}
402404

403405
/// Fuzzy searches for a symbol.

crates/ra_ide_api/src/matching_brace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ mod tests {
2525
fn test_matching_brace() {
2626
fn do_check(before: &str, after: &str) {
2727
let (pos, before) = extract_offset(before);
28-
let file = SourceFile::parse(&before).tree;
29-
let new_pos = match matching_brace(&file, pos) {
28+
let parse = SourceFile::parse(&before);
29+
let new_pos = match matching_brace(parse.tree(), pos) {
3030
None => pos,
3131
Some(pos) => pos,
3232
};

crates/ra_ide_api/src/references.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ pub(crate) fn find_all_refs(
4949
db: &RootDatabase,
5050
position: FilePosition,
5151
) -> Option<ReferenceSearchResult> {
52-
let file = db.parse(position.file_id).tree;
53-
let (binding, analyzer) = find_binding(db, &file, position)?;
52+
let parse = db.parse(position.file_id);
53+
let (binding, analyzer) = find_binding(db, parse.tree(), position)?;
5454
let declaration = NavigationTarget::from_bind_pat(position.file_id, binding);
5555

5656
let references = analyzer
@@ -88,8 +88,8 @@ pub(crate) fn rename(
8888
position: FilePosition,
8989
new_name: &str,
9090
) -> Option<SourceChange> {
91-
let source_file = db.parse(position.file_id).tree;
92-
let syntax = source_file.syntax();
91+
let parse = db.parse(position.file_id);
92+
let syntax = parse.tree().syntax();
9393

9494
if let Some((ast_name, ast_module)) = find_name_and_module_at_offset(syntax, position) {
9595
rename_mod(db, ast_name, ast_module, position, new_name)

0 commit comments

Comments
 (0)