Skip to content

Commit

Permalink
Support comments between name and opening brace
Browse files Browse the repository at this point in the history
Issue #6
  • Loading branch information
gillibrand committed Jun 4, 2024
1 parent 5c8260c commit abb856c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Expand Selection to Function (JavaScript)
=========================================
# Expand Selection to Function (JavaScript)

A command to expand the current selection to the enclosing JavaScript function. This works with named and anonymous functions.

Expand All @@ -17,8 +16,7 @@ Running it again will then select the variable and trailing semicolon:

After expanding, you can restore the last selection, which will contract the selection back to what it was before. You can expand several times then restore several times to return to your original selection, which is useful for quickly visualizing the scope of deeply nested functions.

Usage
-----
## Usage

Both commands are available in the `Selection` menu and bound to the following keyboard shortcuts on Windows and Linux:

Expand All @@ -30,13 +28,15 @@ Or similarly, on OS X:
Expand Selection to Function (JavaScript) ⌥↑ (Option+Up)
Restore Last Selection ⌥↓ (Option+Down)

Compatibility
-------------
## Compatibility

Compatible with both Sublime Text 2 and Sublime Text 3.

Version History
---------------
## Version History

### 6/4/2024

- Support comment between function name and opening brace.

### 1/3/2015

Expand Down
31 changes: 21 additions & 10 deletions expand_selection_to_function_javascript.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,27 @@ def find_previous(self, regex, from_point):
return sublime.Region(a, a + len(last_match.group(0)))

def find_balanced_braces(self, start_point):
"""Returns a region that includes the next balanced set of parens after or from start_point."""

# Next char is expected to be a {
m = __open_brace_re__.match(self.view.substr(Region(start_point, self.view.size())))
if not m:
return None
else:
braces_start = start_point + len(m.group(0))
"""Returns a region that includes the next balanced set of braces after or from start_point. If the start_point
is NOT an opening brade, this will will move forward down until it finds one."""

# Next char is expected to be a {, but we'll keep walking until we find the first one. This is to accommodate
# the case where there is a comment between the function name and first brace.
skip_count = 0
braces_start = None
while start_point < self.view.size() - 1:
m = __open_brace_re__.match(self.view.substr(Region(start_point, self.view.size())))
if not m:
# Move to next char (we're probably in an comment)
start_point += 1
skip_count += 1
if skip_count >= 500:
# Give up if we skip too many. Might just be malformed and don't want to search too long
return None
else:
braces_start = start_point + len(m.group(0))
break

if braces_start is None: return None

# Inside the first bracket, we are at depth 1.
# Increase and decrease the depth based on { and }. Once at depth 0, we're balanced
Expand Down Expand Up @@ -241,5 +254,3 @@ def run(self, edit):
sel.clear()
for region in new_regions:
sel.add(region)


0 comments on commit abb856c

Please sign in to comment.