-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: parse property
style.*
in a non-shape map
- Loading branch information
Showing
12 changed files
with
237 additions
and
221 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,43 @@ | ||
// This is a generated file. Not intended for manual editing. | ||
package org.jetbrains.plugins.d2.lang.psi | ||
|
||
import com.intellij.extapi.psi.ASTDelegatePsiElement | ||
import com.intellij.extapi.psi.ASTWrapperPsiElement | ||
import com.intellij.lang.ASTNode | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.impl.source.tree.SharedImplUtil | ||
import com.intellij.ui.ColorHexUtil | ||
import org.jetbrains.plugins.d2.NAMED_COLORS | ||
import java.awt.Color | ||
|
||
class ShapeProperty(node: ASTNode) : ASTWrapperPsiElement(node) | ||
class ShapeProperty(node: ASTNode) : AstWrapperPsiElement(node) | ||
|
||
class ShapePropertyKey(node: ASTNode) : ASTWrapperPsiElement(node) | ||
class ShapePropertyKey(node: ASTNode) : AstWrapperPsiElement(node) | ||
|
||
class IdPropertyMap(node: ASTNode) : ASTWrapperPsiElement(node) | ||
class IdProperty(node: ASTNode) : ASTWrapperPsiElement(node) | ||
class IdPropertyMap(node: ASTNode) : AstWrapperPsiElement(node) | ||
|
||
class D2Array(node: ASTNode) : ASTWrapperPsiElement(node) | ||
class D2Array(node: ASTNode) : AstWrapperPsiElement(node) | ||
|
||
sealed interface PropertyValue : PsiElement | ||
|
||
class OtherValue(node: ASTNode) : ASTWrapperPsiElement(node), PropertyValue | ||
class OtherValue(node: ASTNode) : AstWrapperPsiElement(node), PropertyValue | ||
|
||
class StringValue(node: ASTNode) : ASTWrapperPsiElement(node), PropertyValue, ColorValueProvider { | ||
class StringValue(node: ASTNode) : AstWrapperPsiElement(node), PropertyValue, ColorValueProvider { | ||
override fun getColor(): Color? = NAMED_COLORS.get(text.removeSurrounding("\"")) | ||
} | ||
|
||
class UnquotedStringValue(node: ASTNode) : ASTWrapperPsiElement(node), PropertyValue, ColorValueProvider { | ||
class UnquotedStringValue(node: ASTNode) : AstWrapperPsiElement(node), PropertyValue, ColorValueProvider { | ||
override fun getColor(): Color? = NAMED_COLORS.get(text) | ||
} | ||
|
||
class ColorValue(node: ASTNode) : ASTWrapperPsiElement(node), PropertyValue, ColorValueProvider { | ||
class ColorValue(node: ASTNode) : AstWrapperPsiElement(node), PropertyValue, ColorValueProvider { | ||
override fun getColor(): Color? = ColorHexUtil.fromHexOrNull(text.removeSurrounding("\"")) | ||
} | ||
|
||
// toString doesn't print node element type | ||
sealed class AstWrapperPsiElement(private val node: ASTNode) : ASTDelegatePsiElement() { | ||
override fun getParent(): PsiElement = SharedImplUtil.getParent(node) | ||
|
||
override fun getNode(): ASTNode = node | ||
|
||
override fun toString(): String = javaClass.simpleName | ||
} |
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,58 @@ | ||
package org.jetbrains.plugins.d2.lang.psi | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiReference | ||
import com.intellij.psi.util.descendants | ||
|
||
internal class ShapePsiReference(private val reference: ShapeId) : PsiReference { | ||
override fun getElement() = reference | ||
|
||
override fun getRangeInElement() = reference.getValueTextRange() | ||
|
||
// todo FQN | ||
override fun resolve(): PsiElement? { | ||
var lastCandidate: ShapeId? = null | ||
for (element in reference.containingFile.descendants(canGoInside = { it !is IdPropertyMap && it !is D2Array && it !is ShapeProperty })) { | ||
if (element is ShapeDeclaration) { | ||
val shapeId = element.findId() | ||
if (shapeId != null && compare(shapeId, reference)) { | ||
return shapeId | ||
} | ||
} else if (element is ShapeId) { | ||
if (compare(element, reference)) { | ||
lastCandidate = element | ||
} | ||
} | ||
|
||
if (element === reference) { | ||
break | ||
} | ||
} | ||
|
||
return lastCandidate | ||
} | ||
|
||
override fun getCanonicalText(): String = reference.name ?: "" | ||
|
||
override fun handleElementRename(newElementName: String): PsiElement? { | ||
return reference.setName(newElementName) | ||
} | ||
|
||
override fun bindToElement(element: PsiElement): PsiElement? = null | ||
|
||
override fun isReferenceTo(element: PsiElement): Boolean { | ||
if (element !is ShapeId) { | ||
return false | ||
} | ||
|
||
// todo FQN | ||
// check first text length to avoid string compare | ||
return compare(reference, element) | ||
} | ||
|
||
override fun isSoft() = true | ||
} | ||
|
||
private fun compare(a: ShapeId, b: ShapeId): Boolean { | ||
return a.getValueTextRange().length == b.getValueTextRange().length && a.name == b.name | ||
} |
Oops, something went wrong.