|
#!/usr/bin/env ruby |
|
# |
|
# !!! Obselete Gist !!! |
|
# Go to http://github.com/nowhereman/snippets-converter |
|
# TmSnippets2NetBeans |
|
# Quick and dirty code to transform TextMate Snippets into NetBeans Code Templates |
|
# Based on tmsnippets2gedit (http://github.com/spyou/tmsnippets2gedit) |
|
# |
|
# Copyright (c) 2010 Nowhere Man |
|
# |
|
# Last update 29 April 2010 |
|
# |
|
# TmSnippets2NetBeans is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License |
|
# as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
# |
|
# TmSnippets2NetBeans is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|
# |
|
# You should have received a copy of the GNU General Public License along with TmSnippets2NetBeans; if not, write to the |
|
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
|
|
require 'test/unit' |
|
|
|
class TestSnippetsFormat < Test::Unit::TestCase |
|
|
|
def setup |
|
@snippets = [] |
|
@snippets << [ "${1: ${2: cool} }", " ${2: cool} "] |
|
@snippets << [ "${1: cool $2 }", " cool $2 "] |
|
@snippets << [ "${1: ${2: $cool} }", " ${2: $cool} "] |
|
@snippets << [ "${1: [ ${2: cool} ] }", " [ ${2: cool} ] "] |
|
@snippets << [ "${1: ${2: cool} ${3: guy} }", " ${2: cool} ${3: guy} "] |
|
@snippets << [ "${1: [ ${2: cool} ${3: guy} ] }", " [ ${2: cool} ${3: guy} ] "] |
|
@snippets << [ "${1: [ ${2: cool} ${3: guy ${4:tralala}} ] }", " [ ${2: cool} guy ${4:tralala} ] "] |
|
@snippets << [ "${1: [ ${2: cool} ${3: guy { ${4:tralala} } } ] }", " [ ${2: cool} guy { ${4:tralala} } ] "] |
|
@snippets << [ "should_have_db_indices :${1:object_id}${2:, [:${3:commentable_type}, :${4:commentable_id}]}$0", "should_have_db_indices :${1:object_id}, [:${3:commentable_type}, :${4:commentable_id}]$0" ] |
|
|
|
snippet = 'context "${1:description}" do |
|
${2:setup do |
|
${3: cool} |
|
end |
|
|
|
}should "${4:description}" do |
|
${5: guy} |
|
end |
|
end' |
|
|
|
expected_snippet = 'context "${1:description}" do |
|
setup do |
|
${3: cool} |
|
end |
|
|
|
should "${4:description}" do |
|
${5: guy} |
|
end |
|
end' |
|
|
|
@snippets << [ snippet, expected_snippet ] |
|
@snippets << [ "[ ${3::${4:key} => ${5:value}${6:, :${7:key} => ${8:value} } } ]", "[ :${4:key} => ${5:value}, :${7:key} => ${8:value} ]" ] |
|
@snippets << [ " { ${3::${4:key} => ${5:value}${6:, :${7:key} => ${8:value}}} } ", " { :${4:key} => ${5:value}, :${7:key} => ${8:value} } " ] |
|
|
|
snippet = 'context "on POST to :${1:create}" do |
|
setup do |
|
post :$1, :${2:user} => { ${3::${4:key} => ${5:value}${6:, :${7:key} => ${8:value} } } } |
|
end |
|
|
|
${9:should "${10:description}" do |
|
$0 |
|
end} |
|
end' |
|
|
|
expected_snippet = 'context "on POST to :${1:create}" do |
|
setup do |
|
post :$1, :${2:user} => { :${4:key} => ${5:value}, :${7:key} => ${8:value} } |
|
end |
|
|
|
should "${10:description}" do |
|
$0 |
|
end |
|
end' |
|
@snippets << [ snippet, expected_snippet ] |
|
|
|
snippet = 'context "${1:description}" do |
|
${2:setup do |
|
$3 |
|
end |
|
|
|
}should "${4:description}" do |
|
$0 |
|
end |
|
end' |
|
|
|
expected_snippet = 'context "${1:description}" do |
|
setup do |
|
$3 |
|
end |
|
|
|
should "${4:description}" do |
|
$0 |
|
end |
|
end' |
|
@snippets << [ snippet, expected_snippet ] |
|
end |
|
|
|
|
|
def test_snippets_conversion |
|
@snippets.each do |snippet, expected_snippet| |
|
|
|
# Nested tab stops, e.g '${1: $2 }' |
|
nested_tab_stop = /((\$\{[0-9]{1,5}:[^${}]*)\$([0-9]{1,5})([^${}]*\}))+/m |
|
snippet.gsub!(nested_tab_stop, '\2:nested_tab_stop\3:\4') |
|
|
|
snippet.gsub!(/\$([0-9]{1,5})/, ':tab_stop\1:') # Tab Stop, e.g '$0' |
|
snippet.gsub!(/\$([^{][^0-9]+[^:])/, ':dollar:\1') # Dollar, e.g. '$titi' |
|
|
|
# Place holders, e.g. '${1: cool }' |
|
place_holders = /\$\{((?>[^${}]+)|(\1))+\}/m |
|
place_holders_matches = snippet.scan(place_holders) |
|
|
|
place_holders_matches.flatten.each_with_index do |place_holder, idx| |
|
snippet.gsub!(/\$\{#{place_holder}\}/m, ":place_holders#{idx}:") if place_holder |
|
end |
|
|
|
# Nested place holders, e.g. '${1: ${3:cool} }' |
|
nested_place_holders = /(\$\{[0-9]{1,5}:(([^${}]*(\$\{[0-9]{1,5}:|\{)[^${}]+\}[^${}]*)|[^${}]+)\})+/m |
|
i = 0 |
|
loop do |
|
i += 1 |
|
# puts i # debug |
|
break if !snippet.gsub!(nested_place_holders, '\2') || i > 20 |
|
end |
|
|
|
place_holders_matches.flatten.each_with_index do |place_holder, idx| |
|
snippet.gsub!(/:place_holders#{idx}:/m, "\$\{#{place_holder}\}") if place_holder |
|
end |
|
|
|
# Nested tab stops |
|
snippet.gsub!(/:nested_tab_stop([0-9]{1,5}):/, '$\1') |
|
nested_tab_stop = /(\$\{[0-9]{1,5}:([^${}]*\$[0-9]{1,5}[^${}]*)\})+/m |
|
i = 0 |
|
loop do |
|
i += 1 |
|
# puts i # debug |
|
break if !snippet.gsub!(nested_tab_stop, '\2') || i > 20 |
|
end |
|
|
|
snippet.gsub!(/:tab_stop([0-9]{1,5}):/, '$\1') |
|
snippet.gsub!(/:dollar:/, '$') |
|
|
|
assert_equal expected_snippet, snippet |
|
end |
|
|
|
end |
|
|
|
end |