Commit fcbd87a4 authored by Zhanna Gorelova's avatar Zhanna Gorelova Committed by intellij-monorepo-bot
Browse files

[reactor][java-uast] IDEA-314873: space between comment and code now is preserved

GitOrigin-RevId: 45c7f995af2c19b33a01cdb261186788285c869b
parent a5e7a79d
Branches unavailable Tags unavailable
No related merge requests found
Showing with 33 additions and 17 deletions
+33 -17
......@@ -138,18 +138,35 @@ internal class JavaUastCodeGenerationPlugin : UastCodeGenerationPlugin {
private fun PsiElementFactory.createExpressionStatement(expression: PsiExpression, oldElement: UElement? = null): PsiStatement? {
val comments = oldElement?.comments
val textStatement = if (comments.isNullOrEmpty()) "x;" else createStatementTextWithComment(comments)
val textStatement = if (comments.isNullOrEmpty()) "x;" else createStatementTextWithComment(comments, oldElement)
val statement = createStatementFromText(textStatement, null) as? PsiExpressionStatement ?: return null
statement.expression.replace(expression)
return statement
}
private fun createStatementTextWithComment(comments: List<UComment>): String {
val comment = comments.joinToString(separator = " ") { it.text }
private fun createStatementTextWithComment(comments: List<UComment>, oldElement: UElement): String {
val comment = comments.joinToString(prefix = getCommentPrefix(comments, oldElement), separator = " ") { it.text }
return "x;$comment"
}
private fun getCommentPrefix(comments: List<UComment>, oldElement: UElement): String {
val lastCommentPsi = comments.last().sourcePsi
val sourcePsi = oldElement.sourcePsi
val defaultPrefix = " "
if (sourcePsi == null) return defaultPrefix
val isCommentInChildren = sourcePsi.children.filterIsInstance<PsiComment>().any { it == lastCommentPsi }
val siblings = if (isCommentInChildren) {
sourcePsi.firstChild.siblings()
} else {
sourcePsi.siblings(withSelf = false)
}
val beforeComment = siblings.takeWhile { it != lastCommentPsi }.lastOrNull()
return "x; $comment"
return (beforeComment as? PsiWhiteSpace)?.text ?: defaultPrefix
}
class JavaUastElementFactory(private val project: Project) : UastElementFactory {
......@@ -413,7 +430,6 @@ class JavaUastElementFactory(private val project: Project) : UastElementFactory
return JavaUBlockExpression(blockStatement, null)
}
override fun createLambdaExpression(parameters: List<UParameterInfo>, body: UExpression, context: PsiElement?): ULambdaExpression? {
//TODO: smart handling cases, when parameters types should exist in code
val lambda = psiFactory.createExpressionFromText(
......
......@@ -15,11 +15,14 @@
*/
package org.jetbrains.uast.java.internal
import com.intellij.psi.*
import com.intellij.psi.PsiComment
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiWhiteSpace
import org.jetbrains.uast.UComment
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UExpression
import org.jetbrains.uast.UParameter
import org.jetbrains.uast.java.isSemicolon
interface JavaUElementWithComments : UElement {
override val comments: List<UComment>
......@@ -41,10 +44,4 @@ interface JavaUElementWithComments : UElement {
}
return sibling as? PsiComment
}
private fun PsiElement?.isSemicolon(): Boolean {
if (this !is PsiJavaToken) return false
return tokenType == JavaTokenType.SEMICOLON
}
}
\ No newline at end of file
......@@ -17,10 +17,7 @@ package org.jetbrains.uast.java
import com.intellij.lang.Language
import com.intellij.lang.java.JavaLanguage
import com.intellij.psi.JavaTokenType
import com.intellij.psi.PsiAnnotation
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiModifierListOwner
import com.intellij.psi.*
import com.intellij.psi.impl.source.tree.CompositeElement
import com.intellij.psi.tree.IElementType
import org.jetbrains.uast.UDeclaration
......@@ -94,3 +91,9 @@ internal fun <T> lazyPub(initializer: () -> T): Lazy<T> = lazy(LazyThreadSafetyM
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
internal inline fun <reified T : Any> Any?.asSafely(): @kotlin.internal.NoInfer T? = this as? T
fun PsiElement?.isSemicolon(): Boolean {
if (this !is PsiJavaToken) return false
return tokenType == JavaTokenType.SEMICOLON
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment