private async Task TestType(Type type)
        {
            string expectedResult = TypeTextProvider.GetTypeText(type);
            SolutionWithCodeAnalysis       solution = SolutionWithCodeAnalysis.GetSolutionForSourceCode(expectedResult);
            LoadedDocumentWithCodeAnalysis document = await solution.Projects.First().Documents.First().LoadAsync();

            string documentText = document.ToSourceCode();

            documentText.TrimEnd().Replace("\r", string.Empty).Should().Be(expectedResult.TrimEnd().Replace("\r", string.Empty));
        }
Example #2
0
        private static async Task GenerateWrapperImplementations()
        {
            string baseDirectory = Path.GetFullPath(
                Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), @"..\..\..\.."));
            SolutionWithCodeAnalysis solution = await SolutionWithCodeAnalysis.OpenAsync(
                Path.Combine(baseDirectory, @"CSharpDom.sln"));

            ProjectWithCodeAnalysis project = solution.Projects.First(p => p.Syntax.Name == "CSharpDom");

            project.Lock();
            foreach (DocumentWithCodeAnalysis document in project.Documents
                     .Where(document => document.FullFilePath.Contains(@"CSharpDom\Wrappers\Internal"))
                     .OrderBy(document => document.FullFilePath))
            {
                document.IsLocked = true;
                bool isEdited = false;
                LoadedDocumentWithCodeAnalysis loadedDocument = await document.LoadAsync();

                using (CodeAnalysisSettings.AllowEdits(loadedDocument))
                {
                    SealedClassWithCodeAnalysis @class = loadedDocument.Namespaces.First().Classes.SealedClasses.FirstOrDefault();
                    if (@class == null)
                    {
                        continue;
                    }

                    foreach (SealedClassPropertyWithCodeAnalysis property in @class.Properties
                             .Where(property => property.GetAccessor.Body.Statements.FirstOrDefault() is ThrowStatementWithCodeAnalysis)
                             .ToArray())
                    {
                        isEdited = true;
                        string propertyName = property.Name;
                        string fieldName    = propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);
                        SealedClassFieldWithCodeAnalysis field = new SealedClassFieldWithCodeAnalysis(
                            ClassMemberVisibilityModifier.Private,
                            new DelegateReferenceWithCodeAnalysis("Func", property.PropertyType),
                            fieldName);
                        @class.Fields.Fields.Add(field);
                        IList <IStatementWithCodeAnalysis> statements = property.GetAccessor.Body.Statements;
                        statements.Clear();
                        statements.Add(
                            StatementFactory.Return(ExpressionFactory.MethodCall(ExpressionFactory.Identifier(fieldName))));
                    }
                }

                if (isEdited)
                {
                    string sourceCode = loadedDocument.ToSourceCode();
                    File.WriteAllText(document.FullFilePath, sourceCode);
                }
            }
        }
Example #3
0
        private static async Task LoadImplementInterfaceAsync()
        {
            var assembly      = Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features");
            var configuration = new ContainerConfiguration().WithAssembly(assembly);
            var container     = configuration.CreateContainer();
            var test          = container.GetExports <CodeFixProvider>().ToArray();
            var test2         = test.FirstOrDefault(provider =>
                                                    (Attribute.GetCustomAttribute(provider.GetType(), typeof(ExportCodeFixProviderAttribute)) as ExportCodeFixProviderAttribute)?.Name == "ImplementInterface");
            string solutionFile = Path.Combine(
                Path.GetDirectoryName(typeof(Program).Assembly.Location),
                @"..\..\..\..\CSharpDom.sln");

            solutionFile = Path.GetFullPath(solutionFile);
            SolutionWithCodeAnalysis solution = await SolutionWithCodeAnalysis.OpenAsync(solutionFile);

            //(Workspace, ProjectWithCodeAnalysis) project = await ProjectWithCodeAnalysis.OpenWithWorkspaceAsync(projectFile);
            DocumentWithCodeAnalysis document = solution.Projects
                                                .First(project => project.Syntax.Name == "CSharpDom.EditableInterfaceGenerator")
                                                .Documents.First();
            Compilation compilation = await document.Syntax.Project.GetCompilationAsync();

            //SyntaxGenerator generator = document.Syntax.get
            LoadedDocumentWithCodeAnalysis loadedDocument = await document.LoadAsync();

            var syntax = loadedDocument.Namespaces.First().Classes.SealedClasses.First().ImplementedInterfaces.First().Syntax;
            List <CodeAction> actions = new List <CodeAction>();
            var            descriptor = new DiagnosticDescriptor("CS0535", "CS0535", "CS0535", "CS0535", DiagnosticSeverity.Error, true);
            CodeFixContext context    = new CodeFixContext(
                document.Syntax,
                Diagnostic.Create(descriptor, Location.Create(syntax.SyntaxTree, syntax.Span)),
                (action, diagnostics) => actions.Add(action),
                CancellationToken.None);
            await test2.RegisterCodeFixesAsync(context);

            var operations = await actions[0].GetOperationsAsync(CancellationToken.None);

            foreach (var operation in operations)
            {
                operation.Apply(solution.Syntax.Workspace, CancellationToken.None);
            }
            actions.GetHashCode();
        }