Skip to content

Commit

Permalink
Add generic parameter keywords to Kotlin lexer (#1504)
Browse files Browse the repository at this point in the history
Kotlin allows three keywords to be used inside a list of generic
parameters: `in`, `out` and `reified`. In addition, it supports the use
of `:`. This commit adds support for these elements to the Kotlin lexer.
  • Loading branch information
goodhoko committed Apr 17, 2020
1 parent 42ec130 commit 2b704db
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/rouge/lexers/kotlin.rb
Expand Up @@ -105,7 +105,8 @@ class Kotlin < RegexLexer
state :generic_parameters do
rule class_name, Name::Class
rule %r'(<)', Punctuation, :generic_parameters
rule %r'(,)', Punctuation
rule %r'(reified|out|in)', Keyword
rule %r'([,:])', Punctuation
rule %r'(\s+)', Text
rule %r'(>)', Punctuation, :pop!
end
Expand Down
6 changes: 6 additions & 0 deletions spec/lexers/kotlin_spec.rb
Expand Up @@ -31,5 +31,11 @@
it 'recognizes label reference in break statement' do
assert_tokens_equal 'break@label', ["Keyword", "break"], ["Name.Decorator", "@label"]
end

it 'recognizes type modifiers' do
for modifer in ['reified', 'out', 'in'] do
assert_tokens_equal "<#{modifer} T>", ["Punctuation", "<"], ["Keyword", modifer], ["Text", " "], ["Name.Class", "T"], ["Punctuation", ">"]
end
end
end
end
6 changes: 5 additions & 1 deletion spec/visual/samples/kotlin
Expand Up @@ -162,10 +162,14 @@ fun `String`.`backTickedExtensionFunction`() {
return
}

fun <T, V> String.genericExtensionFunction() {
fun <T, V : Comparable> String.genericExtensionFunction() {
return
}

inline fun <reified T> membersOf() = T::class.members
public interface List<out E> : Collection<E> {}
public interface Comparable<in T> {}

@Deprecated("This is an annotation")
fun anAnnotatedFunction() = {
}
Expand Down

0 comments on commit 2b704db

Please sign in to comment.