Skip to content

Commit

Permalink
Throw an exception if the solution context is accessed after ClearSol…
Browse files Browse the repository at this point in the history
…utionContext
  • Loading branch information
sharwell committed Apr 26, 2023
1 parent 7a854cb commit 3bfbb7f
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions src/Features/LanguageServer/Protocol/Handler/RequestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,66 @@ internal readonly struct RequestContext
/// The workspace this request is for, if applicable. This will be present if <see cref="Document"/> is
/// present. It will be <see langword="null"/> if <c>requiresLSPSolution</c> is false.
/// </summary>
public Workspace? Workspace => _lspSolution?.Value.Workspace;
public Workspace? Workspace
{
get
{
if (_lspSolution is null)
{
// This request context never had a workspace instance
return null;
}

// The workspace is available unless it has been cleared by a call to ClearSolutionContext. Explicitly throw
// for attempts to access this property after it has been manually cleared.
return _lspSolution.Value.Workspace ?? throw new InvalidOperationException();
}
}

/// <summary>
/// The solution state that the request should operate on, if the handler requires an LSP solution, or <see langword="null"/> otherwise
/// </summary>
public Solution? Solution => _lspSolution?.Value.Solution;
public Solution? Solution
{
get
{
if (_lspSolution is null)
{
// This request context never had a solution instance
return null;
}

// The solution is available unless it has been cleared by a call to ClearSolutionContext. Explicitly throw
// for attempts to access this property after it has been manually cleared.
return _lspSolution.Value.Solution ?? throw new InvalidOperationException();
}
}

/// <summary>
/// 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
/// <see cref="ITextDocumentIdentifierHandler{RequestType, TextDocumentIdentifierType}.GetTextDocumentIdentifier(RequestType)"/>.
/// </summary>
public Document? Document => _lspSolution?.Value.Document;
public Document? Document
{
get
{
if (_lspSolution is null)
{
// This request context never had a solution instance
return null;
}

// The solution is available unless it has been cleared by a call to ClearSolutionContext. Explicitly throw
// for attempts to access this property after it has been manually cleared. Note that we can't rely on
// Document being null for this check, because it is not always provided as part of the solution context.
if (_lspSolution.Value.Workspace is null)
{
throw new InvalidOperationException();
}

return _lspSolution.Value.Document;
}
}

/// <summary>
/// The LSP server handling the request.
Expand Down

0 comments on commit 3bfbb7f

Please sign in to comment.