33
44using System ;
55using System . ComponentModel . Composition ;
6- using System . Diagnostics ;
76using System . Threading ;
87using System . Threading . Tasks ;
98using Microsoft . AspNetCore . Razor ;
@@ -60,13 +59,7 @@ internal sealed class HtmlRequestInvoker(
6059
6160 // If the request is for a text document, we need to update the Uri to point to the Html document,
6261 // and most importantly set it back again before leaving the method in case a caller uses it.
63- DocumentUri ? originalUri = null ;
64- var textDocumentRequest = request as ITextDocumentParams ;
65- if ( textDocumentRequest is not null )
66- {
67- originalUri = textDocumentRequest . TextDocument . DocumentUri ;
68- textDocumentRequest . TextDocument . DocumentUri = new ( htmlDocument . Uri ) ;
69- }
62+ UpdateTextDocumentUri ( request , new ( htmlDocument . Uri ) , out var originalUri ) ;
7063
7164 try
7265 {
@@ -91,15 +84,35 @@ internal sealed class HtmlRequestInvoker(
9184 }
9285 finally
9386 {
94- Debug . Assert (
95- ( textDocumentRequest is null && originalUri is null ) ||
96- ( textDocumentRequest is not null && originalUri is not null ) , "textDocumentRequest and originalUri should either both be null or both be non-null" ) ;
97-
98- // Reset the Uri if we changed it.
99- if ( textDocumentRequest is not null && originalUri is not null )
87+ if ( originalUri is not null )
10088 {
101- textDocumentRequest . TextDocument . DocumentUri = originalUri ;
89+ UpdateTextDocumentUri ( request , originalUri , out _ ) ;
10290 }
10391 }
10492 }
93+
94+ private void UpdateTextDocumentUri < TRequest > ( TRequest request , DocumentUri uri , out DocumentUri ? originalUri ) where TRequest : notnull
95+ {
96+ switch ( request )
97+ {
98+ case ITextDocumentParams { TextDocument : { } textDocument } :
99+ originalUri = textDocument . DocumentUri ;
100+ textDocument . DocumentUri = uri ;
101+ break ;
102+ case VSInternalDiagnosticParams { TextDocument : { } textDocument } :
103+ // VSInternalDiagnosticParams doesn't implement the interface because the TextDocument property is nullable
104+ originalUri = textDocument . DocumentUri ;
105+ textDocument . DocumentUri = uri ;
106+ break ;
107+ case VSInternalRelatedDocumentParams { TextDocument : { } textDocument } :
108+ // We don't implement the endpoint that uses this, but it's the only other thing, at time of writing, in the LSP
109+ // protocol library that isn't handled by the above two cases.
110+ originalUri = textDocument . DocumentUri ;
111+ textDocument . DocumentUri = uri ;
112+ break ;
113+ default :
114+ originalUri = default ;
115+ break ;
116+ }
117+ }
105118}
0 commit comments