Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3996: INT & ANN: Implement intention and fix for struct fields expansion instead of `..` r=vlad20012 a=shevtsiv <!-- Hello and thank you for the pull request! We don't have any strict rules about pull requests, but you might check https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTING.md for some hints! Note that we need an electronic CLA for contributions: https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTING.md#cla After you sign the CLA, please add your name to https://github.com/intellij-rust/intellij-rust/blob/master/CONTRIBUTORS.txt :) --> In this pull request I intend to do the following: 1) Create E0023 and E0027 error classes and implement analysis for them. 2) Create fix for those errors. 3) Create intention for struct fields expansion. 4) Create a module with shared code for using it in the fix and intention. Once these steps are complete I will squash all the commits and #3928 should be closed after merge. 4094: TY&RES: implement hygiene & infer types in macros r=undin a=vlad20012 Now we take into account any custom macros in type inference Co-authored-by: shevtsiv <rostykshevtsiv@gmail.com> Co-authored-by: vlad20012 <beskvlad@gmail.com>
- Loading branch information
Showing
30 changed files
with
1,649 additions
and
41 deletions.
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
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
23 changes: 23 additions & 0 deletions
23
...main/kotlin/org/rust/lang/core/psi/RsWithMacroExpansionsRecursiveElementWalkingVisitor.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,23 @@ | ||
/* | ||
* Use of this source code is governed by the MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
|
||
package org.rust.lang.core.psi | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiRecursiveElementWalkingVisitor | ||
import org.rust.lang.core.psi.ext.expansion | ||
|
||
abstract class RsWithMacroExpansionsRecursiveElementWalkingVisitor : PsiRecursiveElementWalkingVisitor() { | ||
override fun visitElement(element: PsiElement) { | ||
if (element is RsMacroCall && element.macroArgument != null) { | ||
val expansion = element.expansion ?: return | ||
for (expandedElement in expansion.elements) { | ||
visitElement(expandedElement) | ||
} | ||
} else { | ||
super.visitElement(element) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.