void FirstCompilation() { string ResultStr = String.Empty; DynamicCompile.CompileCode("return 0;", out ResultStr); Dispatcher.BeginInvoke(new Action(OpenMainWindow)); }
public static void Renderer(Plotter plotter, string pathToCSharpTemplate) { if (Path.GetExtension(pathToCSharpTemplate).ToLower() != ".cs") { return; } void uiThread() { //----- dynamic compile MetadataReference[] moreReferences = new MetadataReference[] { MetadataReference.CreateFromFile(typeof(OxyPlot.PlotModel).GetTypeInfo().Assembly.Location), MetadataReference.CreateFromFile(typeof(TuringTrader.ReportTemplate).GetTypeInfo().Assembly.Location), }; var assy = DynamicCompile.CompileSource(pathToCSharpTemplate, moreReferences); if (assy == null) { Output.WriteLine("Plotter: can't compile template {0}", pathToCSharpTemplate); return; } //----- instantiate template var templateType = assy.GetTypes() .Where(t => t.IsSubclassOf(typeof(ReportTemplate))) .FirstOrDefault(); if (templateType == null) { Output.WriteLine("Plotter: can't load template {0}", pathToCSharpTemplate); return; } ReportTemplate template = (ReportTemplate)Activator.CreateInstance(templateType); template.PlotData = plotter.AllData; template.PlotTitle = plotter.Title; //----- open dialog var report = new Report(template); report.ShowDialog(); } // The calling thread must be STA, because many UI components require this. // https://stackoverflow.com/questions/2329978/the-calling-thread-must-be-sta-because-many-ui-components-require-this Thread thread = new Thread(uiThread); thread.SetApartmentState(ApartmentState.STA); thread.Start(); //thread.Join(); // wait for window to close }
public void CompileCodeThreadFunc() { try { string Code = EditorCode.Dispatcher.Invoke(new Func <string>(() => { return(EditorCode.Text); })); string ResultStr = String.Empty; Brush TextColor = CompiledColor; if (Code == String.Empty) { ResultStr = String.Empty; } else { if (!DynamicCompile.CompileCode(Code, out ResultStr)) { TextColor = ErrorColor; } else if (ResultStr == String.Empty) // Nothing to return, like with code: "Math.Pow(2, 2)" { string ResultStrPrev = ResultStr; if (!DynamicCompile.CompileCode("return " + Code + "", out ResultStr)) // If error - take last result without "return". { ResultStr = ResultStrPrev; } } } PlotViewer.Dispatcher.Invoke(() => { PlotViewer.Model = Plot.Model; PlotViewer.InvalidatePlot(); }); EditorResult.Dispatcher.Invoke(() => { EditorResult.Text = ResultStr; EditorResult.Foreground = TextColor; }); } catch { } }
public static IEnumerable <string> GetCodeAutocompletionList(string Code, int CarretPosition) { CodeAutocompletion_AlreadyEnteredStrLen = 0; string FullCode = DynamicCompile.GetCodeTemplate(Code, false); SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(FullCode); CSharpCompilation compilation = CSharpCompilation.Create("SharpSmartMath.cs").AddReferences(DynamicCompile.GetMetadataReferences()).AddSyntaxTrees(syntaxTree); SemanticModel semanticModel = compilation.GetSemanticModel(syntaxTree); int TemplateOffset = DynamicCompile.CodeTemplate.IndexOf(DynamicCompile.Template_CodeString); Match match = Regex.Match(Code.Substring(0, CarretPosition), @"\S*", RegexOptions.RightToLeft); if (!match.Success || match.Length <= 0 || match.Index + match.Length < CarretPosition) { return(null); } TextSpan MatchSpan = new TextSpan(TemplateOffset + match.Index, match.Length); SyntaxNode syntaxNode = syntaxTree.GetRoot().DescendantNodes(MatchSpan).Where(s => !(s is IdentifierNameSyntax)).Last(); if (syntaxNode is MemberAccessExpressionSyntax) { MemberAccessExpressionSyntax memberAccessNode = (MemberAccessExpressionSyntax)syntaxNode; ITypeSymbol lhsType = semanticModel.GetTypeInfo(memberAccessNode.Expression).Type; IEnumerable <string> WordsList = lhsType.GetMembers().Where( s => s.CanBeReferencedByName && s.DeclaredAccessibility == Accessibility.Public && s.Name.StartsWith(memberAccessNode.Name.ToString()) ).Select(s => s.Name).Distinct(); CodeAutocompletion_AlreadyEnteredStrLen = memberAccessNode.Name.GetLocation().SourceSpan.Length; return(WordsList); } return(null); }
public static string FormatCode(string Code) { string FullCode = DynamicCompile.GetCodeTemplate(Code, false); Workspace workspace = new AdhocWorkspace(); Solution solution = workspace.CurrentSolution; Project project = solution.AddProject("SharpSmartMath", "SharpSmartMath", LanguageNames.CSharp); Document DocumentTemplate = project.AddDocument("SharpSmartMath.cs", DynamicCompile.CodeTemplate); string FormattedTemplate = Formatter.FormatAsync(DocumentTemplate).Result.GetTextAsync().Result.ToString(); Document DocumentCode = project.AddDocument("SharpSmartMath.cs", FullCode); string FormattedCode = Formatter.FormatAsync(DocumentCode).Result.GetTextAsync().Result.ToString(); int CodeStartPos = FormattedTemplate.IndexOf(DynamicCompile.Template_CodeString); int CodeEndLen = FormattedTemplate.Length - CodeStartPos - DynamicCompile.Template_CodeString.Length; FormattedCode = FormattedCode.Remove(FormattedCode.Length - CodeEndLen, CodeEndLen).Remove(0, CodeStartPos); string[] Lines = FormattedCode.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); int SpacesCount = Lines.Min(str => str.Length > 0 ? str.Length - str.TrimStart().Length : int.MaxValue); return(string.Join(Environment.NewLine, Lines.Select(str => str.Remove(0, Math.Min(SpacesCount, str.Length))))); }