示例#1
0
        private async Task SetupCodeControlAsync()
        {
            CodeControl.DebugLevel = 2;
            CodeControl.JTF2       = JTF2;
            CodeControl.Document   = Document;
            var tree = await Document.GetSyntaxTreeAsync();

            CodeControl.SyntaxTree = tree;
            var model = await Document.GetSemanticModelAsync();

            CodeControl.SemanticModel = model;
            if (model != null)
            {
                CodeControl.Compilation = model.Compilation;
            }

            CodeControl.AddHandler(RoslynCodeControl.ContentChangedEvent, new RoslynCodeControl.ContentChangedRoutedEventHandler(CodeControlContentChanged));
            CodeControl.AddHandler(RoslynCodeBase.RenderStartEvent, new RoutedEventHandler((sender, args) =>
            {
                StartTime = DateTime.Now;
                Debug.WriteLine("render start");
            }));
            CodeControl.AddHandler(RoslynCodeBase.RenderCompleteEvent, new RoutedEventHandler((sender, args) =>
            {
                var span = DateTime.Now - StartTime;
                Debug.WriteLine("render complete " + span);
            }));
            await CodeControl.UpdateFormattedTextAsync();

            var d = ViewModel.RunAnalyzers(CodeControl, AnalyzerContexts, tree, model);

            _diagnosticsList.Clear();
            foreach (var diagnostic in d)
            {
                _diagnosticsList.Add(diagnostic);
            }
        }
示例#2
0
 public Task UpdateFormattedTextAsync()
 {
     return(CodeControl.UpdateFormattedTextAsync());
 }
        private async Task SetupCodeControlAsync()
        {
            var w = new AdhocWorkspace(Host);

            w.AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create()));
            var projectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(),
                                                 "Code Project", "code", LanguageNames.CSharp);
            var w2 = w.CurrentSolution.AddProject(projectInfo);

            w.TryApplyChanges(w2);

            DocumentInfo documentInfo;
            var          filename = Filename;

            if (filename != null)
            {
                documentInfo = DocumentInfo.Create(DocumentId.CreateNewId(projectInfo.Id), "Default",
                                                   null, SourceCodeKind.Regular, new FileTextLoader(filename, Encoding.UTF8), filename);
            }
            else
            {
                documentInfo = DocumentInfo.Create(DocumentId.CreateNewId(projectInfo.Id), "Default",
                                                   null, SourceCodeKind.Regular);
            }

            w2 = w.CurrentSolution.AddDocument(documentInfo);
            w.TryApplyChanges(w2);
            CodeControl.XOffset         = 100;
            CodeControl.JTF2            = JTF2;
            CodeControl.PreviewKeyDown += CodeControlOnPreviewKeyDown;
            var doc = w.CurrentSolution.GetDocument(documentInfo.Id);

            if (doc != null)
            {
                ClassDiagram.Document = doc;
                CodeControl.Document  = doc;
                var tree = await doc.GetSyntaxTreeAsync();

                // ReSharper disable once AssignNullToNotNullAttribute
                CodeControl.SyntaxTree = tree;
                var model = await doc.GetSemanticModelAsync();

                // ReSharper disable once AssignNullToNotNullAttribute
                CodeControl.SemanticModel = model;
                CodeControl.Compilation   = model.Compilation;
            }

            DateTime startTime = default;

            CodeControl.AddHandler(RoslynCodeBase.RenderStartEvent, new RoutedEventHandler((sender, args) =>
            {
                startTime = DateTime.Now;
                Debug.WriteLine("render start");
            }));
            CodeControl.AddHandler(RoslynCodeBase.RenderCompleteEvent, new RoutedEventHandler((sender, args) =>
            {
                var span = DateTime.Now - startTime;
                var msg  = $"render complete time is {span}";
                Debug.WriteLine(msg);
                ProtoLogger.Instance.LogAction(msg);
            }));
            await CodeControl.UpdateFormattedTextAsync();

            CodeControlRendered  = true;
            Rendering.Visibility = Visibility.Collapsed;
        }
        private async Task SetupCodeControlAsync()
        {
            var w = new AdhocWorkspace(Host);

#if false
            foreach (var fieldInfo in w.GetType().GetFields(BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic))
            {
                var v = fieldInfo.GetValue(w);
                Debug.WriteLine($"{fieldInfo.Name}: {v}");
            }

            var langSvc = w.Services.GetLanguageServices(LanguageNames.CSharp);
            var method  = langSvc.GetType().GetMethod("GetService", BindingFlags.Instance | BindingFlags.Public);

            List <object> services = new List <object>();
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    foreach (var type in assembly.GetTypes())
                    {
                        if (typeof(ILanguageService).IsAssignableFrom(type))
                        {
                            try
                            {
                                if (!type.ContainsGenericParameters)
                                {
                                    var m      = method.MakeGenericMethod(new[] { type });
                                    var result = m.Invoke(langSvc, new object[] { });
                                    if (result != null)
                                    {
                                        Debug.WriteLine(result);
                                        services.Add(result);
                                    }
                                }
                            }
                            catch
                            {
                            }

                            if (type.IsPublic)
                            {
                                Debug.WriteLine(String.Format("{0:G5}", type.IsPublic.ToString()) + " " +
                                                type.FullName);
                            }
                        }
                    }
                }
                catch
                {
                }
            }
            foreach (object service in services)
            {
                foreach (var methodInfo in service.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    Debug.WriteLine((methodInfo.ReturnType?.FullName ?? "") + " " + service.GetType().FullName + "." + methodInfo.Name + "( " +
                                    string.Join(", ", methodInfo.GetParameters().Select(p => (p.IsOptional ? " optional " : "") + p.ParameterType + " " + p.Name)) + ")");
                }

                if (service.GetType().Name == "CSharpCodeCleanupService")
                {
                }
            }

            // w.Services.FindLanguageServices<IFormattingService>(metadata =>
            // {
            // foreach (var (key, value) in metadata)
            // {

            // Debug.WriteLine($"{key} = {value}");
            // }

            // Debug.WriteLine("");

            // return false;
            // });
#endif
            w.AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create()));
            var projectInfo = ProjectInfo.Create(ProjectId.CreateNewId(), VersionStamp.Create(),
                                                 "Code Project", "code", LanguageNames.CSharp);
            var w2 = w.CurrentSolution.AddProject(projectInfo);
            w.TryApplyChanges(w2);

            DocumentInfo documentInfo;
            var          filename = Filename;
            if (filename != null)
            {
                documentInfo = DocumentInfo.Create(DocumentId.CreateNewId(projectInfo.Id), "Default",
                                                   null, SourceCodeKind.Regular, new FileTextLoader(filename, Encoding.UTF8), filename);
            }
            else
            {
                documentInfo = DocumentInfo.Create(DocumentId.CreateNewId(projectInfo.Id), "Default",
                                                   null, SourceCodeKind.Regular);
            }

            w2 = w.CurrentSolution.AddDocument(documentInfo);
            w.TryApplyChanges(w2);

            Project1  = w.CurrentSolution.GetProject(projectInfo.Id);
            Document1 = w.CurrentSolution.GetDocument(documentInfo.Id);


            var qui       = w.Services.GetLanguageServices(LanguageNames.CSharp).GetService <QuickInfoService>();
            var textAsync = await Document1.GetTextAsync();

            if (false)
            {
                for (var i = 0; i < textAsync.Length; i++)
                {
                    var re = await qui.GetQuickInfoAsync(Document1, i);

                    if (re != null)
                    {
                        Debug.WriteLine(re.Span.ToString());
                        Debug.WriteLine("tags = " + string.Join(";", re.Tags));
                        foreach (var reRelatedSpan in re.RelatedSpans)
                        {
                            Debug.WriteLine("relatedspan " + reRelatedSpan.ToString());
                        }

                        foreach (var quickInfoSection in re.Sections)
                        {
                            Debug.WriteLine("" + i + " Text(" + quickInfoSection.Text + ") Kind(" + quickInfoSection.Kind + ") TaggedParts(" +
                                            String.Join(", ", quickInfoSection.TaggedParts) + ")");
                        }
                    }
                }
            }

            CodeControl.DebugLevel = 2;
            CodeControl.JTF2       = JTF2;
            CodeControl.Document   = Document1;
            var tree = await Document1.GetSyntaxTreeAsync();

            CodeControl.SyntaxTree = tree;
            var model = await Document1.GetSemanticModelAsync();

            CodeControl.SemanticModel = model;
            CodeControl.Compilation   = model.Compilation;

            // CodeControl.AddHandler(RoslynCodeControl.ContentChangedEvent, new RoslynCodeControl.ContentChangedRoutedEventHandler(CodeControlContentChanged));
            CodeControl.AddHandler(RoslynCodeBase.RenderStartEvent, new RoutedEventHandler((sender, args) =>
            {
                // StartTime = DateTime.Now;
                Debug.WriteLine("render start");
            }));
            CodeControl.AddHandler(RoslynCodeBase.RenderCompleteEvent, new RoutedEventHandler((sender, args) =>
            {
                // var span = DateTime.Now - StartTime;
                Debug.WriteLine("render complete ");
            }));

            await CodeControl.UpdateFormattedTextAsync();
        }