从 IWpfTextView 获取非 cs 文件的文档路径
Get Path of the document from IWpfTextView for non cs files
我想在 VS 中的文本编辑器中添加一些按钮。在创建 ViewPort 管理器时的 ViewPortTextViewCreationListener 中,我想知道文档的路径或它
public void TextViewCreated(IWpfTextView textView) {
var result = _textDocumentFactoryService.TryGetTextDocument(TextView.TextBuffer, out ITextDocument textDocument);
new ViewPortSwitcher(textView);
}
我已经尝试使用 ITextDocumentFactoryService
从 TextBuffer 中获取 ITextDocument
(参见答案 here)。如果我打开 cs 文件,它会正常工作。但是如果我打开 cshtml 文件 TryGetTextDocument returns false.
终于,我找到了解决方案 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;
}
我想在 VS 中的文本编辑器中添加一些按钮。在创建 ViewPort 管理器时的 ViewPortTextViewCreationListener 中,我想知道文档的路径或它
public void TextViewCreated(IWpfTextView textView) {
var result = _textDocumentFactoryService.TryGetTextDocument(TextView.TextBuffer, out ITextDocument textDocument);
new ViewPortSwitcher(textView);
}
我已经尝试使用 ITextDocumentFactoryService
从 TextBuffer 中获取 ITextDocument
(参见答案 here)。如果我打开 cs 文件,它会正常工作。但是如果我打开 cshtml 文件 TryGetTextDocument returns false.
终于,我找到了解决方案 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;
}