لغة عرض النص في تمديد VS2010
-
26-09-2019 - |
سؤال
أنا جديد في إنشاء Addins for Visual Studio ، لكنني تمكنت من إنشاء أداة simpe لـ 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. هل هناك أي طريقة أخرى للحصول على اسم الملف / اللغة؟
المحلول
يبدو أن هذا يعمل:
// 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;
هذا يعيد "BASIC" لـ VB.NET ، وهو بالضبط ما أحتاج إلى معرفته.
لا تنتمي إلى StackOverflow