Commit Graph

211 Commits

Author SHA1 Message Date
bors
d5366b5c19 Auto merge of #16265 - Patryk27:suggest-pub-crate-imports, r=Veykril
fix: Acknowledge `pub(crate)` imports in import suggestions

rust-analyzer has logic that discounts suggesting `use`s for private imports, but that logic is unnecessarily strict - for instance given this code:

```rust
mod foo {
    pub struct Foo;
}

pub(crate) use self::foo::*;

mod bar {
    fn main() {
        Foo$0;
    }
}
```

... RA will suggest to add `use crate::foo::Foo;`, which not only makes the code overly verbose (especially in larger code bases), but also is disjoint with what rustc itself suggests.

This commit adjusts the logic, so that `pub(crate)` imports are taken into account when generating the suggestions; considering rustc's behavior, I think this change doesn't warrant any extra configuration flag.

Note that this is my first commit to RA, so I guess the approach taken here might be suboptimal - certainly feels somewhat hacky, maybe there's some better way of finding out the optimal import path 😅
2024-01-11 09:54:22 +00:00
Patryk Wychowaniec
76aaf17794
Suggest pub(crate) imports
rust-analyzer has logic that discounts suggesting `use`s for private
imports, but that logic is unnecessarily strict - for instance given
this code:

```rust
mod foo {
    pub struct Foo;
}

pub(crate) use self::foo::*;

mod bar {
    fn main() {
        Foo$0;
    }
}
```

... RA will suggest to add `use crate::foo::Foo;`, which not only makes
the code overly verbose (especially in larger code bases), but also is
disjoint with what rustc itself suggests.

This commit adjusts the logic, so that `pub(crate)` imports are taken
into account when generating the suggestions; considering rustc's
behavior, I think this change doesn't warrant any extra configuration
flag.

Note that this is my first commit to RA, so I guess the approach taken
here might be suboptimal - certainly feels somewhat hacky, maybe there's
some better way of finding out the optimal import path 😅
2024-01-10 18:21:16 +01:00
Lukas Wirth
4d3a0dc329 Replace SourceRootCrates hashset output with slice for deterministic order 2024-01-10 14:51:51 +01:00
Lukas Wirth
9673915045 Make DefDatabase::lang_attr transparent 2024-01-09 20:52:10 +01:00
Lukas Wirth
06aaf20f10 Some minor perf improvements 2024-01-09 20:43:17 +01:00
bors
51ac6de6f3 Auto merge of #16277 - roife:fix-issue16276, r=Veykril
Resolve panic in `generate_delegate_methods`

Fixes #16276

This PR addresses two issues:

1. When using `PathTransform`, it searches for the node corresponding to the `path` in the `source_scope` during `make::fn_`. Therefore, we need to perform the transform before `make::fn_` (similar to the problem in issue #15804). Otherwise, even though the tokens are the same, their offsets (i.e., `span`) differ, resulting in the error "Can't find CONST_ARG@xxx."

2. As mentioned in the first point, `PathTransform` searches for the node corresponding to the `path` in the `source_scope`. Thus, when transforming paths, we should update nodes from right to left (i.e., use **reverse of preorder** (right -> left -> root) instead of **postorder** (left -> right -> root)). Reasons are as follows:

    In the red-green tree (rowan), we do not store absolute ranges but instead store the length of each node and dynamically calculate offsets (spans). Therefore, when modifying the left-side node (such as nodes are inserted or deleted), it causes all right-side nodes' spans to change. This, in turn, leads to PathTransform being unable to find nodes with the same paths (due to different spans), resulting in errors.
2024-01-09 15:52:34 +00:00
bors
da6f7e2c7b Auto merge of #16275 - davidsemakula:ast-path-segments, r=Veykril
fix: Fix `ast::Path::segments` implementation

calling `ast::Path::segments` on a qualifier currently returns all the segments of the top path instead of just the segments of the qualifier.

The issue can be summarized by the simple failing test below:
```rust
#[test]
fn path_segments() {
    //use ra_ap_syntax::ast;
    let path: ast::Path = ...; // e.g. `ast::Path` for "foo::bar::item".

    let path_segments: Vec<_> = path.segments().collect();
    let qualifier_segments: Vec<_> = path.qualifier().unwrap().segments().collect();
    assert_eq!(path_segments.len(), qualifier_segments.len() + 1); // Fails because `LHS = RHS`.
}
```

This PR:
- Fixes the implementation of `ast::Path::segments`
- Fixes `ast::Path::segments` callers that either implicitly relied on behavior of previous implementation or exhibited other "wrong" behavior directly related to the result of `ast::Path::segments` (all callers have been reviewed, only one required modification)
- Removes unnecessary (and now unused) `ast::Path::segments` alternatives
2024-01-09 15:41:48 +00:00
Matthias Krüger
476e10e961 remove redundant clones 2024-01-07 00:17:48 +01:00
Lukas Wirth
963568b46f feat: IDE features for primitive tuple fields 2024-01-06 15:04:58 +01:00
roife
872951d2d9 Replace 'postorder' with 'reverse of preorder' to traverse the AST in path_transform 2024-01-06 20:07:38 +08:00
davidsemakula
89d6b011c4 remove unnecessary ast::Path::segments alternatives 2024-01-06 12:53:56 +03:00
Lukas Wirth
cc2b79feeb internal: Speed up import searching some more 2024-01-05 12:58:20 +01:00
Lukas Wirth
4f75e0f910 Remove completion limit for trait importing method completions 2024-01-05 12:03:08 +01:00
Lukas Wirth
d60638e5e6 Deduplicate some code 2024-01-05 11:34:18 +01:00
Lukas Wirth
2666349392 Remove limit from symbol_index::Query 2024-01-04 19:20:10 +01:00
Lukas Wirth
0af780ea3e Fix symbol_index::Query::search_maps not adhering to case-sensitivity correctly 2024-01-04 19:12:56 +01:00
Lukas Wirth
c3a29e5528 Remove limit from import_map::Query 2024-01-04 18:12:25 +01:00
Nicolas Guichard
73d9c77f2a scip: Populate SymbolInformation::kind
SymbolInformation::kind is finer-grained than the SCIP symbol suffix.
This also fixes a bug where all type aliases where treated like type
parameters.

```
trait SomeTrait {
  type AssociatedType; // ← this is SomeTrait#[AssociatedType]
}

type MyTypeAlias = u8; // ← this used to be [MyTypeAlias]
                       //   and now is MyTypeAlias#
```
2024-01-03 13:05:36 +01:00
Nicolas Guichard
b24914970f Refactor label & docs from ide::hover::render to ide-db::defs::Definition
To build the SymbolInformation::signature_documentation we need access
to the “label” when building the TokenStaticData, preferably without
any markdown markup.
Therefore this refactors ide::hover::render::definition and its helper
functions to give easier access to the label alone.
2024-01-03 10:54:57 +01:00
Nicolas Guichard
495a55689b scip: Populate SymbolInformation::enclosing_symbol
For local variables, this gets the moniker from the enclosing
definition and stores it into the TokenStaticData.
Then it builds the scip symbol for that moniker when building the
SymbolInformation.
2024-01-03 10:54:57 +01:00
bors
86e559bf3f Auto merge of #16211 - tetsuharuohzeki:update-lint, r=Veykril
Use Cargo's [workspace.lints.*] to config clippy

This change begin to use [`[workspace.lints.*]`](https://doc.rust-lang.org/cargo/reference/workspaces.html#the-lints-table) that is stabilized since [Rust 1.74](https://blog.rust-lang.org/2023/11/16/Rust-1.74.0.html).

By this change, we make the configure more readable and simplify `xargo lint` more.
2024-01-02 14:53:22 +00:00
bors
df94a87ea8 Auto merge of #16139 - jimmyhmiller:master, r=Veykril
Make functions in impl have a container name

fixes #16015
2024-01-02 14:08:37 +00:00
bors
34df29620a Auto merge of #16112 - roife:rewrite-generate-delete-trait, r=Veykril
fix: rewrite code_action `generate_delegate_trait`

I've made substantial enhancements to the "generate delegate trait" code action in rust-analyzer. Here's a summary of the changes:

#### Resolved the "Can’t find CONST_ARG@158..159 in AstIdMap" error

Fix #15804, fix #15968, fix #15108

The issue stemmed from an incorrect application of PathTransform in the original code. Previously, a new 'impl' was generated first and then transformed, causing PathTransform to fail in locating the correct AST node, resulting in an error. I rectified this by performing the transformation before generating the new 'impl' (using make::impl_trait), ensuring a step-by-step transformation of associated items.

#### Rectified generation of `Self` type

`generate_delegate_trait` is unable to properly handle trait with `Self` type.

Let's take the following code as an example:

```rust
trait Trait {
    fn f() -> Self;
}

struct B {}
impl Trait for B {
    fn f() -> B { B{} }
}

struct S {
    b: B,
}
```

Here, if we implement `Trait` for `S`, the type of `f` should be `() -> Self`, i.e. `() -> S`. However we cannot automatically generate a function that constructs `S`.

To ensure that the code action doesn't generate delegate traits for traits with Self types, I add a function named `has_self_type` to handle it.

#### Extended support for generics in structs and fields within this code action

The former version of `generate_delegate_trait` cannot handle structs with generics properly. Here's an example:

```rust
struct B<T> {
    a: T
}

trait Trait<T> {
    fn f(a: T);
}

impl<T1, T2> Trait<T1> for B<T2> {
    fn f(a: T1) -> T2 { self.a }
}

struct A {}
struct S {
    b$0 : B<A>,
}
```

The former version  will generates improper code:

```rust
impl<T1, T2> Trait<T1, T2> for S {
    fn f(&self, a: T1) -> T1 {
        <B as Trait<T1, T2>>::f( &self.b , a)
    }
}
```

The rewritten version can handle generics properly:

```rust
impl<T1> Trait<T1> for S {
    fn f(&self, a: T1) -> T1 {
        <B<A> as Trait<T1>>::f(&self.b, a)
    }
}
```

See more examples in added unit tests.

I enabled support for generic structs in `generate_delegate_trait` through the following steps (using the code example provided):

1. Initially, to prevent conflicts between the generic parameters in struct `S` and the ones in the impl of `B`, I renamed the generic parameters of `S`.
2. Then, since `B`'s parameters are instantiated within `S`, the original generic parameters of `B` needed removal within `S` (to avoid errors from redundant parameters). An important consideration here arises when Trait and B share parameters in `B`'s impl. In such cases, these shared generic parameters cannot be removed.
3. Next, I addressed the matching of types between `B`'s type in `S` and its type in the impl. Given that some generic parameters in the impl are instantiated in `B`, I replaced these parameters with their instantiated results using PathTransform. For instance, in the example provided, matching `B<A>` and `B<T2>`, where `T2` is instantiated as `A`, I replaced all occurrences of `T2` in the impl with `A` (i.e. apply the instantiated generic arguments to the params).
4. Finally, I performed transformations on each assoc item (also to prevent the initial issue) and handled redundant where clauses.

For a more detailed explanation, please refer to the code and comments. I welcome suggestions and any further questions!
2024-01-02 12:30:19 +00:00
bors
ee0d99d98e Auto merge of #16081 - riverbl:trailing-whitespace, r=Veykril
Don't trim trailing whitespace from doc comments

Don't trim trailing whitespace from doc comments as multiple trailing spaces indicates a hard line break in Markdown.

I'd have liked to add a unit test for `docs_from_attrs`, but couldn't find a reasonable way to get an `&Attrs` object for use in the test.

Fixes #15877.
2024-01-02 10:30:58 +00:00
Tetsuharu Ohzeki
efc87092b3 Use Cargo's [workspace.lints.*] to config clippy 2023-12-29 23:51:32 +09:00
Lukas Wirth
4ec81230db Remove usages of Span::DUMMY 2023-12-20 12:53:46 +01:00
Lukas Wirth
002e611d09 fix: Deduplicate annotations 2023-12-19 08:49:00 +01:00
Lukas Wirth
f49a2fed3f internal: Move out WithFixture into dev-dep only crate 2023-12-18 15:24:08 +01:00
Lukas Wirth
66e29be1bd internal: Split out a span crate 2023-12-18 14:08:33 +01:00
Lukas Wirth
35620306a6 internal: Move proc-macro knowledge out of base-db 2023-12-18 12:37:18 +01:00
Jimmy Miller
b67b352ac7 Make functions in impl have a container name
fixes #16015
2023-12-17 13:44:47 -05:00
roife
59aa791fe6 fix: rewrite code_action generate_delegate_trait 2023-12-13 11:22:42 +08:00
Lukas Wirth
7cc6b0f2e9 Partially revert #16101 2023-12-12 22:53:40 +01:00
bors
dd42c1457d Auto merge of #16101 - Veykril:search-depedencies-fix, r=Veykril
fix: Fix `import_map::search_dependencies` getting confused by assoc and non assoc items with the same name

No test case as creating one is kind of tricky... Ideally the code should be restructured such that this collision wouldn't matter in the first place, its kind of a mess.

Fixes https://github.com/rust-lang/rust-analyzer/issues/16074
Fixes https://github.com/rust-lang/rust-analyzer/issues/16080
Fixes https://github.com/rust-lang/rust-analyzer/issues/15845
2023-12-12 14:51:25 +00:00
Lukas Wirth
ca995d765d fix: Fix import_map::search_dependencies getting confused by assoc and non assoc items with the same name 2023-12-12 15:45:42 +01:00
Lukas Wirth
1604ad1a6d Bump DEFAULT_QUERY_SEARCH_LIMIT from 40 to 100 2023-12-12 15:36:22 +01:00
Lukas Wirth
34ec665ba1 Simplify and improve perf of import_assets::import_for_item 2023-12-12 11:35:34 +01:00
Lukas Wirth
18591ae5c8 Always render the path to be imported in the completion detail 2023-12-11 18:37:12 +01:00
riverbl
99b30ba22f Don't trim trailing whitespace from doc comments
Don't trim trailing whitespace from doc comments as multiple trailing spaces indicates a hard line break in Markdown.
2023-12-10 20:54:22 +00:00
Lukas Wirth
35fbc0210c Fallback to method resolution on unresolved field access with matching method name 2023-12-08 16:36:41 +01:00
Ryan Mehri
7e768cbe70 fix: prefer keeping Self if it is in the same impl def 2023-12-08 12:30:14 +01:00
Ryan Mehri
934358e95c fix: resolve Self type references in delegate method assist 2023-12-08 12:29:34 +01:00
Lukas Wirth
9cb13b6efb Allow navigation targets to be duplicated when the focus range lies in the macro definition site 2023-12-06 12:38:19 +01:00
Lukas Wirth
d2cd30007c Implicit format args support 2023-12-05 17:07:00 +01:00
Lukas Wirth
5b8e386bae Improve macro descension API 2023-12-05 17:06:57 +01:00
Igor Matuszewski
a7224c998d Don't explicitly warn against semicolon_in_expressions_from_macros
This has been warn-by-default for two years now and has already been
added to the future-incompat lints in 1.68.
2023-12-05 11:35:09 +01:00
Lukas Wirth
81410ab500 Cleanup FileId stuff 2023-12-02 19:32:53 +01:00
Lukas Wirth
c11737cd63 Simplify include handling 2023-12-01 14:58:57 +01:00
Lukas Wirth
7a8c4c001b Turn macro_expand from query to normal function 2023-11-28 17:23:51 +01:00
Lukas Wirth
92d447f976 🧹 2023-11-28 10:55:40 +01:00
Lukas Wirth
c43078f99d Re-implement InFile wrappers as type aliases over generic InFileWrapper 2023-11-28 10:55:40 +01:00
Lukas Wirth
30093a6d81 spans always come from real file 2023-11-28 10:55:39 +01:00
Lukas Wirth
394d11b0fa Fix float-split hack not setting up spans correctly 2023-11-28 10:55:39 +01:00
Lukas Wirth
05f375eae2 hygiene 2.0 2023-11-28 10:55:39 +01:00
Lukas Wirth
890eb17b4e Replace ID based TokenMap with proper relative text-ranges / spans 2023-11-28 10:55:39 +01:00
Laurențiu Nicola
da24b6fb12 Bump memchr 2023-11-15 21:24:20 +02:00
Laurențiu Nicola
2814d646cb Bump rayon 2023-11-15 21:14:56 +02:00
Laurențiu Nicola
b18ce4f8ea Bump xshell 2023-11-15 21:12:18 +02:00
Laurențiu Nicola
1fc056ec93 Bump indexmap 2023-11-15 13:21:34 +02:00
Laurențiu Nicola
cb8434e594 Bump either 2023-11-15 13:07:36 +02:00
Laurențiu Nicola
f53368dd3f Bump tracing 2023-11-15 13:00:32 +02:00
Laurențiu Nicola
f66df10f87 Bump itertools 2023-11-15 12:53:56 +02:00
Lukas Wirth
ba61766217 Add config for preferring / ignoring prelude modules in find_path 2023-11-11 14:56:38 +01:00
Young-Flash
e0276dc5dd fix: find Self reference 2023-11-10 19:54:43 +08:00
Young-Flash
b84940b199 make generate_mut_trait_impl assist trigged for std trait only 2023-11-05 12:27:10 +08:00
Young-Flash
a723acf346 simplify the code 2023-10-31 21:03:26 +08:00
Young-Flash
4f5f7e2800 feat: import trait with alias 2023-10-22 21:39:00 +08:00
bors
954fb1d673 Auto merge of #15727 - DaniPopes:lintgen-unstable-book, r=HKalbasi
feat: generate descriptors for all unstable features

Most unstable features don't have their own chapter in the unstable book, so a rustc helper tool (`src/tools/unstable-book-gen`) generates shims to fill the gaps.
Run this tool to generate the full unstable-book source before parsing it.
2023-10-20 16:42:23 +00:00
bors
dca63d1b21 Auto merge of #15713 - Veykril:flyimport-completions-short, r=Veykril
Do flyimport completions by prefix search for short paths

Fixes https://github.com/rust-lang/rust-analyzer/issues/15711
2023-10-09 07:56:02 +00:00
DaniPopes
0c4be03456
feat: generate descriptors for all unstable features 2023-10-09 09:36:47 +02:00
DaniPopes
c5d9bfaa52
internal: fix automatic rustc/rustdoc lint generation 2023-10-07 23:24:55 +02:00
Lukas Wirth
4af730eb26 Do flyimport completions by prefix search for short paths 2023-10-05 13:21:12 +02:00
DaniPopes
53f5c1c13f
internal: re-generate lints.rs 2023-09-29 14:20:17 +02:00
DaniPopes
588c7d9182
minor: hover_simple refactor 2023-09-24 22:47:29 +02:00
bors
5855bd8579 Auto merge of #15587 - dfireBird:fix-15128, r=Veykril
Fix autoimport does nothing when importing trait that is as _ imports

Potentially fixes #15128

There are two cases of imports:
1. With simple path
2. With use tree list (or say complex path).

On deeper inspection, the [`recursive_merge`](994df3d6a3/crates/ide-db/src/imports/merge_imports.rs (L87)) function (called by [`try_merge_trees_mut`)](994df3d6a3/crates/ide-db/src/imports/merge_imports.rs (L69)) is meaningful only in the case of complex path (i.e when the UseTree contains a UseTreeList).

The [`recursive_merge`](994df3d6a3/crates/ide-db/src/imports/merge_imports.rs (L87)) function has [match with `Ok` arm](994df3d6a3/crates/ide-db/src/imports/merge_imports.rs (L106)), that is only executed when both LHS and RHS has `PathSegment` with same `NameRef`. The removal of underscore is implemented in this arm in the case of complex path.

For simple paths, the underscore is removed by checking if both LHS and RHS are simple paths and if their `Path` is same (the check is done [here](994df3d6a3/crates/ide-db/src/imports/merge_imports.rs (L74))) and remove the underscore if one is found (I made an assumption here that RHS will always be what rust-analyzer suggests to import, because at this point I'm not sure how to remove underscore with help of `ted::replace`).
2023-09-22 07:39:11 +00:00
Laurențiu Nicola
d39b45a58d Merge branch 'master' into sync-from-rust 2023-09-18 12:04:59 +03:00
bors
089ae47ebe Auto merge of #15606 - Veykril:annotation-above-item-fi, r=Veykril
fix: Fix lens location "above_whole_item" breaking lenses

Fixes https://github.com/rust-lang/rust-analyzer/issues/15602
2023-09-13 20:02:06 +00:00
Lukas Wirth
712e67cf11 fix: Fix lens location "above_whole_item" breaking lenses 2023-09-13 22:01:04 +02:00
dfireBird
df1239bf92
add tests for insert use with renamed imports
Tested for two cases:
1. Simple Use
2. Complex Use
2023-09-11 17:43:53 +05:30
dfireBird
d79486529e
remove as _ on auto importing on trait that is aliased with _ 2023-09-11 17:43:53 +05:30
Ali Bektas
0118741632 v4 2023-09-10 23:25:36 +02:00
Ali Bektas
a0c8bee35e Add more comments as requested 2023-09-10 23:19:58 +02:00
Ali Bektas
6dc7fa9423 v3 2023-09-10 23:19:58 +02:00
Ali Bektas
43edb51b21 Generalize disallowing of definition renaming 2023-09-10 23:19:58 +02:00
Ali Bektas
7ae70a06ce Disallow renaming of non-local structs 2023-09-10 23:19:58 +02:00
Andy Caldwell
7e786ea4cf Rework no_coverage to coverage(off) 2023-09-08 12:46:06 +01:00
Lukas Wirth
ca6ddd8ea3 Enable rust_analyzer for cfgs when code is being analyzed by rust-analyzer 2023-09-08 10:49:12 +02:00
Lukas Wirth
c0e402637e Emit builtin#format_args in builtin format_args expander 2023-09-06 18:08:20 +02:00
Lukas Wirth
9b8eb807a3 Parse builtin# syntax 2023-09-05 10:36:35 +02:00
Lukas Wirth
81f0108067 Remove markdown module from rust-analyzer crate 2023-09-02 17:27:52 +02:00
Lukas Wirth
8eddc64f86 Simplify 2023-09-02 17:12:57 +02:00
Lukas Wirth
b1575528c0 Move doc comment handling into ide-db 2023-09-02 16:27:26 +02:00
Laurențiu Nicola
30d8aa1bec Merge commit '9b3d03408c66749d56466bb09baf2a7177deb6ce' into sync-from-ra 2023-08-21 12:44:09 +03:00
Matthias Krüger
3ea1928fb2 Rollup merge of #114562 - Trolldemorted:thiscall, r=oli-obk
stabilize abi_thiscall

Closes https://github.com/rust-lang/rust/issues/42202, stabilizing the use of the "thiscall" ABI.

FCP was substituted by a poll, and the poll has been accepted.
2023-08-07 16:47:57 +02:00
Benedikt Radtke
96b60ed278 stabilize abi_thiscall 2023-08-07 14:11:03 +02:00
Laurențiu Nicola
aa55ce9567 Merge commit 'baee6b338b0ea076cd7a9f18d47f175dd2ba0e5d' into sync-from-ra 2023-08-07 12:03:15 +03:00
Laurențiu Nicola
4704881b64 Merge commit '37f84c101bca43b11027f30ab0c2852f9325bc3d' into sync-from-ra 2023-07-17 16:49:15 +03:00
Laurențiu Nicola
9326cf7f0c Merge commit 'cd3bf9fe51676b520c546460e6d8919b8c8ff99f' into sync-from-ra 2023-06-19 09:14:04 +03:00
Laurențiu Nicola
c48062fe2a Merge commit 'aa9bc8612514d216f84eec218dfd19ab83f3598a' into sync-from-ra 2023-06-05 12:04:23 +03:00
Laurențiu Nicola
dbf04a5ee2 ⬆️ rust-analyzer 2023-03-20 08:31:01 +02:00