mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-24 12:03:31 +00:00
introduce FileRange
This commit is contained in:
parent
d7440a5f49
commit
02924174bb
3 changed files with 29 additions and 19 deletions
|
@ -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<SourceChange> {
|
||||
let file = self.file_syntax(file_id);
|
||||
let offset = range.start();
|
||||
pub fn assists(&self, frange: FileRange) -> Vec<SourceChange> {
|
||||
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<Option<String>> {
|
||||
let file = self.db.source_file(file_id);
|
||||
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
|
||||
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()))
|
||||
|
|
|
@ -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<SourceChange> {
|
||||
let file = self.imp.file_syntax(position.file_id);
|
||||
|
@ -346,8 +346,8 @@ impl Analysis {
|
|||
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
|
||||
self.imp.completions(position)
|
||||
}
|
||||
pub fn assists(&self, file_id: FileId, range: TextRange) -> Cancelable<Vec<SourceChange>> {
|
||||
Ok(self.imp.assists(file_id, range))
|
||||
pub fn assists(&self, frange: FileRange) -> Cancelable<Vec<SourceChange>> {
|
||||
Ok(self.imp.assists(frange))
|
||||
}
|
||||
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
|
||||
self.imp.diagnostics(file_id)
|
||||
|
@ -358,8 +358,8 @@ impl Analysis {
|
|||
) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> {
|
||||
self.imp.resolve_callable(position)
|
||||
}
|
||||
pub fn type_of(&self, file_id: FileId, range: TextRange) -> Cancelable<Option<String>> {
|
||||
self.imp.type_of(file_id, range)
|
||||
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
|
||||
self.imp.type_of(frange)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue