Skip to content

Commit

Permalink
Merge branch 'master' of http://github.com/mads379/align.tmbundle
Browse files Browse the repository at this point in the history
Conflicts:
	Commands/Align Source.tmCommand
  • Loading branch information
tbates committed Sep 3, 2012
2 parents cb4dda8 + 98b77e1 commit cf9ae38
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 28 deletions.
42 changes: 30 additions & 12 deletions Commands/Align Source.tmCommand
Expand Up @@ -6,7 +6,11 @@
<string>nop</string>
<key>command</key>
<string>#!/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)
Expand All @@ -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.
Expand Down Expand Up @@ -162,16 +166,30 @@ end
=begin
Great, lets get to the formatting then.
=end
fallback_regexps = ["/(,)(?!$)/","/\}/","/&lt;-/","/\s[-+\/*|]?(=)\s/","/\s(=&gt;)\s/","/:/","/\/\//"].join('ø')
regexps_hash = (ENV['TM_SOURCE_ALIGNMENT_PATTERN'] || fallback_regexps).
split('ø').
map do |r|
md = r.match("/(.*)/(.*)")
{ "regexp" =&gt; Regexp.new(md[1]),
"spacing" =&gt; md[2] }
end
text = STDIN.readlines().join()
fallback = [
{ "regexp" =&gt; "(,)(?!$)", "spacing" =&gt; "" },
{ "regexp" =&gt; "\}", "spacing" =&gt; "" },
{ "regexp" =&gt; "&lt;-", "spacing" =&gt; "" },
{ "regexp" =&gt; "\s[-+\/*|]?(=)\s", "spacing" =&gt; "" },
{ "regexp" =&gt; "\s(=&gt;)\s", "spacing" =&gt; "" },
{ "regexp" =&gt; ":", "spacing" =&gt; "" },
{ "regexp" =&gt; "\/\/" , "spacing" =&gt; ""}
]
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)
Expand Down
24 changes: 24 additions & 0 deletions 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'
}
6 changes: 5 additions & 1 deletion Preferences/Alignment Patterns (CSS).tmPreferences
Expand Up @@ -14,7 +14,11 @@
<key>name</key>
<string>TM_SOURCE_ALIGNMENT_PATTERN</string>
<key>value</key>
<string>/:/b</string>
<string>[
{
"regexp" : ":",
"spacing" : "after"
}]</string>
</dict>
</array>
</dict>
Expand Down
32 changes: 32 additions & 0 deletions Preferences/Alignment Patterns (Ruby).tmPreferences
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Alignment Patterns (Ruby)</string>
<key>scope</key>
<string>source.ruby</string>
<key>settings</key>
<dict>
<key>shellVariables</key>
<array>
<dict>
<key>name</key>
<string>TM_SOURCE_ALIGNMENT_PATTERN</string>
<key>value</key>
<string>[
{
"regexp" : "=&gt;",
"spacing" : "before"
},
{
"regexp" : ":",
"spacing" : "after"
}]</string>
</dict>
</array>
</dict>
<key>uuid</key>
<string>014C8ED7-9501-4479-B890-4A15125FF379</string>
</dict>
</plist>
30 changes: 29 additions & 1 deletion Preferences/Alignment Patterns (Scala).tmPreferences
Expand Up @@ -14,7 +14,35 @@
<key>name</key>
<string>TM_SOURCE_ALIGNMENT_PATTERN</string>
<key>value</key>
<string>/(,)(?!$)/ø/\}/ø/&lt;-/ø/\s[-+\/*|]?(=)\s/ø/\s(=&gt;)\s/ø/:/ø/\/\//</string>
<string>[
{
"regexp" : "(,)(?!$)",
"spacing" : ""
},
{
"regexp" : "\}",
"spacing" : ""
},
{
"regexp" : "&lt;-",
"spacing" : ""
},
{
"regexp" : "\s[-+\/*|]?(=)\s",
"spacing" : ""
},
{
"regexp" : "\s(=&gt;)\s",
"spacing" : ""
},
{
"regexp" : ":",
"spacing" : ""
},
{
"regexp" : "\/\/",
"spacing" : ""
}]</string>
</dict>
</array>
</dict>
Expand Down
13 changes: 0 additions & 13 deletions README

This file was deleted.

49 changes: 49 additions & 0 deletions 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
4 changes: 3 additions & 1 deletion info.plist
Expand Up @@ -7,7 +7,9 @@
<key>contactName</key>
<string>Mads Hartmann Jensen</string>
<key>description</key>
<string></string>
<string>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</string>
<key>name</key>
<string>Align</string>
<key>ordering</key>
Expand Down

0 comments on commit cf9ae38

Please sign in to comment.