|
| 1 | +use std::collections::HashMap; |
| 2 | + |
| 3 | +use lsp_types::{RenameFilesParams, TextEdit, Uri, WillRenameFilesRequest, WorkspaceEdit}; |
| 4 | +use ruff_db::system::SystemPathBuf; |
| 5 | +use ruff_db::{Db as _, files::File}; |
| 6 | +use ty_ide::{PathRename, will_rename_paths_in_files}; |
| 7 | +use ty_project::{Db as _, ProjectDatabase}; |
| 8 | + |
| 9 | +use crate::document::ToRangeExt; |
| 10 | +use crate::server::api::traits::{ |
| 11 | + BackgroundRequestHandler, RequestHandler, RetriableRequestHandler, |
| 12 | +}; |
| 13 | +use crate::session::SessionSnapshot; |
| 14 | +use crate::session::client::Client; |
| 15 | + |
| 16 | +/// Handles `workspace/willRenameFiles` requests for Python modules and regular packages. |
| 17 | +pub(crate) struct WillRenameFilesHandler; |
| 18 | + |
| 19 | +impl RequestHandler for WillRenameFilesHandler { |
| 20 | + type RequestType = WillRenameFilesRequest; |
| 21 | +} |
| 22 | + |
| 23 | +impl BackgroundRequestHandler for WillRenameFilesHandler { |
| 24 | + fn run( |
| 25 | + snapshot: &SessionSnapshot, |
| 26 | + client: &Client, |
| 27 | + params: RenameFilesParams, |
| 28 | + ) -> crate::server::Result<Option<WorkspaceEdit>> { |
| 29 | + let encoding = snapshot.position_encoding(); |
| 30 | + let mut all_changes: HashMap<Uri, Vec<TextEdit>> = HashMap::new(); |
| 31 | + let mut paths = Vec::new(); |
| 32 | + let mut project_index = None; |
| 33 | + |
| 34 | + for file_rename in ¶ms.files { |
| 35 | + let (Some(old_path), Some(new_path)) = ( |
| 36 | + file_uri_to_path(&file_rename.old_uri), |
| 37 | + file_uri_to_path(&file_rename.new_uri), |
| 38 | + ) else { |
| 39 | + return Ok(None); |
| 40 | + }; |
| 41 | + |
| 42 | + let Some(rename_project_index) = snapshot.project_index_for_path(&old_path) else { |
| 43 | + return Ok(None); |
| 44 | + }; |
| 45 | + if snapshot |
| 46 | + .enclosing_project_index_for_path(&new_path) |
| 47 | + .is_some_and(|index| index != rename_project_index) |
| 48 | + { |
| 49 | + return Ok(unsupported_move(client)); |
| 50 | + } |
| 51 | + if project_index |
| 52 | + .replace(rename_project_index) |
| 53 | + .is_some_and(|project_index| project_index != rename_project_index) |
| 54 | + { |
| 55 | + return Ok(unsupported_move(client)); |
| 56 | + } |
| 57 | + paths.push((old_path, new_path)); |
| 58 | + } |
| 59 | + |
| 60 | + // Use the project that owns the old path. Files in other workspaces stay unchanged even if |
| 61 | + // that project can import them through an extra search path. |
| 62 | + let Some(project_index) = project_index else { |
| 63 | + return Ok(None); |
| 64 | + }; |
| 65 | + let Some(db) = snapshot.projects().get(project_index) else { |
| 66 | + return Ok(None); |
| 67 | + }; |
| 68 | + let Some(workspace_settings) = snapshot.workspace_settings(project_index) else { |
| 69 | + return Ok(None); |
| 70 | + }; |
| 71 | + if workspace_settings.is_language_services_disabled() { |
| 72 | + return Ok(None); |
| 73 | + } |
| 74 | + |
| 75 | + // The filesystem tells file moves from folder moves. A folder without an initializer is a |
| 76 | + // namespace package, which the IDE layer deliberately leaves unchanged. |
| 77 | + let mut renames = Vec::with_capacity(paths.len()); |
| 78 | + for (old_path, new_path) in paths { |
| 79 | + if db.system().is_directory(&old_path) { |
| 80 | + if !["__init__.py", "__init__.pyi"] |
| 81 | + .into_iter() |
| 82 | + .any(|name| db.system().is_file(&old_path.join(name))) |
| 83 | + { |
| 84 | + return Ok(None); |
| 85 | + } |
| 86 | + renames.push(PathRename::directory(old_path, new_path)); |
| 87 | + } else { |
| 88 | + if !matches!(old_path.extension(), Some("py" | "pyi")) { |
| 89 | + return Ok(None); |
| 90 | + } |
| 91 | + if old_path.extension() != new_path.extension() { |
| 92 | + return Ok(unsupported_move(client)); |
| 93 | + } |
| 94 | + renames.push(PathRename::file(old_path, new_path)); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + let project = db.project(); |
| 99 | + let indexed_files = project.files(db); |
| 100 | + let open_files = project.open_files(db); |
| 101 | + let files = (&indexed_files) |
| 102 | + .into_iter() |
| 103 | + .chain(open_files.iter().copied()); |
| 104 | + |
| 105 | + let Ok(edits) = will_rename_paths_in_files(db, &renames, files, |file| { |
| 106 | + file_is_in_rename_scope(snapshot, db, project_index, file) |
| 107 | + }) else { |
| 108 | + return Ok(unsupported_move(client)); |
| 109 | + }; |
| 110 | + // Convert ty's file and byte ranges to the URI and position ranges required by LSP. |
| 111 | + for edit in edits { |
| 112 | + let (file, range, new_text) = edit.into_parts(); |
| 113 | + if !file_is_in_rename_scope(snapshot, db, project_index, file) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + let Some(range) = range.to_lsp_range(db, file, encoding) else { |
| 117 | + return Ok(unsupported_move(client)); |
| 118 | + }; |
| 119 | + let Some(location) = range.into_location() else { |
| 120 | + return Ok(unsupported_move(client)); |
| 121 | + }; |
| 122 | + |
| 123 | + all_changes.entry(location.uri).or_default().push(TextEdit { |
| 124 | + range: location.range, |
| 125 | + new_text, |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + if all_changes.values_mut().any(|edits| { |
| 130 | + edits.sort_by_key(|edit| (edit.range.start, edit.range.end)); |
| 131 | + edits.dedup(); |
| 132 | + edits.windows(2).any(|edits| { |
| 133 | + edits[0].range.start == edits[1].range.start |
| 134 | + || edits[0].range.end > edits[1].range.start |
| 135 | + }) |
| 136 | + }) { |
| 137 | + return Ok(unsupported_move(client)); |
| 138 | + } |
| 139 | + |
| 140 | + if all_changes.is_empty() { |
| 141 | + Ok(None) |
| 142 | + } else { |
| 143 | + Ok(Some(WorkspaceEdit { |
| 144 | + changes: Some(all_changes), |
| 145 | + ..Default::default() |
| 146 | + })) |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +impl RetriableRequestHandler for WillRenameFilesHandler { |
| 152 | + const RETRY_ON_CANCELLATION: bool = true; |
| 153 | +} |
| 154 | + |
| 155 | +/// Converts an LSP file URI to a system path. |
| 156 | +fn file_uri_to_path(uri: &str) -> Option<SystemPathBuf> { |
| 157 | + let uri = Uri::parse(uri).ok()?; |
| 158 | + SystemPathBuf::from_path_buf(uri.to_file_path().ok()?).ok() |
| 159 | +} |
| 160 | + |
| 161 | +/// Returns whether the selected project owns a file or the file has no on-disk path. |
| 162 | +fn file_is_in_rename_scope( |
| 163 | + snapshot: &SessionSnapshot, |
| 164 | + db: &ProjectDatabase, |
| 165 | + project_index: usize, |
| 166 | + file: File, |
| 167 | +) -> bool { |
| 168 | + file.path(db).as_system_path().is_none_or(|path| { |
| 169 | + snapshot |
| 170 | + .enclosing_project_index_for_path(path) |
| 171 | + .is_none_or(|index| index == project_index) |
| 172 | + }) |
| 173 | +} |
| 174 | + |
| 175 | +/// Warns the user that ty deliberately left an unsafe move unchanged. |
| 176 | +fn unsupported_move(client: &Client) -> Option<WorkspaceEdit> { |
| 177 | + client.show_warning_message( |
| 178 | + "ty could not safely update imports and references for this move. \ |
| 179 | + No automated code changes were applied.", |
| 180 | + ); |
| 181 | + None |
| 182 | +} |
0 commit comments