public static GraphNodeId GetIdForDocument(Document document)
 {
     return
         (GraphNodeId.GetNested(
              GraphNodeId.GetPartial(CodeGraphNodeIdName.Assembly, new Uri(document.Project.FilePath, UriKind.RelativeOrAbsolute)),
              GraphNodeId.GetPartial(CodeGraphNodeIdName.File, new Uri(document.FilePath, UriKind.RelativeOrAbsolute))));
 }
Beispiel #2
0
        private GraphNodeId GetGraphNodeId(string projectPath, GraphNode parentNode, string modelId)
        {
            var partialValues = new List <GraphNodeId>
            {
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Assembly,
                                       new Uri(projectPath, UriKind.RelativeOrAbsolute)),
                GraphNodeId.GetPartial(CodeGraphNodeIdName.File,
                                       new Uri(modelId.ToLowerInvariant(), UriKind.RelativeOrAbsolute))
            };

            var parents = string.Empty;

            if (parentNode != null)
            {
                // to ensure Graph id for node is unique we add a hashcodes for node's parents separated by ';'
                parents = parentNode.Id.GetNestedValueByName <string>(CodeGraphNodeIdName.Namespace);
                if (string.IsNullOrEmpty(parents))
                {
                    var currentProject = parentNode.Id.GetValue(CodeGraphNodeIdName.Assembly) ?? projectPath;
                    parents = currentProject.GetHashCode().ToString();
                }
            }
            else
            {
                parents = projectPath.GetHashCode().ToString();
            }

            parents = parents + ";" + modelId.GetHashCode();
            partialValues.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.Namespace, parents));

            return(GraphNodeId.GetNested(partialValues.ToArray()));
        }
        private GraphNode GetOrCreatePackageNode(IGraphContext context, GraphNode parent, IVsPackageMetadata package)
        {
            var parentId = parent.GetValue <GraphNodeId>("Id");
            var nodeId   = GraphNodeId.GetNested(
                parentId,
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Member, package.Id),
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Parameter, package.VersionString));
            var node = context.Graph.Nodes.Get(nodeId);

            if (node == null)
            {
                using (var scope = new GraphTransactionScope())
                {
                    node = context.Graph.Nodes.GetOrCreate(nodeId, package.Id, ReferencesGraphSchema.PackageCategory);

                    node.SetValue <string>(DgmlNodeProperties.Icon, GraphIcons.Package);
                    node.SetValue <IVsPackageMetadata>(ReferencesGraphSchema.PackageProperty, package);

                    // Establish the relationship with the parent node.
                    context.Graph.Links.GetOrCreate(parent, node, null, GraphCommonSchema.Contains);

                    context.OutputNodes.Add(node);
                    scope.Complete();
                }
            }

            return(node);
        }
Beispiel #4
0
        internal static async Task <GraphNodeId> GetIdForTypeAsync(
            ITypeSymbol symbol,
            Solution solution,
            CancellationToken cancellationToken
            )
        {
            var nodes = await GetPartialsForNamespaceAndTypeAsync(
                symbol,
                true,
                solution,
                cancellationToken
                )
                        .ConfigureAwait(false);

            var partials = nodes.ToArray();

            if (partials.Length == 1)
            {
                return(partials[0]);
            }
            else
            {
                return(GraphNodeId.GetNested(partials));
            }
        }
        private GraphNodeId GetTopLevelGraphNodeId(string projectPath, IDependencyNode nodeInfo)
        {
            var partialValues = new List <GraphNodeId>
            {
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Assembly, new Uri(projectPath, UriKind.RelativeOrAbsolute))
            };

            var projectFolder = Path.GetDirectoryName(projectPath)?.ToLowerInvariant();

            if (nodeInfo.Flags.Contains(DependencyNode.CustomItemSpec))
            {
                if (nodeInfo.Name != null)
                {
                    partialValues.Add(GraphNodeId.GetPartial(
                                          CodeGraphNodeIdName.File,
                                          new Uri(Path.Combine(projectFolder, nodeInfo.Name.ToLowerInvariant()),
                                                  UriKind.RelativeOrAbsolute)));
                }
            }
            else
            {
                var fullItemSpecPath = MakeRooted(projectFolder, nodeInfo.Id.ItemSpec);
                if (!string.IsNullOrEmpty(fullItemSpecPath))
                {
                    partialValues.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.File,
                                                             new Uri(fullItemSpecPath.ToLowerInvariant(), UriKind.RelativeOrAbsolute)));
                }
            }

            return(GraphNodeId.GetNested(partialValues.ToArray()));
        }
        private static async Task <GraphNodeId> GetPartialForNamedTypeAsync(INamedTypeSymbol namedType, GraphNodeIdName nodeName, Solution solution, CancellationToken cancellationToken, bool isInGenericArguments = false)
        {
            // If this is a simple type, then we don't have much to do
            if (namedType.ContainingType == null && namedType.ConstructedFrom == namedType && namedType.Arity == 0)
            {
                return(GraphNodeId.GetPartial(nodeName, namedType.Name));
            }
            else
            {
                // For a generic type, we need to populate "type" property with the following form:
                //
                //      Type = (Name =...GenericParameterCount = GenericArguments =...ParentType =...)
                //
                //  where "Name" contains a symbol name
                //    and "GenericParameterCount" contains the number of type parameters,
                //    and "GenericArguments" contains its type parameters' node information.
                //    and "ParentType" contains its containing type's node information.

                var partials = new List <GraphNodeId>();

                partials.Add(GraphNodeId.GetPartial(CodeQualifiedName.Name, namedType.Name));

                if (namedType.Arity > 0)
                {
                    partials.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.GenericParameterCountIdentifier, namedType.Arity.ToString()));
                }

                // For the property "GenericArguments", we only populate them
                // when type parameters are constructed using instance types (i.e., namedType.ConstructedFrom != namedType).
                // However, there is a case where we need to populate "GenericArguments" even though arguments are not marked as "constructed"
                // because a symbol is not marked as "constructed" when a type is constructed using its own type parameters.
                // To distinguish this case, we use "isInGenericArguments" flag which we pass either to populate arguments recursively or to populate "ParentType".

                bool hasGenericArguments = (namedType.ConstructedFrom != namedType || isInGenericArguments) && namedType.TypeArguments != null && namedType.TypeArguments.Any();

                if (hasGenericArguments)
                {
                    var genericArguments = new List <GraphNodeId>();
                    foreach (var arg in namedType.TypeArguments)
                    {
                        var nodes = await GetPartialsForNamespaceAndTypeAsync(arg, includeNamespace : true, solution : solution, cancellationToken : cancellationToken, isInGenericArguments : true).ConfigureAwait(false);

                        genericArguments.Add(GraphNodeId.GetNested(nodes.ToArray()));
                    }

                    partials.Add(GraphNodeId.GetArray(
                                     CodeGraphNodeIdName.GenericArgumentsIdentifier,
                                     genericArguments.ToArray()));
                }

                if (namedType.ContainingType != null)
                {
                    partials.Add(await GetPartialForTypeAsync(namedType.ContainingType, CodeGraphNodeIdName.ParentType, solution, cancellationToken, hasGenericArguments).ConfigureAwait(false));
                }

                return(GraphNodeId.GetPartial(nodeName, MakeCollectionIfNecessary(false, partials.ToArray())));
            }
        }
Beispiel #7
0
        private static GraphNodeId GetTopLevelGraphNodeId(string projectPath, string modelId)
        {
            string projectFolder = Path.GetDirectoryName(projectPath)?.ToLowerInvariant() ?? string.Empty;
            var    filePath      = new Uri(Path.Combine(projectFolder, modelId.ToLowerInvariant()), UriKind.RelativeOrAbsolute);

            return(GraphNodeId.GetNested(
                       GraphNodeId.GetPartial(CodeGraphNodeIdName.Assembly, new Uri(projectPath, UriKind.RelativeOrAbsolute)),
                       GraphNodeId.GetPartial(CodeGraphNodeIdName.File, filePath)));
        }
        private static object MakeCollectionIfNecessary(bool isHomogeneous, GraphNodeId[] array)
        {
            // Place the array of GraphNodeId's into the collection if necessary, so to make them appear in VS Properties Panel
            if (array.Length > 1)
            {
                return(new GraphNodeIdCollection(false, array));
            }

            return(GraphNodeId.GetNested(array));
        }
Beispiel #9
0
        private GraphNodeId GetGraphNodeId(string projectPath, IDependencyNode nodeInfo)
        {
            var partialValues = new List <GraphNodeId>();

            partialValues.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.Assembly,
                                                     new Uri(projectPath, UriKind.RelativeOrAbsolute)));
            partialValues.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.File,
                                                     new Uri(nodeInfo.Id.ToString().ToLowerInvariant(),
                                                             UriKind.RelativeOrAbsolute)));
            return(GraphNodeId.GetNested(partialValues.ToArray()));
        }
Beispiel #10
0
        private GraphNodeId GetTopLevelGraphNodeId(string projectPath, string modelId)
        {
            var partialValues = new List<GraphNodeId>
            {
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Assembly, new Uri(projectPath, UriKind.RelativeOrAbsolute))
            };

            var projectFolder = Path.GetDirectoryName(projectPath)?.ToLowerInvariant() ?? string.Empty;
            var filePath = new Uri(Path.Combine(projectFolder, modelId.ToLowerInvariant()), UriKind.RelativeOrAbsolute);

            partialValues.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.File, filePath));

            return GraphNodeId.GetNested(partialValues.ToArray());
        }
Beispiel #11
0
        public static GraphNodeId GetId(this IProjectNode node)
        {
            var project = node.As <Project>();

            if (project.Kind == EnvDTE.Constants.vsProjectKindUnmodeled)
            {
                return(null);
            }

            var fileName = GetProjectFileUri(project);

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

            return(GraphNodeId.GetNested(CodeGraphNodeIdName.Assembly, fileName));
        }
        public static GraphNode CreateNode(string projectPath,
                                           string nodeIdString,
                                           IDependencyNode node = null)
        {
            var graph = new Graph();
            var id    = GraphNodeId.GetNested(
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Assembly, new Uri(projectPath, UriKind.RelativeOrAbsolute)),
                GraphNodeId.GetPartial(CodeGraphNodeIdName.File, new Uri(nodeIdString, UriKind.RelativeOrAbsolute))
                );

            var graphNode = graph.Nodes.GetOrCreate(id);

            if (node != null)
            {
                graphNode.SetValue(DependenciesGraphSchema.DependencyNodeProperty, node);
            }

            return(graphNode);
        }
        private void CreateNode(IGraphContext context, GraphNode parent, PaketMetadata metadata)
        {
            var parentId = parent.GetValue <GraphNodeId>("Id");
            var id       = GraphNodeId.GetNested(
                parentId,
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Member, metadata.Id),
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Parameter, metadata.VersionString));

            var node = context.Graph.Nodes.Get(id);

            if (node == null)
            {
                string label = metadata.Id + " " + metadata.VersionString;

                node = context.Graph.Nodes.GetOrCreate(id, label, PaketGraphSchema.PaketCategory);

                node.SetValue(DgmlNodeProperties.ContainsChildren, false);
                node.SetValue(CodeNodeProperties.SourceLocation, new SourceLocation(new Uri(parentId.GetFileName(), UriKind.Absolute),
                                                                                    new Position(PaketGraphSchema.GetDisplayIndex(node), 1)));
                node.SetValue(DgmlNodeProperties.Icon, GraphIcons.Package);
            }

            context.Graph.Links.GetOrCreate(parent, node, null, GraphCommonSchema.Contains);
        }
        private GraphNodeId GetGraphNodeId(string projectPath, IDependencyNode nodeInfo, GraphNode parentNode)
        {
            var partialValues = new List <GraphNodeId>
            {
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Assembly,
                                       new Uri(projectPath, UriKind.RelativeOrAbsolute)),
                GraphNodeId.GetPartial(CodeGraphNodeIdName.File,
                                       new Uri(nodeInfo.Id.ToString().ToLowerInvariant(),
                                               UriKind.RelativeOrAbsolute))
            };

            // to ensure Graph id for node is unique we add a hashcodes for node's parents separated by ';'
            var parents = parentNode.Id.GetNestedValueByName <string>(CodeGraphNodeIdName.Namespace);

            if (string.IsNullOrEmpty(parents))
            {
                parents = projectPath.GetHashCode().ToString();
            }

            parents = parents + ";" + nodeInfo.Id.GetHashCode();
            partialValues.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.Namespace, parents));

            return(GraphNodeId.GetNested(partialValues.ToArray()));
        }
Beispiel #15
0
        public static async Task <GraphNodeId> GetIdForMemberAsync(ISymbol member, Solution solution, CancellationToken cancellationToken)
        {
            var partials = new List <GraphNodeId>();

            partials.AddRange(await GetPartialsForNamespaceAndTypeAsync(member.ContainingType, true, solution, cancellationToken).ConfigureAwait(false));

            var parameters = member.GetParameters();

            if (parameters.Any() || member.GetArity() > 0)
            {
                var memberPartials = new List <GraphNodeId>();

                memberPartials.Add(GraphNodeId.GetPartial(CodeQualifiedName.Name, member.MetadataName));

                if (member.GetArity() > 0)
                {
                    memberPartials.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.GenericParameterCountIdentifier, member.GetArity().ToString()));
                }

                if (parameters.Any())
                {
                    var parameterTypeIds = new List <GraphNodeId>();
                    foreach (var p in parameters)
                    {
                        var parameterIds = await GetPartialsForNamespaceAndTypeAsync(p.Type, true, solution, cancellationToken).ConfigureAwait(false);

                        var nodes = parameterIds.ToList();
                        if (p.IsRefOrOut())
                        {
                            nodes.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.ParamKind, ParamKind.Ref));
                        }

                        parameterTypeIds.Add(GraphNodeId.GetNested(nodes.ToArray()));
                    }

                    if (member is IMethodSymbol methodSymbol && methodSymbol.MethodKind == MethodKind.Conversion)
                    {
                        // For explicit/implicit conversion operators, we need to include the return type in the method Id,
                        // because there can be several conversion operators with same parameters and only differ by return type.
                        // For example,
                        //
                        //     public class Class1
                        //     {
                        //         public static explicit (explicit) operator int(Class1 c) { ... }
                        //         public static explicit (explicit) operator double(Class1 c) { ... }
                        //     }

                        var nodes = await GetPartialsForNamespaceAndTypeAsync(methodSymbol.ReturnType, true, solution, cancellationToken).ConfigureAwait(false);

                        List <GraphNodeId> returnTypePartial = nodes.ToList();
                        returnTypePartial.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.ParamKind, Microsoft.VisualStudio.GraphModel.CodeSchema.ParamKind.Return));

                        GraphNodeId returnCollection = GraphNodeId.GetNested(returnTypePartial.ToArray());
                        parameterTypeIds.Add(returnCollection);
                    }

                    memberPartials.Add(GraphNodeId.GetArray(
                                           CodeGraphNodeIdName.OverloadingParameters,
                                           parameterTypeIds.ToArray()));
                }

                partials.Add(GraphNodeId.GetPartial(
                                 CodeGraphNodeIdName.Member,
                                 MakeCollectionIfNecessary(false, memberPartials.ToArray())));
            }
            else
            {
                partials.Add(GraphNodeId.GetPartial(CodeGraphNodeIdName.Member, member.MetadataName));
            }

            return(GraphNodeId.GetNested(partials.ToArray()));
        }
Beispiel #16
0
 public static GraphNodeId GetId(this IItemNode node)
 {
     return(GraphNodeId.GetNested(
                GraphNodeId.GetPartial(CodeGraphNodeIdName.Assembly, GetProjectFileUri(node.OwningProject.As <Project>())),
                GraphNodeId.GetPartial(CodeGraphNodeIdName.File, new Uri(node.PhysicalPath, UriKind.RelativeOrAbsolute))));
 }