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
IDE: add imports after pasting code #7597
Conversation
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.
The copy-paste processor adds wrong import in this case:
- Before
mod a {
pub struct S;
pub fn foo1() -> S { S }
}
mod b {
struct S;
fn foo2() -> S { S } // copied
}
/*caret*/
- After
use crate::a::S;
mod a {
pub struct S;
pub fn foo1() -> S { S }
}
mod b {
struct S;
fn foo2() -> S { S }
}
fn foo2() -> S { S }
Right. In this case, Should I change it so that if the original item cannot be imported, the auto import won't do anything? Or should we display some kind of conflict dialog like in the move refactoring? |
I think import map should be used even if there is a single candidate, to ensure that we add import for the correct item |
@Kobzol I thinks these are two independent features, and you should keep current implementation (taking into account #7597 (comment)) |
a2a81c4
to
583fc92
Compare
I fixed some things and added support for pat bindings. |
583fc92
to
cc1ff36
Compare
src/main/kotlin/org/rust/ide/typing/paste/RsImportCopyPasteProcessor.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/org/rust/ide/typing/paste/RsImportCopyPasteProcessor.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/org/rust/ide/typing/paste/RsImportCopyPasteProcessor.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/org/rust/ide/typing/paste/RsImportCopyPasteProcessor.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/org/rust/ide/typing/paste/RsImportCopyPasteProcessor.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/org/rust/ide/typing/paste/RsImportCopyPasteProcessor.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/org/rust/ide/typing/paste/RsImportCopyPasteProcessor.kt
Outdated
Show resolved
Hide resolved
override fun visitPatBinding(binding: RsPatBinding) { | ||
if (data.importMap.elementToFqn(binding, range) != null) { | ||
val referenceName = binding.referenceName | ||
val isNameInScope = binding.hasInScope(referenceName, VALUES) |
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.
I am not sure about this check. If we do the check, then import will not be added, and we may end up with binding recognized as variable name (user will see the warning about other match arms unreachable). If we don't do the check, import will be added and we end up with duplicated names in scope (user will see the corresponding error). I don't know which error is easier to fix (user need to change code after paste anyway). What do you think?
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.
I think that we should not do the check after all. Duplicated names are a more explicit error that makes it easy to see where the problem is, the other error is a bit more subtle.
src/main/kotlin/org/rust/ide/typing/paste/RsImportCopyPasteProcessor.kt
Outdated
Show resolved
Hide resolved
src/test/kotlin/org/rust/ide/typing/paste/RsCopyPasteTestBase.kt
Outdated
Show resolved
Hide resolved
Currently, if the thing that we would normally import is private, it will not be imported. What about this: if we encounter an element at some relative position which should be resolved to a fully qualified item, but the item is not offered by the auto import fix (since it is probably private), we would instead fully qualify that path. Then instead of an unresolved path (or a path resolved to some unrelated local item), the user would be left with a fully qualified path and a red underline with a quick action to make that item public. A similar approach with fully qualifying the name could also be used if the name to be imported is already present in the target scope. But this can be left for another PR. |
I like this idea, but lets do it in another PR for easier review process |
src/main/kotlin/org/rust/ide/typing/paste/RsImportCopyPasteProcessor.kt
Outdated
Show resolved
Hide resolved
44d781b
to
12e3fe3
Compare
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.
bors r+
Build succeeded: |
9556: IDE: Temporary disable adding imports after pasting code r=dima74 a=dima74 Looks like ["Insert imports on paste"](#7597) may be slow (https://youtrack.jetbrains.com/issue/VIM-2769, #9441 (comment)), so lets disable it by default for now Co-authored-by: Dmitry Murzin <diralik@yandex.ru>
9556: IDE: Temporary disable adding imports after pasting code r=dima74 a=dima74 Looks like ["Insert imports on paste"](#7597) may be slow (https://youtrack.jetbrains.com/issue/VIM-2769, #9441 (comment)), so lets disable it by default for now Co-authored-by: Dmitry Murzin <diralik@yandex.ru>
9556: IDE: Temporary disable adding imports after pasting code r=dima74 a=dima74 Looks like ["Insert imports on paste"](#7597) may be slow (https://youtrack.jetbrains.com/issue/VIM-2769, #9441 (comment)), so lets disable it by default for now Co-authored-by: Dmitry Murzin <diralik@yandex.ru>
9556: IDE: Temporary disable adding imports after pasting code r=dima74 a=dima74 Looks like ["Insert imports on paste"](#7597) may be slow (https://youtrack.jetbrains.com/issue/VIM-2769, #9441 (comment)), so lets disable it by default for now Co-authored-by: Dmitry Murzin <diralik@yandex.ru>
9441: IDE: qualify unimportable elements in import after paste r=dima74 a=Kobzol This is an improvement of #7597. Changes: - Fixes proper import of non-trivial paths (like `a::b`). - Unimportable paths are now fully qualified (if the items are in the same crate). changelog: Fully qualify paths from the local crate that cannot be imported after paste. Co-authored-by: Jakub Beránek <berykubik@gmail.com>
This PR adds a copy/paste postprocessor that tries to import unresolved items from the pasted code. There is also some crude support for selecting between ambiguous imports.
I took inspiration from the Kotlin plugin, mainly with the data flavor thing, I'm not sure how that's supposed to work.
Fixes: #7241
changelog: Automatically import unresolved items after copy/pasting Rust code.