Skip to content

Commit

Permalink
Add support for Ruby endless method (#617)
Browse files Browse the repository at this point in the history
* Add support for Ruby endless method

Resolves #598

* Fix lint

* Update version
  • Loading branch information
facelessuser authored Aug 5, 2023
1 parent 822566d commit b641b81
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# BracketHighlighter

## 2.31

- **NEW**: Add support for Ruby endless methods.
- **NEW**: `validate` plugin method has been updated to accept a View, but will fallback to the legacy approach
for backwards compatibility.

## 2.30.1

- **FIX**: Fix PHP angle matching rule.
Expand Down
13 changes: 11 additions & 2 deletions bh_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,22 @@ def validate(self, b, bracket_type, scope_bracket=False):
if bracket.validate is not None:
try:
match = bracket.validate(
self.view,
bracket.name,
bh_plugin.BracketRegion(b.begin, b.end),
bracket_type,
self.search.get_buffer()
)
except Exception:
log("Plugin Bracket Find Error:\n%s" % str(traceback.format_exc()))
except TypeError:
try:
match = bracket.validate(
bracket.name,
bh_plugin.BracketRegion(b.begin, b.end),
bracket_type,
self.search.get_buffer()
)
except Exception:
log("Plugin Bracket Find Error:\n%s" % str(traceback.format_exc()))
return match

def compare(self, first, second, scope_bracket=False):
Expand Down
2 changes: 1 addition & 1 deletion bh_core.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@
{
"name": "ruby",
"open": "((?:(?<=^)|(?<==)|(?<=<<))\\s*\\b(?:if|begin|case)\\b(?!:)|^\\s*(?:(?:private|public|protected)\\s*)?def\\b[\\p{Ll}\\p{Lu}]*\\b(?!:)|^\\s*\\b(?:for|until|unless|while|class|module)\\b(?!:)|(?<!:)\\bdo\\b(?!:)|(?<=return)\\s*\\b(?:begin|case|for|until|while|class|module)\\b(?!:))",
"close": "(?<=[\\s;])(end)\\b(?!:)",
"close": "((?<=[\\s;])end\\b(?!:)|(?<!\\s)\\s*=)",
"style": "default",
"scope_exclude": ["string", "comment"],
"plugin_library": "bh_modules.rubykeywords",
Expand Down
37 changes: 37 additions & 0 deletions bh_modules/rubykeywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,44 @@
import re

RE_DEF = re.compile(r"\s*(?:(?:private|public|protected)\s+)?(def).*?")
RE_DEF_ENDLESS = re.compile(
r"^[ \t]*((?:private|public|protected)\s+)?def\s+(?:[\w\.=]+)[?!]?\s*(\([^)]+\)\s*|\s+)=",
re.M
)
RE_KEYWORD = re.compile(r"(\s*\b)[\w\W]*")
SPECIAL_KEYWORDS = ('do',)
NORMAL_KEYWORDS = ('for', 'until', 'unless', 'while', 'class', 'module', 'if', 'begin', 'case')
RE_PREVIOUS = re.compile(r'.*([^\s])\s*=$')


def validate(view, name, bracket, bracket_side, bfr):
"""Check if bracket is lowercase."""

b = bfr[bracket.begin:bracket.end]
if bracket_side == 1 and b.strip() == '=':
if view.match_selector(bracket.begin, 'meta.function.parameters.default-value.ruby, string, comment'):
return False
view.score_selector
left = max(0, bracket.begin - 1)
previous = bfr[left:bracket.end]
m = RE_PREVIOUS.match(previous)
if m:
s = left + m.start(1)
selector = 'meta.function.ruby entity.name.function.ruby, meta.function.parameters.ruby'
if not view.match_selector(s, selector):
return False
return True


def compare(name, first, second, bfr):
"""Differentiate 'repeat..until' from '*..end' brackets."""

brackets = (bfr[first.begin:first.end], bfr[second.begin:second.end])
if brackets[1].lstrip() == '=' and RE_DEF.match(brackets[0]):
return True
if brackets[1] == 'end':
return True
return False


def post_match(view, name, style, first, second, center, bfr, threshold):
Expand All @@ -24,6 +59,8 @@ def post_match(view, name, style, first, second, center, bfr, threshold):
m = RE_DEF.match(open_bracket)
if m:
first = first.move(first.begin + m.start(1), first.begin + m.end(1))
if second and bfr[second.begin:second.end].lstrip() == '=':
second = first
else:
m = RE_KEYWORD.match(open_bracket)
if m:
Expand Down
2 changes: 1 addition & 1 deletion messages.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"install": "messages/install.md",
"2.30.0": "messages/recent.md"
"2.31.0": "messages/recent.md"
}
9 changes: 5 additions & 4 deletions messages/recent.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# BracketHighlighter 2.30.0
# BracketHighlighter 2.31.0

New release!

A restart might be required. If you see issues immediately after the update
please try restarting.

## 2.30.0
## 2.31

- **NEW**: When defining key bindings `type` is now defaulted to `['__all__']` if not set.
- **FIX**: Ensure Jinja2 support for works for the Jinja2 package (support existed for some older package).
- **NEW**: Add support for Ruby endless methods.
- **NEW**: `validate` plugin method has been updated to accept a View, but will fallback
to the legacy approach for backwards compatibility.
2 changes: 1 addition & 1 deletion support.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import webbrowser
import re

__version__ = "2.30.1"
__version__ = "2.31.0"
__pc_name__ = 'BracketHighlighter'

CSS = '''
Expand Down

0 comments on commit b641b81

Please sign in to comment.