I'm writing a DiagnosticAnalyzer
, and register a SyntaxNode
action for SyntaxKind.Attribute
. The attribute names some other file in the project.
For example, the code being analyzed may include
[RelatedFile("otherFileName.foo")]
interface Whatever {...}
In my analysis callback, I want to be able to access the contents of the related file, from the perspective of the project being analyzed. So, I need to:
- Extract the file name from the
SyntaxNode
. I can do this. - Get an object describing the document containing the code being analyzed. I don't know how to do this.
- Get an object describing the project containing the code being analyzed. I don't know how to do this.
- Find out if the project contains a document by the specified name. I can do this.
- Open and parse or update the contents of that file. I think I can do this.
I get stuck on steps (2) and (3). (Yes, I may not strictly need to do step 2, but I'd still like to know how.)
From the SyntaxNodeAnalysisContext
parameter, I can get the Workspace
object, and from that the Solution
object, and from that a collection of Project
objects. But I don't see a way to relate the particular SyntaxNode
back to the Project
or Document
from which it came.
Any ideas about how to do this would be appreciated.