Artiom
Artiom

Reputation: 7847

Get Path of the document from IWpfTextView for non cs files

I want to add some buttons to the text editor in VS. In the ViewPortTextViewCreationListener when ViewPort manager is created I would like to know the path of the document or it

public void TextViewCreated(IWpfTextView textView) {
    var result = _textDocumentFactoryService.TryGetTextDocument(TextView.TextBuffer, out ITextDocument textDocument);
    new ViewPortSwitcher(textView);
}

I've tried to use ITextDocumentFactoryService to get ITextDocument from TextBuffer (see answer here). If I open cs file it works properly. But if I open cshtml file TryGetTextDocument returns false.

Upvotes: 2

Views: 387

Answers (1)

Artiom
Artiom

Reputation: 7847

Finally, I've found the solution see MSDN forum:

public static string GetPath(this IWpfTextView textView) {
    textView.TextBuffer.Properties.TryGetProperty(typeof(IVsTextBuffer), out IVsTextBuffer bufferAdapter);
    var persistFileFormat = bufferAdapter as IPersistFileFormat;

    if (persistFileFormat == null) {
        return null;
    }
    persistFileFormat.GetCurFile(out string filePath, out _);
    return filePath;
}

Upvotes: 5

Related Questions