Skip to content

Commit

Permalink
ColorSettingsPage test was implemented. Minor fixes in its behaviour …
Browse files Browse the repository at this point in the history
…and in SmkSyntaxAnnotator behaviour.

Resolves: #431
  • Loading branch information
Dmitry committed Nov 13, 2021
1 parent bf4fb8e commit 88a79e3
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 99 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Released on ...
- TODO (see [#NNN](https://github.com/JetBrains-Research/snakecharm/issues/NNN))

### Added
- Color Settings Page (see [#431](https://github.com/JetBrains-Research/snakecharm/issues/431))
- Weak warnings for unused 'log' sections in 'use rule' (see [#414](https://github.com/JetBrains-Research/snakecharm/issues/414))
- Weak warnings for unused 'log' sections (see [#300](https://github.com/JetBrains-Research/snakecharm/issues/300))
- Support for 'module' and 'use' keywords (see [#355](https://github.com/JetBrains-Research/snakecharm/issues/355))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,72 @@
package com.jetbrains.snakecharm.lang.highlighter

import com.intellij.codeHighlighting.RainbowHighlighter
import com.intellij.lang.Language
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
import com.intellij.openapi.editor.colors.TextAttributesKey
import com.intellij.openapi.fileTypes.SyntaxHighlighter
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory
import com.intellij.openapi.options.colors.AttributesDescriptor
import com.intellij.openapi.options.colors.ColorDescriptor
import com.intellij.openapi.options.colors.ColorSettingsPage
import com.intellij.openapi.options.colors.RainbowColorSettingsPage
import com.jetbrains.python.PythonLanguage
import com.jetbrains.python.highlighting.PyRainbowVisitor
import com.jetbrains.python.psi.LanguageLevel
import com.jetbrains.snakecharm.SnakemakeBundle
import com.jetbrains.snakecharm.SnakemakeIcons
import com.jetbrains.snakecharm.lang.SnakemakeLanguageDialect
import com.jetbrains.snakecharm.stringLanguage.lang.highlighter.SmkSLSyntaxHighlighter
import javax.swing.Icon

class SmkColorSettingsPage : ColorSettingsPage {
class SmkColorSettingsPage : RainbowColorSettingsPage {
override fun getAttributeDescriptors(): Array<AttributesDescriptor> = arrayOf(
AttributesDescriptor(SnakemakeBundle.message("smk.color.keyword"), SnakemakeSyntaxHighlighterFactory.SMK_KEYWORD),
AttributesDescriptor(SnakemakeBundle.message("smk.color.definition"), SnakemakeSyntaxHighlighterFactory.SMK_FUNC_DEFINITION),
AttributesDescriptor(SnakemakeBundle.message("smk.color.subsection"), SnakemakeSyntaxHighlighterFactory.SMK_DECORATOR),
AttributesDescriptor(SnakemakeBundle.message("smk.color.run"), SnakemakeSyntaxHighlighterFactory.SMK_PREDEFINED_DEFINITION),

AttributesDescriptor(SnakemakeBundle.message("smk.color.string.text"), SnakemakeSyntaxHighlighterFactory.SMK_TEXT),
AttributesDescriptor(SnakemakeBundle.message("smk.color.string.SL.content"), SmkSLSyntaxHighlighter.STRING_CONTENT),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.keyword"),
SnakemakeSyntaxHighlighterFactory.SMK_KEYWORD
),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.definition"),
SnakemakeSyntaxHighlighterFactory.SMK_FUNC_DEFINITION
),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.subsection"),
SnakemakeSyntaxHighlighterFactory.SMK_DECORATOR
),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.run"),
SnakemakeSyntaxHighlighterFactory.SMK_PREDEFINED_DEFINITION
),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.keyword.arg"),
SnakemakeSyntaxHighlighterFactory.SMK_KEYWORD_ARGUMENT
),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.string.text"),
SnakemakeSyntaxHighlighterFactory.SMK_TEXT
),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.string.tqs"),
SnakemakeSyntaxHighlighterFactory.SMK_TRIPLE_QUOTED_STRING
),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.string.SL.content"),
SmkSLSyntaxHighlighter.STRING_CONTENT
),
AttributesDescriptor(SnakemakeBundle.message("smk.color.string.SL.braces"), SmkSLSyntaxHighlighter.BRACES),
AttributesDescriptor(SnakemakeBundle.message("smk.color.string.SL.comma"), SmkSLSyntaxHighlighter.COMMA),
AttributesDescriptor(SnakemakeBundle.message("smk.color.string.SL.format"), SmkSLSyntaxHighlighter.FORMAT_SPECIFIER),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.string.SL.format"),
SmkSLSyntaxHighlighter.FORMAT_SPECIFIER
),
AttributesDescriptor(SnakemakeBundle.message("smk.color.string.SL.key"), SmkSLSyntaxHighlighter.ACCESS_KEY),
AttributesDescriptor(SnakemakeBundle.message("smk.color.string.SL.reference"), SmkSLSyntaxHighlighter.IDENTIFIER),
AttributesDescriptor(SnakemakeBundle.message("smk.color.string.SL.wildcard"), SmkSLSyntaxHighlighter.HIGHLIGHTING_WILDCARDS_KEY)
AttributesDescriptor(
SnakemakeBundle.message("smk.color.string.SL.reference"),
SmkSLSyntaxHighlighter.IDENTIFIER
),
AttributesDescriptor(
SnakemakeBundle.message("smk.color.string.SL.wildcard"),
SmkSLSyntaxHighlighter.HIGHLIGHTING_WILDCARDS_KEY
)
)

override fun getColorDescriptors(): Array<ColorDescriptor> = ColorDescriptor.EMPTY_ARRAY
Expand All @@ -47,33 +85,37 @@ class SmkColorSettingsPage : ColorSettingsPage {
}

override fun getDemoText(): String =
"""
<keyword>rule</keyword> <identifiers>NAME</identifiers>:
<sectionName>input</sectionName>:
<text>"file_1.txt"</text>
<sectionName>output</sectionName>:
<text>"file_2.txt"</text>
<sectionName>shell</sectionName>:
<text>"</text><injectedText>touch </injectedText><braces>{</braces><reference>output</reference><braces>}</braces><text>"</text>
number = 0.451 # Python elements are configured in Python color settings
<keyword>use</keyword> <keyword>rule</keyword> NAME <keyword>as</keyword> <identifiers>NAME_2</identifiers> <keyword>with</keyword>:
<sectionName>input</sectionName>:
<text>"{<wildcard>name</wildcard>}.bin"</text>
<sectionName>output</sectionName>:
<text>"{<wildcard>name</wildcard>}.bin"</text>
<sectionName>message</sectionName>:
<text>"</text><injectedText>Float number: </injectedText><braces>{</braces><reference>number</reference><formatSpecifier>:2f</formatSpecifier><braces>}</braces><text>"</text>
<sectionName>threads</sectionName>:
1
<keyword>rule</keyword> <identifiers>NAME3</identifiers>:
<sectionName>output</sectionName>:
<text>"</text><injectedText>file_</injectedText><braces>{</braces><reference>number</reference><comma>,</comma> \d+<braces>}</braces><text>"</text>,
<accessKey>arg</accessKey> = <text>"file_1.txt"</text>
<run>run</run>:
x = 2
"<keyword>rule</keyword> <identifiers>NAME</identifiers>:\n" +
" <TQS>\"\"\"\n" +
" Syntax Highlighting Demo" +
RainbowHighlighter.generatePaletteExample("\n ") +
"\n \"\"\"</TQS>\n" +
"""
<sectionName>input</sectionName>:
<text>"file_1.txt"</text>
<sectionName>output</sectionName>:
<text>"file_2.txt"</text>
<sectionName>shell</sectionName>:
<text>"</text><injectedText>touch </injectedText><braces>{</braces><reference>output</reference><braces>}</braces><text>"</text>
<localVar>number</localVar> = 0.451 # Python elements are configured in Python color settings
<keyword>use</keyword> <keyword>rule</keyword> NAME <keyword>as</keyword> <identifiers>NAME_2</identifiers> <keyword>with</keyword>:
<sectionName>input</sectionName>:
<text>"{<wildcard>name</wildcard>}.bin"</text>
<sectionName>output</sectionName>:
<text>"{<wildcard>name</wildcard>}.bin"</text>
<sectionName>message</sectionName>:
<text>"</text><injectedText>Float number: </injectedText><braces>{</braces><reference>number</reference><formatSpecifier>:2f</formatSpecifier><braces>}</braces><text>"</text>
<sectionName>threads</sectionName>:
1
<keyword>rule</keyword> <identifiers>NAME3</identifiers>:
<sectionName>output</sectionName>:
<text>"</text><injectedText>file_</injectedText><braces>{</braces><wildcard>number</wildcard><comma>,</comma> <braces>}</braces><text>"</text>,
<keywordArg>arg</keywordArg> = <text>"file_1.txt"</text>
<run>run</run>:
<localVar>x</localVar> = 2
""".trimIndent()

override fun getAdditionalHighlightingTagToDescriptorMap(): MutableMap<String, TextAttributesKey> =
Expand All @@ -83,6 +125,8 @@ class SmkColorSettingsPage : ColorSettingsPage {
it["sectionName"] = SnakemakeSyntaxHighlighterFactory.SMK_DECORATOR
it["run"] = SnakemakeSyntaxHighlighterFactory.SMK_PREDEFINED_DEFINITION
it["text"] = SnakemakeSyntaxHighlighterFactory.SMK_TEXT
it["TQS"] = SnakemakeSyntaxHighlighterFactory.SMK_TRIPLE_QUOTED_STRING
it["keywordArg"] = SnakemakeSyntaxHighlighterFactory.SMK_KEYWORD_ARGUMENT

it["injectedText"] = SmkSLSyntaxHighlighter.STRING_CONTENT
it["braces"] = SmkSLSyntaxHighlighter.BRACES
Expand All @@ -91,5 +135,13 @@ class SmkColorSettingsPage : ColorSettingsPage {
it["accessKey"] = SmkSLSyntaxHighlighter.ACCESS_KEY
it["reference"] = SmkSLSyntaxHighlighter.IDENTIFIER
it["wildcard"] = SmkSLSyntaxHighlighter.HIGHLIGHTING_WILDCARDS_KEY

it["localVar"] = DefaultLanguageHighlighterColors.LOCAL_VARIABLE
it.putAll(RainbowHighlighter.createRainbowHLM())
}

override fun isRainbowType(type: TextAttributesKey?): Boolean =
PyRainbowVisitor.Holder.HIGHLIGHTING_KEYS.contains(type)

override fun getLanguage(): Language = SnakemakeLanguageDialect
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,15 @@ object SmkSyntaxAnnotator : SmkAnnotator() {
st.getSectionKeywordNode()?.let {
addHighlightingAnnotation(it, SnakemakeSyntaxHighlighterFactory.SMK_DECORATOR)
}
if (st is SmkArgsSection) {
st.keywordArguments?.forEach {
it.keywordNode?.psi?.let { name ->
addHighlightingAnnotation(
name,
SnakemakeSyntaxHighlighterFactory.SMK_KEYWORD_ARGUMENT
)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,39 @@ import com.jetbrains.python.psi.impl.PythonLanguageLevelPusher
*/
class SnakemakeSyntaxHighlighterFactory : SyntaxHighlighterFactory() {
companion object {
val SMK_KEYWORD = TextAttributesKey.createTextAttributesKey("SMK_KEYWORD",
val SMK_KEYWORD = TextAttributesKey.createTextAttributesKey(
"SMK_KEYWORD",
DefaultLanguageHighlighterColors.KEYWORD
)
val SMK_FUNC_DEFINITION = TextAttributesKey.createTextAttributesKey("SMK_FUNC_DEFINITION",
val SMK_FUNC_DEFINITION = TextAttributesKey.createTextAttributesKey(
"SMK_FUNC_DEFINITION",
DefaultLanguageHighlighterColors.FUNCTION_DECLARATION
)
val SMK_DECORATOR = TextAttributesKey.createTextAttributesKey("SMK_DECORATOR",
val SMK_DECORATOR = TextAttributesKey.createTextAttributesKey(
"SMK_DECORATOR",
DefaultLanguageHighlighterColors.METADATA
)
val SMK_PREDEFINED_DEFINITION: TextAttributesKey =
PyHighlighter.PY_PREDEFINED_DEFINITION // IDK why, but explicit creating via '.createText...' works improperly

val SMK_KEYWORD_ARGUMENT = TextAttributesKey.createTextAttributesKey(
"SMK_KEYWORD_ARGUMENT",
DefaultLanguageHighlighterColors.PARAMETER
)
val SMK_TEXT = TextAttributesKey.createTextAttributesKey("SMK_TEXT", DefaultLanguageHighlighterColors.STRING)
val SMK_TRIPLE_QUOTED_STRING = TextAttributesKey.createTextAttributesKey(
"SMK_TRIPLE_QUOTED_STRING",
DefaultLanguageHighlighterColors.STRING
)
}

private val myMap = FactoryMap.create<LanguageLevel, PyHighlighter> { key ->
object : PyHighlighter(key) {
override fun getTokenHighlights(tokenType: IElementType?): Array<TextAttributesKey> {
if (tokenType === PyTokenTypes.SINGLE_QUOTED_UNICODE) {
return arrayOf(SMK_TEXT)
return when (tokenType) {
PyTokenTypes.SINGLE_QUOTED_UNICODE -> arrayOf(SMK_TEXT)
PyTokenTypes.TRIPLE_QUOTED_UNICODE -> arrayOf(SMK_TRIPLE_QUOTED_STRING)
else -> super.getTokenHighlights(tokenType)
}
return super.getTokenHighlights(tokenType)
}

override fun createHighlightingLexer(level: LanguageLevel) = SnakemakeHighlightingLexer(level)
Expand All @@ -49,7 +60,10 @@ class SnakemakeSyntaxHighlighterFactory : SyntaxHighlighterFactory() {

override fun getSyntaxHighlighter(project: Project?, virtualFile: VirtualFile?): SyntaxHighlighter {
val level = when {
project != null && virtualFile != null -> PythonLanguageLevelPusher.getLanguageLevelForVirtualFile(project, virtualFile)
project != null && virtualFile != null -> PythonLanguageLevelPusher.getLanguageLevelForVirtualFile(
project,
virtualFile
)
else -> LanguageLevel.getDefault()
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/SnakemakeBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,9 @@ smk.color.keyword=Keyword
smk.color.definition=Name definition
smk.color.subsection=Subsection
smk.color.run=Run section
smk.color.keyword.arg=Keyword argument
smk.color.string.text=String//Text
smk.color.string.tqs=String//Triple quoted string
smk.color.string.SL.content=String//SnakemakeSL//String content
smk.color.string.SL.braces=String//SnakemakeSL//Braces
smk.color.string.SL.comma=String//SnakemakeSL//Comma
Expand Down
Loading

0 comments on commit 88a79e3

Please sign in to comment.