3
3
4
4
using System ;
5
5
using System . ComponentModel . Composition ;
6
- using System . Diagnostics ;
7
6
using System . Threading ;
8
7
using System . Threading . Tasks ;
9
8
using Microsoft . AspNetCore . Razor ;
@@ -60,13 +59,7 @@ internal sealed class HtmlRequestInvoker(
60
59
61
60
// If the request is for a text document, we need to update the Uri to point to the Html document,
62
61
// 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 ) ;
70
63
71
64
try
72
65
{
@@ -91,15 +84,35 @@ internal sealed class HtmlRequestInvoker(
91
84
}
92
85
finally
93
86
{
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 )
100
88
{
101
- textDocumentRequest . TextDocument . DocumentUri = originalUri ;
89
+ UpdateTextDocumentUri ( request , originalUri , out _ ) ;
102
90
}
103
91
}
104
92
}
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
+ }
105
118
}
0 commit comments