Esempio n. 1
0
        public void AddFileCodeModel(FileCodeModel fileCodeModel)
        {
            var handle = new ComHandle<EnvDTE80.FileCodeModel2, FileCodeModel>(fileCodeModel);
            var globalNodeKeys = fileCodeModel.GetCurrentNodeKeys();

            _nodeKeysMap.Add(handle, globalNodeKeys);
        }
Esempio n. 2
0
        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;
        }
        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);
        }
Esempio n. 4
0
        public ComHandle <EnvDTE80.FileCodeModel2, FileCodeModel> GetOrCreateFileCodeModel(string filePath, object parent)
        {
            // First try
            {
                var cacheEntry = GetCacheEntry(filePath);
                if (cacheEntry != null)
                {
                    var comHandle = cacheEntry.Value.ComHandle;
                    if (comHandle != null)
                    {
                        return(comHandle.Value);
                    }
                }
            }

            // Check that we know about this file!
            var hostDocument = _project.GetCurrentDocumentFromPath(filePath);

            if (hostDocument == null)
            {
                // Matches behavior of native (C#) implementation
                throw Exceptions.ThrowENotImpl();
            }

            // Create object (outside of lock)
            var newFileCodeModel = FileCodeModel.Create(_state, parent, hostDocument.Id, new TextManagerAdapter());
            var newCacheEntry    = new CacheEntry(newFileCodeModel);

            // Second try (object might have been added by another thread at this point!)
            lock (_cacheGate)
            {
                var cacheEntry = GetCacheEntry(filePath);
                if (cacheEntry != null)
                {
                    var comHandle = cacheEntry.Value.ComHandle;
                    if (comHandle != null)
                    {
                        return(comHandle.Value);
                    }
                }

                // Note: Using the indexer here (instead of "Add") is relevant since the old
                //       WeakReference entry is likely still in the cache (with a Null target, of course)
                _cache[filePath] = newCacheEntry;

                return(newFileCodeModel);
            }
        }
Esempio n. 5
0
        public ComHandle <EnvDTE80.FileCodeModel2, FileCodeModel> GetOrCreateFileCodeModel(string filePath, object?parent)
        {
            // First try
            {
                var cacheEntry = GetCacheEntry(filePath);
                if (cacheEntry != null)
                {
                    var comHandle = cacheEntry.Value.ComHandle;
                    if (comHandle != null)
                    {
                        return(comHandle.Value);
                    }
                }
            }

            // Check that we know about this file!
            var documentId = State.Workspace.CurrentSolution.GetDocumentIdsWithFilePath(filePath).Where(id => id.ProjectId == _projectId).FirstOrDefault();

            if (documentId == null || State.Workspace.CurrentSolution.GetDocument(documentId) == null)
            {
                // Matches behavior of native (C#) implementation
                throw Exceptions.ThrowENotImpl();
            }

            // Create object (outside of lock)
            var newFileCodeModel = FileCodeModel.Create(State, parent, documentId, isSourceGeneratorOutput: false, new TextManagerAdapter());
            var newCacheEntry    = new CacheEntry(newFileCodeModel);

            // Second try (object might have been added by another thread at this point!)
            lock (_cacheGate)
            {
                var cacheEntry = GetCacheEntry(filePath);
                if (cacheEntry != null)
                {
                    var comHandle = cacheEntry.Value.ComHandle;
                    if (comHandle != null)
                    {
                        return(comHandle.Value);
                    }
                }

                // Note: Using the indexer here (instead of "Add") is relevant since the old
                //       WeakReference entry is likely still in the cache (with a Null target, of course)
                _cache[filePath] = newCacheEntry;

                return(newFileCodeModel);
            }
        }
Esempio n. 6
0
        public EnvDTE.TextPoint CreateTextPoint(FileCodeModel fileCodeModel, VirtualTreePoint point)
        {
            if (!fileCodeModel.TryGetDocument(out var document))
            {
                return(null);
            }

            var hierarchyOpt = fileCodeModel.Workspace.GetHierarchy(document.Project.Id);

            using var invisibleEditor = new InvisibleEditor(fileCodeModel.ServiceProvider, document.FilePath, hierarchyOpt, needsSave: false, needsUndoDisabled: false);
            var vsTextLines = invisibleEditor.VsTextLines;

            var line   = point.GetContainingLine();
            var column = point.Position - line.Start + point.VirtualSpaces;

            Marshal.ThrowExceptionForHR(vsTextLines.CreateTextPoint(line.LineNumber, column, out var textPoint));
            return((EnvDTE.TextPoint)textPoint);
        }
Esempio n. 7
0
        public EnvDTE.TextPoint CreateTextPoint(FileCodeModel fileCodeModel, VirtualTreePoint point)
        {
            var workspace = fileCodeModel.Workspace as VisualStudioWorkspaceImpl;
            var hostDocument = workspace.GetHostDocument(fileCodeModel.GetDocumentId());
            if (hostDocument == null)
            {
                return null;
            }

            using (var invisibleEditor = new InvisibleEditor(fileCodeModel.ServiceProvider, hostDocument.FilePath, needsSave: false, needsUndoDisabled: false))
            {
                var vsTextLines = invisibleEditor.VsTextLines;

                var line = point.GetContainingLine();
                var column = point.Position - line.Start + point.VirtualSpaces;
                Marshal.ThrowExceptionForHR(vsTextLines.CreateTextPoint(line.LineNumber, column, out var textPoint));
                return (EnvDTE.TextPoint)textPoint;
            }
        }
Esempio n. 8
0
        public EnvDTE.TextPoint CreateTextPoint(FileCodeModel fileCodeModel, VirtualTreePoint point)
        {
            var workspace    = fileCodeModel.Workspace as VisualStudioWorkspaceImpl;
            var hostDocument = workspace.GetHostDocument(fileCodeModel.GetDocumentId());

            if (hostDocument == null)
            {
                return(null);
            }

            using (var invisibleEditor = new InvisibleEditor(fileCodeModel.ServiceProvider, hostDocument.FilePath, needsSave: false, needsUndoDisabled: false))
            {
                var vsTextLines = invisibleEditor.VsTextLines;

                var line   = point.GetContainingLine();
                var column = point.Position - line.Start + point.VirtualSpaces;
                Marshal.ThrowExceptionForHR(vsTextLines.CreateTextPoint(line.LineNumber, column, out var textPoint));
                return((EnvDTE.TextPoint)textPoint);
            }
        }
 public override EnvDTE.CodeElement CreateUnknownRootNamespaceCodeElement(CodeModelState state, FileCodeModel fileCodeModel)
 {
     return (EnvDTE.CodeElement)CodeNamespace.CreateUnknown(state, fileCodeModel, (int)SyntaxKind.NamespaceDeclaration, string.Empty);
 }
        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();
            }
        }
Esempio n. 12
0
 public EnvDTE.TextPoint CreateTextPoint(FileCodeModel fileCodeModel, VirtualTreePoint point)
 {
     return new TextPoint(point);
 }
 internal bool TryGetFileCodeModelInstanceWithoutCaringWhetherRcwIsAlive(out FileCodeModel fileCodeModel)
 {
     return(_fileCodeModelWeakComHandle.TryGetManagedObjectWithoutCaringWhetherNativeObjectIsAlive(out fileCodeModel));
 }
Esempio n. 14
0
 public EnvDTE.FileCodeModel CreateFileCodeModel(SourceGeneratedDocument sourceGeneratedDocument)
 {
     // Unlike for "regular" documents, we make no effort to cache these between callers or hold them for longer lifetimes with
     // events.
     return(FileCodeModel.Create(GetCodeModelCache().State, parent: null, sourceGeneratedDocument.Id, isSourceGeneratorOutput: true, new TextManagerAdapter()).Handle);
 }
 internal bool TryGetFileCodeModelInstanceWithoutCaringWhetherRcwIsAlive(out FileCodeModel fileCodeModel)
 {
     return _fileCodeModelWeakComHandle.TryGetManagedObjectWithoutCaringWhetherNativeObjectIsAlive(out fileCodeModel);
 }