-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Constant value replacement. Closes #22
- Loading branch information
1 parent
a765358
commit 2735856
Showing
13 changed files
with
263 additions
and
26 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
78 changes: 78 additions & 0 deletions
78
src/main/kotlin/org/ton/intellij/func/inspection/style/FuncConstantExpressionInspection.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,78 @@ | ||
package org.ton.intellij.func.inspection.style | ||
|
||
import com.intellij.codeInspection.* | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import org.ton.intellij.func.eval.FuncConstantExpressionEvaluator | ||
import org.ton.intellij.func.eval.FuncIntValue | ||
import org.ton.intellij.func.inspection.FuncInspectionBase | ||
import org.ton.intellij.func.psi.* | ||
import org.ton.intellij.util.InspectionBundle | ||
|
||
private const val MAX_RESULT_LENGTH_TO_DISPLAY = 40 | ||
|
||
class FuncConstantExpressionInspection : FuncInspectionBase() { | ||
override fun buildFuncVisitor( | ||
holder: ProblemsHolder, | ||
session: LocalInspectionToolSession, | ||
): FuncVisitor = object : FuncVisitor() { | ||
override fun visitExpression(expression: FuncExpression) { | ||
val parent = expression.parent | ||
if (expression is FuncLiteralExpression && expression.integerLiteral != null) return | ||
if (parent is FuncBinExpression && (parent.binaryOp.eq == null || parent.right != expression)) return | ||
if (parent is FuncExpression && parent !is FuncBinExpression) return | ||
val value = FuncConstantExpressionEvaluator.compute(holder.project, expression) ?: return | ||
val valueText = buildString { | ||
if (value is FuncIntValue && expression is FuncLiteralExpression && expression.stringLiteral != null) { | ||
append("0x") | ||
val hex = value.value.toString(16) | ||
append(hex) | ||
} else { | ||
append(value.toString()) | ||
} | ||
} | ||
val message = if (valueText.length > MAX_RESULT_LENGTH_TO_DISPLAY) { | ||
InspectionBundle.message("fix.constant.expression.name.short") | ||
} else { | ||
InspectionBundle.message("fix.constant.expression.name", valueText) | ||
} | ||
val range = TextRange.from(expression.startOffsetInParent, expression.textLength) | ||
holder.registerProblem( | ||
parent, | ||
message, | ||
ProblemHighlightType.INFORMATION, | ||
range, | ||
FuncComputeConstantExpressionFix(expression, valueText) | ||
) | ||
} | ||
} | ||
} | ||
|
||
class FuncComputeConstantExpressionFix( | ||
element: PsiElement, | ||
private val valueText: String | ||
) : LocalQuickFixAndIntentionActionOnPsiElement(element), LocalQuickFix { | ||
override fun getFamilyName(): String = | ||
InspectionBundle.message("fix.constant.expression.family.name") | ||
|
||
override fun getText(): String { | ||
return if (valueText.length < MAX_RESULT_LENGTH_TO_DISPLAY) { | ||
InspectionBundle.message("fix.constant.expression.name", valueText) | ||
} else { | ||
InspectionBundle.message("fix.constant.expression.name.short") | ||
} | ||
} | ||
|
||
override fun invoke( | ||
project: Project, | ||
file: PsiFile, | ||
editor: Editor?, | ||
startElement: PsiElement, | ||
endElement: PsiElement | ||
) { | ||
startElement.replace(FuncPsiFactory[project].createExpression(valueText)) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/org/ton/intellij/func/intentions/FuncConstantExpressionIntention.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,40 @@ | ||
package org.ton.intellij.func.intentions | ||
|
||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiElement | ||
import org.ton.intellij.func.eval.FuncConstantExpressionEvaluator | ||
import org.ton.intellij.func.eval.FuncIntValue | ||
import org.ton.intellij.func.psi.FuncExpression | ||
import org.ton.intellij.func.psi.FuncLiteralExpression | ||
import org.ton.intellij.func.psi.FuncPsiFactory | ||
import org.ton.intellij.util.InspectionBundle | ||
|
||
class FuncConstantExpressionIntention : PsiElementBaseIntentionAction() { | ||
override fun getFamilyName(): String { | ||
return InspectionBundle.message("fix.constant.expression.family.name") | ||
} | ||
|
||
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement): Boolean { | ||
if (element !is FuncExpression) return false | ||
if (element is FuncLiteralExpression && element.integerLiteral != null) return false | ||
val parent = element.parent | ||
if (parent is FuncExpression) return false | ||
return true | ||
} | ||
|
||
override fun invoke(project: Project, editor: Editor?, element: PsiElement) { | ||
val value = FuncConstantExpressionEvaluator.compute(project, element as? FuncExpression ?: return) ?: return | ||
val valueText = buildString { | ||
if (value is FuncIntValue && element is FuncLiteralExpression && element.stringLiteral != null) { | ||
append("0x") | ||
val hex = value.value.toString(16) | ||
append(hex) | ||
} else { | ||
append(value.toString()) | ||
} | ||
} | ||
element.replace(FuncPsiFactory[project].createExpression(valueText)) | ||
} | ||
} |
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
77 changes: 77 additions & 0 deletions
77
src/main/kotlin/org/ton/intellij/tact/inspections/style/TactConstantExpressionInspection.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,77 @@ | ||
package org.ton.intellij.tact.inspections.style | ||
|
||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement | ||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.codeInspection.ProblemsHolder | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiElementVisitor | ||
import com.intellij.psi.PsiFile | ||
import org.ton.intellij.tact.eval.TactConstantExpressionEvaluator | ||
import org.ton.intellij.tact.inspections.TactLocalInspectionTool | ||
import org.ton.intellij.tact.psi.TactExpression | ||
import org.ton.intellij.tact.psi.TactIntegerExpression | ||
import org.ton.intellij.tact.psi.TactPsiFactory | ||
import org.ton.intellij.tact.psi.TactVisitor | ||
import org.ton.intellij.util.InspectionBundle | ||
|
||
private const val MAX_RESULT_LENGTH_TO_DISPLAY = 40 | ||
|
||
class TactConstantExpressionInspection : TactLocalInspectionTool() { | ||
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor = | ||
ComputeConstantVisitor(holder) | ||
|
||
private class ComputeConstantVisitor( | ||
val holder: ProblemsHolder | ||
) : TactVisitor() { | ||
override fun visitExpression(expression: TactExpression) { | ||
if (expression is TactIntegerExpression) return | ||
val parent = expression.parent | ||
if (parent is TactExpression) return | ||
val value = TactConstantExpressionEvaluator.compute(holder.project, expression) ?: return | ||
val valueText = value.toString() | ||
val message = if (valueText.length > MAX_RESULT_LENGTH_TO_DISPLAY) { | ||
InspectionBundle.message("fix.constant.expression.name.short") | ||
} else { | ||
InspectionBundle.message("fix.constant.expression.name", valueText) | ||
} | ||
val range = TextRange.from(expression.startOffsetInParent, expression.textLength) | ||
holder.registerProblem( | ||
parent, | ||
message, | ||
ProblemHighlightType.INFORMATION, | ||
range, | ||
TactComputeConstantExpressionFix(expression, valueText) | ||
) | ||
} | ||
} | ||
|
||
private class TactComputeConstantExpressionFix( | ||
element: PsiElement, | ||
private val valueText: String | ||
) : LocalQuickFixAndIntentionActionOnPsiElement(element), LocalQuickFix { | ||
override fun getFamilyName(): String = | ||
InspectionBundle.message("fix.constant.expression.family.name") | ||
|
||
override fun getText(): String { | ||
return if (valueText.length < MAX_RESULT_LENGTH_TO_DISPLAY) { | ||
InspectionBundle.message("fix.constant.expression.name", valueText) | ||
} else { | ||
InspectionBundle.message("fix.constant.expression.name.short") | ||
} | ||
} | ||
|
||
override fun invoke( | ||
project: Project, | ||
file: PsiFile, | ||
editor: Editor?, | ||
startElement: PsiElement, | ||
endElement: PsiElement | ||
) { | ||
startElement.replace(TactPsiFactory[project].createExpression(valueText)) | ||
} | ||
} | ||
} |
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.