From c5d0ef51630028339a59a29e41066444d61c083b Mon Sep 17 00:00:00 2001 From: Andrew Lord Date: Fri, 21 Sep 2018 23:11:35 +0100 Subject: [PATCH 1/3] Recognize function parameters and return --- lib/rouge/lexers/kotlin.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/rouge/lexers/kotlin.rb b/lib/rouge/lexers/kotlin.rb index 4e6021cc14..d2d6dd0731 100644 --- a/lib/rouge/lexers/kotlin.rb +++ b/lib/rouge/lexers/kotlin.rb @@ -57,6 +57,13 @@ class Kotlin < RegexLexer groups Keyword, Text push :function end + rule %r'(#{name_backtick})(:)(\s+)(#{name_backtick})(<)' do + groups Name::Variable, Punctuation, Text, Name::Class, Punctuation + push :generic_parameters + end + rule %r'(#{name_backtick})(:)(\s+)(#{name_backtick})' do + groups Name::Variable, Punctuation, Text, Name::Class + end rule %r'\b(package|import)(\s+)' do groups Keyword, Text push :package @@ -94,6 +101,10 @@ class Kotlin < RegexLexer rule %r'(>)', Punctuation, :pop! end + state :function_parameter do + + end + state :property do rule id, Name::Property, :pop! end From 60d9fed76b5773f624e42848b1bf202b9235f75b Mon Sep 17 00:00:00 2001 From: Andrew Lord Date: Sat, 22 Sep 2018 00:51:22 +0100 Subject: [PATCH 2/3] Move fallback rules to the bottom Simple rules for when no complex rules are match, such as: comments, punctuation, operators, text, strings, numbers etc. This allows for specific rules to be tried first. --- lib/rouge/lexers/kotlin.rb | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/rouge/lexers/kotlin.rb b/lib/rouge/lexers/kotlin.rb index d2d6dd0731..cb8b30ac32 100644 --- a/lib/rouge/lexers/kotlin.rb +++ b/lib/rouge/lexers/kotlin.rb @@ -30,21 +30,6 @@ class Kotlin < RegexLexer id = %r'(#{name_backtick})' state :root do - rule %r'^\s*\[.*?\]', Name::Attribute - rule %r'[^\S\n]+', Text - rule %r'\\\n', Text # line continuation - rule %r'//.*?$', Comment::Single - rule %r'/[*].*?[*]/'m, Comment::Multiline - rule %r'\n', Text - rule %r'::|!!|\?[:.]', Operator - rule %r"(\.\.)", Operator - rule %r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation - rule %r'[{}]', Punctuation - rule %r'@"(""|[^"])*"'m, Str - rule %r'""".*?"""'m, Str - rule %r'"(\\\\|\\"|[^"\n])*["\n]'m, Str - rule %r"'\\.'|'[^\\]'", Str::Char - rule %r"[0-9](\.[0-9]+)?([eE][+-][0-9]+)?[flFL]?|0[xX][0-9a-fA-F]+[Ll]?", Num rule /@#{id}/, Name::Decorator rule %r'\b(companion)(\s+)(object)\b' do groups Keyword, Text, Keyword @@ -74,6 +59,21 @@ class Kotlin < RegexLexer end rule %r/\bfun\b/, Keyword rule /\b(?:#{keywords.join('|')})\b/, Keyword + rule %r'^\s*\[.*?\]', Name::Attribute + rule %r'[^\S\n]+', Text + rule %r'\\\n', Text # line continuation + rule %r'//.*?$', Comment::Single + rule %r'/[*].*?[*]/'m, Comment::Multiline + rule %r'\n', Text + rule %r'::|!!|\?[:.]', Operator + rule %r"(\.\.)", Operator + rule %r'[~!%^&*()+=|\[\]:;,.<>/?-]', Punctuation + rule %r'[{}]', Punctuation + rule %r'@"(""|[^"])*"'m, Str + rule %r'""".*?"""'m, Str + rule %r'"(\\\\|\\"|[^"\n])*["\n]'m, Str + rule %r"'\\.'|'[^\\]'", Str::Char + rule %r"[0-9](\.[0-9]+)?([eE][+-][0-9]+)?[flFL]?|0[xX][0-9a-fA-F]+[Ll]?", Num rule id, Name end From aebe68ffddff9e77eb966cbecaba3ed627aaf819 Mon Sep 17 00:00:00 2001 From: Andrew Lord Date: Sat, 22 Sep 2018 00:51:37 +0100 Subject: [PATCH 3/3] Recognize return type on functions --- lib/rouge/lexers/kotlin.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/rouge/lexers/kotlin.rb b/lib/rouge/lexers/kotlin.rb index cb8b30ac32..887ab92d5e 100644 --- a/lib/rouge/lexers/kotlin.rb +++ b/lib/rouge/lexers/kotlin.rb @@ -30,7 +30,13 @@ class Kotlin < RegexLexer id = %r'(#{name_backtick})' state :root do - rule /@#{id}/, Name::Decorator + rule %r'(\))(\s*)(:)(\s+)(#{name_backtick})(<)' do + groups Punctuation, Text, Punctuation, Text, Name::Class, Punctuation + push :generic_parameters + end + rule %r'(\))(\s*)(:)(\s+)(#{name_backtick})' do + groups Punctuation, Text, Punctuation, Text, Name::Class + end rule %r'\b(companion)(\s+)(object)\b' do groups Keyword, Text, Keyword end @@ -74,6 +80,7 @@ class Kotlin < RegexLexer rule %r'"(\\\\|\\"|[^"\n])*["\n]'m, Str rule %r"'\\.'|'[^\\]'", Str::Char rule %r"[0-9](\.[0-9]+)?([eE][+-][0-9]+)?[flFL]?|0[xX][0-9a-fA-F]+[Ll]?", Num + rule /@#{id}/, Name::Decorator rule id, Name end @@ -101,10 +108,6 @@ class Kotlin < RegexLexer rule %r'(>)', Punctuation, :pop! end - state :function_parameter do - - end - state :property do rule id, Name::Property, :pop! end