Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for labels to Kotlin lexer #1496

Merged
merged 3 commits into from Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/rouge/lexers/kotlin.rb
Expand Up @@ -53,6 +53,9 @@ class Kotlin < RegexLexer
groups Keyword::Declaration, Text
push :property
end
rule %r'(return|continue|break|this|super)(@#{name})?' do
groups Keyword, Name::Decorator
end
rule %r'\bfun\b', Keyword
rule %r'\b(?:#{keywords.join('|')})\b', Keyword
rule %r'^\s*\[.*?\]', Name::Attribute
Expand All @@ -78,6 +81,7 @@ class Kotlin < RegexLexer
end
rule class_name, Name::Class
rule %r'(#{name})(?=\s*[({])', Name::Function
rule %r'(#{name})@', Name::Decorator # label
rule name, Name
end

Expand Down
8 changes: 8 additions & 0 deletions spec/lexers/kotlin_spec.rb
Expand Up @@ -23,5 +23,13 @@
it 'recognizes one-line comments not followed by a newline (#797)' do
assert_tokens_equal '// comment', ['Comment.Single', '// comment']
end

it 'recognizes label' do
assert_tokens_equal 'label@', ["Name.Decorator", "label@"]
end

it 'recognizes label reference in break statement' do
assert_tokens_equal 'break@label', ["Keyword", "break"], ["Name.Decorator", "@label"]
end
end
end
6 changes: 6 additions & 0 deletions spec/visual/samples/kotlin
Expand Up @@ -172,4 +172,10 @@ fun anAnnotatedFunction() = {

val (a, b) = pair

loop@ for (i in 1..100) {
for (j in 1..100) {
if (...) break@loop
}
}

// comment at EOF (#797)