Use CodeDocument with SetText without the AmbientParseRequestDispatcherProvider.Dispatcher

SyntaxEditor for WPF Forum

Posted 2 months ago by Josh
Version: 18.1.2
Avatar

Hello,

We currently have the  AmbientParseRequestDispatcherProvider.Dispatcher setup, but on that background thread, we are using the CodeDocument to do more parsing on macros and would like the CodeDocument to just use the current thread thats its on and when we do the SetText method, the parsing would be done synchronously and not perform an async parse using the AmbientParseRequestDispatcherProvider.Dispatcher.

Is this possible?

Thanks!

Comments (10)

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Josh,

If you are looking to parse some text synchronously on the main UI thread (where you simply configure a syntax language) to get a parse data result, you could call the Parse extension method with a string, as described in this documentation topic.  Behind the scenes, it makes a document and does everything it needs to do to provide the parse data result.


Actipro Software Support

Posted 2 months ago by Josh
Avatar

How can I execute a secondary parse within the background thread synchronously ?

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

You could use the same extension method described there to execute a parse synchronously from either a main or worker thread.


Actipro Software Support

Posted 2 months ago by Josh
Avatar

I was able to get it to work using the extension method.

When using the CodeDocument, SetText method, it will use the dispatcher.  Is there a way to make that synchronous?

[Modified 2 months ago]

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

If using a CodeDocument, there is a way to do a synchronous parse but it requires a lot more work.  If you are just using the CodeDocument to get a parse data result for some text, then the Parse extension method is an easy one-line call that does the same thing.  Is there a specific reason you need to use a CodeDocument?

Is the text for this secondary synchronous parse the same text as the original document text, or is this some expanded/altered version?  You mentioned the use of macros above.


Actipro Software Support

Posted 2 months ago by Josh
Avatar

The text needed to be parsed is sometimes expanded, and sometimes different text.

For example:

int foo = 3
int foo2 = myFunc(foo)

In this case... we actually look at the logic of myFunc and parse the logic of myFunc under the context of where its getting called checking for anything invalid.

We want to use the CodeDocument because we have current code related to macros that we are trying to track snapshots etc...

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

If you set a special variation of your syntax language to the CodeDocument that doesn't have an IParser language service, then no automatic parsing will occur on SetText changes.  You could create a CodeDocument, assign this special syntax language variation, set your text, make a new ParseRequest, and invoke the IParser.Parse method manually.


Actipro Software Support

Posted 2 months ago by Josh
Avatar

Is there a way to "wait" until the CodeDocument is done parsing and the ParseData is ready?

Posted 2 months ago by Josh
Avatar

Also, if I was to use the CodeDocument that doesnt have a IParser language service and I manually send a ParseRequest... does that take care of the snapshot?

Our code relies on having a snapshot.

Can you provide a sample to what your describing with not setting the parser?

When I tried, I got the following error when I'm creating the ParseRequest here:

				var parseRequest = new ParseRequest("processmacro", codeDocument.CurrentSnapshot.GetMergedBufferReader(), newLanguage, codeDocument)
				{
					Snapshot = codeDocument.CurrentSnapshot
				};

System.ArgumentNullException: 'Value cannot be null. Parameter name: parser'

[Modified 2 months ago]

Posted 2 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Yes all documents have snapshots, so that's not a problem.  The null ref exception there is likely because you passed in a "newLanguage" that doesn't have an IParser language service.  Instead of that overload, use the ParseRequest constructor overload that takes an IParser in place of ISyntaxLanguage and pass in the IParser instance.  That will resolve the exception.


Actipro Software Support

The latest build of this product (v22.1.3) was released 20 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.