Langue pour Voir Texte dans VS2010 Extension
-
26-09-2019 - |
Question
Je suis nouveau à la construction addins pour Visual Studio, mais ont réussi à construire un outil de simpe pour VS2010 qui fait une manipulation peu de texte dans la fenêtre de code active. J'ai au point où je dois connaître la langue (VB.Net, C # ou autre) de la vue texte actuel.
J'ai essayé d'obtenir le nom de fichier (donc je peux regarder l'extension pour déterminer la langue) en utilisant le le code suivant:
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;
Malheureusement pathAsObject retourne toujours null. Est-il un autre moyen d'obtenir le nom de fichier / langue?
La solution
présente comme suit les travaux:
// 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;
Ce rendement « de base » pour VB.Net, ce qui est exactement ce que je dois savoir.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow