Skip to content

Commit

Permalink
Merge #4195
Browse files Browse the repository at this point in the history
4195: COMP: Assoc functions have higher priority than methods r=mchernyavsky a=mchernyavsky

Fixes #4185.

Co-authored-by: mchernyavsky <mikhail.chernyavsky@jetbrains.com>
  • Loading branch information
bors[bot] and mchernyavsky committed Aug 4, 2019
2 parents 685f648 + 8d722ff commit e78fe38
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/main/kotlin/org/rust/lang/core/completion/LookupElements.kt
Expand Up @@ -28,18 +28,19 @@ import org.rust.lang.core.types.ty.TyTypeParameter
import org.rust.lang.core.types.ty.TyUnknown
import org.rust.lang.core.types.type

const val KEYWORD_PRIORITY = 40.0
const val KEYWORD_PRIORITY = 80.0
const val PRIMITIVE_TYPE_PRIORITY = KEYWORD_PRIORITY
private const val VARIABLE_PRIORITY = 5.0
private const val ENUM_VARIANT_PRIORITY = 4.0
private const val FIELD_DECL_PRIORITY = 3.0
private const val INHERENT_IMPL_MEMBER_PRIORITY = 2.0
private const val ASSOC_FN_PRIORITY = 2.0
private const val DEFAULT_PRIORITY = 0.0
private const val MACRO_PRIORITY = -0.1
private const val DEPRECATED_PRIORITY = -1.0

private const val EXPECTED_TYPE_PRIORITY_OFFSET = 20.0
private const val LOCAL_PRIORITY_OFFSET = 10.0
private const val EXPECTED_TYPE_PRIORITY_OFFSET = 40.0
private const val LOCAL_PRIORITY_OFFSET = 20.0
private const val INHERENT_IMPL_MEMBER_PRIORITY_OFFSET = 0.1

fun createLookupElement(
element: RsElement,
Expand All @@ -59,10 +60,14 @@ fun createLookupElement(
element is RsPatBinding -> VARIABLE_PRIORITY
element is RsEnumVariant -> ENUM_VARIANT_PRIORITY
element is RsFieldDecl -> FIELD_DECL_PRIORITY
element is RsAbstractable && element.owner.isInherentImpl -> INHERENT_IMPL_MEMBER_PRIORITY
element is RsFunction && element.isAssocFn -> ASSOC_FN_PRIORITY
else -> DEFAULT_PRIORITY
}

if (element is RsAbstractable && element.owner.isInherentImpl) {
priority += INHERENT_IMPL_MEMBER_PRIORITY_OFFSET
}

if (forSimplePath && !element.canBeExported) {
// It's visible and can't be exported = it's local
priority += LOCAL_PRIORITY_OFFSET
Expand Down
Expand Up @@ -75,6 +75,68 @@ class RsCompletionSortingTest : RsTestBase() {
RsFunction::class to "a"
))

fun `test inherited before non-inherited`() = doTest("""
struct S;
impl S {
fn foo1() {}
fn foo3() {}
}
trait T {
fn foo2();
fn foo4();
}
impl T for S {
fn foo2() {}
fn foo4() {}
}
fn foo() { S::/*caret*/ }
""", listOf(
RsFunction::class to "foo1",
RsFunction::class to "foo3",
RsFunction::class to "foo2",
RsFunction::class to "foo4"
))

fun `test assoc fns before methods`() = doTest("""
struct S;
impl S {
fn foo1() {}
fn foo3(&self) {}
fn foo5() {}
fn foo7(&self) {}
}
trait T {
fn foo2();
fn foo4(&self);
fn foo6();
fn foo8(&self);
}
impl T for S {
fn foo2() {}
fn foo4(&self) {}
fn foo6() {}
fn foo8(&self) {}
}
fn foo() { S::/*caret*/ }
""", listOf(
RsFunction::class to "foo1",
RsFunction::class to "foo5",
RsFunction::class to "foo2",
RsFunction::class to "foo6",
RsFunction::class to "foo3",
RsFunction::class to "foo7",
RsFunction::class to "foo4",
RsFunction::class to "foo8"
))

fun `test locals before non-locals`() = doTest("""
struct foo2;
const foo3: () = ();
Expand Down

0 comments on commit e78fe38

Please sign in to comment.