Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show embedded language classification in syntax visualizer #1012

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public enum SyntaxCategory
SyntaxToken,
SyntaxTrivia,
Operation,
EmbeddedClassification,
}

// A control for visually displaying the contents of a SyntaxTree.
Expand Down Expand Up @@ -113,6 +114,9 @@ private class SyntaxTag
public event SyntaxTokenDelegate? SyntaxTokenDirectedGraphRequested;
public event SyntaxTokenDelegate? SyntaxTokenNavigationToSourceRequested;

public delegate void SyntaxClassifiedSpanDelegate(ClassifiedSpan span);
public event SyntaxClassifiedSpanDelegate? SyntaxClassifiedSpanNavigationToSourceRequested;

public delegate void SyntaxTriviaDelegate(SyntaxTrivia trivia);
public event SyntaxTriviaDelegate? SyntaxTriviaDirectedGraphRequested;
public event SyntaxTriviaDelegate? SyntaxTriviaNavigationToSourceRequested;
Expand Down Expand Up @@ -205,6 +209,10 @@ public SyntaxVisualizerControl()
ClassifiedSpan = classifiedSpans.FirstOrDefault(s => s.TextSpan.Contains(trivia.Span));
break;

case ClassifiedSpan span:
ClassifiedSpan = span;
break;

default:
ClassifiedSpan = null;
break;
Expand Down Expand Up @@ -518,6 +526,9 @@ private void ExpandPathTo(TreeViewItem? item)

foreach (TreeViewItem item in current.Items)
{
if (item == null)
continue;

if (category != SyntaxCategory.Operation && ((SyntaxTag)item.Tag).Category == SyntaxCategory.Operation)
{
// Do not prefer navigating to IOperation nodes when clicking in source code
Expand Down Expand Up @@ -780,6 +791,40 @@ private void AddNode(TreeViewItem? parentItem, SyntaxNode? node)
}
}

private void AddEmbeddedClassification(TreeViewItem parentItem, ClassifiedSpan classifiedSpan)
{
var tag = new SyntaxTag
{
Category = SyntaxCategory.EmbeddedClassification,
FullSpan = classifiedSpan.TextSpan,
Span = classifiedSpan.TextSpan,
ParentItem = parentItem
};

var item = CreateTreeViewItem(tag, $"{classifiedSpan.ClassificationType} {classifiedSpan.TextSpan}", false);

item.Selected += new RoutedEventHandler((sender, e) =>
{
_isNavigatingFromTreeToSource = true;

typeTextLabel.Visibility = Visibility.Visible;
kindTextLabel.Visibility = Visibility.Hidden;
typeValueLabel.Content = classifiedSpan.GetType().Name;
kindValueLabel.Content = null;
_propertyGrid.SelectedObject = classifiedSpan;

if (!_isNavigatingFromSourceToTree && SyntaxClassifiedSpanNavigationToSourceRequested != null)
{
SyntaxClassifiedSpanNavigationToSourceRequested(classifiedSpan);
}

_isNavigatingFromTreeToSource = false;
e.Handled = true;
});

parentItem.Items.Add(item);
}

private void AddToken(TreeViewItem parentItem, SyntaxToken token)
{
var kind = token.GetKind();
Expand Down Expand Up @@ -877,6 +922,23 @@ private void AddToken(TreeViewItem parentItem, SyntaxToken token)
}
}
}

if (token.GetKind().Contains("String") && item.Items.Count == 0 && classifiedSpans != null) // no child nodes and tokens
{
var embeddedClassifications = classifiedSpans.Where(cs => token.Span.Contains(cs.TextSpan));

if (embeddedClassifications.Count() > 1)
{
foreach (var classifiedSpan in embeddedClassifications.OrderBy(cs => cs.TextSpan.Start))
{
// skip the full span itself
if (classifiedSpan.TextSpan == token.Span)
continue;

AddEmbeddedClassification(item, classifiedSpan);
}
}
}
}

private void AddTrivia(TreeViewItem parentItem, SyntaxTrivia trivia, bool isLeadingTrivia)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ internal SyntaxVisualizerContainer(SyntaxVisualizerToolWindow parent)

syntaxVisualizer.SyntaxNodeNavigationToSourceRequested += node => NavigateToSource(node?.Span);
syntaxVisualizer.SyntaxTokenNavigationToSourceRequested += token => NavigateToSource(token.Span);
syntaxVisualizer.SyntaxClassifiedSpanNavigationToSourceRequested += span => NavigateToSource(span.TextSpan);
syntaxVisualizer.SyntaxTriviaNavigationToSourceRequested += trivia => NavigateToSource(trivia.Span);
}

Expand Down