コード例 #1
0
ファイル: NamespaceGenerator.cs プロジェクト: belav/roslyn
        internal static SyntaxNode GenerateNamespaceDeclaration(
            ICodeGenerationService service,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options,
            CancellationToken cancellationToken
            )
        {
            options ??= CodeGenerationOptions.Default;
            GetNameAndInnermostNamespace(
                @namespace,
                options,
                out var name,
                out var innermostNamespace
                );

            var declaration = GetDeclarationSyntaxWithoutMembers(
                @namespace,
                innermostNamespace,
                name,
                options
                );

            declaration = options.GenerateMembers
                ? service.AddMembers(
                declaration,
                innermostNamespace.GetMembers(),
                options,
                cancellationToken
                )
                : declaration;

            return(AddFormatterAndCodeGeneratorAnnotationsTo(declaration));
        }
コード例 #2
0
        public static MemberDeclarationSyntax GenerateNamedTypeDeclaration(
            ICodeGenerationService service,
            INamedTypeSymbol namedType,
            CodeGenerationDestination destination,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            options = options ?? CodeGenerationOptions.Default;

            var declaration = GetDeclarationSyntaxWithoutMembers(namedType, destination, options);

            if (namedType.IsComImport)
            {
                // If we're generating a ComImport type, then do not attempt to do any
                // reordering of members.
                options = options.With(autoInsertionLocation: false, sortMembers: false);
            }

            // If we are generating members then make sure to exclude properties that cannot be generated.
            // Reason: Calling AddProperty on a propertysymbol that can't be generated (like one with params) causes
            // the getter and setter to get generated instead. Since the list of members is going to include
            // the method symbols for the getter and setter, we don't want to generate them twice.
            declaration = options.GenerateMembers && namedType.TypeKind != TypeKind.Delegate
                ? service.AddMembers(declaration,
                                     GetMembers(namedType).Where(s => s.Kind != SymbolKind.Property || PropertyGenerator.CanBeGenerated((IPropertySymbol)s)),
                                     options,
                                     cancellationToken)
                : declaration;

            return AddCleanupAnnotationsTo(ConditionallyAddDocumentationCommentTo(declaration, namedType, options));
        }
コード例 #3
0
        private string RunTemplateInternal(ModelElement rootElement, ICodeGenerationService codeGenerationService)
        {
            TemplateResult result = RunTemplateReturningErrors(rootElement, codeGenerationService);

            Assert.AreNotEqual <string>("ErrorGeneratingOutput", result.ContentResults);
            return(result.ContentResults);
        }
コード例 #4
0
        public static MemberDeclarationSyntax GenerateNamedTypeDeclaration(
            ICodeGenerationService service,
            INamedTypeSymbol namedType,
            CodeGenerationDestination destination,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            options = options ?? CodeGenerationOptions.Default;

            var declaration = GetDeclarationSyntaxWithoutMembers(namedType, destination, options);

            if (namedType.IsComImport)
            {
                // If we're generating a ComImport type, then do not attempt to do any
                // reordering of members.
                options = options.With(autoInsertionLocation: false, sortMembers: false);
            }

            // If we are generating members then make sure to exclude properties that cannot be generated.
            // Reason: Calling AddProperty on a propertysymbol that can't be generated (like one with params) causes
            // the getter and setter to get generated instead. Since the list of members is going to include
            // the method symbols for the getter and setter, we don't want to generate them twice.
            declaration = options.GenerateMembers && namedType.TypeKind != TypeKind.Delegate
                ? service.AddMembers(declaration,
                                     GetMembers(namedType).Where(s => s.Kind != SymbolKind.Property || PropertyGenerator.CanBeGenerated((IPropertySymbol)s)),
                                     options,
                                     cancellationToken)
                : declaration;

            return(AddCleanupAnnotationsTo(ConditionallyAddDocumentationCommentTo(declaration, namedType, options)));
        }
コード例 #5
0
        // TODO: Refactor dependency mess
        public SoundGeneratorViewModel(ISessionContextService sessionContext,
                                       IDialogService dialogService,
                                       IFoldersExplorerFactory <SoundEvent, Sound> factory,
                                       IPreferenceService preferenceService,
                                       ISoundJsonUpdaterFactory jsonUpdaterFactory,
                                       IEditorFormFactory <Sound> editorFormFactory,
                                       IUniqueValidator <SoundEvent> soundEventValidator,
                                       ICodeGenerationService codeGenerationService)
            : base(sessionContext, dialogService, factory)
        {
            Preferences = preferenceService.GetOrCreate <SoundsGeneratorPreferences>();

            PreferenceService     = preferenceService;
            JsonUpdaterFactory    = jsonUpdaterFactory;
            SoundEventValidator   = soundEventValidator;
            CodeGenerationService = codeGenerationService;

            Explorer.AllowFileExtensions(".ogg");
            Explorer.OpenFileDialog.Filter                = "Sound file (*.ogg) | *.ogg";
            Explorer.OpenFileDialog.Multiselect           = true;
            Explorer.OpenFileDialog.CheckFileExists       = true;
            Explorer.OpenFileDialog.ValidateNames         = true;
            Explorer.OpenFolderDialog.ShowNewFolderButton = true;

            FileEditor             = editorFormFactory.Create();
            FileEditor.ItemEdited += OnSoundEdited;
        }
コード例 #6
0
 public MaterialGeneratorViewModel(ISessionContextService sessionContext,
                                   IEditorFormFactory <Material> editorFormFactory,
                                   IUniqueValidator <Material> validator,
                                   ICodeGenerationService codeGenerationService)
     : base(sessionContext, editorFormFactory, validator, codeGenerationService)
 {
 }
コード例 #7
0
 public BlockGeneratorViewModel(ISessionContextService sessionContext,
                                IEditorFormFactory <Block> editorFormFactory,
                                IUniqueValidator <Block> validator,
                                ICodeGenerationService codeGenerationService)
     : base(sessionContext, editorFormFactory, validator, codeGenerationService)
 {
 }
コード例 #8
0
ファイル: NamespaceGenerator.cs プロジェクト: belav/roslyn
        public static CompilationUnitSyntax AddNamespaceTo(
            ICodeGenerationService service,
            CompilationUnitSyntax destination,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options,
            IList <bool> availableIndices,
            CancellationToken cancellationToken
            )
        {
            var declaration = GenerateNamespaceDeclaration(
                service,
                @namespace,
                options,
                cancellationToken
                );

            if (!(declaration is NamespaceDeclarationSyntax))
            {
                throw new ArgumentException(
                          CSharpWorkspaceResources.Namespace_can_not_be_added_in_this_destination
                          );
            }

            var members = Insert(
                destination.Members,
                (NamespaceDeclarationSyntax)declaration,
                options,
                availableIndices
                );

            return(destination.WithMembers(members));
        }
コード例 #9
0
        private static RecordDeclarationSyntax GenerateRecordMembers(
            ICodeGenerationService service,
            CSharpCodeGenerationContextInfo info,
            RecordDeclarationSyntax recordDeclaration,
            ImmutableArray <ISymbol> members,
            CancellationToken cancellationToken)
        {
            if (!info.Context.GenerateMembers)
            {
                members = ImmutableArray <ISymbol> .Empty;
            }

            // For a record, add record parameters if we have a primary constructor.
            var primaryConstructor = members.OfType <IMethodSymbol>().FirstOrDefault(m => CodeGenerationConstructorInfo.GetIsPrimaryConstructor(m));

            if (primaryConstructor != null)
            {
                var parameterList = ParameterGenerator.GenerateParameterList(primaryConstructor.Parameters, isExplicit: false, info);
                recordDeclaration = recordDeclaration.WithParameterList(parameterList);

                // remove the primary constructor from the list of members to generate.
                members = members.Remove(primaryConstructor);

                // remove any fields/properties that were created by the primary constructor
                members = members.WhereAsArray(m => m is not IPropertySymbol and not IFieldSymbol || !primaryConstructor.Parameters.Any(static (p, m) => p.Name == m.Name, m));
コード例 #10
0
        private void OnElementDeleted(object sender, ElementDeletedEventArgs e)
        {
            IArtifactLinkContainer artifactContainer = ModelCollector.GetArtifacts(e.ModelElement);

            if (artifactContainer != null && artifactContainer.ArtifactLinks != null)
            {
                ICodeGenerationService codeGenerationService = GetService <ICodeGenerationService>();
                codeGenerationService.ValidateDeleteFromCollection(artifactContainer.ArtifactLinks);
            }
        }
コード例 #11
0
        private static IArtifactLinkContainer FilterValidLinks(IServiceProvider serviceProvider, IArtifactLinkContainer links)
        {
            ICodeGenerationService codeGenerationService = serviceProvider.GetService(typeof(ICodeGenerationService)) as ICodeGenerationService;

            if (codeGenerationService == null ||
                links == null || !codeGenerationService.AreValid(links.ArtifactLinks))
            {
                return(null);
            }
            return(links);
        }
コード例 #12
0
 public static SyntaxNode UpdateCompilationUnitOrNamespaceDeclaration(
     ICodeGenerationService service,
     SyntaxNode declaration,
     IList <ISymbol> newMembers,
     CodeGenerationOptions options,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, options, cancellationToken);
     return(AddCleanupAnnotationsTo(declaration));
 }
コード例 #13
0
        private TemplateResult RunTemplateReturningErrors(ModelElement rootElement, ICodeGenerationService codeGenerationService)
        {
            TextTemplateHost host = new TextTemplateHost(DomainModel, rootElement, rootElement, codeGenerationService, ContractType);

            host.ResourceResolver = new AssemblyResourceResolver();
            Engine engine = new Engine();
            string result = engine.ProcessTemplate(Template, host);

            string[] errors = GetErrors(host);
            return(new TemplateResult(host.GenerateOutput ? result : string.Empty, errors));
        }
コード例 #14
0
ファイル: NamespaceGenerator.cs プロジェクト: EkardNT/Roslyn
 public static SyntaxNode UpdateCompilationUnitOrNamespaceDeclaration(
     ICodeGenerationService service,
     SyntaxNode declaration,
     IList<ISymbol> newMembers,
     CodeGenerationOptions options,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, options, cancellationToken);
     return AddCleanupAnnotationsTo(declaration);
 }
コード例 #15
0
 public static CompilationUnitSyntax AddNamedTypeTo(
     ICodeGenerationService service,
     CompilationUnitSyntax destination,
     INamedTypeSymbol namedType,
     CodeGenerationOptions options,
     IList<bool> availableIndices)
 {
     var declaration = GenerateNamedTypeDeclaration(service, namedType, CodeGenerationDestination.CompilationUnit, options);
     var members = Insert(destination.Members, declaration, options, availableIndices);
     return destination.WithMembers(members);
 }
コード例 #16
0
 public static MemberDeclarationSyntax UpdateNamedTypeDeclaration(
     ICodeGenerationService service,
     MemberDeclarationSyntax declaration,
     IList <ISymbol> newMembers,
     CodeGenerationOptions options,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, options, cancellationToken);
     return(AddFormatterAndCodeGeneratorAnnotationsTo(declaration));
 }
コード例 #17
0
ファイル: MembersPuller.cs プロジェクト: stark-lang/stark
        private static void ChangeMemberToPublicAndNonStatic(
            ICodeGenerationService codeGenerationService,
            DocumentEditor editor,
            SyntaxNode memberDeclaration,
            ISymbol member)
        {
            var modifiers = DeclarationModifiers.From(member).WithIsStatic(false);

            editor.SetAccessibility(memberDeclaration, Accessibility.Public);
            editor.SetModifiers(memberDeclaration, modifiers);
        }
コード例 #18
0
ファイル: NamedTypeGenerator.cs プロジェクト: nemec/roslyn
 public static MemberDeclarationSyntax UpdateNamedTypeDeclaration(
     ICodeGenerationService service,
     MemberDeclarationSyntax declaration,
     IList<ISymbol> newMembers,
     CodeGenerationOptions options,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, options, cancellationToken);
     return AddCleanupAnnotationsTo(declaration);
 }
コード例 #19
0
 public static SyntaxNode UpdateCompilationUnitOrNamespaceDeclaration(
     ICodeGenerationService service,
     SyntaxNode declaration,
     IList <ISymbol> newMembers,
     CSharpCodeGenerationContextInfo info,
     CancellationToken cancellationToken)
 {
     declaration = RemoveAllMembers(declaration);
     declaration = service.AddMembers(declaration, newMembers, info, cancellationToken);
     return(AddFormatterAndCodeGeneratorAnnotationsTo(declaration));
 }
コード例 #20
0
        public static TypeDeclarationSyntax AddNamedTypeTo(
            ICodeGenerationService service,
            TypeDeclarationSyntax destination,
            INamedTypeSymbol namedType,
            CodeGenerationOptions options,
            IList <bool> availableIndices)
        {
            var declaration = GenerateNamedTypeDeclaration(service, namedType, GetDestination(destination), options);
            var members     = Insert(destination.Members, declaration, options, availableIndices);

            return(AddMembersTo(destination, members));
        }
コード例 #21
0
        public static CompilationUnitSyntax AddNamedTypeTo(
            ICodeGenerationService service,
            CompilationUnitSyntax destination,
            INamedTypeSymbol namedType,
            CodeGenerationOptions options,
            IList <bool> availableIndices)
        {
            var declaration = GenerateNamedTypeDeclaration(service, namedType, CodeGenerationDestination.CompilationUnit, options);
            var members     = Insert(destination.Members, declaration, options, availableIndices);

            return(destination.WithMembers(members));
        }
コード例 #22
0
 public SimpleInitViewModelBase(ISessionContextService sessionContext,
                                IEditorFormFactory <TModel> editorFormFactory,
                                IUniqueValidator <TModel> validator,
                                ICodeGenerationService codeGenerationService)
     : base(sessionContext)
 {
     Validator              = validator;
     CodeGenerationService  = codeGenerationService;
     EditorForm             = editorFormFactory.Create();
     EditorForm.ItemEdited += OnModelEdited;
     EditorForm.Validator   = validator;
 }
コード例 #23
0
        public static TypeDeclarationSyntax AddNamedTypeTo(
            ICodeGenerationService service,
            TypeDeclarationSyntax destination,
            INamedTypeSymbol namedType,
            CodeGenerationOptions options,
            IList<bool> availableIndices)
        {
            var declaration = GenerateNamedTypeDeclaration(service, namedType, GetDestination(destination), options);
            var members = Insert(destination.Members, declaration, options, availableIndices);

            return AddMembersTo(destination, members);
        }
コード例 #24
0
 public TestContext(string initial, string expected, bool compareTokens = true, string forceLanguage = null, bool ignoreResult = false)
 {
     _expected          = expected.NormalizeLineEndings();
     _language          = forceLanguage != null ? forceLanguage : GetLanguage(initial);
     this.IsVisualBasic = _language == LanguageNames.VisualBasic;
     _compareTokens     = compareTokens;
     _ignoreResult      = ignoreResult;
     _workspace         = CreateWorkspaceFromFile(initial.NormalizeLineEndings(), this.IsVisualBasic, null, null);
     this.Document      = _workspace.CurrentSolution.Projects.Single().Documents.Single();
     this.SemanticModel = Document.GetSemanticModelAsync().Result;
     this.SyntaxTree    = SemanticModel.SyntaxTree;
     this.Service       = Document.Project.LanguageServices.GetService <ICodeGenerationService>();
 }
コード例 #25
0
 public static NamespaceDeclarationSyntax AddNamedTypeTo(
     ICodeGenerationService service,
     NamespaceDeclarationSyntax destination,
     INamedTypeSymbol namedType,
     CodeGenerationOptions options,
     IList<bool> availableIndices)
 {
     var declaration = GenerateNamedTypeDeclaration(service, namedType, CodeGenerationDestination.Namespace, options);
     var members = Insert(destination.Members, declaration, options, availableIndices);
     return ConditionallyAddFormattingAnnotationTo(
         destination.WithMembers(members),
         members);
 }
コード例 #26
0
        public static TypeDeclarationSyntax AddNamedTypeTo(
            ICodeGenerationService service,
            TypeDeclarationSyntax destination,
            INamedTypeSymbol namedType,
            CSharpCodeGenerationContextInfo info,
            IList <bool>?availableIndices,
            CancellationToken cancellationToken)
        {
            var declaration = GenerateNamedTypeDeclaration(service, namedType, GetDestination(destination), info, cancellationToken);
            var members     = Insert(destination.Members, declaration, info, availableIndices);

            return(AddMembersTo(destination, members, cancellationToken));
        }
コード例 #27
0
        public static CompilationUnitSyntax AddNamedTypeTo(
            ICodeGenerationService service,
            CompilationUnitSyntax destination,
            INamedTypeSymbol namedType,
            CSharpCodeGenerationContextInfo info,
            IList <bool>?availableIndices,
            CancellationToken cancellationToken)
        {
            var declaration = GenerateNamedTypeDeclaration(service, namedType, CodeGenerationDestination.CompilationUnit, info, cancellationToken);
            var members     = Insert(destination.Members, declaration, info, availableIndices);

            return(destination.WithMembers(members));
        }
コード例 #28
0
        public static NamespaceDeclarationSyntax AddNamedTypeTo(
            ICodeGenerationService service,
            NamespaceDeclarationSyntax destination,
            INamedTypeSymbol namedType,
            CodeGenerationOptions options,
            IList <bool> availableIndices)
        {
            var declaration = GenerateNamedTypeDeclaration(service, namedType, CodeGenerationDestination.Namespace, options);
            var members     = Insert(destination.Members, declaration, options, availableIndices);

            return(ConditionallyAddFormattingAnnotationTo(
                       destination.WithMembers(members),
                       members));
        }
コード例 #29
0
 internal CodeGenerationServiceProvider(
     ITypeMappingService typeMappingService
     , ICodeGenerationService codeGenerationService
     , ICodeWriterFilterService codeFilterService
     , IMetadataProviderService metadataProviderService
     , INamingService namingService
     )
 {
     this.TypeMappingService      = typeMappingService;
     this.CodeGenerationService   = codeGenerationService;
     this.CodeWriterFilterService = codeFilterService;
     this.MetadataProviderService = metadataProviderService;
     this.NamingService           = namingService;
 }
コード例 #30
0
        public static BaseNamespaceDeclarationSyntax AddNamedTypeTo(
            ICodeGenerationService service,
            BaseNamespaceDeclarationSyntax destination,
            INamedTypeSymbol namedType,
            CSharpCodeGenerationContextInfo info,
            IList <bool>?availableIndices,
            CancellationToken cancellationToken)
        {
            var declaration = GenerateNamedTypeDeclaration(service, namedType, CodeGenerationDestination.Namespace, info, cancellationToken);
            var members     = Insert(destination.Members, declaration, info, availableIndices);

            return(ConditionallyAddFormattingAnnotationTo(
                       destination.WithMembers(members),
                       members));
        }
コード例 #31
0
        private static void ChangeEventToPublicAndNonStatic(
            ICodeGenerationService codeGenerationService,
            DocumentEditor editor,
            IEventSymbol eventSymbol,
            SyntaxNode eventDeclaration,
            DeclarationModifiers modifiers
            )
        {
            var declaration = editor.Generator.GetDeclaration(eventDeclaration);
            var isEventHasExplicitAddOrRemoveMethod =
                (eventSymbol.AddMethod != null && !eventSymbol.AddMethod.IsImplicitlyDeclared) ||
                (
                    eventSymbol.RemoveMethod != null &&
                    !eventSymbol.RemoveMethod.IsImplicitlyDeclared
                );

            // There are three situations here:
            // 1. Single Event.
            // 2. Several events exist in one declaration.
            // 3. Event has add or remove method(user declared).
            // For situation 1, declaration is EventFieldDeclaration, eventDeclaration is variableDeclaration.
            // For situation 2, declaration and eventDeclaration are both EventDeclaration, which are same.
            // For situation 3, it is same as situation 2, but has add or remove method.
            if (declaration.Equals(eventDeclaration) && !isEventHasExplicitAddOrRemoveMethod)
            {
                // Several events are declared in same line
                var publicAndNonStaticSymbol = CodeGenerationSymbolFactory.CreateEventSymbol(
                    eventSymbol,
                    accessibility: Accessibility.Public,
                    modifiers: modifiers
                    );
                var options = new CodeGenerationOptions(generateMethodBodies: false);
                var publicAndNonStaticSyntax = codeGenerationService.CreateEventDeclaration(
                    publicAndNonStaticSymbol,
                    destination: CodeGenerationDestination.ClassType,
                    options: options
                    );
                // Insert a new declaration and remove the original declaration
                editor.InsertAfter(declaration, publicAndNonStaticSyntax);
                editor.RemoveNode(eventDeclaration);
            }
            else
            {
                // Handle both single event and event has add or remove method
                editor.SetAccessibility(declaration, Accessibility.Public);
                editor.SetModifiers(declaration, modifiers);
            }
        }
コード例 #32
0
ファイル: NamespaceGenerator.cs プロジェクト: EkardNT/Roslyn
        public static CompilationUnitSyntax AddNamespaceTo(
            ICodeGenerationService service,
            CompilationUnitSyntax destination,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options,
            IList<bool> availableIndices)
        {
            var declaration = GenerateNamespaceDeclaration(service, @namespace, options);
            if (!(declaration is NamespaceDeclarationSyntax))
            {
                throw new ArgumentException(CSharpWorkspaceResources.NamespaceCanNotBeAddedIn);
            }

            var members = Insert(destination.Members, (NamespaceDeclarationSyntax)declaration, options, availableIndices);
            return destination.WithMembers(members);
        }
コード例 #33
0
ファイル: NamespaceGenerator.cs プロジェクト: Rickinio/roslyn
        public static NamespaceDeclarationSyntax AddNamespaceTo(
            ICodeGenerationService service,
            NamespaceDeclarationSyntax destination,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options,
            IList<bool> availableIndices,
            CancellationToken cancellationToken)
        {
            var declaration = GenerateNamespaceDeclaration(service, @namespace, options, cancellationToken);
            if (!(declaration is NamespaceDeclarationSyntax))
            {
                throw new ArgumentException(CSharpWorkspaceResources.Namespace_can_not_be_added_in_this_destination);
            }

            var members = Insert(destination.Members, (NamespaceDeclarationSyntax)declaration, options, availableIndices);
            return destination.WithMembers(members);
        }
コード例 #34
0
        internal static SyntaxNode GenerateNamespaceDeclaration(
            ICodeGenerationService service,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options,
            CancellationToken cancellationToken)
        {
            options = options ?? CodeGenerationOptions.Default;
            GetNameAndInnermostNamespace(@namespace, options, out var name, out var innermostNamespace);

            var declaration = GetDeclarationSyntaxWithoutMembers(@namespace, innermostNamespace, name, options);

            declaration = options.GenerateMembers
                    ? service.AddMembers(declaration, innermostNamespace.GetMembers(), options, cancellationToken)
                    : declaration;

            return AddCleanupAnnotationsTo(declaration);
        }
コード例 #35
0
 public TestContext(
     string expected,
     bool ignoreResult,
     string language,
     TestWorkspace workspace,
     SemanticModel semanticModel)
 {
     _expected          = expected.NormalizeLineEndings();
     _language          = language;
     this.IsVisualBasic = _language == LanguageNames.VisualBasic;
     _ignoreResult      = ignoreResult;
     Workspace          = workspace;
     this.Document      = Workspace.CurrentSolution.Projects.Single().Documents.Single();
     this.SemanticModel = semanticModel;
     this.SyntaxTree    = SemanticModel.SyntaxTree;
     this.Service       = Document.Project.LanguageServices.GetService <ICodeGenerationService>();
 }
コード例 #36
0
        internal static SyntaxNode GenerateNamespaceDeclaration(
            ICodeGenerationService service,
            INamespaceSymbol @namespace,
            CodeGenerationDestination destination,
            CSharpCodeGenerationContextInfo info,
            CancellationToken cancellationToken)
        {
            GetNameAndInnermostNamespace(@namespace, info, out var name, out var innermostNamespace);

            var declaration = GetDeclarationSyntaxWithoutMembers(
                @namespace, innermostNamespace, name, destination, info);

            declaration = info.Context.GenerateMembers
                ? service.AddMembers(declaration, innermostNamespace.GetMembers(), info, cancellationToken)
                : declaration;

            return(AddFormatterAndCodeGeneratorAnnotationsTo(declaration));
        }
コード例 #37
0
        public static CompilationUnitSyntax AddNamespaceTo(
            ICodeGenerationService service,
            CompilationUnitSyntax destination,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options,
            IList <bool> availableIndices)
        {
            var declaration = GenerateNamespaceDeclaration(service, @namespace, options);

            if (!(declaration is PackageDeclarationSyntax))
            {
                throw new ArgumentException(CSharpWorkspaceResources.NamespaceCanNotBeAddedIn);
            }

            var members = Insert(destination.Members, (PackageDeclarationSyntax)declaration, options, availableIndices);

            return(destination.WithMembers(members));
        }
コード例 #38
0
        private static RecordDeclarationSyntax GenerateRecordMembers(
            ICodeGenerationService service,
            CodeGenerationOptions options,
            RecordDeclarationSyntax recordDeclaration,
            ImmutableArray <ISymbol> members,
            CancellationToken cancellationToken)
        {
            // For a record, add record parameters if we have a primary constructor.
            var primaryConstructor = members.OfType <IMethodSymbol>().FirstOrDefault(m => CodeGenerationConstructorInfo.GetIsPrimaryConstructor(m));

            if (primaryConstructor != null)
            {
                var parameterList = ParameterGenerator.GenerateParameterList(primaryConstructor.Parameters, isExplicit: false, options);
                recordDeclaration = recordDeclaration.WithParameterList(parameterList);

                // remove the primary constructor from the list of members to generate.
                members = members.Remove(primaryConstructor);

                // remove any properties that were created by the primary constructor
                members = members.WhereAsArray(m => m is not IPropertySymbol property || !primaryConstructor.Parameters.Any(p => p.Name == property.Name));
            }

            // remove any implicit overrides to generate.
            members = members.WhereAsArray(m => !m.IsImplicitlyDeclared);

            // If there are no members, just make a simple record with no body
            if (members.Length == 0)
            {
                recordDeclaration = recordDeclaration.WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
            }
            else
            {
                // Otherwise, give the record a body.
                recordDeclaration = recordDeclaration.WithOpenBraceToken(SyntaxFactory.Token(SyntaxKind.OpenBraceToken))
                                    .WithCloseBraceToken(SyntaxFactory.Token(SyntaxKind.CloseBraceToken));
            }

            if (options.GenerateMembers)
            {
                recordDeclaration = service.AddMembers(recordDeclaration, members, options, cancellationToken);
            }

            return(recordDeclaration);
        }
コード例 #39
0
        internal static SyntaxNode GenerateNamespaceDeclaration(
            ICodeGenerationService service,
            INamespaceSymbol @namespace,
            CodeGenerationOptions options)
        {
            options = options ?? CodeGenerationOptions.Default;

            string           name;
            INamespaceSymbol innermostNamespace;

            GetNameAndInnermostNamespace(@namespace, options, out name, out innermostNamespace);

            var declaration = GetDeclarationSyntaxWithoutMembers(@namespace, innermostNamespace, name, options);

            declaration = options.GenerateMembers
                    ? service.AddMembers(declaration, innermostNamespace.GetMembers(), options)
                    : declaration;

            return(AddCleanupAnnotationsTo(declaration));
        }
コード例 #40
0
 protected AbstractMetadataAsSourceService(ICodeGenerationService codeGenerationService)
 {
     _codeGenerationService = codeGenerationService;
 }
コード例 #41
0
		private TemplateResult RunTemplateReturningErrors(ModelElement rootElement, ICodeGenerationService codeGenerationService)
		{
			TextTemplateHost host = new TextTemplateHost(DomainModel, rootElement, rootElement, codeGenerationService, ContractType);
			host.ResourceResolver = new AssemblyResourceResolver();
			Engine engine = new Engine();
			string result = engine.ProcessTemplate(Template, host);
			string[] errors = GetErrors(host);
			return new TemplateResult(host.GenerateOutput ? result : string.Empty, errors);
		}
コード例 #42
0
		private string RunTemplateInternal(ModelElement rootElement, ICodeGenerationService codeGenerationService)
		{
			TemplateResult result = RunTemplateReturningErrors(rootElement, codeGenerationService);
			Assert.AreNotEqual<string>("ErrorGeneratingOutput", result.ContentResults);
			return result.ContentResults;
		}
コード例 #43
0
 public CustomCodeGenerationService(ICodeGenerationService service) : base(service) {}
 protected BaseCustomCodeGenerationService(ICodeGenerationService service)
 {
     _defaultService = service;
 }