Looking for ideas on parsing a C# class for its using directives

SyntaxEditor .NET Languages Add-on for WPF Forum

Posted 4 years ago by Scott Jeslis
Version: 18.1.0684
Avatar

Not sure if I'm supposed to use ParseData, a reader, etc.

We're using SyntaxEditor as a built-in C# code editor. When the user compiles, I want to parse the using directives and add any new ones to the IEnumerable collection with namespaces for the Microsoft compiler.

Comments (4)

Posted 4 years ago by Scott Jeslis
Avatar

I ended up using the GetReader, thanks!

var reader = codeDocument.GetReader(0);
 
			while (reader.GoToNextToken())
			{
				var codeLine = reader.SnapshotLine.Text;
				if (codeLine.StartsWith("using"))
				{
					// Parse ot the namespace reference
					var reference = codeLine.Split(' ')[1];
 
					// Strip of trailing semi-colon
					// Namespace to compiler reference list
					AddNamespaceReference(reference.TrimEnd(';'));
				}
				else
				{
					break;
				}
			}
Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Scott,

Do I understand correctly that you want to basically examine the document's AST that was built by our C# parser and look for using directives?  If so, you can see the AST in the Document Outline tool window of our SDI Code Editor demo.  The default document shows how a CompilationUnit root AST node contains a UsingDirectiveSection and that has UsingDirectives inside it.  The latest AST is available in the document's ParseData property.


Actipro Software Support

Posted 4 years ago by Scott Jeslis
Avatar

Thanks I might try that method!

Posted 4 years ago by Scott Jeslis
Avatar

Hmm... Seems more complex than my string searching solution above :-D

The latest build of this product (v24.1.2) was released 0 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.