| 1 | package Padre::Plugin::Snippet; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Padre::Plugin (); | ||
| 7 | |||
| 8 | our $VERSION = '0.01'; | ||
| 9 | our @ISA = 'Padre::Plugin'; | ||
| 10 | |||
| 11 | # Child modules we need to unload when disabled | ||
| 12 | use constant CHILDREN => qw{ | ||
| 13 | Padre::Plugin::Snippet::Role::NeedsSaveAsEvent | ||
| 14 | Padre::Plugin::Snippet::Document | ||
| 15 | Padre::Plugin::Snippet::Preferences | ||
| 16 | Padre::Plugin::Snippet::FBP::Preferences | ||
| 17 | }; | ||
| 18 | |||
| 19 | # Store the current configuration object for _plugin_config consumers | ||
| 20 | my $config; | ||
| 21 | |||
| 22 | # Called when Padre wants to check what package versions this | ||
| 23 | # plugin needs | ||
| 24 | sub padre_interfaces { | ||
| 25 | 'Padre::Plugin' => 0.94, 'Padre::Wx::Editor' => 0.94, 'Padre::Wx::Role::Main' => 0.94; | ||
| 26 | } | ||
| 27 | |||
| 28 | # Called when Padre wants a name for the plugin | ||
| 29 | sub plugin_name { | ||
| 30 | Wx::gettext('Snippet'); | ||
| 31 | } | ||
| 32 | |||
| 33 | # Called when the plugin is enabled by Padre | ||
| 34 | sub plugin_enable { | ||
| 35 | my $self = shift; | ||
| 36 | |||
| 37 | # Read the plugin configuration, and | ||
| 38 | my $config = $self->config_read; | ||
| 39 | unless ( defined $config ) { | ||
| 40 | |||
| 41 | # No configuration, let us create it | ||
| 42 | $config = {}; | ||
| 43 | } | ||
| 44 | |||
| 45 | # Make sure defaults are respected if they are undefined. | ||
| 46 | unless ( defined $config->{type} ) { | ||
| 47 | $config->{type} = 'Moose'; | ||
| 48 | } | ||
| 49 | unless ( defined $config->{feature_snippets} ) { | ||
| 50 | $config->{feature_snippets} = 'Moose'; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Write the plugin's configuration | ||
| 54 | $self->config_write($config); | ||
| 55 | |||
| 56 | # Update configuration attribute | ||
| 57 | $self->{config} = $config; | ||
| 58 | |||
| 59 | # Generate missing Padre's events | ||
| 60 | # TODO remove once Padre 0.96 is released | ||
| 61 | require Padre::Plugin::Snippet::Role::NeedsPluginEvent; | ||
| 62 | Padre::Plugin::Snippet::Role::NeedsPluginEvent->meta->apply( $self->main ); | ||
| 63 | |||
| 64 | # Highlight the current editor. This is needed when a plugin is enabled | ||
| 65 | # for the first time | ||
| 66 | $self->editor_changed; | ||
| 67 | |||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | |||
| 71 | # Called when the plugin is disabled by Padre | ||
| 72 | sub plugin_disable { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | # TODO: Switch to Padre::Unload once Padre 0.96 is released | ||
| 76 | for my $package (CHILDREN) { | ||
| 77 | require Padre::Unload; | ||
| 78 | Padre::Unload->unload($package); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | # # Called when Padre wants to display plugin menu items | ||
| 83 | # sub menu_plugins { | ||
| 84 | # my $self = shift; | ||
| 85 | # my $main = $self->main; | ||
| 86 | # my $menu_item = Wx::MenuItem->new( undef, -1, Wx::gettext('Snippet') . "...\tF9", ); | ||
| 87 | |||
| 88 | # Wx::Event::EVT_MENU( | ||
| 89 | # $main, | ||
| 90 | # $menu_item, | ||
| 91 | # sub { | ||
| 92 | # }, | ||
| 93 | # ); | ||
| 94 | |||
| 95 | # return $menu_item; | ||
| 96 | # } | ||
| 97 | |||
| 98 | sub editor_changed { | ||
| 99 | my $self = shift; | ||
| 100 | my $document = $self->current->document or return; | ||
| 101 | my $editor = $self->current->editor or return; | ||
| 102 | |||
| 103 | # Always cleanup current document | ||
| 104 | if ( defined $self->{document} ) { | ||
| 105 | $self->{document}->cleanup; | ||
| 106 | $self->{document} = undef; | ||
| 107 | } | ||
| 108 | |||
| 109 | # Only on Perl documents | ||
| 110 | return unless $document->isa('Padre::Document::Perl'); | ||
| 111 | |||
| 112 | # Create a new snippet document | ||
| 113 | require Padre::Plugin::Snippet::Document; | ||
| 114 | $self->{document} = Padre::Plugin::Snippet::Document->new( | ||
| 115 | editor => $editor, | ||
| 116 | document => $document, | ||
| 117 | config => $self->{config}, | ||
| 118 | ); | ||
| 119 | |||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | __END__ |