-
Notifications
You must be signed in to change notification settings - Fork 213
Skip deprecated exports in autoimport
#1694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| use std::cmp::Ordering; | ||
| use std::cmp::Reverse; | ||
| use std::collections::BTreeMap; | ||
|
|
||
|
|
@@ -1676,7 +1677,9 @@ impl<'a> Transaction<'a> { | |
| let error_range = error.range(); | ||
| if error_range.contains_range(range) { | ||
| let unknown_name = module_info.code_at(error_range); | ||
| for handle_to_import_from in self.search_exports_exact(unknown_name) { | ||
| for (handle_to_import_from, export) in | ||
| self.search_exports_exact(unknown_name) | ||
| { | ||
| let (position, insert_text, _) = insert_import_edit( | ||
| &ast, | ||
| self.config_finder(), | ||
|
|
@@ -1686,7 +1689,12 @@ impl<'a> Transaction<'a> { | |
| import_format, | ||
| ); | ||
| let range = TextRange::at(position, TextSize::new(0)); | ||
| let title = format!("Insert import: `{}`", insert_text.trim()); | ||
| let mut title = format!("Insert import: `{}`", insert_text.trim()); | ||
|
|
||
| if export.deprecation.is_some() { | ||
| title.push_str(" (deprecated)"); | ||
| } | ||
|
|
||
| code_actions.push((title, module_info.dupe(), range, insert_text)); | ||
| } | ||
|
|
||
|
|
@@ -1709,7 +1717,16 @@ impl<'a> Transaction<'a> { | |
| _ => {} | ||
| } | ||
| } | ||
| code_actions.sort_by(|(title1, _, _, _), (title2, _, _, _)| title1.cmp(title2)); | ||
|
|
||
| // Sort code actions: non-deprecated first, then alphabetically | ||
| code_actions.sort_by(|(title1, _, _, _), (title2, _, _, _)| { | ||
| match (title1.contains("deprecated"), title2.contains("deprecated")) { | ||
| (true, false) => Ordering::Greater, | ||
| (false, true) => Ordering::Less, | ||
| _ => title1.cmp(title2), | ||
| } | ||
| }); | ||
|
Comment on lines
+1722
to
+1728
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also feels very non-idiomatic... |
||
|
|
||
| Some(code_actions) | ||
| } | ||
|
|
||
|
|
@@ -2934,11 +2951,11 @@ impl<'a> Transaction<'a> { | |
| (result, is_incomplete) | ||
| } | ||
|
|
||
| pub fn search_exports_exact(&self, name: &str) -> Vec<Handle> { | ||
| pub fn search_exports_exact(&self, name: &str) -> Vec<(Handle, Export)> { | ||
| self.search_exports(|handle, exports| { | ||
| if let Some(export) = exports.get(&Name::new(name)) { | ||
| match export { | ||
| ExportLocation::ThisModule(_) => vec![handle.dupe()], | ||
| if let Some(export_location) = exports.get(&Name::new(name)) { | ||
| match export_location { | ||
| ExportLocation::ThisModule(export) => vec![(handle.dupe(), export.clone())], | ||
| // Re-exported modules like `foo` in `from from_module import foo` | ||
| // should likely be ignored in autoimport suggestions | ||
| // because the original export in from_module will show it. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -236,3 +236,87 @@ my_export | |
| report.trim() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_import_from_stdlib() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test is great, i'd like to keep it the one concern would be that different python / typeshed versions change where |
||
| let report = get_batched_lsp_operations_report_allow_error( | ||
| &[("a", "TypeVar('T')\n# ^")], | ||
| get_test_report, | ||
| ); | ||
| // TODO: Ideally `typing` would be preferred over `ast`. | ||
| assert_eq!( | ||
| r#" | ||
| # a.py | ||
| 1 | TypeVar('T') | ||
| ^ | ||
| Code Actions Results: | ||
| # Title: Insert import: `from ast import TypeVar` | ||
|
|
||
| ## Before: | ||
| TypeVar('T') | ||
| # ^ | ||
| ## After: | ||
| from ast import TypeVar | ||
| TypeVar('T') | ||
| # ^ | ||
| # Title: Insert import: `from typing import TypeVar` | ||
|
|
||
| ## Before: | ||
| TypeVar('T') | ||
| # ^ | ||
| ## After: | ||
| from typing import TypeVar | ||
| TypeVar('T') | ||
| # ^ | ||
| "# | ||
| .trim(), | ||
| report.trim() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_take_deprecation_into_account_in_sorting_of_actions() { | ||
| let report = get_batched_lsp_operations_report_allow_error( | ||
| &[ | ||
| ( | ||
| "a", | ||
| "from warnings import deprecated\n@deprecated('')\ndef my_func(): pass", | ||
| ), | ||
| ("b", "def my_func(): pass"), | ||
| ("c", "my_func()\n# ^"), | ||
| ], | ||
| get_test_report, | ||
| ); | ||
| assert_eq!( | ||
| r#" | ||
| # a.py | ||
|
|
||
| # b.py | ||
|
|
||
| # c.py | ||
| 1 | my_func() | ||
| ^ | ||
| Code Actions Results: | ||
| # Title: Insert import: `from b import my_func` | ||
|
|
||
| ## Before: | ||
| my_func() | ||
| # ^ | ||
| ## After: | ||
| from b import my_func | ||
| my_func() | ||
| # ^ | ||
| # Title: Insert import: `from a import my_func` (deprecated) | ||
|
|
||
| ## Before: | ||
| my_func() | ||
| # ^ | ||
| ## After: | ||
| from a import my_func | ||
| my_func() | ||
| # ^ | ||
| "# | ||
| .trim(), | ||
| report.trim() | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can probably be done more efficiently 😅