Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement fix and intention for actual struct fields expansion
- Loading branch information
Showing
13 changed files
with
1,077 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/org/rust/ide/annotator/fixes/AddStructFieldsPatFix.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package org.rust.ide.annotator.fixes | ||
|
||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import org.rust.ide.utils.expandStructFields | ||
import org.rust.ide.utils.expandTupleStructFields | ||
import org.rust.lang.core.psi.RsPatStruct | ||
import org.rust.lang.core.psi.RsPatTupleStruct | ||
import org.rust.lang.core.psi.RsPsiFactory | ||
|
||
class AddStructFieldsPatFix( | ||
element: PsiElement | ||
) : LocalQuickFixAndIntentionActionOnPsiElement(element) { | ||
override fun getText() = "Add missing fields" | ||
|
||
override fun getFamilyName() = text | ||
|
||
override fun invoke(project: Project, file: PsiFile, editor: Editor?, pat: PsiElement, endElement: PsiElement) { | ||
val factory = RsPsiFactory(project) | ||
if (pat is RsPatStruct) { | ||
expandStructFields(factory, pat) | ||
} else if (pat is RsPatTupleStruct) { | ||
expandTupleStructFields(factory, editor, pat) | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/org/rust/ide/intentions/AddStructFieldsPatIntention.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package org.rust.ide.intentions | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import org.rust.ide.utils.expandStructFields | ||
import org.rust.ide.utils.expandTupleStructFields | ||
import org.rust.lang.core.psi.* | ||
import org.rust.lang.core.psi.ext.elementType | ||
|
||
class AddStructFieldsPatIntention : RsElementBaseIntentionAction<AddStructFieldsPatIntention.Context>() { | ||
override fun getText() = "Replace .. with actual fields" | ||
|
||
override fun getFamilyName() = text | ||
|
||
data class Context( | ||
val structBody: RsPat | ||
) | ||
|
||
override fun findApplicableContext(project: Project, editor: Editor, element: PsiElement): Context? { | ||
return if (element.elementType == RsElementTypes.DOTDOT | ||
&& (element.context is RsPatStruct || element.context is RsPatTupleStruct)) { | ||
Context(element.context as RsPat) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
override fun invoke(project: Project, editor: Editor, ctx: Context) { | ||
val factory = RsPsiFactory(project) | ||
val structBody = ctx.structBody | ||
if (structBody is RsPatStruct) { | ||
expandStructFields(factory, structBody) | ||
} else if (structBody is RsPatTupleStruct) { | ||
expandTupleStructFields(factory, editor, structBody) | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/main/kotlin/org/rust/ide/utils/StructFieldsExpander.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package org.rust.ide.utils | ||
|
||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.impl.source.tree.LeafPsiElement | ||
import org.rust.lang.core.psi.* | ||
import org.rust.lang.core.psi.ext.* | ||
import org.rust.lang.core.resolve.ref.deepResolve | ||
import org.rust.openapiext.buildAndRunTemplate | ||
import org.rust.openapiext.createSmartPointer | ||
|
||
fun expandStructFields(factory: RsPsiFactory, patStruct: RsPatStruct) { | ||
val declaration = patStruct.path.reference.deepResolve() as? RsFieldsOwner ?: return | ||
val hasTrailingComma = patStruct.rbrace.getPrevNonCommentSibling()?.elementType == RsElementTypes.COMMA | ||
patStruct.dotdot?.delete() | ||
val existingFields = patStruct.patFieldList | ||
val bodyFieldNames = existingFields.map { it.kind.fieldName }.toSet() | ||
val missingFields = declaration.fields | ||
.filter { it.name !in bodyFieldNames && !it.queryAttributes.hasCfgAttr() } | ||
.map { factory.createPatField(it.name!!) } | ||
|
||
if (existingFields.isEmpty()) { | ||
addFieldsToPat(factory, patStruct, missingFields, hasTrailingComma) | ||
return | ||
} | ||
|
||
val fieldPositions = declaration.fields.withIndex().associate { it.value.name!! to it.index } | ||
var insertedFieldsAmount = 0 | ||
for (missingField in missingFields) { | ||
val missingFieldPosition = fieldPositions[missingField.kind.fieldName]!! | ||
for (existingField in existingFields) { | ||
val existingFieldPosition = fieldPositions[existingField.kind.fieldName] ?: continue | ||
if (missingFieldPosition < existingFieldPosition) { | ||
patStruct.addBefore(missingField, existingField) | ||
patStruct.addAfter(factory.createComma(), existingField.getPrevNonCommentSibling()) | ||
insertedFieldsAmount++ | ||
break | ||
} | ||
} | ||
} | ||
addFieldsToPat(factory, patStruct, missingFields.drop(insertedFieldsAmount), hasTrailingComma) | ||
} | ||
|
||
fun expandTupleStructFields(factory: RsPsiFactory, editor: Editor?, patTuple: RsPatTupleStruct) { | ||
val declaration = patTuple.path.reference.deepResolve() as? RsFieldsOwner ?: return | ||
val hasTrailingComma = patTuple.rparen.getPrevNonCommentSibling()?.elementType == RsElementTypes.COMMA | ||
val bodyFields = patTuple.childrenOfType<RsPatIdent>() | ||
val missingFieldsAmount = declaration.fields.size - bodyFields.size | ||
addFieldsToPat(factory, patTuple, createTupleStructMissingFields(factory, missingFieldsAmount), hasTrailingComma) | ||
patTuple.dotdot?.delete() | ||
editor?.buildAndRunTemplate(patTuple, patTuple.childrenOfType<RsPatBinding>().map { it.createSmartPointer() }) | ||
} | ||
|
||
private fun createTupleStructMissingFields(factory: RsPsiFactory, amount: Int): List<RsPatBinding> { | ||
val missingFields = ArrayList<RsPatBinding>(amount) | ||
for (i in 0 until amount) { | ||
missingFields.add(factory.createPatBinding("_$i")) | ||
} | ||
return missingFields | ||
} | ||
|
||
private fun addFieldsToPat(factory: RsPsiFactory, pat: RsPat, fields: List<PsiElement>, hasTrailingComma: Boolean) { | ||
var anchor = determineOrCreateAnchor(factory, pat) | ||
for (missingField in fields) { | ||
pat.addAfter(missingField, anchor) | ||
// Do not insert comma if we are in the middle of pattern | ||
// since it will cause double comma in patterns with a trailing comma. | ||
if (fields.last() == missingField) { | ||
if (anchor.nextSibling?.getNextNonCommentSibling()?.elementType != RsElementTypes.DOTDOT) { | ||
pat.addAfter(factory.createComma(), anchor.nextSibling) | ||
} | ||
} else { | ||
pat.addAfter(factory.createComma(), anchor.nextSibling) | ||
} | ||
anchor = anchor.nextSibling.nextSibling as LeafPsiElement | ||
} | ||
if (!hasTrailingComma) { | ||
anchor.delete() | ||
} | ||
} | ||
|
||
private fun determineOrCreateAnchor(factory: RsPsiFactory, pat: RsPat): PsiElement { | ||
val dots = pat.childrenOfType<LeafPsiElement>().firstOrNull { it.elementType == RsElementTypes.DOTDOT } | ||
if (dots != null) { | ||
// Picking dots prev sibling as anchor allows as to fill the pattern starting from dots position | ||
// instead of filling pattern starting from the end. | ||
return dots.getPrevNonCommentSibling()!! as LeafPsiElement | ||
} | ||
val lastElementInBody = pat.lastChild.getPrevNonCommentSibling()!! | ||
return if (lastElementInBody !is LeafPsiElement) { | ||
pat.addAfter(factory.createComma(), lastElementInBody) | ||
lastElementInBody.nextSibling | ||
} else { | ||
lastElementInBody | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/resources/intentionDescriptions/AddStructFieldsPatIntention/after.rs.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
struct Bar { baz: i32, quux: i32, eggs:i32 } | ||
fn foo(bar: Bar) { | ||
let Bar { baz, quux, eggs } = bar; | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/resources/intentionDescriptions/AddStructFieldsPatIntention/before.rs.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
struct Bar { baz: i32, quux: i32, eggs:i32 } | ||
fn foo(bar: Bar) { | ||
let Bar { baz, .. } = bar; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/resources/intentionDescriptions/AddStructFieldsPatIntention/description.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<html> | ||
<body> | ||
This intention expands actual struct fields instead of `..`. | ||
</body> | ||
</html> |
Oops, something went wrong.