Skip to content
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

Generating a function in another module doesn't work #4551

Closed
fannheyward opened this issue May 21, 2020 · 7 comments · Fixed by #5480
Closed

Generating a function in another module doesn't work #4551

fannheyward opened this issue May 21, 2020 · 7 comments · Fixed by #5480
Labels
E-unknown It's unclear if the issue is E-hard or E-easy without digging in

Comments

@fannheyward
Copy link
Contributor

The new Add function code action by SnippetTextEdit will fail to generate the function.

For example, there is no bar function in foo.rs, foo::bar(); provides Add function action, but will not generate the function after the action fired.

@lnicola
Copy link
Member

lnicola commented May 21, 2020

It works for me when the module already exists:

fn main() {
    bar::foo(42);
}

mod bar {
}

It also works in the current module:

mod bar {
    pub(crate) fn foo(arg: i32) {
        self::baz();
        // or even
        crate::bar::baz();
    }
}

But it doesn't work with bar::baz because that's actually bar::bar::baz, which doesn't exist.

EDIT: ah, you're testing with a different file.

@matklad matklad added the E-unknown It's unclear if the issue is E-hard or E-easy without digging in label May 23, 2020
@matklad
Copy link
Member

matklad commented May 23, 2020

cc @TimoFreiberg

@TimoFreiberg
Copy link
Contributor

I won't be able to look into this for another few days, but I'm planning on doing so

@fannheyward
Copy link
Contributor Author

After some tests, I found that Add function action in same module with a correct textEdit range, but if the module in a different file, the range is incorrect.

@TimoFreiberg
Copy link
Contributor

I can't reproduce this issue with current versions, it works properly in nvim using coc-rust-analyzer. I can't try VS Code right now as it's somehow borked on my machine 🤷
I'd be very thankful if anyone could try to reproduce the bug using VS Code to see if it's fixed on the latest nightly.

@fannheyward
Copy link
Contributor Author

Still not work with RA+VSCode. Yes, coc-rust-analyzer works for this, but fails to do selection, I just disable it because the wrong range, will look into this later.

@TimoFreiberg
Copy link
Contributor

TimoFreiberg commented Jul 21, 2020

Aw...
https://github.com/rust-analyzer/rust-analyzer/blob/a3ff2751b4cf8625e2874a482fe4d58da3d02891/editors/code/src/snippets.ts#L9
vscode.window.visibleTextEditors seems to only include editors whose content is displayed at that moment.
It works when I open the other file in a separate split, but only then.

As expected, adding a await vscode.window.showTextDocument(uri, {}) before the linked line fixes it, and I personally like if the editor jumps to the generated code even if it's another file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-unknown It's unclear if the issue is E-hard or E-easy without digging in
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants