1

私は Visual Studio のアドインを作成するのは初めてですが、現在アクティブなコード ウィンドウでテキストを少し操作する VS2010 用の簡単なツールを作成することができました。現在のテキスト ビューの言語 (VB.Net、C# など) を知る必要があるところまで来ました。

次のコードを使用して、ファイル名を取得しようとしました(拡張子を調べて言語を判断できます)。

IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
int mustHaveFocus = 1;//means true
txtMgr.GetActiveView(mustHaveFocus, null, out currentTextView);

userData = currentTextView as IVsUserData;
if (userData == null)// no text view 
{
    Console.WriteLine("No text view is currently open");
    return;
}

object pathAsObject;
Guid monikerGuid = typeof(IVsUserData).GUID;
userData.GetData(ref monikerGuid, out pathAsObject);
string docPath = (string)pathAsObject;

残念ながら、pathAsObject は常に null を返します。ファイル名/言語を取得する他の方法はありますか?

4

1 に答える 1

1

これが機能するように見えます:

// Get the current text view.
IVsTextManager txtMgr = (IVsTextManager)GetService(typeof(SVsTextManager));
int mustHaveFocus = 1;//means true
txtMgr.GetActiveView(mustHaveFocus, null, out currentTextView);

userData = currentTextView as IVsUserData;
if (userData == null)// no text view 
{
    Console.WriteLine("No text view is currently open");
    return;
}

// In the next 4 statments, I am trying to get access to the editor's view 
object holder;
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out holder);
viewHost = (IWpfTextViewHost)holder;

// Get a snapshot of the current editor's text.
allText = viewHost.TextView.TextSnapshot.GetText();

// Get the language for the current editor.
string language = viewHost.TextViewtextView.TextDataModel.ContentType.TypeName;

これは、VB.Net の「基本」を返します。これは、まさに私が知る必要があることです。

于 2010-09-29T16:05:00.057 に答える