Skip to content

Instantly share code, notes, and snippets.

@hex108
Created June 13, 2019 07:44
Show Gist options
  • Save hex108/f268fbd451088b50359bac677514c3ed to your computer and use it in GitHub Desktop.
Save hex108/f268fbd451088b50359bac677514c3ed to your computer and use it in GitHub Desktop.
Generate vim tags for protocol buffer files
#!/usr/bin/perl
use strict;
my $tag_file = "tags";
check_and_backup($tag_file);
my @proto_files = `find . -name *.proto`;
for my $proto_file (@proto_files){
chomp($proto_file);
print "Generate tags for $proto_file\n";
genearte_tags($proto_file, $tag_file);
}
sort_tag_file($tag_file);
### Sub functions
sub check_and_backup{
my $file = @_[0];
if(-e $file){
warn "Warning: File(\"$file\") already exists. Back up it to $file\.bak.";
`mv $file ${file}.bak`;
}
}
sub genearte_tags{
my ($proto_file, $tag_file) = @_ ;
open PROTO, "<", $proto_file;
open TAGS, ">>", $tag_file;
while(<PROTO>){
chomp;
if(/^\s*message\s+(\S+)\s*\{\s*$/){
print TAGS "$1";
print TAGS "\t";
print TAGS "$proto_file";
print TAGS "\t";
print TAGS "\/\^$_\$/";
print TAGS "\n";
}
}
close PROTO;
close TAGS;
}
sub sort_tag_file{
my $tag_file = @_[0];
my $tmp_file = "/tmp/tmp_file_for_tags.tmp";
`cat $tag_file | sort -k1 > $tmp_file; mv $tmp_file $tag_file`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment