-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 #814 from Golmote/prism-kotlin
Add support for Kotlin
- Loading branch information
Showing
14 changed files
with
520 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
(function (Prism) { | ||
Prism.languages.kotlin = Prism.languages.extend('clike', { | ||
'keyword': { | ||
// The lookbehind prevents wrong highlighting of e.g. kotlin.properties.get | ||
pattern: /(^|[^.])\b(?:abstract|annotation|as|break|by|catch|class|companion|const|constructor|continue|crossinline|data|do|else|enum|final|finally|for|fun|get|if|import|in|init|inline|inner|interface|internal|is|lateinit|noinline|null|object|open|out|override|package|private|protected|public|reified|return|sealed|set|super|tailrec|this|throw|to|try|val|var|when|where|while)\b/, | ||
lookbehind: true | ||
}, | ||
'function': [ | ||
/\w+(?=\s*\()/, | ||
{ | ||
pattern: /(\.)\w+(?=\s*\{)/, | ||
lookbehind: true | ||
} | ||
], | ||
'number': /\b(?:0[bx][\da-fA-F]+|\d+(?:\.\d+)?(?:e[+-]?\d+)?[fFL]?)\b/, | ||
'operator': /\+[+=]?|-[-=>]?|==?=?|!(?:!|==?)?|[\/*%<>]=?|[?:]:?|\.\.|&&|\|\||\b(?:and|inv|or|shl|shr|ushr|xor)\b/ | ||
}); | ||
|
||
delete Prism.languages.kotlin["class-name"]; | ||
|
||
Prism.languages.insertBefore('kotlin', 'string', { | ||
'raw-string': { | ||
pattern: /(["'])\1\1[\s\S]*?\1{3}/, | ||
alias: 'string' | ||
// See interpolation below | ||
} | ||
}); | ||
Prism.languages.insertBefore('kotlin', 'keyword', { | ||
'annotation': { | ||
pattern: /\B@(?:\w+:)?(?:[A-Z]\w*|\[[^\]]+\])/, | ||
alias: 'builtin' | ||
} | ||
}); | ||
Prism.languages.insertBefore('kotlin', 'function', { | ||
'label': { | ||
pattern: /\w+@|@\w+/, | ||
alias: 'symbol' | ||
} | ||
}); | ||
|
||
var interpolation = [ | ||
{ | ||
pattern: /\$\{[^}]+\}/, | ||
inside: { | ||
delimiter: { | ||
pattern: /^\$\{|\}$/, | ||
alias: 'variable' | ||
}, | ||
rest: Prism.util.clone(Prism.languages.kotlin) | ||
} | ||
}, | ||
{ | ||
pattern: /\$\w+/, | ||
alias: 'variable' | ||
} | ||
]; | ||
|
||
Prism.languages.kotlin['string'] = { | ||
pattern: Prism.languages.kotlin['string'], | ||
inside: { | ||
interpolation: interpolation | ||
} | ||
}; | ||
Prism.languages.kotlin['raw-string'].inside = { | ||
interpolation: interpolation | ||
}; | ||
|
||
}(Prism)); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<h1>Kotlin</h1> | ||
<p>To use this language, use the class "language-kotlin".</p> | ||
|
||
<h2>Numbers</h2> | ||
<pre><code>123 | ||
123L | ||
0x0F | ||
0b00001011 | ||
123.5 | ||
123.5e10 | ||
123.5f | ||
123.5F</code></pre> | ||
|
||
<h2>Strings and interpolation</h2> | ||
<pre><code>'2' | ||
'\uFF00' | ||
'\'' | ||
|
||
"foo $bar \"baz" | ||
""" | ||
foo ${40 + 2} | ||
baz${bar()} | ||
"""</code></pre> | ||
|
||
<h2>Labels</h2> | ||
<pre><code>loop@ for (i in 1..100) { | ||
for (j in 1..100) { | ||
if (...) | ||
break@loop | ||
} | ||
}</code></pre> | ||
|
||
<h2>Annotations</h2> | ||
<pre><code>public class MyTest { | ||
lateinit var subject: TestSubject | ||
|
||
@SetUp fun setup() { | ||
subject = TestSubject() | ||
} | ||
|
||
@Test fun test() { | ||
subject.method() // dereference directly | ||
} | ||
}</code></pre> | ||
|
||
<h2>Full example</h2> | ||
<pre><code>package com.example.html | ||
|
||
interface Element { | ||
fun render(builder: StringBuilder, indent: String) | ||
|
||
override fun toString(): String { | ||
val builder = StringBuilder() | ||
render(builder, "") | ||
return builder.toString() | ||
} | ||
} | ||
|
||
class TextElement(val text: String): Element { | ||
override fun render(builder: StringBuilder, indent: String) { | ||
builder.append("$indent$text\n") | ||
} | ||
} | ||
|
||
abstract class Tag(val name: String): Element { | ||
val children = arrayListOf<Element>() | ||
val attributes = hashMapOf<String, String>() | ||
|
||
protected fun initTag<T: Element>(tag: T, init: T.() -> Unit): T { | ||
tag.init() | ||
children.add(tag) | ||
return tag | ||
} | ||
|
||
override fun render(builder: StringBuilder, indent: String) { | ||
builder.append("$indent<$name${renderAttributes()}>\n") | ||
for (c in children) { | ||
c.render(builder, indent + " ") | ||
} | ||
builder.append("$indent</$name>\n") | ||
} | ||
|
||
private fun renderAttributes(): String? { | ||
val builder = StringBuilder() | ||
for (a in attributes.keySet()) { | ||
builder.append(" $a=\"${attributes[a]}\"") | ||
} | ||
return builder.toString() | ||
} | ||
} | ||
|
||
abstract class TagWithText(name: String): Tag(name) { | ||
operator fun String.plus() { | ||
children.add(TextElement(this)) | ||
} | ||
} | ||
|
||
class HTML(): TagWithText("html") { | ||
fun head(init: Head.() -> Unit) = initTag(Head(), init) | ||
|
||
fun body(init: Body.() -> Unit) = initTag(Body(), init) | ||
} | ||
|
||
class Head(): TagWithText("head") { | ||
fun title(init: Title.() -> Unit) = initTag(Title(), init) | ||
} | ||
|
||
class Title(): TagWithText("title") | ||
|
||
abstract class BodyTag(name: String): TagWithText(name) { | ||
fun b(init: B.() -> Unit) = initTag(B(), init) | ||
fun p(init: P.() -> Unit) = initTag(P(), init) | ||
fun h1(init: H1.() -> Unit) = initTag(H1(), init) | ||
fun a(href: String, init: A.() -> Unit) { | ||
val a = initTag(A(), init) | ||
a.href = href | ||
} | ||
} | ||
|
||
class Body(): BodyTag("body") | ||
|
||
class B(): BodyTag("b") | ||
class P(): BodyTag("p") | ||
class H1(): BodyTag("h1") | ||
class A(): BodyTag("a") { | ||
public var href: String | ||
get() = attributes["href"]!! | ||
set(value) { | ||
attributes["href"] = value | ||
} | ||
} | ||
|
||
fun html(init: HTML.() -> Unit): HTML { | ||
val html = HTML() | ||
html.init() | ||
return html | ||
}</code></pre> | ||
|
||
<h2>Known failures</h2> | ||
<p>There are certain edge cases where Prism will fail. | ||
There are always such cases in every regex-based syntax highlighter. | ||
However, Prism dares to be open and honest about them. | ||
If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug. | ||
</p> | ||
|
||
<h3>Comment-like substrings</h3> | ||
<pre><code>"foo /* bar */";</code></pre> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@Deprecated(SUBSYSTEM_DEPRECATED) | ||
@SetUp | ||
@Suppress | ||
@field:Ann | ||
@file:JvmName | ||
@set:[Inject VisibleForTesting] | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["annotation", "@Deprecated"], ["punctuation", "("], "SUBSYSTEM_DEPRECATED", ["punctuation", ")"], | ||
["annotation", "@SetUp"], | ||
["annotation", "@Suppress"], | ||
["annotation", "@field:Ann"], | ||
["annotation", "@file:JvmName"], | ||
["annotation", "@set:[Inject VisibleForTesting]"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for annotations. |
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,16 @@ | ||
foo() | ||
foo_Bar_42() | ||
list.filter {} | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["function", "foo"], ["punctuation", "("], ["punctuation", ")"], | ||
["function", "foo_Bar_42"], ["punctuation", "("], ["punctuation", ")"], | ||
"\r\nlist", ["punctuation", "."], | ||
["function", "filter"], ["punctuation", "{"], ["punctuation", "}"] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for functions. |
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,46 @@ | ||
"$foo ${bar} ${'$'} ${foobar()}" | ||
""" | ||
$foo ${bar} | ||
${'$'} ${foobar()} | ||
""" | ||
|
||
---------------------------------------------------- | ||
|
||
[ | ||
["string", [ | ||
"\"", | ||
["interpolation", "$foo"], | ||
["interpolation", [ | ||
["delimiter", "${"], "bar", ["delimiter", "}"] | ||
]], | ||
["interpolation", [ | ||
["delimiter", "${"], ["string", "'$'"], ["delimiter", "}"] | ||
]], | ||
["interpolation", [ | ||
["delimiter", "${"], | ||
["function", "foobar"], ["punctuation", "("], ["punctuation", ")"], | ||
["delimiter", "}"] | ||
]], | ||
"\"" | ||
]], | ||
["raw-string", [ | ||
"\"\"\"\r\n", | ||
["interpolation", "$foo"], | ||
["interpolation", [ | ||
["delimiter", "${"], "bar", ["delimiter", "}"] | ||
]], | ||
["interpolation", [ | ||
["delimiter", "${"], ["string", "'$'"], ["delimiter", "}"] | ||
]], | ||
["interpolation", [ | ||
["delimiter", "${"], | ||
["function", "foobar"], ["punctuation", "("], ["punctuation", ")"], | ||
["delimiter", "}"] | ||
]], | ||
"\r\n\"\"\"" | ||
]] | ||
] | ||
|
||
---------------------------------------------------- | ||
|
||
Checks for string interpolation. |
Oops, something went wrong.