microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/language_service/src/openqasm/rename.rs
62lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use std::sync::Arc; |
| 5 | |
| 6 | use crate::openqasm::get_reference_locations; |
| 7 | use crate::qsc_utils::into_range; |
| 8 | use log::trace; |
| 9 | |
| 10 | use qsc::Span; |
| 11 | use qsc::line_column::{Encoding, Position, Range}; |
| 12 | use qsc::location::Location; |
| 13 | |
| 14 | pub fn prepare_rename( |
| 15 | sources: &[(Arc<str>, Arc<str>)], |
| 16 | source_name: &str, |
| 17 | position: Position, |
| 18 | position_encoding: Encoding, |
| 19 | ) -> Option<(Range, String)> { |
| 20 | let (res, id) = |
| 21 | super::find_symbol_in_sources(sources, source_name, position, position_encoding); |
| 22 | let id = id?; |
| 23 | let symbol = &res.symbols[id]; |
| 24 | |
| 25 | // If the symbol is a built-in symbol, we can't rename it, return None |
| 26 | if symbol.span == Span::default() { |
| 27 | return None; |
| 28 | } |
| 29 | |
| 30 | let range = into_range(position_encoding, symbol.span, &res.source_map); |
| 31 | let name = symbol.name.clone(); |
| 32 | trace!("prepare_rename: found symbol {name} at {range:?}"); |
| 33 | Some((range, name)) |
| 34 | } |
| 35 | |
| 36 | pub fn get_rename( |
| 37 | sources: &[(Arc<str>, Arc<str>)], |
| 38 | source_name: &str, |
| 39 | position: Position, |
| 40 | position_encoding: Encoding, |
| 41 | ) -> Vec<Location> { |
| 42 | let (res, id) = |
| 43 | super::find_symbol_in_sources(sources, source_name, position, position_encoding); |
| 44 | |
| 45 | let Some(id) = id else { |
| 46 | return vec![]; |
| 47 | }; |
| 48 | |
| 49 | // If the symbol is a built-in symbol, we can't rename it |
| 50 | let symbol = &res.symbols[id]; |
| 51 | if symbol.span == Span::default() { |
| 52 | return vec![]; |
| 53 | } |
| 54 | |
| 55 | get_reference_locations( |
| 56 | position_encoding, |
| 57 | &res.program, |
| 58 | &res.source_map, |
| 59 | &res.symbols, |
| 60 | id, |
| 61 | ) |
| 62 | } |