Move renames into ra_analysis

This commit is contained in:
DJMcNab 2018-12-30 18:14:55 +00:00
parent 0f95d8523e
commit c402b007a3
3 changed files with 39 additions and 15 deletions

View file

@ -288,7 +288,11 @@ impl AnalysisImpl {
Some(it) => it,
};
let mut ret = vec![(position.file_id, binding.syntax().range())];
let mut ret = binding
.name()
.into_iter()
.map(|name| (position.file_id, name.syntax().range()))
.collect::<Vec<_>>();
ret.extend(
descr
.scopes(&*self.db)
@ -505,7 +509,25 @@ impl AnalysisImpl {
let infer = function.infer(&*self.db)?;
Ok(infer.type_of_node(node).map(|t| t.to_string()))
}
pub fn rename(
&self,
position: FilePosition,
new_name: &str,
) -> Cancelable<Vec<SourceFileEdit>> {
let res = self
.find_all_refs(position)?
.iter()
.map(|(file_id, text_range)| SourceFileEdit {
file_id: *file_id,
edit: {
let mut builder = ra_text_edit::TextEditBuilder::new();
builder.replace(*text_range, new_name.into());
builder.finish()
},
})
.collect::<Vec<_>>();
Ok(res)
}
fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileSymbol)>> {
let name = name_ref.text();
let mut query = Query::new(name.to_string());

View file

@ -367,6 +367,13 @@ impl Analysis {
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
self.imp.type_of(frange)
}
pub fn rename(
&self,
position: FilePosition,
new_name: &str,
) -> Cancelable<Vec<SourceFileEdit>> {
self.imp.rename(position, new_name)
}
}
pub struct LibraryData {

View file

@ -557,24 +557,19 @@ pub fn handle_rename(world: ServerWorld, params: RenameParams) -> Result<Option<
.into());
}
let refs = world
let renames = world
.analysis()
.find_all_refs(FilePosition { file_id, offset })?;
if refs.is_empty() {
.rename(FilePosition { file_id, offset }, &*params.new_name)?;
if renames.is_empty() {
return Ok(None);
}
let mut changes = HashMap::new();
for r in refs {
if let Ok(loc) = to_location(r.0, r.1, &world, &line_index) {
for edit in renames {
changes
.entry(loc.uri)
.entry(file_id.try_conv_with(&world)?)
.or_insert_with(Vec::new)
.push(TextEdit {
range: loc.range,
new_text: params.new_name.clone(),
});
}
.extend(edit.edit.conv_with(&line_index));
}
Ok(Some(WorkspaceEdit {