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

Auto-complete for primitive types #2647

Closed
cynecx opened this issue Dec 22, 2019 · 12 comments
Closed

Auto-complete for primitive types #2647

cynecx opened this issue Dec 22, 2019 · 12 comments
Labels
A-completion autocompletion A-macro macro expansion

Comments

@cynecx
Copy link
Contributor

cynecx commented Dec 22, 2019

Inherent methods for primitive types aren't available when auto-completing.

eg.

let a = [1, 2]; // array/slice (since arrays coerce to slice)
a.{len, first, last, ...}(...);

let b = u32::from_le(12);

...
@cynecx
Copy link
Contributor Author

cynecx commented Dec 22, 2019

This issue seems to be related: #1139. However that one is closed and this is still an issue.

@flodiebold flodiebold added the A-completion autocompletion label Dec 26, 2019
@JoshMcguigan
Copy link
Contributor

I also see this issue for slices, even though this test exists.

My initial guess is the slice type needs to be added to LangItemTarget, and perhaps this causes things to work in the test, but not work when the slice implementation is from an external crate?

@flodiebold
Copy link
Member

@JoshMcguigan the slice methods work for me on current master. Are you sure you're running the newest version?

Completion for u32:: doesn't work yet; I haven't investigated, but since the methods actually resolve once you've written them, it seems to be a problem in the completion code.

@JoshMcguigan
Copy link
Contributor

JoshMcguigan commented Mar 1, 2020

Ah yes, my issue here is that by default coc-rust-analyzer does not use the cargo installed version of rust analyzer. I pointed coc-rust-analyzer at the cargo installed binary and the slice completions and type inference started working.

Thanks for double checking me here.

@davemilter
Copy link

Recent version (2020-03-02) seems changed behaviour

struct Foo {
    i: i32,
}

impl Foo {
    fn to_packet_bytes(self) -> [u8; 4] {
        self.i.to_le_bytes()
    }
}

fn main() {
    println!("Results:")
}

it recognize self.i.to_le_bytes(), but if I choose jump to source code it jumps to
.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/num/mod.rs:1440,
while first mention of to_le_bytes is on line 2230, but in the same module.

@edwin0cheng
Copy link
Member

@davemilter I can confirm that the self.i.to_le_bytes() jump to a wrong place.

Note that to_le_bytes is defined in a big macro_rules, and related to some bulitn-macros (i.e. concat).

So my wild guess is it related to #3368...

@edwin0cheng edwin0cheng added the A-macro macro expansion label Mar 3, 2020
@Dushistov
Copy link

No competition for copy_from_slice I suppose also should be related to this issue:

fn main() {
    let mut a = [0u8; 4];
    let b = [17u8; 3];
    a[0..3].|
//    a[0..3].copy_from_slice(&b);
    println!("a: {:?}", a);
}

@edwin0cheng
Copy link
Member

edwin0cheng commented May 7, 2020

I just tested it , and the following works:

fn main() {
    let mut a = [0u8; 4];
    let b = [17u8; 3];
    a[0usize..3usize].<|>   // a lot of functions appears in completion
    println!("a: {:?}", a);
}

So I think the remain problem related to handle integer variables in Chalk.

closed in favor of #2534

@eric-burel
Copy link

eric-burel commented Sep 8, 2022

Sorry to dig and old thread but google will point me there when trying to understand why I have no autocompletion for primitive types.
I've noticed that VS Code suggest me to add mod primitive_types; in src/main.rs if I want primitive types (eg a Character) to be detected correctly. When I add this line, I indeed have the correct type hints and method completion for a variable that is a character.

However I couldn't achieve the same in the "rustling" tutorial.

Expected (I could only reproduce in an existing project by adding mod primitive_types, but not in "rustlings"):
image

Actual (no auto-completion):
image

But it's totally unclear what happens, searching for "mod primitive_types" in Google points me to nothing. I'll update this message if I find more information but for beginners this is still quite confusing how to enable primitive_types completion in VS code.

Update:

  • for the specific case of Rustling, the Readme indicates how to enable Rust analyzer, by generating a rust-project.json. This file correctly setups module roots.
  • it seems that if I create a new file in a project, it will only enjoy Rust Analyzer autocompletion if it is actually imported by src/main.rs or it's dependency. This is different from say TypeScript that will try to analyse all ".ts" file matching the "src" option.
  • Vs Code will propose you to import your file in "main.rs" in the code action suggestion, which will in turn enable autocompletion. The message might be slightly unclear about the fact that "mod yourfile.rs" is added to "main.rs", and not to "yourfile.rs" (this might trick beginners though more advanced rustacean better understand what "mod" is)
  • I am not sure yet about the relation with mod primitive_types;. The project where I needed it is modenv a relatively simple open source project I cloned to get my hands on a real app made by someone else. This seems like a way to have a more global autocompletion, so my "other-file.rs" get's autocompleted eventhough it's not yet imported by my "main.rs". see point above

It seems that for Rust Analyzer to work correctly, the project must have a certain structure. Coming from JS, it's like needing a proper "tsconfig.json" to enable TypeScript analysis on all files.

@flodiebold
Copy link
Member

@eric-burel The mod primitive_types suggestion sounds very weird. Do you have a file primitive_types.rs? If you add the mod and ctrl-click on primitive_types, where does it take you? Can you show what exactly the error message and suggestion looks like?

@eric-burel
Copy link

eric-burel commented Sep 8, 2022

Updated my comment, this is because my file was named "primitive_types.rs" lol. And the code suggestion is not very clear:
image

I thought "primitive_type" was some kind of standard module, while it was just my own file. If I rename the file to "test.rs" it will propose "test". And then adding this line to "main.rs" will make my file available to Rust analyzer, in turn activating autocompletion.

It could be interesting in this action to add more information (however I don't know how to do so without bloating the message), maybe about where the "mod foobar" will be added, since it's not the current file but the main.rs or the module root.

@flodiebold
Copy link
Member

I do think the code action should say "Insert mod test; in main.rs".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion autocompletion A-macro macro expansion
Projects
None yet
Development

No branches or pull requests

7 participants