codecadwallader / codemaid

CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
http://www.codemaid.net
GNU Lesser General Public License v3.0
1.88k stars 352 forks source link

ITextBuffer returns null for code file if designer window open #1017

Open carlos-quintero opened 10 months ago

carlos-quintero commented 10 months ago

This problem was reported for my add-in, and since I used code from this repo (linked by Microsoft's documentation) likely it is affected too (I have not verified it).

The bug is the following:

Then the TextBuffer is returned null in the TryGetTextBufferAt method of the file TextDocumentHelper.cs file (https://github.com/codecadwallader/codemaid/blob/dev/CodeMaidShared/Helpers/TextDocumentHelper.cs).

That method contains this call to get a window frame for a file in the desired view:

            IVsWindowFrame windowFrame;

            if (VsShellUtilities.IsDocumentOpen(
              CodeMaidPackage.Instance,
              filePath,
              Guid.Empty,
              out var _,
              out var _,
              out windowFrame))

That call uses the filePath for the code file (such as "(...)\Form1.vb") but for the logical view that you want to get it uses Guid.Empty (that is, any view).

It happens that if the designer window is opened, that call returns the window frame of the designer view for the file "(...)\Form1.Designer.vb". Since it is a designer view, the text buffer is returned null.

The fix would be to use the Guid for the Text logical view (Microsoft.VisualStudio.Shell.Interop.LogicalViewID.TextView) instead of Guid.Empty.

codecadwallader commented 7 months ago

Thanks for sharing this information Carlos.