Hey,
I'm trialing out the editor component with the intention of using it to create Roslyn scripts within my WPF app, but I'm having some difficulties.
The main issue I face is I can't find any docs explaining how to register the scripts globalsType property to make code completion work.
Is there a doc that explains this or perhaps a demo project I've missed?
Hello,
I assume you need to show code completion for objects passed to Scripter.GlobalItems in the editor.
Please take a look at ObjectReference quick start project, there scriptGlobalClass is registered like this:
edit.RegisterCode("global.cs", scriptRun.ScriptHost.GlobalCode);
Then you can use code like
ScriptGlobalClass.RunButton.Text = "Catch me if you can";
(or
using static ScriptGlobalClass;
...
RunButton.Text = "Catch me if you can";
)
If you use TextEditor component instead of IScriptEdit interface, this can be done similarly:
var parser = editor.Lexer as Alternet.Syntax.Parsers.Roslyn.RoslynParser;
if (parser != null)
parser.Repository.AddDocument("global.cs", scriptRun.ScriptHost.GlobalCode);
Please let me know if it helps.
Regards,
Andrew
I assume you need to show code completion for objects passed to Scripter.GlobalItems in the editor.
Please take a look at ObjectReference quick start project, there scriptGlobalClass is registered like this:
edit.RegisterCode("global.cs", scriptRun.ScriptHost.GlobalCode);
Then you can use code like
ScriptGlobalClass.RunButton.Text = "Catch me if you can";
(or
using static ScriptGlobalClass;
...
RunButton.Text = "Catch me if you can";
)
If you use TextEditor component instead of IScriptEdit interface, this can be done similarly:
var parser = editor.Lexer as Alternet.Syntax.Parsers.Roslyn.RoslynParser;
if (parser != null)
parser.Repository.AddDocument("global.cs", scriptRun.ScriptHost.GlobalCode);
Please let me know if it helps.
Regards,
Andrew
Hi Andrew,
Thanks for the quick response.
I spent the weekend playing with the Scripter component but decided in the end to use try and use the built-in Roslyn scripting functionality provided from Microsoft.CodeAnalysis.Scripting instead. This decision was based on the fact I don't need debugging support.
I'm now trying to use the Roslyn provided CSharpScript class which you can see here: https://github.com/dotnet/roslyn/blob/master/src/Scripting/CSharp/CSharpScript.cs
Right now I've a method in my ViewModel which executes the following:
var script = CSharpScript.Create(csharpSource.Text, globalsType: typeof(ScriptApi), options: RoslynScriptOptions);
script.Compile();
var result = await script.RunAsync(scriptingApi);
....
This is working perfectly and so now I'm looking to try and get code completion of editor component to work with the GlobalsType parameter which is passed to the the above Create method.
Do you have any suggestions for this use case?
----
In an ideal world the editors parser should allow me to reuse Roslyn types for setting things up. It'd be great to see something like:
var parser = new CsParser(new RoslynSolution(SourceCodeKind.Script)) as RoslynScriptParser;
parser.GlobalsType = typeof(myTypeHere);
parser.ScriptOptions = _roslynScriptOptions;
......
With the ScriptOptions allowing to reuse the ScriptOptions.cs class from Roslyn:
https://github.com/dotnet/roslyn/blob/master/src/Scripting/Core/ScriptOptions.cs
This would make using the editor component with Roslyn's scripting capabilities a breeze!
Thanks for the quick response.
I spent the weekend playing with the Scripter component but decided in the end to use try and use the built-in Roslyn scripting functionality provided from Microsoft.CodeAnalysis.Scripting instead. This decision was based on the fact I don't need debugging support.
I'm now trying to use the Roslyn provided CSharpScript class which you can see here: https://github.com/dotnet/roslyn/blob/master/src/Scripting/CSharp/CSharpScript.cs
Right now I've a method in my ViewModel which executes the following:
var script = CSharpScript.Create(csharpSource.Text, globalsType: typeof(ScriptApi), options: RoslynScriptOptions);
script.Compile();
var result = await script.RunAsync(scriptingApi);
....
This is working perfectly and so now I'm looking to try and get code completion of editor component to work with the GlobalsType parameter which is passed to the the above Create method.
Do you have any suggestions for this use case?
----
In an ideal world the editors parser should allow me to reuse Roslyn types for setting things up. It'd be great to see something like:
var parser = new CsParser(new RoslynSolution(SourceCodeKind.Script)) as RoslynScriptParser;
parser.GlobalsType = typeof(myTypeHere);
parser.ScriptOptions = _roslynScriptOptions;
......
With the ScriptOptions allowing to reuse the ScriptOptions.cs class from Roslyn:
https://github.com/dotnet/roslyn/blob/master/src/Scripting/Core/ScriptOptions.cs
This would make using the editor component with Roslyn's scripting capabilities a breeze!
Hello,
It's should be possible to use script with code completion for fields declared in script global type, here's a code snippet:
public class GlobalType
{
public int NumberOfStudents;
}
public class MyCsSolution : CsSolution
{
private Type globalType;
public MyCsSolution(Type globalType) : base(Microsoft.CodeAnalysis.SourceCodeKind.Script)
{
this.globalType = globalType;
}
public override ProjectId AddProject(string projectName, string projectPath)
{
if (Solution == null)
return null;
var projectId = ProjectId.CreateNewId();
ProjectInfo projectInfo = ProjectInfo.Create(
projectId,
VersionStamp.Default,
projectName,
projectName,
LanguageNames.CSharp,
filePath: projectPath,
parseOptions: GetParserOptions(Conditionals),
compilationOptions: GetCompilationOptions(),
hostObjectType: globalType,
isSubmission: true);
ApplySolutionChanges(s => s.AddProject(projectInfo));
return projectId;
}
...
var globalType = typeof(GlobalType);
var soluton = new MyCsSolution(globalType);
soluton.RegisterAssembly(globalType.Assembly.Location);
var parser = new CsParser(soluton);
sample editor code:
int test()
{
return NumberOfStudents;
}
You can also override GetCompilationOptions to change some of the values declared in ScriptOptions. Please let me know which ScriptOptions you need to pass, and we can provide an example of how to do this.
Hope this helps.
In the coming release we will add globalType as a paramter to the CsSolution constructor, so you will not need to subclass MyCsSolution from it.
Regards,
Andrew
It's should be possible to use script with code completion for fields declared in script global type, here's a code snippet:
public class GlobalType
{
public int NumberOfStudents;
}
public class MyCsSolution : CsSolution
{
private Type globalType;
public MyCsSolution(Type globalType) : base(Microsoft.CodeAnalysis.SourceCodeKind.Script)
{
this.globalType = globalType;
}
public override ProjectId AddProject(string projectName, string projectPath)
{
if (Solution == null)
return null;
var projectId = ProjectId.CreateNewId();
ProjectInfo projectInfo = ProjectInfo.Create(
projectId,
VersionStamp.Default,
projectName,
projectName,
LanguageNames.CSharp,
filePath: projectPath,
parseOptions: GetParserOptions(Conditionals),
compilationOptions: GetCompilationOptions(),
hostObjectType: globalType,
isSubmission: true);
ApplySolutionChanges(s => s.AddProject(projectInfo));
return projectId;
}
...
var globalType = typeof(GlobalType);
var soluton = new MyCsSolution(globalType);
soluton.RegisterAssembly(globalType.Assembly.Location);
var parser = new CsParser(soluton);
sample editor code:
int test()
{
return NumberOfStudents;
}
You can also override GetCompilationOptions to change some of the values declared in ScriptOptions. Please let me know which ScriptOptions you need to pass, and we can provide an example of how to do this.
Hope this helps.
In the coming release we will add globalType as a paramter to the CsSolution constructor, so you will not need to subclass MyCsSolution from it.
Regards,
Andrew