在Visual Studio中以特定行号打开文件 [英] Open a file in Visual Studio at a specific line number

查看:263
本文介绍了在Visual Studio中以特定行号打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实用程序(grep),它给我一个文件名和行号的列表。在我确定devenv是打开文件的正确程序后,我想确保它在指定的行号打开。在emac中,这将是:

  emacs +140 filename.c 
pre>

我没有找到这样的Visual Studio(devenv)。我找到的最近的是:

  devenv /命令Edit.Goto 140filename.c 

但是,这会为每个这样的文件创建一个单独的devenv实例。



这些变体重复使用现有的devenv,但不要去指定的行:

  devenv / CommandEdit.Goto 140/编辑filename.c 
devenv /命令/编辑filename.cEdit.Goto 140

我以为使用多个/ Command参数可能会做到,有正确的一个,因为我得到错误或根本没有响应(除了打开一个空的devenv)。



我可以为devenv写一个特殊的宏,但我会像这个实用程序被没有那个宏的其他人使用。



有没有想法?






好吧,似乎没有办法这样做,因为我想要的。由于它看起来像我需要有专门的代码启动Visual Studio,我决定使用EnvDTE如下所示。希望这将有助于别人。

  #includestdafx.h

// --------------------------------------------- --------------------------
//这段代码是从http://benbuck.com/archives/13 $中被偷来的b $ b //
//这是从名为BenBuck的人的博客,其中有
//似乎没有信息。
// -------------------------------------------- ---------------------------

// import EnvDTE
#pragma warning(disable:4278 )
#pragma warning(disable:4146)
#importlibid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2version(8.0)lcid(0)raw_interfaces_only named_guids
#pragma warning(默认值:4146)
#pragma warning(默认值:4278)

bool visual_studio_open_file(char const * filename,unsigned int line)
{
HRESULT结果;
CLSID clsid;
result = :: CLSIDFromProgID(LVisualStudio.DTE,& clsid);
if(FAILED(result))
return false;

CComPtr< IUnknown>朋克
result = :: GetActiveObject(clsid,NULL,&punk);
if(FAILED(result))
return false;

CComPtr< EnvDTE :: _ DTE> DTE;
DTE = punk;

CComPtr< EnvDTE :: ItemOperations> item_ops;
result = DTE-> get_ItemOperations(& item_ops);
if(FAILED(result))
return false;

CComBSTR bstrFileName(filename);
CComBSTR bstrKind(EnvDTE :: vsViewKindTextView);
CComPtr< EnvDTE :: Window>窗口;
result = item_ops-> OpenFile(bstrFileName,bstrKind,& window);
if(FAILED(result))
return false;

CComPtr< EnvDTE :: Document> doc;
result = DTE-> get_ActiveDocument(& doc);
if(FAILED(result))
return false;

CComPtr< IDispatch> selection_dispatch;
result = doc-> get_Selection(& selection_dispatch);
if(FAILED(result))
return false;

CComPtr< EnvDTE :: TextSelection>选择;
result = selection_dispatch-> QueryInterface(& selection);
if(FAILED(result))
return false;

result = selection-> GotoLine(line,TRUE);
if(FAILED(result))
return false;

return true;
}


解决方案

一种使用直接命令行选项执行此操作的方法。看起来你必须为它写一个宏。

  devenv / commandMacros.MyMacros.Module1.OpenFavoriteFiles

所以,你可以创建一个宏,它接受文件名和行号,然后打开文件并跳转到正确的地点。但是,我不知道你可以指定一个相同的实例标志某处,或不。


I have a utility (grep) that gives me a list of filenames and a line numbers. After I have determined that devenv is the correct program to open a file, I would like to ensure that it is opened at the indicated line number. In emacs, this would be:

emacs +140 filename.c

I have found nothing like this for Visual Studio (devenv). The closest I have found is:

devenv /Command "Edit.Goto 140" filename.c

However, this makes a separate instance of devenv for each such file. I would rather have something that uses an existing instance.

These variations re-use an existing devenv, but don't go to the indicated line:

devenv /Command "Edit.Goto 140" /Edit filename.c
devenv /Command  /Edit filename.c "Edit.Goto 140"

I thought that using multiple "/Command" arguments might do it, but I probably don't have the right one because I either get errors or no response at all (other than opening an empty devenv).

I could write a special macro for devenv, but I would like this utility to be used by others that don't have that macro. And I'm not clear on how to invoke that macro with the "/Command" option.

Any ideas?


Well, it doesn't appear that there is a way to do this as I wanted. Since it looks like I'll need to have dedicated code to start up Visual Studio, I've decided to use EnvDTE as shown below. Hopefully this will help somebody else.

#include "stdafx.h"

//-----------------------------------------------------------------------
// This code is blatently stolen from http://benbuck.com/archives/13
//
// This is from the blog of somebody called "BenBuck" for which there
// seems to be no information.
//-----------------------------------------------------------------------

// import EnvDTE
#pragma warning(disable : 4278)
#pragma warning(disable : 4146)
#import "libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2" version("8.0") lcid("0") raw_interfaces_only named_guids
#pragma warning(default : 4146)
#pragma warning(default : 4278)

bool visual_studio_open_file(char const *filename, unsigned int line)
{
    HRESULT result;
    CLSID clsid;
    result = ::CLSIDFromProgID(L"VisualStudio.DTE", &clsid);
    if (FAILED(result))
        return false;

    CComPtr<IUnknown> punk;
    result = ::GetActiveObject(clsid, NULL, &punk);
    if (FAILED(result))
        return false;

    CComPtr<EnvDTE::_DTE> DTE;
    DTE = punk;

    CComPtr<EnvDTE::ItemOperations> item_ops;
    result = DTE->get_ItemOperations(&item_ops);
    if (FAILED(result))
        return false;

    CComBSTR bstrFileName(filename);
    CComBSTR bstrKind(EnvDTE::vsViewKindTextView);
    CComPtr<EnvDTE::Window> window;
    result = item_ops->OpenFile(bstrFileName, bstrKind, &window);
    if (FAILED(result))
        return false;

    CComPtr<EnvDTE::Document> doc;
    result = DTE->get_ActiveDocument(&doc);
    if (FAILED(result))
        return false;

    CComPtr<IDispatch> selection_dispatch;
    result = doc->get_Selection(&selection_dispatch);
    if (FAILED(result))
        return false;

    CComPtr<EnvDTE::TextSelection> selection;
    result = selection_dispatch->QueryInterface(&selection);
    if (FAILED(result))
        return false;

    result = selection->GotoLine(line, TRUE);
    if (FAILED(result))
        return false;

    return true;
}

解决方案

I can't figure out a way to do this with straight command-line options. It looks like you will have to write a macro for it. Supposedly, you can invoke them like so.

devenv /command "Macros.MyMacros.Module1.OpenFavoriteFiles"

So, you can probably create a macro that takes a filename and a line number, then opens the file and jumps to the proper place. But, I don't know that you can specify a same-instance flag somewhere, or not.

这篇关于在Visual Studio中以特定行号打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆