public static Tuple<TestWorkspace, EnvDTE.FileCodeModel> CreateWorkspaceAndFileCodeModel(string file)
        {
            var workspace = CSharpWorkspaceFactory.CreateWorkspaceFromFile(file, exportProvider: VisualStudioTestExportProvider.ExportProvider);

            try
            {
                var project = workspace.CurrentSolution.Projects.Single();
                var document = project.Documents.Single().Id;

                var componentModel = new MockComponentModel(workspace.ExportProvider);
                var serviceProvider = new MockServiceProvider(componentModel);
                WrapperPolicy.s_ComWrapperFactory = MockComWrapperFactory.Instance;

                var visualStudioWorkspaceMock = new MockVisualStudioWorkspace(workspace);

                var state = new CodeModelState(serviceProvider, project.LanguageServices, visualStudioWorkspaceMock);

                var codeModel = FileCodeModel.Create(state, null, document, new MockTextManagerAdapter()).Handle;

                return Tuple.Create(workspace, (EnvDTE.FileCodeModel)codeModel);
            }
            catch
            {
                // We threw during creation of the FileCodeModel. Make sure we clean up our workspace or else we leak it
                workspace.Dispose();
                throw;
            }
        }
Exemple #2
0
 private CodeTypeRef(CodeModelState state, object parent, ProjectId projectId, ITypeSymbol typeSymbol)
     : base(state)
 {
     _parentHandle = new ParentHandle<object>(parent);
     _projectId = projectId;
     _symbolId = typeSymbol.GetSymbolKey();
 }
        private EnvDTE.CodeElement CreateInternalCodeMember(CodeModelState state, FileCodeModel fileCodeModel, SyntaxNode node)
        {
            var element = CodeModelService.CreateInternalCodeElement(state, fileCodeModel, node);
            if (IsBatchOpen)
            {
                var codeElement = ComAggregate.TryGetManagedObject<AbstractKeyedCodeElement>(element);
                if (codeElement != null)
                {
                    _batchElements.Add(codeElement);
                }
            }

            return element;
        }
        public override EnvDTE.CodeElement CreateUnknownCodeElement(CodeModelState state, FileCodeModel fileCodeModel, SyntaxNode node)
        {
            switch (node.Kind())
            {
                case SyntaxKind.NamespaceDeclaration:
                    return (EnvDTE.CodeElement)CodeNamespace.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.ClassDeclaration:
                    return (EnvDTE.CodeElement)CodeClass.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                case SyntaxKind.InterfaceDeclaration:
                    return (EnvDTE.CodeElement)CodeInterface.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                case SyntaxKind.StructDeclaration:
                    return (EnvDTE.CodeElement)CodeStruct.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                case SyntaxKind.EnumDeclaration:
                    return (EnvDTE.CodeElement)CodeEnum.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                case SyntaxKind.DelegateDeclaration:
                    return (EnvDTE.CodeElement)CodeDelegate.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.MethodDeclaration:
                case SyntaxKind.ConstructorDeclaration:
                case SyntaxKind.DestructorDeclaration:
                case SyntaxKind.OperatorDeclaration:
                case SyntaxKind.ConversionOperatorDeclaration:
                    return (EnvDTE.CodeElement)CodeFunction.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.PropertyDeclaration:
                case SyntaxKind.IndexerDeclaration:
                    return (EnvDTE.CodeElement)CodeProperty.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.EventDeclaration:
                    return (EnvDTE.CodeElement)CodeEvent.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                case SyntaxKind.VariableDeclarator:
                    var eventFieldDeclaration = node.FirstAncestorOrSelf<EventFieldDeclarationSyntax>();
                    if (eventFieldDeclaration != null)
                    {
                        return (EnvDTE.CodeElement)CodeEvent.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));
                    }

                    goto case SyntaxKind.EnumMemberDeclaration;

                case SyntaxKind.EnumMemberDeclaration:
                    return (EnvDTE.CodeElement)CodeVariable.CreateUnknown(state, fileCodeModel, node.RawKind, GetName(node));

                default:
                    throw Exceptions.ThrowEUnexpected();
            }
        }
        /// <summary>
        /// Do not use this method directly! Instead, go through <see cref="FileCodeModel.GetOrCreateCodeElement{T}(SyntaxNode)"/>
        /// </summary>
        public override EnvDTE.CodeElement CreateInternalCodeElement(
            CodeModelState state,
            FileCodeModel fileCodeModel,
            SyntaxNode node)
        {
            // Attributes, attribute arguments, parameters, imports directives, and
            // accessor functions do not have their own node keys. Rather, they are
            // based off their parents node keys and other data.
            switch (node.Kind())
            {
                case SyntaxKind.Attribute:
                    return (EnvDTE.CodeElement)CreateInternalCodeAttribute(state, fileCodeModel, node);

                case SyntaxKind.AttributeArgument:
                    return (EnvDTE.CodeElement)CreateInternalCodeAttributeArgument(state, fileCodeModel, (AttributeArgumentSyntax)node);

                case SyntaxKind.Parameter:
                    return (EnvDTE.CodeElement)CreateInternalCodeParameter(state, fileCodeModel, (ParameterSyntax)node);

                case SyntaxKind.UsingDirective:
                    return CreateInternalCodeImport(state, fileCodeModel, (UsingDirectiveSyntax)node);
            }

            if (IsAccessorNode(node))
            {
                return (EnvDTE.CodeElement)CreateInternalCodeAccessorFunction(state, fileCodeModel, node);
            }

            var nodeKey = GetNodeKey(node);

            switch (node.Kind())
            {
                case SyntaxKind.ClassDeclaration:
                    return (EnvDTE.CodeElement)CodeClass.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.InterfaceDeclaration:
                    return (EnvDTE.CodeElement)CodeInterface.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.StructDeclaration:
                    return (EnvDTE.CodeElement)CodeStruct.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.EnumDeclaration:
                    return (EnvDTE.CodeElement)CodeEnum.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.EnumMemberDeclaration:
                    return (EnvDTE.CodeElement)CodeVariable.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.DelegateDeclaration:
                    return (EnvDTE.CodeElement)CodeDelegate.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.MethodDeclaration:
                case SyntaxKind.ConstructorDeclaration:
                case SyntaxKind.DestructorDeclaration:
                case SyntaxKind.OperatorDeclaration:
                case SyntaxKind.ConversionOperatorDeclaration:
                    return (EnvDTE.CodeElement)CodeFunction.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.NamespaceDeclaration:
                    return (EnvDTE.CodeElement)CodeNamespace.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.PropertyDeclaration:
                case SyntaxKind.IndexerDeclaration:
                    return (EnvDTE.CodeElement)CodeProperty.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.EventDeclaration:
                    return (EnvDTE.CodeElement)CodeEvent.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                case SyntaxKind.VariableDeclarator:
                    var baseFieldDeclaration = node.FirstAncestorOrSelf<BaseFieldDeclarationSyntax>();
                    if (baseFieldDeclaration != null)
                    {
                        if (baseFieldDeclaration.Kind() == SyntaxKind.FieldDeclaration)
                        {
                            return (EnvDTE.CodeElement)CodeVariable.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                        }
                        else if (baseFieldDeclaration.Kind() == SyntaxKind.EventFieldDeclaration)
                        {
                            return (EnvDTE.CodeElement)CodeEvent.Create(state, fileCodeModel, nodeKey, (int)node.Kind());
                        }
                    }

                    throw Exceptions.ThrowEUnexpected();
                default:
                    throw new InvalidOperationException();
            }
        }
 internal CodeModelProjectCache(AbstractProject project, IServiceProvider serviceProvider, HostLanguageServices languageServices, VisualStudioWorkspace workspace)
 {
     _project = project;
     _state = new CodeModelState(serviceProvider, languageServices, workspace);
 }
 internal CodeModelProjectCache(AbstractProject project, IServiceProvider serviceProvider, HostLanguageServices languageServices, VisualStudioWorkspace workspace)
 {
     _project = project;
     _state   = new CodeModelState(serviceProvider, languageServices, workspace);
 }
Exemple #8
0
        internal static EnvDTE.CodeTypeRef Create(CodeModelState state, object parent, ProjectId projectId, ITypeSymbol typeSymbol)
        {
            var newElement = new CodeTypeRef(state, parent, projectId, typeSymbol);

            return((EnvDTE.CodeTypeRef)ComAggregate.CreateAggregatedObject(newElement));
        }
        internal AbstractCodeModelObject(CodeModelState state)
        {
            Debug.Assert(state != null);

            _state = state;
        }
 internal virtual void Shutdown()
 {
     _state   = null;
     _zombied = true;
 }
 internal CodeModelProjectCache(IThreadingContext threadingContext, ProjectId projectId, ICodeModelInstanceFactory codeModelInstanceFactory, ProjectCodeModelFactory projectFactory, IServiceProvider serviceProvider, HostLanguageServices languageServices, VisualStudioWorkspace workspace)
 {
     _state     = new CodeModelState(threadingContext, serviceProvider, languageServices, workspace, projectFactory);
     _projectId = projectId;
     _codeModelInstanceFactory = codeModelInstanceFactory;
 }
Exemple #12
0
 private RootCodeModel(CodeModelState state, EnvDTE.Project parent, ProjectId projectId)
     : base(state)
 {
     _parentHandle = new ParentHandle <EnvDTE.Project>(parent);
     _projectId    = projectId;
 }
Exemple #13
0
        internal static EnvDTE.CodeModel Create(CodeModelState state, EnvDTE.Project parent, ProjectId projectId)
        {
            var rootCodeModel = new RootCodeModel(state, parent, projectId);

            return((EnvDTE.CodeModel)ComAggregate.CreateAggregatedObject(rootCodeModel));
        }
 public override EnvDTE.CodeElement CreateUnknownRootNamespaceCodeElement(CodeModelState state, FileCodeModel fileCodeModel)
 {
     return (EnvDTE.CodeElement)CodeNamespace.CreateUnknown(state, fileCodeModel, (int)SyntaxKind.NamespaceDeclaration, string.Empty);
 }
Exemple #15
0
 internal static EnvDTE.CodeTypeRef Create(CodeModelState state, object parent, ProjectId projectId, ITypeSymbol typeSymbol)
 {
     var newElement = new CodeTypeRef(state, parent, projectId, typeSymbol);
     return (EnvDTE.CodeTypeRef)ComAggregate.CreateAggregatedObject(newElement);
 }
        public override EnvDTE.CodeTypeRef CreateCodeTypeRef(CodeModelState state, ProjectId projectId, object type)
        {
            var project = state.Workspace.CurrentSolution.GetProject(projectId);
            if (project == null)
            {
                throw Exceptions.ThrowEFail();
            }

            var compilation = project.GetCompilationAsync().Result;

            if (type is EnvDTE.vsCMTypeRef || type is int)
            {
                var specialType = GetSpecialType((EnvDTE.vsCMTypeRef)type);
                return CodeTypeRef.Create(state, null, projectId, compilation.GetSpecialType(specialType));
            }

            string typeName;
            object parent = null;

            if (type is EnvDTE.CodeType)
            {
                var codeType = (EnvDTE.CodeType)type;
                typeName = codeType.FullName;
                parent = codeType;
            }
            else if (type is string)
            {
                typeName = (string)type;
            }
            else
            {
                throw Exceptions.ThrowEInvalidArg();
            }

            var typeSymbol = GetTypeSymbolFromFullName(typeName, compilation);
            if (typeSymbol == null)
            {
                throw Exceptions.ThrowEFail();
            }

            if (typeSymbol.TypeKind == TypeKind.Unknown)
            {
                if (SyntaxFactsService.IsValidIdentifier(typeSymbol.Name))
                {
                    throw Exceptions.ThrowEFail();
                }
            }

            return CodeTypeRef.Create(state, parent, projectId, typeSymbol);
        }
Exemple #17
0
 internal CodeModelProjectCache(ProjectId projectId, ICodeModelInstanceFactory codeModelInstanceFactory, IServiceProvider serviceProvider, HostLanguageServices languageServices, VisualStudioWorkspace workspace)
 {
     _state     = new CodeModelState(serviceProvider, languageServices, workspace);
     _projectId = projectId;
     _codeModelInstanceFactory = codeModelInstanceFactory;
 }