Repository: intellij-rust/intellij-rust
Issue: Completion doesn't work in the middle of word in some cases 

## Environment

* **IntelliJ Rust plugin version:**
* **Rust toolchain version:**
* **IDE name and version:**
* **Operating system:**

## Problem description
Completion doesn't work if call it in the middle of a word in some cases 

## Steps to reproduce

```rust
trait Foo { type Item; }

struct S<T>(T);
impl<T: Foo> S<T> {
    fn foo()
        where
            T::Item: ?Sized 
    {}
}
```

https://user-images.githubusercontent.com/1213122/194058760-ea254582-b9a2-4047-9604-653c9d2316fe.mov



Changed files:
src/test/kotlin/org/rust/lang/core/completion/RsFullMacroArgumentCompletionTest.kt
src/main/kotlin/org/rust/lang/core/completion/RsCompletionContributor.kt
src/test/kotlin/org/rust/lang/core/completion/RsTypeAwareCompletionTest.kt

diff --git a/src/main/kotlin/org/rust/lang/core/completion/RsCompletionContributor.kt b/src/main/kotlin/org/rust/lang/core/completion/RsCompletionContributor.kt
index a4ab9f6e3..812ab71bf 100644
--- a/src/main/kotlin/org/rust/lang/core/completion/RsCompletionContributor.kt
+++ b/src/main/kotlin/org/rust/lang/core/completion/RsCompletionContributor.kt
@@ -17,6 +17,8 @@ import org.rust.lang.core.completion.lint.RsRustcLintCompletionProvider
 import org.rust.lang.core.completion.sort.RS_COMPLETION_WEIGHERS
 import org.rust.lang.core.completion.sort.RsCompletionWeigher
 import org.rust.lang.core.or
+import org.rust.lang.core.psi.RsElementTypes
+import org.rust.lang.core.psi.ext.elementType
 import org.rust.stdext.exhaustive
 
 class RsCompletionContributor : CompletionContributor() {
@@ -52,6 +54,14 @@ class RsCompletionContributor : CompletionContributor() {
         extend(type, provider.elementPattern, provider)
     }
 
+    override fun beforeCompletion(context: CompletionInitializationContext) {
+        super.beforeCompletion(context)
+        // TODO use `in RS_IDENTIFIER_TOKENS` instead of `== RsElementTypes.IDENTIFIER`
+        if (context.file.findElementAt(context.startOffset)?.elementType == RsElementTypes.IDENTIFIER) {
+            context.dummyIdentifier = CompletionInitializationContext.DUMMY_IDENTIFIER_TRIMMED
+        }
+    }
+
     override fun fillCompletionVariants(parameters: CompletionParameters, result: CompletionResultSet) {
         super.fillCompletionVariants(parameters, withRustSorter(parameters, result))
     }
diff --git a/src/test/kotlin/org/rust/lang/core/completion/RsFullMacroArgumentCompletionTest.kt b/src/test/kotlin/org/rust/lang/core/completion/RsFullMacroArgumentCompletionTest.kt
index 81aabda30..2222fabf0 100644
--- a/src/test/kotlin/org/rust/lang/core/completion/RsFullMacroArgumentCompletionTest.kt
+++ b/src/test/kotlin/org/rust/lang/core/completion/RsFullMacroArgumentCompletionTest.kt
@@ -90,4 +90,21 @@ class RsFullMacroArgumentCompletionTest : RsCompletionTestBase() {
         impl Foo { fn bar(&self) {} }
         foo!(Foo, bar(/*caret*/));
     """)
+
+    // TODO extra `()`
+    fun `test method with a caret in the middle if the identifier`() = doSingleCompletion("""
+        macro_rules! foo {
+            ($ e:expr, $ i:ident) => { fn foo() { $ e.$ i(); } };
+        }
+        struct Foo;
+        impl Foo { fn bar(&self) {} }
+        foo!(Foo, b/*caret*/ar);
+    """, """
+        macro_rules! foo {
+            ($ e:expr, $ i:ident) => { fn foo() { $ e.$ i(); } };
+        }
+        struct Foo;
+        impl Foo { fn bar(&self) {} }
+        foo!(Foo, bar(/*caret*/)ar);
+    """)
 }
diff --git a/src/test/kotlin/org/rust/lang/core/completion/RsTypeAwareCompletionTest.kt b/src/test/kotlin/org/rust/lang/core/completion/RsTypeAwareCompletionTest.kt
index 142d01b5a..6cf316065 100644
--- a/src/test/kotlin/org/rust/lang/core/completion/RsTypeAwareCompletionTest.kt
+++ b/src/test/kotlin/org/rust/lang/core/completion/RsTypeAwareCompletionTest.kt
@@ -256,4 +256,18 @@ class RsTypeAwareCompletionTest : RsCompletionTestBase() {
             S.fo/*caret*/;
         }
     """)
+
+    fun `test complete associated type inside a where bound in the middle of an identifier`() = doSingleCompletion("""
+        trait Foo { type Item; }
+        struct S<T>(T);
+        impl<T: Foo> S<T> {
+            fn foo() where T::It/*caret*/m: ?Sized {}
+        }
+    """, """
+        trait Foo { type Item; }
+        struct S<T>(T);
+        impl<T: Foo> S<T> {
+            fn foo() where T::Item/*caret*/m: ?Sized {}
+        }
+    """)
 }