From 02924174bb084d73cab67af6665ddf00e91983f6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 28 Dec 2018 18:03:03 +0300 Subject: introduce FileRange --- crates/ra_analysis/src/imp.rs | 24 ++++++++++++++---------- crates/ra_analysis/src/lib.rs | 16 ++++++++-------- crates/ra_db/src/lib.rs | 8 +++++++- 3 files changed, 29 insertions(+), 19 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index e6663810d..fcb4cd957 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -23,7 +23,7 @@ use crate::{ AnalysisChange, Cancelable, completion::{CompletionItem, completions}, - CrateId, db, Diagnostic, FileId, FilePosition, FileSystemEdit, + CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, Query, ReferenceResolution, RootChange, SourceChange, SourceFileEdit, symbol_index::{LibrarySymbolsQuery, SymbolIndex, SymbolsDatabase}, }; @@ -404,19 +404,21 @@ impl AnalysisImpl { Ok(res) } - pub fn assists(&self, file_id: FileId, range: TextRange) -> Vec { - let file = self.file_syntax(file_id); - let offset = range.start(); + pub fn assists(&self, frange: FileRange) -> Vec { + let file = self.file_syntax(frange.file_id); + let offset = frange.range.start(); let actions = vec![ ra_editor::flip_comma(&file, offset).map(|f| f()), ra_editor::add_derive(&file, offset).map(|f| f()), ra_editor::add_impl(&file, offset).map(|f| f()), ra_editor::make_pub_crate(&file, offset).map(|f| f()), - ra_editor::introduce_variable(&file, range).map(|f| f()), + ra_editor::introduce_variable(&file, frange.range).map(|f| f()), ]; actions .into_iter() - .filter_map(|local_edit| Some(SourceChange::from_local_edit(file_id, local_edit?))) + .filter_map(|local_edit| { + Some(SourceChange::from_local_edit(frange.file_id, local_edit?)) + }) .collect() } @@ -487,13 +489,15 @@ impl AnalysisImpl { Ok(None) } - pub fn type_of(&self, file_id: FileId, range: TextRange) -> Cancelable> { - let file = self.db.source_file(file_id); + pub fn type_of(&self, frange: FileRange) -> Cancelable> { + let file = self.db.source_file(frange.file_id); let syntax = file.syntax(); - let node = find_covering_node(syntax, range); + let node = find_covering_node(syntax, frange.range); let parent_fn = ctry!(node.ancestors().find_map(FnDef::cast)); let function = ctry!(source_binder::function_from_source( - &*self.db, file_id, parent_fn + &*self.db, + frange.file_id, + parent_fn )?); let infer = function.infer(&*self.db)?; Ok(infer.type_of_node(node).map(|t| t.to_string())) diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index b26e9e9ff..3fa4189ce 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -38,7 +38,7 @@ pub use ra_editor::{ pub use hir::FnSignatureInfo; pub use ra_db::{ - Canceled, Cancelable, FilePosition, + Canceled, Cancelable, FilePosition, FileRange, CrateGraph, CrateId, SourceRootId, FileId }; @@ -287,9 +287,9 @@ impl Analysis { let file = self.imp.file_syntax(file_id); ra_editor::syntax_tree(&file) } - pub fn join_lines(&self, file_id: FileId, range: TextRange) -> SourceChange { - let file = self.imp.file_syntax(file_id); - SourceChange::from_local_edit(file_id, ra_editor::join_lines(&file, range)) + pub fn join_lines(&self, frange: FileRange) -> SourceChange { + let file = self.imp.file_syntax(frange.file_id); + SourceChange::from_local_edit(frange.file_id, ra_editor::join_lines(&file, frange.range)) } pub fn on_enter(&self, position: FilePosition) -> Option { let file = self.imp.file_syntax(position.file_id); @@ -346,8 +346,8 @@ impl Analysis { pub fn completions(&self, position: FilePosition) -> Cancelable>> { self.imp.completions(position) } - pub fn assists(&self, file_id: FileId, range: TextRange) -> Cancelable> { - Ok(self.imp.assists(file_id, range)) + pub fn assists(&self, frange: FileRange) -> Cancelable> { + Ok(self.imp.assists(frange)) } pub fn diagnostics(&self, file_id: FileId) -> Cancelable> { self.imp.diagnostics(file_id) @@ -358,8 +358,8 @@ impl Analysis { ) -> Cancelable)>> { self.imp.resolve_callable(position) } - pub fn type_of(&self, file_id: FileId, range: TextRange) -> Cancelable> { - self.imp.type_of(file_id, range) + pub fn type_of(&self, frange: FileRange) -> Cancelable> { + self.imp.type_of(frange) } } diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 3028db17c..7181f2950 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -8,7 +8,7 @@ pub mod mock; use std::sync::Arc; use ra_editor::LineIndex; -use ra_syntax::{TextUnit, SourceFileNode}; +use ra_syntax::{TextUnit, TextRange, SourceFileNode}; pub use crate::{ cancelation::{Canceled, Cancelable}, @@ -70,3 +70,9 @@ pub struct FilePosition { pub file_id: FileId, pub offset: TextUnit, } + +#[derive(Clone, Copy, Debug)] +pub struct FileRange { + pub file_id: FileId, + pub range: TextRange, +} -- cgit v1.2.3