static public List <SyntakTreeDecorator> AddChildsToNodes(SyntaxNode rootNode, Node parentGraphNode, List <Node> allNodes, List <Edge> allEdges, int currentLevel, string commitId, string path) { var kind = rootNode.Kind(); // break early for stuff if ( (kind == SyntaxKind.UsingDirective) || (kind == SyntaxKind.SimpleBaseType) || (kind == SyntaxKind.ExpressionStatement) || (kind == SyntaxKind.LocalDeclarationStatement) || (kind == SyntaxKind.NotEqualsExpression) || (kind == SyntaxKind.EnumMemberDeclaration) || (kind == SyntaxKind.FieldDeclaration) || (kind == SyntaxKind.AttributeList) //|| (kind == SyntaxKind.ConstructorDeclaration) || (kind == SyntaxKind.MethodDeclaration) || (kind == SyntaxKind.DestructorDeclaration) //|| (kind == SyntaxKind.ClassDeclaration) || (kind == SyntaxKind.Block) || (kind == SyntaxKind.ParameterList) || (kind == SyntaxKind.ObjectCreationExpression) || (kind == SyntaxKind.GenericName) ) { return(null); } var results = new List <SyntakTreeDecorator>(); var childs = rootNode.ChildNodes().ToList(); foreach (var c in childs) { var nodeName = GetName(c); var n = GenerateSyntaxNode(c, commitId, path); var n2 = new SyntakTreeDecorator(); n2.equivilantGraphNode = n; n2.node = c; n.SyntaxNode = c; n.DistanceFromSyntaxRoot = currentLevel; if (allNodes != null) { allNodes.Add(n); } if (parentGraphNode != null) { switch (parentGraphNode.Type) { case Node.NodeType.Syntax: allEdges.Add(new Edge(parentGraphNode, n, Edge.EdgeType.SyntaxHierarchialyAbove)); break; case Node.NodeType.FileCS: allEdges.Add(new Edge(parentGraphNode, n, Edge.EdgeType.InFile)); break; default: allEdges.Add(new Edge(parentGraphNode, n, Edge.EdgeType.Generic)); break; } } var childtrees = AddChildsToNodes(c, n, allNodes, allEdges, currentLevel + 1, commitId, path); if (childtrees != null) { n2.childs.AddRange(childtrees); } results.Add(n2); } return(results); }
static public SyntakTreeDecorator SyntaxTreeFromString(string content, Node fileNode, List <Node> allNodes, List <Edge> allEdges, string commitId, string path) { var foo = CSharpSyntaxTree.ParseText(content); var enrichedTree = new SyntakTreeDecorator(); enrichedTree.node = foo.GetRoot(); enrichedTree.equivilantGraphNode = GenerateSyntaxNode(foo.GetRoot(), commitId, path); nodeId++; var subEdges = new List <Edge>(); var subNodes = new List <Node>(); var childTrees = AddChildsToNodes(foo.GetRoot(), enrichedTree.equivilantGraphNode, subNodes, subEdges, 1, commitId, path); if (allNodes != null) { allNodes.Add(enrichedTree.equivilantGraphNode); allNodes.AddRange(subNodes); } if (allEdges != null) { allEdges.Add(new Edge(fileNode, enrichedTree.equivilantGraphNode, Edge.EdgeType.InFile)); allEdges.AddRange(subEdges); } enrichedTree.childs.AddRange(childTrees); return(enrichedTree); }
public static FindBelongingResult FindBelongingThing(SyntakTreeDecorator toFind, SyntakTreeDecorator possibleMatches) { var singleItemList = new List <SyntakTreeDecorator>(); singleItemList.Add(possibleMatches); return(FindBelongingThing(toFind, singleItemList)); }
public static FindBelongingResult FindBelongingThing(SyntakTreeDecorator toFind, List <SyntakTreeDecorator> possibleMatches) { var toFindContentString = ContentString(toFind.node); var toFindFullString = toFind.node.ToFullString().Trim(); { var identical = possibleMatches.Find(n => n.node.ToFullString().Trim().Equals(toFindFullString)); if (identical != null) { return(new FindBelongingResult() { treeNode = identical, wasModified = false, howModified = ModificationKind.noModification }); } var equiv = possibleMatches.Find(n => n.node.IsEquivalentTo(toFind.node)); if (equiv != null) { return(new FindBelongingResult() { treeNode = equiv, wasModified = false, howModified = ModificationKind.noModification }); } } { var sameKind = possibleMatches.FindAll(n => n.node.Kind().Equals(toFind.node.Kind())); if (sameKind != null) { foreach (var s in sameKind) { bool?haveSameName = HaveSameName(toFind.node.Kind(), s.node, toFind.node); if (haveSameName == true || haveSameName == null) // same name, or no name { return(new FindBelongingResult() { treeNode = s, wasModified = !ContentString(s.node).Equals(toFindContentString), howModified = ModificationKind.contentChanged }); } } } if (sameKind.Count == 1) { var s = sameKind[0]; bool sameContent = ContentString(s.node).Equals(toFindContentString); ModificationKind how = ModificationKind.nameChanged; if (!sameContent) { how = how | ModificationKind.contentChanged; } return(new FindBelongingResult() { treeNode = s, wasModified = true, howModified = how }); } } return(null); }