Skip to content

Commit 3bfbb7f

Browse files
committed
Throw an exception if the solution context is accessed after ClearSolutionContext
1 parent 7a854cb commit 3bfbb7f

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

src/Features/LanguageServer/Protocol/Handler/RequestContext.cs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,66 @@ internal readonly struct RequestContext
5858
/// The workspace this request is for, if applicable. This will be present if <see cref="Document"/> is
5959
/// present. It will be <see langword="null"/> if <c>requiresLSPSolution</c> is false.
6060
/// </summary>
61-
public Workspace? Workspace => _lspSolution?.Value.Workspace;
61+
public Workspace? Workspace
62+
{
63+
get
64+
{
65+
if (_lspSolution is null)
66+
{
67+
// This request context never had a workspace instance
68+
return null;
69+
}
70+
71+
// The workspace is available unless it has been cleared by a call to ClearSolutionContext. Explicitly throw
72+
// for attempts to access this property after it has been manually cleared.
73+
return _lspSolution.Value.Workspace ?? throw new InvalidOperationException();
74+
}
75+
}
6276

6377
/// <summary>
6478
/// The solution state that the request should operate on, if the handler requires an LSP solution, or <see langword="null"/> otherwise
6579
/// </summary>
66-
public Solution? Solution => _lspSolution?.Value.Solution;
80+
public Solution? Solution
81+
{
82+
get
83+
{
84+
if (_lspSolution is null)
85+
{
86+
// This request context never had a solution instance
87+
return null;
88+
}
89+
90+
// The solution is available unless it has been cleared by a call to ClearSolutionContext. Explicitly throw
91+
// for attempts to access this property after it has been manually cleared.
92+
return _lspSolution.Value.Solution ?? throw new InvalidOperationException();
93+
}
94+
}
6795

6896
/// <summary>
6997
/// The document that the request is for, if applicable. This comes from the <see cref="TextDocumentIdentifier"/> returned from the handler itself via a call to
7098
/// <see cref="ITextDocumentIdentifierHandler{RequestType, TextDocumentIdentifierType}.GetTextDocumentIdentifier(RequestType)"/>.
7199
/// </summary>
72-
public Document? Document => _lspSolution?.Value.Document;
100+
public Document? Document
101+
{
102+
get
103+
{
104+
if (_lspSolution is null)
105+
{
106+
// This request context never had a solution instance
107+
return null;
108+
}
109+
110+
// The solution is available unless it has been cleared by a call to ClearSolutionContext. Explicitly throw
111+
// for attempts to access this property after it has been manually cleared. Note that we can't rely on
112+
// Document being null for this check, because it is not always provided as part of the solution context.
113+
if (_lspSolution.Value.Workspace is null)
114+
{
115+
throw new InvalidOperationException();
116+
}
117+
118+
return _lspSolution.Value.Document;
119+
}
120+
}
73121

74122
/// <summary>
75123
/// The LSP server handling the request.

0 commit comments

Comments
 (0)