generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#49: implement semantic highlighting
Works for all symbols and keywords. Namespaces are treated as separate highlighted elements. For symbols that was the behaviour by default and for keywords the ns range had to be manually added.
- Loading branch information
1 parent
91eb0f7
commit 2864e0b
Showing
4 changed files
with
100 additions
and
7 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
76 changes: 76 additions & 0 deletions
76
.../com/github/brcosta/cljstuffplugin/extensions/codeInsight/daemon/ClojureRainbowVisitor.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,76 @@ | ||
package com.github.brcosta.cljstuffplugin.extensions.codeInsight.daemon | ||
|
||
import com.github.brcosta.cljstuffplugin.extensions.ClojureAnnotator.Companion.isNamespaced | ||
import com.github.brcosta.cljstuffplugin.extensions.ClojureColorsAndFontsPageEx.Companion.SEMANTIC_HIGHLIGHTING | ||
import com.intellij.codeInsight.daemon.RainbowVisitor | ||
import com.intellij.codeInsight.daemon.UsedColors | ||
import com.intellij.codeInsight.daemon.impl.HighlightInfo | ||
import com.intellij.codeInsight.daemon.impl.HighlightVisitor | ||
import com.intellij.openapi.util.UserDataHolderEx | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import cursive.psi.api.ClKeyword | ||
import cursive.psi.api.ClojureFile | ||
import cursive.psi.api.symbols.ClSymbol | ||
|
||
/** | ||
* Enables semantic highlighting for all symbols and keywords. Namespaces are treated as separate highlighted elements. | ||
*/ | ||
class ClojureRainbowVisitor : RainbowVisitor() { | ||
override fun suitableForFile(file: PsiFile): Boolean { | ||
return file is ClojureFile | ||
} | ||
|
||
override fun visit(element: PsiElement) { | ||
val maybeName = when (element) { | ||
is ClSymbol -> element.name | ||
is ClKeyword -> element.name | ||
else -> null | ||
} | ||
maybeName?.let { elementName -> | ||
element.context?.let { context -> | ||
fun separateNsHighlighting(keyword: ClKeyword): List<HighlightInfo> { | ||
fun rangedInfo(name: String, start: Int, end: Int): HighlightInfo { | ||
val color = | ||
UsedColors.getOrAddColorIndex((context as UserDataHolderEx), name, highlighter.colorsCount) | ||
return highlighter.getInfo(color, start, end, SEMANTIC_HIGHLIGHTING) | ||
} | ||
|
||
val start = keyword.textRange.startOffset | ||
val nsEnd = start + keyword.text.indexOf('/') | ||
return listOf( | ||
rangedInfo( | ||
keyword.namespace, | ||
start, | ||
nsEnd, | ||
), | ||
rangedInfo( | ||
elementName, | ||
nsEnd, | ||
keyword.textRange.endOffset, | ||
) | ||
) | ||
} | ||
|
||
if (element is ClKeyword && isNamespaced(element)) | ||
separateNsHighlighting(element).forEach(::addInfo) | ||
else | ||
// separate ns highlighting for symbols works by default | ||
addInfo( | ||
getInfo( | ||
context, | ||
element, | ||
elementName, | ||
SEMANTIC_HIGHLIGHTING | ||
) | ||
) | ||
|
||
} | ||
} | ||
} | ||
|
||
override fun clone(): HighlightVisitor { | ||
return ClojureRainbowVisitor() | ||
} | ||
|
||
} |
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