The interactive window that is part of the CTP uses the Syntax.IsCompleteSubmission
API for this purpose.
How to know if an input is only partially complete in Roslyn's ScripEngine
-
31-08-2022 - |
سؤال
I'm building a REPL with Roslyn.
How can I figure out if the input to the session is only partially complete:
> 1+1;
> 2 //that works
> int MyMethod(string a){
//error here, i'd like to continue writing the expression
This code shows the problem:
var engine = new ScriptEngine();
var session = engine.CreateSession();
var input = "int MyMethod(string a){";
session.Execute(input); //Exception: error CS1513: } expected
What do I have to do to find out if the input
is complete or not? So I can show another line in my REPL until the expression is complete.
المحلول
نصائح أخرى
You can use SyntaxTree.ParseText
and GetDiagnostics()
to check if syntax is correct. If not, you will probably want to append this line to some buffer and check this buffer until it will be valid.
var st = SyntaxTree.ParseText(input);
var errors = st.GetDiagnostics();
Note: there always be some error (even for simple 1+1
expression) so you will have to filter it out by default:
لا تنتمي إلى StackOverflow