-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from SpineEventEngine/new-extensions
Move extensions from `validation` library
- Loading branch information
Showing
10 changed files
with
453 additions
and
16 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
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
72 changes: 72 additions & 0 deletions
72
psi-java/src/main/kotlin/io/spine/tools/psi/java/PsiElementExts.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,72 @@ | ||
/* | ||
* Copyright 2025, TeamDev. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Redistribution and use in source and/or binary forms, with or without | ||
* modification, must retain the above copyright notice and the following | ||
* disclaimer. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package io.spine.tools.psi.java | ||
|
||
import com.intellij.psi.PsiElement | ||
|
||
/** | ||
* Looks for the first child of this [PsiElement], the text representation | ||
* of which satisfies both [startsWith] and [contains] criteria. | ||
* | ||
* This method performs a depth-first search of the PSI hierarchy. | ||
* So, the second direct child of this [PsiElement] is checked only | ||
* when the first child and all its descendants are checked. | ||
* | ||
* @return the found element, or `null` if this [PsiElement] does not contain such an element. | ||
*/ | ||
public fun PsiElement.findFirstByText( | ||
startsWith: String, | ||
contains: String = startsWith | ||
): PsiElement? = children.firstNotNullOfOrNull { element -> | ||
val text = element.text | ||
when { | ||
!text.contains(contains) -> null | ||
text.startsWith(startsWith) -> element | ||
else -> element.findFirstByText(startsWith, contains) | ||
} | ||
} | ||
|
||
/** | ||
* Returns the first child of this [PsiElement], the text representation | ||
* of which satisfies both [startsWith] and [contains] criteria. | ||
* | ||
* This method performs a depth-first search of the PSI hierarchy. | ||
* So, the second direct child of this [PsiElement] is checked only | ||
* when the first child and all its descendants are checked. | ||
* | ||
* @throws [IllegalStateException] if this [PsiElement] does not contain such an element. | ||
*/ | ||
public fun PsiElement.getFirstByText( | ||
startsWith: String, | ||
contains: String = startsWith | ||
): PsiElement = | ||
findFirstByText(startsWith, contains) | ||
?: error { | ||
"The element was not found." + | ||
"Search criteria: [startsWith=`$startsWith`, contains=`$contains`]." + | ||
"Searched element: `$this`." | ||
} |
100 changes: 100 additions & 0 deletions
100
psi-java/src/main/kotlin/io/spine/tools/psi/java/PsiStatements.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,100 @@ | ||
/* | ||
* Copyright 2025, TeamDev. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Redistribution and use in source and/or binary forms, with or without | ||
* modification, must retain the above copyright notice and the following | ||
* disclaimer. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package io.spine.tools.psi.java | ||
|
||
import com.intellij.psi.PsiCodeBlock | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiElementFactory | ||
|
||
/** | ||
* A list of statements extracted from the given [PsiCodeBlock], | ||
* excluding the surrounding braces. | ||
* | ||
* This type addresses several challenges: | ||
* | ||
* 1. In PSI, a code block cannot be created without curly braces. As a result, even if | ||
* you only need a list of statements, you must create a full block with braces. | ||
* 2. Inserting such a block into another code block requires range copying | ||
* to skip the braces. | ||
* 3. While a list of statements can be [retrieved][PsiCodeBlock.getStatements] directly from | ||
* the code block, formatting elements like whitespace and newlines are omitted, leaving | ||
* only the raw statements. Adding these statements individually to a method body often | ||
* results in invalid or non-compilable Java code. | ||
*/ | ||
public class PsiStatements(codeBlock: PsiCodeBlock) { | ||
|
||
/** | ||
* All children of [PsiCodeBlock] without right and left braces. | ||
*/ | ||
private val children = codeBlock.children | ||
.copyOfRange(1, codeBlock.children.size - 1) | ||
|
||
/** | ||
* Returns the first child of this element. | ||
*/ | ||
public val firstChild: PsiElement = children.first() | ||
|
||
/** | ||
* Returns the last child of this element. | ||
*/ | ||
public val lastChild: PsiElement = children.last() | ||
} | ||
|
||
/** | ||
* Creates a new [PsiStatements] from the given [text]. | ||
* | ||
* @param text The text of the statements to create. | ||
* @param context The PSI element used as context for resolving references. | ||
*/ | ||
public fun PsiElementFactory.createStatementsFromText( | ||
text: String, | ||
context: PsiElement? | ||
): PsiStatements { | ||
val codeBlock = createCodeBlockFromText("{$text}", context) | ||
return PsiStatements(codeBlock) | ||
} | ||
|
||
/** | ||
* Adds the given [statements] to this [PsiElement]. | ||
*/ | ||
public fun PsiElement.add(statements: PsiStatements) { | ||
addRange(statements.firstChild, statements.lastChild) | ||
} | ||
|
||
/** | ||
* Adds the given [statements] to this [PsiElement] after the [anchor]. | ||
*/ | ||
public fun PsiElement.addAfter(statements: PsiStatements, anchor: PsiElement) { | ||
addRangeAfter(statements.firstChild, statements.lastChild, anchor) | ||
} | ||
|
||
/** | ||
* Adds the given [statements] to this [PsiElement] before the [anchor]. | ||
*/ | ||
public fun PsiElement.addBefore(statements: PsiStatements, anchor: PsiElement) { | ||
addRangeBefore(statements.firstChild, statements.lastChild, anchor) | ||
} |
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
Oops, something went wrong.