| 1 | package Padre::Plugin::Moose; | ||
| 2 | |||
| 3 | use 5.008; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Padre::Plugin (); | ||
| 7 | |||
| 8 | our $VERSION = '0.21'; | ||
| 9 | our @ISA = 'Padre::Plugin'; | ||
| 10 | |||
| 11 | # Child modules we need to unload when disabled | ||
| 12 | use constant CHILDREN => qw{ | ||
| 13 | Padre::Plugin::Moose | ||
| 14 | Padre::Plugin::Moose::Role::CanGenerateCode | ||
| 15 | Padre::Plugin::Moose::Role::CanHandleInspector | ||
| 16 | Padre::Plugin::Moose::Role::CanProvideHelp | ||
| 17 | Padre::Plugin::Moose::Role::HasClassMembers | ||
| 18 | Padre::Plugin::Moose::Role::NeedsPluginEvent | ||
| 19 | Padre::Plugin::Moose::Attribute | ||
| 20 | Padre::Plugin::Moose::Class | ||
| 21 | Padre::Plugin::Moose::ClassMember | ||
| 22 | Padre::Plugin::Moose::Constructor | ||
| 23 | Padre::Plugin::Moose::Destructor | ||
| 24 | Padre::Plugin::Moose::Method | ||
| 25 | Padre::Plugin::Moose::Program | ||
| 26 | Padre::Plugin::Moose::Role | ||
| 27 | Padre::Plugin::Moose::Subtype | ||
| 28 | Padre::Plugin::Moose::Util | ||
| 29 | Padre::Plugin::Moose::Assistant | ||
| 30 | Padre::Plugin::Moose::Preferences | ||
| 31 | Padre::Plugin::Moose::FBP::Assistant | ||
| 32 | Padre::Plugin::Moose::FBP::Preferences | ||
| 33 | }; | ||
| 34 | |||
| 35 | # Called when Padre wants to check what package versions this | ||
| 36 | # plugin needs | ||
| 37 | sub padre_interfaces { | ||
| 38 | 'Padre::Plugin' => 0.94, | ||
| 39 | 'Padre::Document' => 0.94, | ||
| 40 | 'Padre::Wx::Main' => 0.94, | ||
| 41 | 'Padre::Wx::Theme' => 0.94, | ||
| 42 | 'Padre::Wx::Editor' => 0.94, | ||
| 43 | 'Padre::Wx::Role::Main' => 0.94, | ||
| 44 | 'Padre::Wx::Role::Dialog' => 0.94, | ||
| 45 | ; | ||
| 46 | } | ||
| 47 | |||
| 48 | # Called when Padre wants a name for the plugin | ||
| 49 | sub plugin_name { | ||
| 50 | Wx::gettext('Moose'); | ||
| 51 | } | ||
| 52 | |||
| 53 | # Called when the plugin is enabled by Padre | ||
| 54 | sub plugin_enable { | ||
| 55 | my $self = shift; | ||
| 56 | |||
| 57 | # Read the plugin configuration, and | ||
| 58 | my $config = $self->config_read; | ||
| 59 | unless ( defined $config ) { | ||
| 60 | |||
| 61 | # No configuration, let us create it | ||
| 62 | $config = {}; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Make sure defaults are respected if they are undefined. | ||
| 66 | unless ( defined $config->{type} ) { | ||
| 67 | $config->{type} = 'Moose'; | ||
| 68 | } | ||
| 69 | unless ( defined $config->{namespace_autoclean} ) { | ||
| 70 | $config->{namespace_autoclean} = 0; | ||
| 71 | } | ||
| 72 | unless ( defined $config->{comments} ) { | ||
| 73 | $config->{comments} = 1; | ||
| 74 | } | ||
| 75 | unless ( defined $config->{sample_code} ) { | ||
| 76 | $config->{sample_code} = 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | # Write the plugin's configuration | ||
| 80 | $self->config_write($config); | ||
| 81 | |||
| 82 | # Update configuration attribute | ||
| 83 | $self->{config} = $config; | ||
| 84 | |||
| 85 | # Generate missing Padre's events | ||
| 86 | # TODO remove once Padre 0.96 is released | ||
| 87 | require Padre::Plugin::Moose::Role::NeedsPluginEvent; | ||
| 88 | Padre::Plugin::Moose::Role::NeedsPluginEvent->meta->apply( $self->main ); | ||
| 89 | |||
| 90 | # Highlight the current editor. This is needed when a plugin is enabled | ||
| 91 | # for the first time | ||
| 92 | $self->editor_changed; | ||
| 93 | |||
| 94 | return 1; | ||
| 95 | } | ||
| 96 | |||
| 97 | # Called when the plugin is disabled by Padre | ||
| 98 | sub plugin_disable { | ||
| 99 | my $self = shift; | ||
| 100 | |||
| 101 | # Destroy resident dialog | ||
| 102 | if ( defined $self->{assistant} ) { | ||
| 103 | $self->{assistant}->Destroy; | ||
| 104 | $self->{assistant} = undef; | ||
| 105 | } | ||
| 106 | |||
| 107 | # TODO: Switch to Padre::Unload once Padre 0.96 is released | ||
| 108 | for my $package (CHILDREN) { | ||
| 109 | require Padre::Unload; | ||
| 110 | Padre::Unload->unload($package); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | # Called when Padre wants to display plugin menu items | ||
| 115 | sub menu_plugins { | ||
| 116 | my $self = shift; | ||
| 117 | my $main = $self->main; | ||
| 118 | my $menu_item = Wx::MenuItem->new( undef, -1, Wx::gettext('Moose Assistant') . "...\tF8", ); | ||
| 119 | |||
| 120 | Wx::Event::EVT_MENU( | ||
| 121 | $main, | ||
| 122 | $menu_item, | ||
| 123 | sub { | ||
| 124 | $self->show_assistant; | ||
| 125 | }, | ||
| 126 | ); | ||
| 127 | |||
| 128 | return $menu_item; | ||
| 129 | } | ||
| 130 | |||
| 131 | # Shows the Moose assistant dialog. Creates it only once if needed | ||
| 132 | sub show_assistant { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | eval { | ||
| 136 | unless ( defined $self->{assistant} ) | ||
| 137 | { | ||
| 138 | require Padre::Plugin::Moose::Assistant; | ||
| 139 | $self->{assistant} = Padre::Plugin::Moose::Assistant->new($self); | ||
| 140 | } | ||
| 141 | }; | ||
| 142 | if ($@) { | ||
| 143 | $self->main->error( sprintf( Wx::gettext('Error: %s'), $@ ) ); | ||
| 144 | } else { | ||
| 145 | $self->{assistant}->run; | ||
| 146 | } | ||
| 147 | |||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | # Called when an editor is opened | ||
| 152 | sub editor_enable { | ||
| 153 | my $self = shift; | ||
| 154 | my $editor = shift; | ||
| 155 | my $document = shift; | ||
| 156 | |||
| 157 | # Only on Perl documents | ||
| 158 | return unless $document->isa('Padre::Document::Perl'); | ||
| 159 | |||
| 160 | require Padre::Plugin::Moose::Util; | ||
| 161 | Padre::Plugin::Moose::Util::add_moose_keywords_highlighting( $self->{config}->{type}, $document, $editor ); | ||
| 162 | } | ||
| 163 | |||
| 164 | # Called when an editor is changed | ||
| 165 | sub editor_changed { | ||
| 166 | my $self = shift; | ||
| 167 | my $current = $self->current or return; | ||
| 168 | my $document = $current->document or return; | ||
| 169 | my $editor = $current->editor or return; | ||
| 170 | |||
| 171 | # Only on Perl documents | ||
| 172 | return unless $document->isa('Padre::Document::Perl'); | ||
| 173 | |||
| 174 | require Padre::Plugin::Moose::Util; | ||
| 175 | Padre::Plugin::Moose::Util::add_moose_keywords_highlighting( $self->{config}->{type}, $document, $editor ); | ||
| 176 | } | ||
| 177 | |||
| 178 | 1; | ||
| 179 | |||
| 180 | __END__ |