diff --git a/Commands/Align Source.tmCommand b/Commands/Align Source.tmCommand index bbf34a2..a705735 100644 --- a/Commands/Align Source.tmCommand +++ b/Commands/Align Source.tmCommand @@ -6,7 +6,11 @@ nop command #!/usr/bin/env ruby -W0 -# Aligns the source + +require 'rubygems' +require 'json' + +# Alignes the source def align(text, regexp_hash, width) text.to_a.map do |line| if is_all_whitespace(line) @@ -29,7 +33,7 @@ end # This is chosen by the bundle developer by using the regexp options 'a' # or 'b' where a is after and b is before def shouldInsertBefore(line, regexp_hash) - !(regexp_hash['spacing'] == "a") + !(regexp_hash['spacing'] == "after") end # Finds the width of the line with the most text before the regexp. @@ -162,16 +166,30 @@ end =begin Great, lets get to the formatting then. =end -fallback_regexps = ["/(,)(?!$)/","/\}/","/<-/","/\s[-+\/*|]?(=)\s/","/\s(=>)\s/","/:/","/\/\//"].join('ø') - -regexps_hash = (ENV['TM_SOURCE_ALIGNMENT_PATTERN'] || fallback_regexps). - split('ø'). - map do |r| - md = r.match("/(.*)/(.*)") - { "regexp" => Regexp.new(md[1]), - "spacing" => md[2] } - end -text = STDIN.readlines().join() + +fallback = [ + { "regexp" => "(,)(?!$)", "spacing" => "" }, + { "regexp" => "\}", "spacing" => "" }, + { "regexp" => "<-", "spacing" => "" }, + { "regexp" => "\s[-+\/*|]?(=)\s", "spacing" => "" }, + { "regexp" => "\s(=>)\s", "spacing" => "" }, + { "regexp" => ":", "spacing" => "" }, + { "regexp" => "\/\/" , "spacing" => ""} +] + +regexps_hash = ENV['TM_SOURCE_ALIGNMENT_PATTERN'] + +if regexps_hash.nil? + regexps_hash = fallback +else + regexps_hash = JSON.parse(regexps_hash) +end + +regexps_hash.each do |kv| + kv["regexp"] = Regexp.new(kv["regexp"]) +end + +text = STDIN.readlines().join() if ENV['TM_SELECTED_TEXT'].nil? format_block_containing_line(text, ENV['TM_LINE_NUMBER'].to_i - 1, regexps_hash) diff --git a/Examples/Example.rb b/Examples/Example.rb new file mode 100644 index 0000000..7feb7f2 --- /dev/null +++ b/Examples/Example.rb @@ -0,0 +1,24 @@ +# standard Hash-literal syntax: +{ + :short => 'bar', + :much_longer => 'baz' +} + + +# should become: +{ + :short => 'bar', + :much_longer => 'baz' +} + +# but 1.9 Hash-literal syntax: +{ + short: 'bar', + much_longer: 'baz' +} + +# should become: +{ + short: 'bar', + much_longer: 'baz' +} \ No newline at end of file diff --git a/Preferences/Alignment Patterns (CSS).tmPreferences b/Preferences/Alignment Patterns (CSS).tmPreferences index 76ffe16..cade526 100644 --- a/Preferences/Alignment Patterns (CSS).tmPreferences +++ b/Preferences/Alignment Patterns (CSS).tmPreferences @@ -14,7 +14,11 @@ name TM_SOURCE_ALIGNMENT_PATTERN value - /:/b + [ + { + "regexp" : ":", + "spacing" : "after" + }] diff --git a/Preferences/Alignment Patterns (Ruby).tmPreferences b/Preferences/Alignment Patterns (Ruby).tmPreferences new file mode 100644 index 0000000..b3a4112 --- /dev/null +++ b/Preferences/Alignment Patterns (Ruby).tmPreferences @@ -0,0 +1,32 @@ + + + + + name + Alignment Patterns (Ruby) + scope + source.ruby + settings + + shellVariables + + + name + TM_SOURCE_ALIGNMENT_PATTERN + value + [ + { + "regexp" : "=>", + "spacing" : "before" + }, + { + "regexp" : ":", + "spacing" : "after" + }] + + + + uuid + 014C8ED7-9501-4479-B890-4A15125FF379 + + diff --git a/Preferences/Alignment Patterns (Scala).tmPreferences b/Preferences/Alignment Patterns (Scala).tmPreferences index c61b29f..546f950 100644 --- a/Preferences/Alignment Patterns (Scala).tmPreferences +++ b/Preferences/Alignment Patterns (Scala).tmPreferences @@ -14,7 +14,35 @@ name TM_SOURCE_ALIGNMENT_PATTERN value - /(,)(?!$)/ø/\}/ø/<-/ø/\s[-+\/*|]?(=)\s/ø/\s(=>)\s/ø/:/ø/\/\// + [ + { + "regexp" : "(,)(?!$)", + "spacing" : "" + }, + { + "regexp" : "\}", + "spacing" : "" + }, + { + "regexp" : "<-", + "spacing" : "" + }, + { + "regexp" : "\s[-+\/*|]?(=)\s", + "spacing" : "" + }, + { + "regexp" : "\s(=>)\s", + "spacing" : "" + }, + { + "regexp" : ":", + "spacing" : "" + }, + { + "regexp" : "\/\/", + "spacing" : "" + }] diff --git a/README b/README deleted file mode 100644 index c59b153..0000000 --- a/README +++ /dev/null @@ -1,13 +0,0 @@ -This is a greatly improved alignment command. Rather than just aligning assignments it will align all sorts of things. - -This is a work in progress and I've published this bundle so people can give it a try. In the long term this will get merged into TextMate. Follow this discussion for more information: http://old.nabble.com/Align-Source-td31636561.html - -What still needs to be done - -- Left/right justify captures -- Enable scope specific regular expressions -- Enable scope specific block separators (currently uses blank lines and indentation to find blocks of code) - -I'm working on this gist and will merge the code with this bundle whenever I think the new features are stable: https://gist.github.com/988793 - -/Mads \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..db87205 --- /dev/null +++ b/README.md @@ -0,0 +1,49 @@ +# align.tmbundle + +This is a greatly improved alignment command. Rather than just aligning +assignments it will align all sorts of things. + +This is a work in progress and I've published this bundle so people can give it +a try. In the long term I hope this will get merged into TextMate. + +## Installation + + gem install json + cd ~/Library/Application\ Support/Avian/Bundles + git clone https://github.com/mads379/align.tmbundle.git + +## How to add alignment rules + +Open the bundle editor (ctrl+alt+cmd+b). Find the Align bundle. Hit `cmd+n` and +choose settings. Set the scope you want, fx `source.ruby` and add something like +the following. + + { shellVariables = ( + { name = 'TM_SOURCE_ALIGNMENT_PATTERN'; + value = '[ + { + "regexp" : "=>", + "spacing" : "before" + }, + { + "regexp" : ":", + "spacing" : "after" + }]'; + }, + ); + } + +The `value` is a simply a JSON array with objects that has a `regexp` and `spacing` +property. The `regexp` is the regular expression to match after and spacing can be +either `before` or `after` depending on weather you want in insert the spaces before +or after the matched text. + +## Missing features + +- Left/right justify captures +- Enable scope specific block separators (currently uses blank lines and indentation to find blocks of code) + +## History + +- Original discussion: http://old.nabble.com/Align-Source-td31636561.html +- Original proof of concept: https://gist.github.com/988793 \ No newline at end of file diff --git a/info.plist b/info.plist index 62108eb..0a7cbb7 100644 --- a/info.plist +++ b/info.plist @@ -7,7 +7,9 @@ contactName Mads Hartmann Jensen description - + This is a greatly improved alignment command. Rather than just aligning assignments it will align all sorts of things. + +It lives at https://github.com/mads379/align.tmbundle name Align ordering