Domanda

Sono nuovo di costruzione di componenti aggiuntivi per Visual Studio, ma sono riusciti a costruire uno strumento simpe per VS2010 che fa un po 'di manipolazione del testo nella finestra del codice attualmente attivo. Ho al punto in cui ho bisogno di conoscere la lingua (VB.Net, C # o altro) della vista testo attuale.

ho cercato di ottenere il nome del file (in modo da poter osservare l'estensione per determinare la lingua) utilizzando il seguente codice:

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;

Purtroppo pathAsObject restituisce sempre null. C'è un altro modo per ottenere il nome del file / lingua?

È stato utile?

Soluzione

appare come funziona questo:

// 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;

Questo restituisce "di base" per VB.Net, che è esattamente quello che ho bisogno di sapere.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top