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

Highlight Ruby method definitions without arguments #1523

Merged
merged 10 commits into from
Dec 1, 2018
8 changes: 7 additions & 1 deletion components/prism-ruby.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
}
};

delete Prism.languages.ruby.function;

Prism.languages.insertBefore('ruby', 'keyword', {
'regex': [
{
Expand Down Expand Up @@ -75,6 +77,10 @@
'symbol': {
pattern: /(^|[^:]):[a-zA-Z_]\w*(?:[?!]|\b)/,
lookbehind: true
},
'function': {
pattern: /(def )[\w.]+/,
Copy link
Member

@RunDevelopment RunDevelopment Aug 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a little fun with a Ruby REPL and found that this pattern will also happily match:

my_function_def variable

But at the same time, it doesn't match

def   
extra_spaces_and_new_line(arg)
end

Changing the lookbehind group to (\bdef\s+) ought to solve these cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. (Although I’ve never seen someone put a newline between def and the method name)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, me either, but the compiler can handle it, so there is probably someone out there using it.

lookbehind: true,
}
});

Expand Down Expand Up @@ -128,4 +134,4 @@
}
}
];
}(Prism));
}(Prism));
2 changes: 1 addition & 1 deletion components/prism-ruby.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/languages/crystal/attribute_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
]],
["attribute", [
["delimiter", "@["],
["function", "CallConvention"], ["punctuation", "("], ["string", [ "\"X86_StdCall\"" ]], ["punctuation", ")"],
["constant", "CallConvention"], ["punctuation", "("], ["string", [ "\"X86_StdCall\"" ]], ["punctuation", ")"],
["delimiter", "]"]
]]
]
Expand Down
3 changes: 2 additions & 1 deletion tests/languages/crystal/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
abstract alias as asm begin break case
class;
def do else elsif
def
do else elsif
end ensure enum extend for fun
if include instance_sizeof
.is_a?
Expand Down
4 changes: 2 additions & 2 deletions tests/languages/erb/erb_in_markup_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ___ERB1___<%= 1 %>___ERB2___<%= 2 %>
["punctuation", "."],
"now",
["punctuation", "."],
["function", "strftime"],
"strftime",
["punctuation", "("],
["string", ["'%A'"]],
["punctuation", ")"],
Expand All @@ -45,4 +45,4 @@ ___ERB1___<%= 1 %>___ERB2___<%= 2 %>

----------------------------------------------------

Checks for ERB inside Markup
Checks for ERB inside Markup
4 changes: 2 additions & 2 deletions tests/languages/haml/tag_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"%html",
["attributes", [
["punctuation", "{"],
["function", "html_attrs"],
"html_attrs",
["punctuation", "("],
["string", ["'fr-fr'"]],
["punctuation", ")"],
Expand Down Expand Up @@ -158,4 +158,4 @@

Checks for tags: basic element names, attributes, html-style attributes,
attribute methods, boolean attributes, class and id shortcuts,
implicit div elements, empty tags and whitespace removal.
implicit div elements, empty tags and whitespace removal.
72 changes: 72 additions & 0 deletions tests/languages/ruby/method_definition_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
class Circle
def self.of_diameter(diameter)
new diameter / 2
end

def initialize(radius)
@radius = radius
end

def circumference
Math::PI * radius ** 2
end

# Seattle style
def grow_by factor:
@radius = @radius * factor
end
end

----------------------------------------------------

[
["keyword", "class"],
["class-name", ["Circle"]],
["keyword", "def"],
["function", "self.of_diameter"],
["punctuation", "("],
"diameter",
["punctuation", ")"],
["keyword", "new"],
["class-name", ["diameter"]],
["operator", "/"],
["number", "2"],
["keyword", "end"],
["keyword", "def"],
["function", "initialize"],
["punctuation", "("],
"radius",
["punctuation", ")"],
["variable", "@radius"],
["operator", "="],
" radius\n ",
["keyword", "end"],
["keyword", "def"],
["function", "circumference"],
["constant", "Math"],
["punctuation", ":"],
["punctuation", ":"],
["constant", "PI"],
["operator", "*"],
" radius ",
["operator", "*"],
["operator", "*"],
["number", "2"],
["keyword", "end"],
["comment", "# Seattle style"],
["keyword", "def"],
["function", "grow_by"],
" factor",
["punctuation", ":"],
["variable", "@radius"],
["operator", "="],
["variable", "@radius"],
["operator", "*"],
" factor\n ",
["keyword", "end"],
["keyword", "end"]
]

----------------------------------------------------

Checks that method definitions are highlighted correctly