Skip to content

Commit

Permalink
Merge pull request #76 from melvic-ybanez/custom-color-scheme
Browse files Browse the repository at this point in the history
Custom color scheme
  • Loading branch information
melvic-ybanez authored Apr 17, 2022
2 parents 7adb54f + 26de4c1 commit cf24b94
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 8 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Dependencies.dependencies

ThisBuild / version := "0.3.0"
ThisBuild / version := "0.4.0-SNAPSHOT"

ThisBuild / scalaVersion := "2.13.8"

assemblyJarName in assembly := "chi-0.3.0.jar"
assemblyJarName in assembly := "chi-0.4.0-snapshot.jar"

lazy val root = (project in file("."))
.settings(
Expand Down
75 changes: 75 additions & 0 deletions src/main/scala/com/melvic/chi/views/ChiTokenMaker.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.melvic.chi.views

import com.melvic.chi.views.ChiTokenMaker.extraTokens
import org.fife.ui.rsyntaxtextarea.modes.CPlusPlusTokenMaker
import org.fife.ui.rsyntaxtextarea.{TokenMap, TokenTypes}

class ChiTokenMaker extends CPlusPlusTokenMaker {
override def addToken(array: Array[Char], start: Int, end: Int, tokenType: Int, startOffset: Int): Unit = {
val newTokenType =
if (tokenType == TokenTypes.IDENTIFIER) {
val newType = extraTokens.get(array, start, end)
if (newType > -1) newType else tokenType
} else tokenType
super.addToken(array, start, end, newTokenType, startOffset)
}
}

object ChiTokenMaker {
lazy val extraTokens = {
val tokens = new TokenMap(false)

val reservedWords = List(
"def",
"case",
"match",
"if",
"while",
"for",
"class",
"public",
"protected",
"private",
"abstract",
"object",
"val",
"var",
"import",
"trait",
"lazy",
"return"
)
val functions = List("identity", "const", "compose", "andThen", "apply", "_1", "_2", "_3")
val dataTypes = List(
"int",
"Int",
"Integer",
"float",
"Float",
"double",
"Double",
"char",
"Char",
"Character",
"String",
"List",
"byte",
"short",
"Byte",
"Short",
"long",
"Long",
"Function",
"BiFunction",
"Either",
"Left",
"Right"
)

reservedWords.foreach(tokens.put(_, TokenTypes.RESERVED_WORD))
functions.foreach(tokens.put(_, TokenTypes.FUNCTION))
dataTypes.foreach(tokens.put(_, TokenTypes.DATA_TYPE))

tokens
}
}
34 changes: 28 additions & 6 deletions src/main/scala/com/melvic/chi/views/TextAreaComponent.scala
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package com.melvic.chi.views

import com.melvic.chi.Config
import com.melvic.chi.config.Preferences
import org.fife.ui.rsyntaxtextarea.{RSyntaxTextArea, SyntaxConstants, Theme}
import org.fife.ui.rsyntaxtextarea.{AbstractTokenMakerFactory, RSyntaxTextArea, SyntaxConstants, Theme, TokenMakerFactory, TokenTypes}
import org.fife.ui.rtextarea.RTextScrollPane

import java.awt.Font
import java.awt.{Color, Font}
import java.awt.font.TextAttribute
import java.io.IOException
import scala.collection.mutable
import scala.jdk.CollectionConverters._

class TextAreaComponent extends RSyntaxTextArea(50, 50) {
setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_SCALA)
setCodeFoldingEnabled(true)
setAntiAliasingEnabled(true)
setup()

updateStyle()
updateScheme()
updateFont()

private def setup(): Unit = {
val atmf = TokenMakerFactory.getDefaultInstance.asInstanceOf[AbstractTokenMakerFactory]
atmf.putMapping("text/chi", "com.melvic.chi.views.ChiTokenMaker")
setSyntaxEditingStyle("text/chi")
setCodeFoldingEnabled(true)
setAntiAliasingEnabled(true)
}

private def updateStyle(): Unit =
try {
val theme = Theme.load(getClass.getResourceAsStream("/org/fife/ui/rsyntaxtextarea/themes/dark.xml"))
Expand All @@ -28,6 +34,22 @@ class TextAreaComponent extends RSyntaxTextArea(50, 50) {
ioe.printStackTrace()
}

private def updateScheme(): Unit = {
val scheme = getSyntaxScheme

setBackground(Color.decode("#212020"))
setCurrentLineHighlightColor(Color.decode("#191819"))
scheme.getStyle(TokenTypes.RESERVED_WORD).foreground = Color.decode("#C792EA")
scheme.getStyle(TokenTypes.RESERVED_WORD_2).foreground = Color.decode("#C792EA")
scheme.getStyle(TokenTypes.DATA_TYPE).foreground = Color.decode("#FFCB6B")
scheme.getStyle(TokenTypes.OPERATOR).foreground = Color.decode("#89DDFF")
scheme.getStyle(TokenTypes.FUNCTION).foreground = Color.decode("#89DDFF")
scheme.getStyle(TokenTypes.SEPARATOR).foreground = Color.decode("#89DDFF")
scheme.getStyle(TokenTypes.IDENTIFIER).foreground = Color.decode("#82AAFF")

revalidate()
}

private def updateFont(): Unit = {
val font = Font.createFont(Font.TRUETYPE_FONT, getClass.getResourceAsStream(Config.DefaultFontPath))

Expand Down

0 comments on commit cf24b94

Please sign in to comment.