Пример #1
0
        public static async Task GenerateEditableInterface(DocumentWithCodeAnalysis document)
        {
            string filePath        = document.FullFilePath;
            string destinationPath = Path.Combine(
                Path.GetDirectoryName(filePath).Replace(@"CSharpDom\Common", @"CSharpDom\Common\Editable"),
                GetNewName(Path.GetFileName(filePath)));

            if (File.Exists(destinationPath))
            {
                return;
            }

            LoadedDocumentWithCodeAnalysis loadedDocument = await document.LoadAsync();

            NamespaceWithCodeAnalysis @namespace = loadedDocument.Namespaces.First();

            if (@namespace.Interfaces.Count == 0)
            {
                return;
            }

            Console.WriteLine($"Writing: {Path.GetFileName(destinationPath)}");
            string namespaceName = @namespace.Name;

            using (CodeAnalysisSettings.AllowEdits(loadedDocument))
            {
                EditUsingDirectives(loadedDocument, namespaceName);
                @namespace.Name = Regex.Replace(namespaceName, "^CSharpDom.Common", "CSharpDom.Common.Editable");
                InterfaceWithCodeAnalysis @interface = @namespace.Interfaces.First();
                string interfaceName = @interface.Name;
                @interface.Name = GetNewName(interfaceName);
                List <ITypeReferenceWithCodeAnalysis> genericParameters = EditInterfaceGenericParameters(@interface);
                EditInterfaceBaseInterfaces(@interface, interfaceName, genericParameters);
                EditInterfaceProperties(@interface);
            }

            Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
            const int maximumLineLength = 120;
            string    sourceCode        = loadedDocument.ToSourceCode(
                new IndentBaseTypeListIfTooLongRule(maximumLineLength),
                new IndentGenericParamterDefinitionsIfTooLongRule(maximumLineLength),
                new IndentMethodParametersIfTooLongRule(maximumLineLength));

            File.WriteAllText(destinationPath, sourceCode);
        }
Пример #2
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();
        }
Пример #3
0
 public static async Task GenerateGenericVisitor(DocumentWithCodeAnalysis document)
 {
     /*string baseDirectory = Path.GetFullPath(
      *  Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), @"..\..\..\.."));
      * LoadedDocumentWithCodeAnalysis genericVisitorDocument = await LoadedDocumentWithCodeAnalysis.LoadFromFileAsync(
      *  Path.Combine(baseDirectory, @"CSharpDom\Common\GenericVisitor.cs"));
      * foreach (StaticClassMethodWithCodeAnalysis method in
      *  genericVisitorDocument.Namespaces.First().Classes.StaticClasses.First().Methods)
      * {
      *  IList<IStatementWithCodeAnalysis> statements = method.Body.Statements;
      *  if (statements.Count == 1 &&
      *      statements[0] is ExpressionStatementWithCodeAnalysis expressionStatement &&
      *      expressionStatement.Expression is MethodCallExpressionWithCodeAnalysis methodCall &&
      *      methodCall.Expression is MemberExpressionWithCodeAnalysis member &&
      *      member.ObjectExpression is IdentifierExpressionWithCodeAnalysis identifier &&
      *      identifier.Name == "GenericChildVisitor")
      *  {
      *      continue;
      *  }
      *
      *  statements.Clear();
      *
      * }*/
 }
Пример #4
0
        public static async Task GenerateBaseClass(DocumentWithCodeAnalysis document)
        {
            string filePath        = document.FullFilePath;
            string destinationPath = Path.Combine(
                Path.GetDirectoryName(filePath).Replace(@"CSharpDom\Common", @"CSharpDom\BaseClasses"),
                GetNewName(Path.GetFileName(filePath)));

            if (File.Exists(destinationPath))
            {
                return;
            }

            LoadedDocumentWithCodeAnalysis loadedDocument = await document.LoadAsync();

            NamespaceWithCodeAnalysis @namespace = loadedDocument.Namespaces.First();

            if (@namespace.Interfaces.Count == 0)
            {
                return;
            }

            InterfaceWithCodeAnalysis @interface = @namespace.Interfaces.First();

            if (@interface.GenericParameters.Count == 0)
            {
                return;
            }

            Console.WriteLine($"Writing: {Path.GetFileName(destinationPath)}");
            string namespaceName = @namespace.Name;

            using (CodeAnalysisSettings.AllowEdits(loadedDocument))
            {
                LoadedDocumentWithCodeAnalysis newDocument = new LoadedDocumentWithCodeAnalysis();
                newDocument.UsingDirectives.Add(new UsingDirectiveWithCodeAnalysis(namespaceName));
                DocumentWithCodeAnalysis baseClassDocument = document.Project.AddDocument(
                    destinationPath,
                    newDocument);
                NamespaceWithCodeAnalysis newNamespace = new NamespaceWithCodeAnalysis(
                    baseClassDocument,
                    namespaceName.Replace("Common", "BaseClasses"));
                AbstractClassWithCodeAnalysis baseClass = new AbstractClassWithCodeAnalysis(
                    baseClassDocument,
                    TypeVisibilityModifier.Public,
                    GetNewName(@interface.Name))
                {
                    GenericParameters = @interface.GenericParameters,
                    BaseClass         = new ClassReferenceWithCodeAnalysis("AbstractExpression")
                };
                ITypeReferenceWithCodeAnalysis[] implementedInterfaceGenericParameters =
                    @interface.GenericParameters
                    .Select(parameter => new GenericParameterReferenceWithCodeAnalysis(parameter.Name))
                    .ToArray();
                baseClass.ImplementedInterfaces.Add(
                    new InterfaceReferenceWithCodeAnalysis(@interface.Name, implementedInterfaceGenericParameters));
                newNamespace.Classes.AbstractClasses.Add(baseClass);
                newDocument.Namespaces.Add(newNamespace);
                await baseClass.ImplementInterfaceAsync(@class => @class.ImplementedInterfaces.First(), baseClassDocument);

                foreach (AbstractClassPropertyWithCodeAnalysis property in baseClass.Properties)
                {
                }
            }

            Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
            const int maximumLineLength = 120;
            string    sourceCode        = loadedDocument.ToSourceCode(
                new IndentBaseTypeListIfTooLongRule(maximumLineLength),
                new IndentGenericParamterDefinitionsIfTooLongRule(maximumLineLength),
                new IndentMethodParametersIfTooLongRule(maximumLineLength));

            File.WriteAllText(destinationPath, sourceCode);
        }