I sometimes need to search code for patterns in a way that goes beyond the regex capabilities of Visual Stu开发者_Go百科dio (e.g. patterns that depend on what was seen previously in the file or on the contents of other files). So I use Perl to analyze the source and output matching lines, along with the file name and line number.
Since this is the exact same format as is produced by the search feature of Visual Studio, I wonder if there is a way to duplicate the functionality where I can double-click on a line and it will display that line in context in Visual Studio. Any ideas?
You can use Perl's Win32::GUI to mock up a window that looks exactly like the search utility to use as a front end for your Perl program. That would allow you to double-click search results and perform actions with those results. Take a look at this link for a way to go to a certain line in a file in VS. Here's a quick example:
use strict;
use Win32::GUI;
#example data structure containing the search text to print, the file location, and the line number for the search text
my $items = [
['first hit', 'C:\file.cs', '30'],
['second hit', 'C:\anotherfile.cs', '245'],
['third hit', 'C:\file.cs', '16']
];
my $main = Win32::GUI::Window->new(
-width => 250,
-height => 250
);
my $listbox = $main->AddListbox(
-name => 'search_hits',
-top => '10',
-left => '10',
-width => '100',
-height => '100',
);
foreach my $item(@$items){
$listbox->InsertItem($item->[0]);
}
$main->Show();
Win32::GUI::Dialog();
sub search_hits_DblClick{
my $index_selected = $listbox->GetCurSel();
exec('devenv /edit '.$items->[$index_selected]->[1].' /command "edit.goto '.$items->[$index_selected]->[2].'"');
}
Yep. Just make sure that you follow the same format:
filename(linenumber): ...
When this appears in the Output window, you should be able to double-click on it and be taken to the proper line in the file.
A colleague pointed me to the article Writing to the Output Window From a Visual Studio .NET Add-in. There is a Visual Studio project type called "Visual Studio Add-In" that I could use to write my own add-in, which could call my Perl script, capture the script's output and send it to the Visual Studio output window.
An even cleaner alternative would be if I could compile my Perl script to .NET, then the whole add-in could be in Perl and I wouldn't have to call an external process.
精彩评论