Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwhitfield committed Aug 11, 2022
1 parent 53ba481 commit ad43085
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ private IEnumerable<Connector> GetNodeLinksFrom(Node node, int desiredGroup, Has

protected override void InternalCompute()
{
if (!_nodeGroups.Any())
{
return;
}

// we now have nodes in their vertical groups, so now we want to try and assign vertical positions, minimizing crossings
// start at min, forward
var minGroup = _nodeGroups.Keys.Min();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ public static class NodeFactory

private static readonly Dictionary<Type, Func<SyntaxNode, SyntaxNode>> _targetTransforms = new Dictionary<Type, Func<SyntaxNode, SyntaxNode>>
{
{ typeof(AnonymousFunctionExpressionSyntax), node => node.Ancestors().First(x => x is VariableDeclaratorSyntax) },
{ typeof(AnonymousMethodExpressionSyntax), node => node.Ancestors().First(x => x is VariableDeclaratorSyntax) },
{ typeof(ParenthesizedLambdaExpressionSyntax), node => node.Ancestors().First(x => x is VariableDeclaratorSyntax) },
{ typeof(SimpleLambdaExpressionSyntax), node => node.Ancestors().First(x => x is VariableDeclaratorSyntax) },
{ typeof(AnonymousFunctionExpressionSyntax), node => node.Ancestors().FirstOrDefault(x => x is VariableDeclaratorSyntax) ?? node },
{ typeof(AnonymousMethodExpressionSyntax), node => node.Ancestors().FirstOrDefault(x => x is VariableDeclaratorSyntax) ?? node },
{ typeof(ParenthesizedLambdaExpressionSyntax), node => node.Ancestors().FirstOrDefault(x => x is VariableDeclaratorSyntax) ?? node },
{ typeof(SimpleLambdaExpressionSyntax), node => node.Ancestors().FirstOrDefault(x => x is VariableDeclaratorSyntax) ?? node },
};

public static bool IsSupportedContainer(SyntaxNode node, out SyntaxNode actualTarget)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ protected VariableContainedFunctionNode(NodeGraph flowChart, FoundReferences fou

public override IEnumerable<SearchableSymbol> GetSearchableSymbols()
{
var variableDeclarator = NodeFoundReferences.SyntaxNode.AncestorsAndSelf().OfType<VariableDeclaratorSyntax>().First();
var targetSymbol = NodeFoundReferences.SemanticModel.GetDeclaredSymbol(variableDeclarator);
if (targetSymbol != null)
var variableDeclarator = NodeFoundReferences.SyntaxNode.AncestorsAndSelf().OfType<VariableDeclaratorSyntax>().FirstOrDefault();
if (variableDeclarator != null)
{
yield return new SearchableSymbol(NodeFoundReferences, new[] { targetSymbol }, NodeFoundReferences.Solution, "variable " + variableDeclarator.Identifier.Text);
var targetSymbol = NodeFoundReferences.SemanticModel.GetDeclaredSymbol(variableDeclarator);
if (targetSymbol != null)
{
yield return new SearchableSymbol(NodeFoundReferences, new[] { targetSymbol }, NodeFoundReferences.Solution, "variable " + variableDeclarator.Identifier.Text);
}
}
}
}
Expand Down
152 changes: 96 additions & 56 deletions src/VisualFindReferences.Core/Graph/Model/SymbolProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,58 @@ public static async Task<FoundReferences> FindReferencesAsync(Action<string> upd
// for each referencing location
foreach (var location in item.Locations)
{
// get the text, syntax node and semantic model
var text = await location.Document.GetTextAsync();
var syntaxNode = await location.Document.GetSyntaxRootAsync().ConfigureAwait(true);
var semanticModel = await location.Document.GetSemanticModelAsync().ConfigureAwait(true);

if (syntaxNode != null && semanticModel != null)
{
// now walk up the syntax tree until we find a node that NodeFactory supports
var current = syntaxNode.FindToken(location.Location.SourceSpan.Start).Parent;

while (current != null)
{
if (NodeFactory.IsSupportedContainer(current, out var actualNode))
{
var containerSymbol = semanticModel.GetDeclaredSymbol(actualNode);
if (containerSymbol != null)
{
if (!outputDictionary.TryGetValue(containerSymbol, out var referencingSymbol))
{
outputDictionary[containerSymbol] = referencingSymbol = new ReferencingSymbol(containerSymbol, current, semanticModel, new List<ReferencingLocation>());
}

referencingSymbol.ReferencingLocations.Add(new ReferencingLocation(location, text));
}
break;
}

current = current.Parent;
}
}
await ProcessLocation(outputDictionary, location);
}
}
}

return new FoundReferences(searchingNode.Symbol, searchingNode.SyntaxNode, searchingNode.SemanticModel, solution, outputDictionary.Values.ToList(), document);
}

private static async Task ProcessLocation(Dictionary<ISymbol, ReferencingSymbol> outputDictionary, ReferenceLocation location)
{
// get the text, syntax node and semantic model
var text = await location.Document.GetTextAsync();
var syntaxNode = await location.Document.GetSyntaxRootAsync().ConfigureAwait(true);
var semanticModel = await location.Document.GetSemanticModelAsync().ConfigureAwait(true);

if (syntaxNode != null && semanticModel != null)
{
WalkTree(outputDictionary, location, text, syntaxNode, semanticModel);
}
}

private static void WalkTree(Dictionary<ISymbol, ReferencingSymbol> outputDictionary, ReferenceLocation location, Microsoft.CodeAnalysis.Text.SourceText text, SyntaxNode syntaxNode, SemanticModel semanticModel)
{
// now walk up the syntax tree until we find a node that NodeFactory supports
var current = syntaxNode.FindToken(location.Location.SourceSpan.Start).Parent;

while (current != null)
{
if (NodeFactory.IsSupportedContainer(current, out var actualNode))
{
AddNode(outputDictionary, location, text, semanticModel, current, actualNode);
break;
}

current = current.Parent;
}
}

private static void AddNode(Dictionary<ISymbol, ReferencingSymbol> outputDictionary, ReferenceLocation location, Microsoft.CodeAnalysis.Text.SourceText text, SemanticModel semanticModel, SyntaxNode current, SyntaxNode actualNode)
{
var containerSymbol = semanticModel.GetDeclaredSymbol(actualNode);
if (containerSymbol != null)
{
if (!outputDictionary.TryGetValue(containerSymbol, out var referencingSymbol))
{
outputDictionary[containerSymbol] = referencingSymbol = new ReferencingSymbol(containerSymbol, current, semanticModel, new List<ReferencingLocation>());
}

referencingSymbol.ReferencingLocations.Add(new ReferencingLocation(location, text));
}
}

public static void ProcessFoundReferences(FoundReferences references, NodeGraph model)
{
var vfrModel = model as VFRNodeGraph;
Expand Down Expand Up @@ -112,6 +127,23 @@ public static void ProcessFoundReferences(FoundReferences references, NodeGraph

bool anyAdded = false;

HandleReferencingSymbols(references, vfrModel, viewModel, nodesToLayOut, targetNode, ref filteredReferenceCount, ref anyAdded);

if (!anyAdded)
{
targetNode.NoMoreReferences = true;
}

if (filteredReferenceCount > 0)
{
viewModel.FilteredReferencesMessage = anyAdded ? "References filtered: " + filteredReferenceCount : "All references (" + filteredReferenceCount + ") were filtered";
}

AnimateAdditions(vfrModel, viewModel, nodesToLayOut, isInitialLayout);
}

private static void HandleReferencingSymbols(FoundReferences references, VFRNodeGraph vfrModel, VFRNodeGraphViewModel viewModel, List<Node> nodesToLayOut, VFRNode targetNode, ref int filteredReferenceCount, ref bool anyAdded)
{
if (references.ReferencingSymbols != null)
{
Func<Project, bool> projectIsIncluded = viewModel.GetProjectFilter();
Expand All @@ -138,26 +170,7 @@ public static void ProcessFoundReferences(FoundReferences references, NodeGraph

anyAdded = true;

// either create a new node or add a referencing location to an existing node
if (!vfrModel.GetNodeFor(referencingSymbol.Symbol, out var referencingNode))
{
var referencingNodeReferences = new FoundReferences(referencingSymbol.Symbol, referencingSymbol.SyntaxNode, referencingSymbol.SemanticModel, references.Solution, referencingLocationsInAllowedProjects);
referencingNode = NodeFactory.Create(referencingSymbol.SyntaxNode, vfrModel, referencingNodeReferences);
if (referencingNode != null)
{
referencingNode.X = targetNode.X;
referencingNode.Y = targetNode.Y;
vfrModel.Nodes.Add(referencingNode);

nodesToLayOut.Add(referencingNode);
}
}
else
{
referencingLocationsInAllowedProjects.Each(referencingNode.NodeFoundReferences.ReferencingLocations.Add);
referencingNode.ReferenceLocationsAdded = false;
referencingNode.ReferenceLocationsAdded = referencingLocationsInAllowedProjects.Count > 0;
}
VFRNode? referencingNode = CreateReferencingNode(references, vfrModel, nodesToLayOut, targetNode, referencingSymbol, referencingLocationsInAllowedProjects);

// create the link, avoiding duplicates
if (referencingNode != null)
Expand All @@ -170,20 +183,40 @@ public static void ProcessFoundReferences(FoundReferences references, NodeGraph
}
}
}
}

if (!anyAdded)
private static VFRNode? CreateReferencingNode(FoundReferences references, VFRNodeGraph vfrModel, List<Node> nodesToLayOut, VFRNode targetNode, ReferencingSymbol referencingSymbol, List<ReferencingLocation> referencingLocationsInAllowedProjects)
{
// either create a new node or add a referencing location to an existing node
if (!vfrModel.GetNodeFor(referencingSymbol.Symbol, out var referencingNode))
{
targetNode.NoMoreReferences = true;
}
var referencingNodeReferences = new FoundReferences(referencingSymbol.Symbol, referencingSymbol.SyntaxNode, referencingSymbol.SemanticModel, references.Solution, referencingLocationsInAllowedProjects);
referencingNode = NodeFactory.Create(referencingSymbol.SyntaxNode, vfrModel, referencingNodeReferences);
if (referencingNode != null)
{
referencingNode.X = targetNode.X;
referencingNode.Y = targetNode.Y;
vfrModel.Nodes.Add(referencingNode);

if (filteredReferenceCount > 0)
nodesToLayOut.Add(referencingNode);
}
}
else
{
viewModel.FilteredReferencesMessage = anyAdded ? "References filtered: " + filteredReferenceCount : "All references (" + filteredReferenceCount + ") were filtered";
referencingLocationsInAllowedProjects.Each(referencingNode.NodeFoundReferences.ReferencingLocations.Add);
referencingNode.ReferenceLocationsAdded = false;
referencingNode.ReferenceLocationsAdded = referencingLocationsInAllowedProjects.Count > 0;
}

return referencingNode;
}

private static void AnimateAdditions(VFRNodeGraph vfrModel, VFRNodeGraphViewModel viewModel, List<Node> nodesToLayOut, bool isInitialLayout)
{
if (viewModel.View != null)
{
viewModel.View.Dispatcher.BeginInvoke(new Action(() => {
viewModel.View.Dispatcher.BeginInvoke(new Action(() =>
{
viewModel.View.UpdateLayout();

var nodesForAlgorithm = new Dictionary<Node, GraphPoint>();
Expand All @@ -205,6 +238,13 @@ public static void ProcessFoundReferences(FoundReferences references, NodeGraph
viewModel.View.StartAnimation(positions, proposedZoomAndPan.Scale, proposedZoomAndPan.StartX, proposedZoomAndPan.StartY);
}));
}
}

private static void HandleReferencingNode(FoundReferences references, VFRNodeGraph? vfrModel, List<Node> nodesToLayOut, VFRNode? targetNode, ref int filteredReferenceCount, ref bool anyAdded, Func<Project, bool> projectIsIncluded, Dictionary<Node, HashSet<Node>> connectorMap, ReferencingSymbol referencingSymbol)
{



}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void RunAction<T>(Func<Action<string>, NodeGraphViewModel, Task<T>> task,
View?.Dispatcher.Invoke(new Action(() =>
{
IsBusy = false;
MessageBox.Show("Error occurred while executing operation: " + e.Message, "Error occurred", MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show("Error occurred while executing operation: " + e.Message + Environment.NewLine + e.StackTrace, "Error occurred", MessageBoxButton.OK, MessageBoxImage.Error);
}));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ private void NodeDoubleClicked(object sender, NodeEventArgs e)
{
GetGoToLocation(location).Execute(vfrNode);
}
else if (vfrNode.SourceDocument != null)
{
var sourceLocation = vfrNode.NodeFoundReferences.SyntaxNode.GetLocation();
if (sourceLocation != null)
{
GetGoToLocation(sourceLocation, vfrNode.SourceDocument).Execute(vfrNode);
}
}
break;
}
}
Expand Down

0 comments on commit ad43085

Please sign in to comment.