Skip to content

Instantly share code, notes, and snippets.

@timsneath
Created March 11, 2017 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timsneath/2fd0732dc467e15d359dd265ef8025bc to your computer and use it in GitHub Desktop.
Save timsneath/2fd0732dc467e15d359dd265ef8025bc to your computer and use it in GitHub Desktop.
Gets the file currently being edited
private string GetCurrentFilenameFromEditor()
{
var textManager = this.ServiceProvider.GetService(typeof(SVsTextManager)) as IVsTextManager;
int mustHaveFocus = 1;
textManager.GetActiveView(mustHaveFocus, null, out IVsTextView textView);
var userData = textView as IVsUserData;
if (userData == null)
{
// no text view is currently open
return String.Empty;
}
else
{
Guid guidViewHost = DefGuidList.guidIWpfTextViewHost;
userData.GetData(ref guidViewHost, out object holder);
IWpfTextViewHost viewHost = (IWpfTextViewHost)holder;
viewHost.TextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument doc);
return doc.FilePath;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment