diff options
author | Oscar Adame Vázquez <oscarad@google.com> | 2022-02-08 17:52:20 -0800 |
---|---|---|
committer | Oscar Adame Vázquez <oscarad@google.com> | 2022-03-21 21:42:40 +0000 |
commit | 872224b0fce3f787332f1242629b6e59c0e0eab9 (patch) | |
tree | e7375c57d11a31df86de6ce06f987bdbd1b277aa /compose-ide-plugin | |
parent | 840c20e1cd1e650d49346f7e5fcde332206ab5b7 (diff) | |
download | idea-872224b0fce3f787332f1242629b6e59c0e0eab9.tar.gz |
[Compose-CL] Autocomplete IDs on special anchors
Special anchors only need the ID they're being constrained to.
Bug: 207030860
Test: completeConstraintIdsInSpecialAnchors
Change-Id: I73ea5a1ad7178b5f47274d4d299eb3f8cc817852
Diffstat (limited to 'compose-ide-plugin')
3 files changed, 42 insertions, 3 deletions
diff --git a/compose-ide-plugin/src/com/android/tools/compose/code/completion/constraintlayout/ConstraintLayoutJsonCompletionContributor.kt b/compose-ide-plugin/src/com/android/tools/compose/code/completion/constraintlayout/ConstraintLayoutJsonCompletionContributor.kt index 26d8955f872..33b99776a7c 100644 --- a/compose-ide-plugin/src/com/android/tools/compose/code/completion/constraintlayout/ConstraintLayoutJsonCompletionContributor.kt +++ b/compose-ide-plugin/src/com/android/tools/compose/code/completion/constraintlayout/ConstraintLayoutJsonCompletionContributor.kt @@ -64,11 +64,18 @@ class ConstraintLayoutJsonCompletionContributor : CompletionContributor() { CompletionType.BASIC, // Complete ConstraintSet names in Extends keyword jsonStringValue() - .withPropertyParentAtLevel(2, KeyWords.Extends), + .withPropertyParentAtLevel(BASE_DEPTH_FOR_LITERAL_IN_PROPERTY, KeyWords.Extends), ConstraintSetNamesProvider ) extend( CompletionType.BASIC, + // Complete IDs on special anchors, they take a single string value + jsonStringValue() + .withPropertyParentAtLevel(BASE_DEPTH_FOR_LITERAL_IN_PROPERTY, SpecialAnchor.values().map { it.keyWord }), + ConstraintIdsProvider + ) + extend( + CompletionType.BASIC, // Complete IDs in the constraint array (first position) jsonStringValue() // First element in the array, ie: there is no PsiElement preceding the desired one at this level diff --git a/compose-ide-plugin/src/com/android/tools/compose/code/completion/constraintlayout/PatternUtils.kt b/compose-ide-plugin/src/com/android/tools/compose/code/completion/constraintlayout/PatternUtils.kt index 84f14bb15d9..19ce943f492 100644 --- a/compose-ide-plugin/src/com/android/tools/compose/code/completion/constraintlayout/PatternUtils.kt +++ b/compose-ide-plugin/src/com/android/tools/compose/code/completion/constraintlayout/PatternUtils.kt @@ -55,12 +55,19 @@ internal inline fun <reified T : PsiElement> psiElement(): PsiElementPattern<T, internal inline fun <reified T : PsiElement> PsiElementPattern<*, *>.withParent() = this.withParent(T::class.java) /** + * Pattern such that when traversing up the tree from the current element, the element at [level] is a [JsonProperty]. And its name matches + * the given [name]. + */ +internal fun PsiElementPattern<*, *>.withPropertyParentAtLevel(level: Int, name: String) = + withPropertyParentAtLevel(level, listOf(name)) + +/** * Pattern such that when traversing up the tree from the current element, the element at [level] is a [JsonProperty]. Which name matches * one of the given [names]. */ -internal fun PsiElementPattern<*, *>.withPropertyParentAtLevel(level: Int, vararg names: String) = +internal fun PsiElementPattern<*, *>.withPropertyParentAtLevel(level: Int, names: Collection<String>) = this.withSuperParent(level, psiElement<JsonProperty>().withChild( - psiElement<JsonReferenceExpression>().withText(StandardPatterns.string().oneOf(*names))) + psiElement<JsonReferenceExpression>().withText(StandardPatterns.string().oneOf(names))) ) /** diff --git a/compose-ide-plugin/testSrc/com/android/tools/compose/code/completion/constraintlayout/ConstraintLayoutJsonCompletionContributorTest.kt b/compose-ide-plugin/testSrc/com/android/tools/compose/code/completion/constraintlayout/ConstraintLayoutJsonCompletionContributorTest.kt index 91b4da64308..cffbee036db 100644 --- a/compose-ide-plugin/testSrc/com/android/tools/compose/code/completion/constraintlayout/ConstraintLayoutJsonCompletionContributorTest.kt +++ b/compose-ide-plugin/testSrc/com/android/tools/compose/code/completion/constraintlayout/ConstraintLayoutJsonCompletionContributorTest.kt @@ -149,6 +149,31 @@ internal class ConstraintLayoutJsonCompletionContributorTest { } @Test + fun completeConstraintIdsInSpecialAnchors() { + @Language("JSON5") + val content = + """ + { + ConstraintSets: { + start: { + id1: { + center: '$caret' + }, + id2: {}, + id3: {} + } + } + } + """.trimIndent() + myFixture.configureByText("myscene.json", content) + myFixture.completeBasic() + val lookupElements = myFixture.lookupElementStrings!! + + assertThat(lookupElements).hasSize(3) + assertThat(lookupElements).containsExactly("id2", "id3", "parent") + } + + @Test fun completeAnchorsInConstraintArray() { @Language("JSON5") var content: String = |