Skip to content

Commit

Permalink
[Textmate] Added option to append a new bundle submenu
Browse files Browse the repository at this point in the history
  • Loading branch information
kattrali committed Jan 25, 2012
1 parent a23967f commit c4da653
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 18 deletions.
6 changes: 6 additions & 0 deletions plugins/textmate/lib/textmate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def self.bundle_context_menus(node)
item("Add new Snippet...") do
CreateNewSnippet.new(node.bundle).run
end
item("Add new Snippet Menu...") do
CreateNewSnippetGroup.new(node.bundle).run
end
if Textmate.storage['load_bundles_menu']
if Textmate.storage['loaded_bundles'].include?(node.text.downcase)
item ("Remove from Bundles Menu") do
Expand All @@ -77,6 +80,9 @@ def self.bundle_context_menus(node)
item("Add new Snippet...") do
CreateNewSnippet.new(node.bundle,node.uuid).run
end
item("Add new Snippet Menu...") do
CreateNewSnippetGroup.new(node.bundle,node.uuid).run
end
end
end
end
Expand Down
30 changes: 29 additions & 1 deletion plugins/textmate/lib/textmate/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def execute
end
plist = {
"name" => name,
"uuid" => Java::JavaUtil::UUID.randomUUID.to_s.upcase,
"uuid" => BundleEditor.generate_id,
"tabTrigger" => "",
"scope" => "",
"content" => ""
Expand Down Expand Up @@ -91,6 +91,34 @@ def execute
end
end

class CreateNewSnippetGroup < Redcar::Command
def initialize bundle,menu=nil
@bundle, @menu = bundle, menu
end

def execute
result = Redcar::Application::Dialog.input("Create Snippet Menu","Choose a name for your new snippet menu:")
if result[:button] == :ok and not result[:value].empty?
@bundle.sub_menus = {} unless @bundle.sub_menus
uuid = BundleEditor.generate_id
@bundle.sub_menus[uuid] = {
"name" => result[:value],
"items" => []
}
if @menu and @bundle.sub_menus[@menu]
@bundle.sub_menus[@menu]['items'] << uuid
else
@bundle.main_menu = {} unless @bundle.main_menu
@bundle.main_menu['items'] = [] unless @bundle.main_menu['items']
@bundle.main_menu['items'] << uuid
end
BundleEditor.write_bundle(@bundle)
BundleEditor.refresh_trees([@bundle.name])
BundleEditor.reload_cache
end
end
end


class ClearBundleMenu < Redcar::Command
def execute
Expand Down
54 changes: 40 additions & 14 deletions plugins/textmate/lib/textmate/editor.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@

module Redcar
module Textmate
class BundleEditor
def self.write_bundle bundle
File.open(File.expand_path(File.join(bundle.path,'info.plist')), 'w') do |f|
f.puts(Plist.plist_to_xml(bundle.plist))
end
end

def self.refresh_trees bundle_names=nil
Redcar.app.windows.map {|w|
w.treebook.trees
}.flatten.select {|t|
t.tree_mirror.is_a?(Redcar::Textmate::TreeMirror)
}.each {|t|
t.tree_mirror.refresh(bundle_names) if bundle_names
t.refresh
}
end

def self.reload_cache
Redcar::Textmate.cache.clear
Redcar::Textmate.cache.cache do
Textmate.all_bundles
end
end

def self.generate_id
Java::JavaUtil::UUID.randomUUID.to_s.upcase
end

def self.rot13 email
email.tr("A-Za-z", "N-ZA-Mn-za-m")
end
end

class EditorController
include Redcar::HtmlController

Expand Down Expand Up @@ -36,22 +70,14 @@ def save name, content, trigger, scope
# @bundle.ordering << @snippet.plist['uuid']
@bundle.snippets << @snippet
Textmate.uuid_hash[@snippet.plist['uuid']] = @snippet
File.open(File.expand_path(File.join(@bundle.path,'info.plist')), 'w') do |f|
f.puts(Plist.plist_to_xml(@bundle.plist))
end
BundleEditor.write_bundle(@bundle)
end
Redcar.app.windows.map {|w|
w.treebook.trees
}.flatten.select {|t|
t.tree_mirror.is_a?(Redcar::Textmate::TreeMirror)
}.each {|t|
t.tree_mirror.refresh([@bundle.name]) if @bundle
t.refresh
}
Redcar::Textmate.cache.clear
Redcar::Textmate.cache.cache do
Textmate.all_bundles
if @bundle
BundleEditor.refresh_trees([@bundle.name])
else
BundleEditor.refresh_trees
end
BundleEditor.reload_cache
close_tab
end

Expand Down
6 changes: 3 additions & 3 deletions plugins/textmate/lib/textmate/tree_mirror.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ def build_children(list, bundle, item)
list << SnippetNode.new(snippet)
#if item has submenus, make a group and add sub-items
elsif sub_menu = bundle.sub_menus[item]
unless sub_menu["items"].size() < 1
group = SnippetGroup.new(sub_menu["name"],item,bundle)
group = SnippetGroup.new(sub_menu["name"],item,bundle)
if sub_menu["items"] and sub_menu["items"].size > 0
sub_menu["items"].each do |sub_item|
build_children(group.children, bundle, sub_item)
end
list << group
end
list << group
end
end
end
Expand Down

0 comments on commit c4da653

Please sign in to comment.