How to get file extension at runtime/ITextBuffer.ContentType is UNKNOWN
pmahend1 opened this issue · 1 comments
I am developing an extension for Visual Studio for Mac. I am trying to get file content type since it will be applicable for only XML type of file extensions.
IdeApp.Workbench.ActiveDocument.GetContent<ITextBuffer>().?ContentType.DisplayName
returns values like code++.xml
xaml
androidXml
etc. But for file contents which are not assigned explicit file property (for example .csproj ,. config, .mobileconfig, .plist , .xamarin) are returning UNKNOWN
So as workaround I want to get file extension of the current active document. How can I do that? I am finding hard time understanding APIs and there is less extension. I thought if I can cast ITextView to ExtensibleTextEditor
would let me get File name/File extension property but I am not getting that class through intellisense.
var textBuffer = IdeApp.Workbench.ActiveDocument?.GetContent<ITextBuffer>();
var textview = IdeApp.Workbench.ActiveDocument.GetContent<ITextView>();
var contentType = textBuffer.ContentType;
Please help!
I was able to get it like below
var textdocument = textBuffer?.Properties?.PropertyList?.FirstOrDefault(x =>x.Key is ITextDocument || x.Value is ITextDocument);
var documentvalue = textdocument?.Value as ITextDocument;
var ext = documentvalue?.FilePath?.Substring(documentvalue?.FilePath?.LastIndexOf(".")??0);
Let me know if there is easier way.