Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
[Nitra.Visualizer] Improve visualization of dependent properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
VladD2 committed May 14, 2015
1 parent 720f4cf commit 106b441
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 29 deletions.
19 changes: 8 additions & 11 deletions Grammars/CSharp/CSharp.Grammar/Utils.n
Expand Up @@ -13,30 +13,27 @@ namespace CSharp
{
module Utils
{
public EnterNamespace(mutable scope : Scope.Table, path : Reference.IAstList, namespaceDeclaration : Namespace) : Scope.Table
public EnterNamespace(mutable ns : NamespaceSymbol, path : Reference.IAstList, namespaceDeclaration : Namespace) : NamespaceSymbol
{
def lastIndex = path.Count - 1;
foreach (name in path with i)
{
mutable symbol;
unless (scope.NameTable.TryGetValue(name, out symbol))
{
symbol = NamespaceSymbol(name);
scope.Define(symbol);
}
unless (ns.Scope.NameTable.TryGetValue(name, out symbol))
symbol = NamespaceSymbol(name, ns);
when (i == lastIndex)
symbol.Declarations ::= namespaceDeclaration;

scope = (symbol :> NamespaceSymbol).Scope;
ns = (symbol :> NamespaceSymbol);
}

scope
ns
}

public ExitNamespace(scopeIn : Scope.Table, scopeOut : Scope) : Scope.Table
public ExitNamespace(nsIn : NamespaceSymbol, nsOut : NamespaceSymbol) : NamespaceSymbol
{
_ = scopeOut;
scopeIn
_ = nsOut;
nsIn
}
}
}
9 changes: 8 additions & 1 deletion Nitra.Visualizer/MainWindow.xaml
Expand Up @@ -181,7 +181,14 @@
</Grid.ColumnDefinitions>
<TreeView Name="_declarationsTreeView" FontSize="16" Grid.ColumnSpan="2" Margin="5,0,0,0" SelectedItemChanged="_declarationsTreeView_SelectedItemChanged" />
<GridSplitter Grid.Column="1" Width="5" HorizontalAlignment="Stretch" />
<WindowsFormsHost Name="_windowsFormsHost" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox Name="_objectType" IsReadOnly="True" Grid.Row="0" Background="LightGray" />
<WindowsFormsHost Name="_windowsFormsHost" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</Grid>
</TabItem>

Expand Down
57 changes: 49 additions & 8 deletions Nitra.Visualizer/MainWindow.xaml.Declarations.cs
Expand Up @@ -23,6 +23,9 @@ public partial class MainWindow
{
public TreeViewItem ObjectToItem(PropertyInfo prop, object obj)
{
if (obj is Symbol2)
{
}
string name = prop == null ? "" : prop.Name;
var tvi = new TreeViewItem { Tag = obj, FontWeight = FontWeights.Normal };
tvi.MouseDoubleClick += TviOnMouseDoubleClick;
Expand Down Expand Up @@ -81,6 +84,12 @@ public TreeViewItem ObjectToItem(PropertyInfo prop, object obj)
{
var xaml = RenderXamlForValue(prop, obj);
tvi.Header = XamlReader.Parse(xaml);

var t = obj.GetType();
var props = t.GetProperties();
if (!(obj is string || t.IsPrimitive) && props.Any(p => !IsIgnoredProperty(p)))
tvi.Items.Add(obj);

return tvi;
}
}
Expand Down Expand Up @@ -134,6 +143,30 @@ private void TviOnExpanded(object sender, RoutedEventArgs routedEventArgs)
tvi.Items.Add(ObjectToItem(null, item));
return;
}

{
var t = obj.GetType();

if (obj is string || t.IsPrimitive)
return;

var props = t.GetProperties();

foreach (var prop in props) //.OrderBy(p => p.Name))
{
if (IsIgnoredProperty(prop))
continue;
try
{
var value = prop.GetValue(obj, null);
tvi.Items.Add(ObjectToItem(prop, value));
}
catch (Exception e)
{
tvi.Items.Add(ObjectToItem(prop, e.Message));
}
}
}
}

private static bool IsIgnoredProperty(PropertyInfo prop)
Expand All @@ -157,7 +190,7 @@ private void UpdateDeclarations(AstRoot<IAst> astRoot)
dynamic ast = astRoot.Content;
try
{
ast.DefScopeIn = new Scope.Table();
ast.NamespaceIn = NamespaceSymbol.RootNamespace;
}
catch { }
astRoot.EvalProperties(new DebugCompilerMessages()); // TODO: display messages in GUI
Expand All @@ -171,13 +204,18 @@ private void UpdateDeclarations(AstRoot<IAst> astRoot)
//}
}

public static string Escape(string str)
{
return str.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;");
}

private static string RenderXamlForDeclaration(string name, IAst ast)
{
var declatation = ast as IDeclaration;
var suffix = declatation == null ? null : (": " + declatation.Name);
var suffix = declatation == null ? null : (": " + Escape(declatation.Name.Text));
return @"
<Span xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
" + (string.IsNullOrWhiteSpace(name) ? null : ("<Span Foreground = 'blue'>" + name + "</Span>: "))
" + (string.IsNullOrWhiteSpace(name) ? null : ("<Span Foreground = 'blue'>" + Escape(name) + "</Span>: "))
+ ast.ToXaml() + suffix + @"
</Span>";
}
Expand All @@ -188,16 +226,16 @@ private static string RenderXamlForValue(PropertyInfo prop, object obj)
var color = isDependent ? "green" : "SlateBlue";
return @"
<Span xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
" + (prop == null ? null : ("<Bold><Span Foreground = '" + color + "'>" + prop.Name + "</Span></Bold>: "))
+ obj + @"
" + (prop == null ? null : ("<Bold><Span Foreground = '" + color + "'>" + Escape(prop.Name) + "</Span></Bold>: "))
+ Escape(obj.ToString()) + @"
</Span>";
}

private static string RenderXamlForlist(string name, IAstList<IAst> items)
{
return @"
<Span xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<Span Foreground = 'blue'>" + name + @"</Span>* <Span Foreground = 'gray'>Count: </Span> " + items.Count + @"
<Span Foreground = 'blue'>" + Escape(name) + @"</Span>* <Span Foreground = 'gray'>Count: </Span> " + items.Count + @"
</Span>";
}

Expand All @@ -206,14 +244,17 @@ private static string RenderXamlForSeq(string name, int count)
{
return @"
<Span xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<Span Foreground = 'green'>" + name + @"</Span> <Span Foreground = 'gray'>(List) Count: </Span> " + count + @"
<Span Foreground = 'green'>" + Escape(name) + @"</Span> <Span Foreground = 'gray'>(List) Count: </Span> " + count + @"
</Span>";
}

private void _declarationsTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.NewValue != null)
_propertyGrid.SelectedObject = ((TreeViewItem)e.NewValue).Tag;
{
_propertyGrid.SelectedObject = ((TreeViewItem) e.NewValue).Tag;
_objectType.Text = _propertyGrid.SelectedObject.GetType().FullName;
}
}

private void TviOnMouseDoubleClick(object sender, MouseButtonEventArgs e)
Expand Down
58 changes: 49 additions & 9 deletions Nitra/Nitra.Runtime/Binding/Scope.n
Expand Up @@ -16,21 +16,61 @@ namespace Nitra.Runtime.Binding
[Record]
public class Symbol2
{
public Name : IReference;
[RecordIgnore] public mutable Declarations : list [IDeclaration] = []
public Name : IReference { get; }
[RecordIgnore] public Declarations : list [IDeclaration] { get; set; default []; }

public override ToString() : string { Name?.Text + " (symbol)" }
}

[Record]
public class NamespaceSymbol : Symbol2
{
[RecordIgnore] public Scope : Scope.Table = Scope.Table();
public static RootNamespace : NamespaceSymbol = NamespaceSymbol(null, null);

public Parent : NamespaceSymbol { get; }
[RecordIgnore] public Scope : Scope.Table { get; set; default Scope.Table(); }

public this(name : IReference, parent : NamespaceSymbol)
{
base(name);

assert2(name == null && parent == null || name != null && parent != null);

when (parent != null)
{
Parent = parent;
parent.Scope.Define(this);
}
}

public FullName : string
{
get
{
def loop(ns : NamespaceSymbol, sb : Text.StringBuilder) : void
{
when (ns.Parent != null)
{
loop(ns.Parent, sb);
when (ns.Parent.Parent != null)
_ = sb.Append('.');
_ = sb.Append(ns.Name);
}
}

def sb = Text.StringBuilder();
loop(this, sb);
sb.ToString()
}
}

public override ToString() : string { (if (Name == null) "<root>" else FullName) + " (namespace)" }
}

public variant Scope
{
| Table
{
NameTable : Hashtable[IReference, Symbol2];
public NameTable : Hashtable[IReference, Symbol2] { get; }

public this() { NameTable = Hashtable(); }

Expand All @@ -50,12 +90,12 @@ namespace Nitra.Runtime.Binding

public override ToString() : string
{
$"..$(NameTable.Keys)"
$"Scope.Table: { ..$(NameTable.Keys) }"
}
}
| Union { Scopes : list[Scope] }
| Hide { Scope : Scope; Hidden : Scope; }
| Filter { Scope : Scope; Predicate : Symbol -> bool; }
| Union { public Scopes : list[Scope] { get; } }
| Hide { public Scope : Scope { get; } public Hidden : Scope { get; } }
| Filter { public Scope : Scope { get; } public Predicate : Symbol -> bool { get; } }
| Nil
}
}

0 comments on commit 106b441

Please sign in to comment.