App-Codit

 view release on metacpan or  search on metacpan

lib/App/Codit/Plugins/Git.pm  view on Meta::CPAN


=head1 DETAILS

This plugin will only load if the git executable is installed.

It adds a document list to the navigation panel with the git icon. 

When a file is opened it will check if it belongs to a git repository. If it does
it will add the repository to the top menu list. If you select a repository
from that list all documents in that repository are loaded in the file list.
Selecting a document in the list will open it if it is not yet opened and select it.

When a file is closed it will check if there are any remaining documents in it's repository
are opened. If none are open it will unselect the repository and remove it from the
top menu list.

It has a context menu that pops with the right mouse button. You can quickly open all files in
the repository, or remove the current selected file from the repository. And you can collapse
and expand the list.

=head1 COMMANDS

Thi Git plugin adds the following commands to Codit.

=over 4

=item B<git_collapse>

Collapses the git document tree and only opens the current selected document, if it is in
the current selected repository.

=item B<git_command> I<$project>, I<$commandstring>

Executes the git command in $commandstring for repository $project.

=item B<git_expand>

Epxands the git document tree.

=item B<git_open_all>

Opens all files in the current selected repository.

=item B<git_remove> I<?$name?>

If $name is not specified, $name is the selected document.

Closes $name, Removes it from the current selected repository
and deletes the file from disk. Use with care.

=item B<git_remove_dialog>

Same as git_remove but first asks nicely if you really want to do this.

=back

=cut

sub new {
	my $class = shift;
	my $self = $class->SUPER::new(@_);
	return undef unless defined $self;

	#load only if git command line is installed
	my $git = `git -v`;
	return undef unless $git =~/^git\sversion/;

	$self->cmdHookAfter('doc_open', 'openDocAfter', $self);
	$self->cmdHookAfter('doc_close', 'closeDocAfter', $self);
	$self->cmdHookBefore('doc_select', 'selectDocBefore', $self);
	$self->cmdConfig(
		git_add => ['gitAdd', $self],
		git_collapse => ['gitCollapse', $self],
		git_command => ['gitCommand', $self],
		git_expand => ['gitExpand', $self],
		git_open_all => ['gitOpenAll', $self],
		git_remove => ['gitRemove', $self],
		git_remove_dialog => ['gitRemoveDialog', $self],
	);

	$self->{PROJECTS} = {};

	my $page = $self->ToolLeftPageAdd('Git', 'git-icon', undef, 'Manage your projects', 250);
	
	my $pframe = $page->Frame->pack(-fill => 'x');
	$pframe->Label(-text => 'Project:')->pack(-side => 'left');

	my $current = '';
	$self->{CURRENT} = \$current;
	my $mb = $pframe->Menubutton(
		-anchor => 'w',
		-textvariable => \$current,
	)->pack(-side => 'left', -expand => 1, -fill => 'x');

	my $menu = $mb->Menu(-tearoff => 0);
	$mb->configure(-menu => $menu);
	$self->{PMENU} = $menu;

	my $nav = $self->extGet('Selector');
	my $gtree = $page->DocumentTree(
		-entryselect => ['selectInternal', $self],
		-diriconcall => ['GetDirIcon', $nav],
		-fileiconcall => ['GetFileIcon', $nav],
		-saveiconcall => ['GetSaveIcon', $nav],
	)->pack(-expand => 1, -fill => 'both');
	my $stack =$self->extGet('MenuBar')->menuStack(@contextmenu);
	$gtree->configure('-contextmenu', $stack);
	$self->{TREE} = $gtree;
	
	$self->after(10, ['doPostConfig', $self]);
	return $self;
}

sub closeDocAfter {
	my ($self, $file) = @_;
	if ($file ne 0) {
		#does the file belong to a project?
		my $project = $self->fileInAnyProject($file);
		if (defined $project) {
			#is any other file of this project open?
			$self->projectRemove($project) unless $self->filesOpenInProject($project);

lib/App/Codit/Plugins/Git.pm  view on Meta::CPAN

			$menu->delete($_);
			last;
		}
	}

	#unselect if this project is currently selected
	$self->projectSelect('') if $self->projectCurrent eq $project;

	#remove
	delete $prj->{$project};
}

sub projectSelect {
	my ($self, $project) = @_;
	$self->projectCurrent($project);
	$self->projectRefresh;
}

sub selectDocBefore {
	my ($self, $file) = @_;
	$self->selectExternal($file);
}

sub selectExternal {
	my ($self, $name) = @_;
	my $cur = $self->projectCurrent;
	return if $cur eq '';
	$self->{TREE}->entrySelect($name) if $self->fileInProject($cur, $name);
}

sub selectInternal {
	my ($self, $name) = @_;
	my $mdi = $self->mdi;
	unless ($mdi->docExists($name)) {
		if (-T $name) {
			$self->cmdExecute('doc_open', $name);
		} else {
			$self->openURL($name)
		}
	}
	$self->cmdExecute('doc_select', $name) if $mdi->docExists($name);
}

sub Unload {
	my $self = shift;
	$self->ToolLeftPageRemove('Git');
	for (
		'git_add',
		'git_collapse',
		'git_command',
		'git_expand',
		'git_open_all',
		'git_remove',
		'git_remove_dialog',
	) {
		$self->cmdRemove($_);
	}
	$self->cmdUnhookAfter('doc_close', 'closeDocAfter', $self);
	$self->cmdUnhookAfter('doc_open', 'openDocAfter', $self);
	$self->cmdUnhookBefore('doc_select', 'selectDocBefore', $self);
	my $flag = $self->SUPER::Unload;
	my $mdi = $self->mdi;
	$self->after(100, ['navContextMenu', $mdi]);
	return $flag;
}

=head1 LICENSE

Same as Perl.

=head1 AUTHOR

Hans Jeuken (hanje at cpan dot org)

=head1 BUGS AND CAVEATS

If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>.

=cut




1;



( run in 1.185 second using v1.01-cache-2.11-cpan-99c4e6809bf )