├── Default.sublime-keymap ├── README.md ├── RubyBlockConverter.py └── test ├── after-BraceToDoEnd.rb ├── after-DoEndToBrace.rb ├── before-BraceToDoEnd.rb └── before-DoEndToBrace.rb /Default.sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | { "keys": ["ctrl+shift+["], "command": "do_end_to_brace" }, 3 | { "keys": ["ctrl+shift+]"], "command": "brace_to_do_end" } 4 | ] 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ruby Block Converter for Sublime Text 2 and 3 2 | ============================== 3 | A command plugin that enables to toggle ruby blocks between braces and `do end`. 4 | 5 | How It Works 6 | -------------- 7 | Place the cursor in the block and run the command: 8 | 9 | ```ruby 10 | # original 11 | foo { bar } 12 | 13 | # run "brace_to_do_end" 14 | foo do 15 | bar 16 | end 17 | 18 | # run "do_end_to_brace" 19 | foo { bar } 20 | ``` 21 | 22 | do_end_to_brace shrinks a block in a line when the block traverses at most 3 lines. 23 | 24 | ```ruby 25 | # original 26 | foo {|a| 27 | a.bar 28 | } 29 | 30 | # run "brace_to_do_end" 31 | foo do |a| 32 | a.bar 33 | end 34 | 35 | # but when revert with "do_end_to_brace", 36 | foo {|a| a.bar } 37 | ``` 38 | 39 | When a block has more than 3 lines, do_end_to_brace leaves those lines untouched. 40 | 41 | ```ruby 42 | # original 43 | foo do 44 | bar 45 | baz 46 | end 47 | 48 | # run "do_end_to_brace" 49 | foo { 50 | bar 51 | baz 52 | } 53 | ``` 54 | 55 | Install 56 | ------- 57 | Use [Package Control](http://wbond.net/sublime_packages/package_control) and search for "Ruby Block Converter." 58 | 59 | Key Binding 60 | ----------- 61 | By default, 62 | `ctrl+shift+[` do_end_to_brace 63 | `ctrl+shift+]` brace_to_do_end 64 | 65 | Compatibility 66 | ------------- 67 | - Mac: Sublime Text 3 ready! 68 | - Linux: Sublime Text 3 ready! 69 | - Other: Not tested 70 | 71 | Future 72 | ------ 73 | I have a plan to conbine these two commands. That should behave like TextMate. 74 | 75 | Contributors 76 | ------------ 77 | - [@dsandstrom](https://github.com/dsandstrom) 78 | 79 | License 80 | ------- 81 | All of Ruby Block Converter for Sublime Text is licensed under the MIT license. 82 | 83 | Copyright (c) 2013 Hiroki Yoshioka 84 | 85 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 86 | 87 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 88 | 89 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 90 | -------------------------------------------------------------------------------- /RubyBlockConverter.py: -------------------------------------------------------------------------------- 1 | try: 2 | import io 3 | except ImportError: 4 | # probably linux 5 | import StringIO 6 | io = StringIO 7 | import tokenize 8 | import re 9 | import sublime, sublime_plugin 10 | 11 | def find(collection, f): 12 | for i in collection: 13 | if f(i): 14 | return i 15 | return None 16 | 17 | def search_points_to_replace(command): 18 | points_to_replace = set() 19 | for region in command.view.sel(): 20 | test_point = region.b 21 | while True: 22 | opening_point = find(command.opening_points, lambda p: p <= test_point) 23 | if opening_point: 24 | if command.blocks[opening_point] >= test_point: 25 | points_to_replace.add(opening_point) 26 | points_to_replace.add(command.blocks[opening_point]) 27 | break 28 | else: 29 | test_point = opening_point - 1 30 | else: 31 | break 32 | 33 | return list(points_to_replace) 34 | 35 | def match_blocks(command, toknum, opening, closing): 36 | view = command.view 37 | opening_points = [] 38 | blocks = {} 39 | 40 | content = view.substr(sublime.Region(0, view.size())) 41 | tokens = tokenize.generate_tokens(io.StringIO(content).readline) 42 | for num, val, start, _, _ in tokens: 43 | if num == toknum: 44 | start_point = view.text_point(start[0] - 1, start[1]) 45 | if val == opening: 46 | opening_points.append(start_point) 47 | elif val == closing: 48 | if opening_points: 49 | blocks[opening_points.pop()] = start_point 50 | 51 | return blocks 52 | 53 | class BraceToDoEndCommand(sublime_plugin.TextCommand): 54 | lines_to_reindent = set() 55 | 56 | def reserve_reindent(self, line): 57 | self.lines_to_reindent = set([l + 1 for l in self.lines_to_reindent]) 58 | self.lines_to_reindent.add(line) 59 | 60 | def reindent(self): 61 | view = self.view 62 | sel = view.sel() 63 | dirty_lines = set() 64 | for line in self.lines_to_reindent: 65 | line_end = sublime.Region(view.line(view.text_point(line, 0)).b) 66 | if not sel.contains(line_end): 67 | sel.add(line_end) 68 | dirty_lines.add(line) 69 | view.run_command('reindent', {'force_indent': False}) 70 | for line in dirty_lines: 71 | sel.subtract(sublime.Region(view.line(view.text_point(line, 0)).b)) 72 | 73 | def run(self, edit): 74 | view = self.view 75 | self.blocks = match_blocks(self, tokenize.OP, '{', '}') 76 | 77 | self.opening_points = list(self.blocks.keys()) 78 | self.opening_points.sort(reverse=True) 79 | 80 | points_to_replace = search_points_to_replace(self) 81 | points_to_replace.sort(reverse = True) 82 | 83 | for p in points_to_replace: 84 | if p in self.opening_points: 85 | # f{ f do 86 | # f{|a| f do |a| 87 | # f{ |a| f do |a| 88 | # f{a} f do\n 89 | # f{|a|a} f do |a|\n 90 | # f{ |a|a} f do |a|\n 91 | # f{|a| a} f do |a|\n 92 | # f{ |a| a} f do |a|\n 93 | # f { |a| a} f do |a|\n 94 | opening_pattern = re.compile('(?P[ \t])?(?P\{ *(?P\|[ ,()\w\t]*\|)?[ \t]*)(?P.*)') 95 | m = opening_pattern.search(view.substr(sublime.Region(p - 1, view.line(p).b))) 96 | heading = '' if m.group('heading') else ' ' 97 | following = ' ' if m.group('args') else '' 98 | if m.group('exp'): 99 | newline = '\n' 100 | self.reserve_reindent(view.rowcol(p)[0] + 1) 101 | else: 102 | newline = '' 103 | region = sublime.Region(p, p + len(m.group('opening'))) 104 | view.replace(edit, region, '%sdo%s%s%s' % (heading, following, m.group('args') or '', newline)) 105 | else: 106 | # a } 107 | # a} 108 | # } 109 | m = re.search('(?P[^ \t]*)(?P[ \t]*)}$', view.substr(sublime.Region(view.line(p).a, p + 1))) 110 | if m.group('exp'): 111 | newline = '\n' 112 | self.reserve_reindent(view.rowcol(p)[0] + 1) 113 | else: 114 | newline = '' 115 | replace_start = p - (len(m.group('spaces')) if m.group('exp') and m.group('spaces') else 0) 116 | end = newline + 'end' 117 | view.replace(edit, sublime.Region(replace_start, p + 1), end) 118 | 119 | sel = view.sel() 120 | after_end = sublime.Region(replace_start + len(end)) 121 | if sel.contains(after_end): 122 | sel.subtract(after_end) 123 | sel.add(sublime.Region(replace_start)) 124 | 125 | self.reindent() 126 | 127 | class DoEndToBraceCommand(sublime_plugin.TextCommand): 128 | def row_span(self, point): 129 | return self.view.rowcol(self.blocks[point])[0] - self.view.rowcol(point)[0] 130 | 131 | def run(self, edit): 132 | view = self.view 133 | self.blocks = match_blocks(self, tokenize.NAME, 'do', 'end') 134 | 135 | self.opening_points = list(self.blocks.keys()) 136 | self.opening_points.sort(reverse = True) 137 | 138 | points_to_replace = search_points_to_replace(self) 139 | points_to_replace.sort(reverse = True) 140 | 141 | for p in points_to_replace: 142 | if p in self.opening_points: 143 | if self.row_span(p) in [1, 2]: 144 | inner_region = sublime.Region(p + 2, self.blocks[p]) 145 | view.replace(edit, inner_region, re.sub('[ \t]*[\r\n]+[ \t]*', ' ', view.substr(inner_region))) 146 | replace_end = p + 3 if view.substr(sublime.Region(p + 2, p + 4)) == ' |' else p + 2 147 | view.replace(edit, sublime.Region(p, replace_end), '{') 148 | else: 149 | view.replace(edit, sublime.Region(p, p + 3), '}') 150 | -------------------------------------------------------------------------------- /test/after-BraceToDoEnd.rb: -------------------------------------------------------------------------------- 1 | f do 2 | end 3 | f do |a| 4 | end 5 | f do |a| 6 | end 7 | f do 8 | end 9 | f do 10 | a 11 | end 12 | f do 13 | a 14 | end 15 | f do 16 | a 17 | end 18 | f do 19 | a 20 | end 21 | f do |a| 22 | a 23 | end 24 | f do |a| 25 | a 26 | end 27 | f do |a| 28 | a 29 | end 30 | f do |a| 31 | a 32 | end 33 | f do |a| 34 | a 35 | end 36 | f do |a| 37 | a 38 | end 39 | f do |a| 40 | a 41 | end 42 | f do |a| 43 | a 44 | end 45 | f do 46 | end 47 | f do |a| 48 | end 49 | f do |a| 50 | end 51 | f do 52 | end 53 | f do 54 | a 55 | end 56 | f do 57 | a 58 | end 59 | f do 60 | a 61 | end 62 | f do 63 | a 64 | end 65 | f do |a| 66 | a 67 | end 68 | f do |a| 69 | a 70 | end 71 | f do |a| 72 | a 73 | end 74 | f do |a| 75 | a 76 | end 77 | f do |a| 78 | a 79 | end 80 | f do |a| 81 | a 82 | end 83 | f do |a| 84 | a 85 | end 86 | f do |a| 87 | a 88 | end 89 | f do 90 | g{ 91 | } 92 | cursor here 93 | end 94 | -------------------------------------------------------------------------------- /test/after-DoEndToBrace.rb: -------------------------------------------------------------------------------- 1 | f { } 2 | f {|a|a } 3 | f {|a|} 4 | f {|a| } 5 | f {|a|a } 6 | f {|a|} 7 | f {|a| } 8 | f { 9 | f 10 | f 11 | } 12 | f { f } 13 | f {|x| 14 | f 15 | f 16 | } 17 | f {|x| f } 18 | -------------------------------------------------------------------------------- /test/before-BraceToDoEnd.rb: -------------------------------------------------------------------------------- 1 | f{ 2 | } 3 | f{|a| 4 | } 5 | f{ |a| 6 | } 7 | f{} 8 | f{a} 9 | f{ a} 10 | f{a } 11 | f{ a } 12 | f{|a|a} 13 | f{|a|a } 14 | f{|a| a} 15 | f{|a| a } 16 | f{ |a|a} 17 | f{ |a|a } 18 | f{ |a| a} 19 | f{ |a| a } 20 | f { 21 | } 22 | f {|a| 23 | } 24 | f { |a| 25 | } 26 | f {} 27 | f {a} 28 | f { a} 29 | f {a } 30 | f { a } 31 | f {|a|a} 32 | f {|a|a } 33 | f {|a| a} 34 | f {|a| a } 35 | f { |a|a} 36 | f { |a|a } 37 | f { |a| a} 38 | f { |a| a } 39 | f{ 40 | g{ 41 | } 42 | cursor here 43 | } 44 | -------------------------------------------------------------------------------- /test/before-DoEndToBrace.rb: -------------------------------------------------------------------------------- 1 | f do end 2 | f do |a|a end 3 | f do |a|end 4 | f do |a| 5 | end 6 | f do|a|a end 7 | f do|a|end 8 | f do|a| 9 | end 10 | f do 11 | f 12 | f 13 | end 14 | f do 15 | f 16 | end 17 | f do |x| 18 | f 19 | f 20 | end 21 | f do |x| 22 | f 23 | end 24 | --------------------------------------------------------------------------------