Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
Add NRefactory.Demo (TreeView for visualization of C# DOM)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Nov 19, 2010
1 parent 2f91b65 commit 716a5f5
Show file tree
Hide file tree
Showing 16 changed files with 710 additions and 18 deletions.
61 changes: 61 additions & 0 deletions ICSharpCode.NRefactory.Demo/ICSharpCode.NRefactory.Demo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{9C19E629-C93E-4ACB-9A4B-13072B5AEF9D}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<OutputType>WinExe</OutputType>
<RootNamespace>ICSharpCode.NRefactory.Demo</RootNamespace>
<AssemblyName>ICSharpCode.NRefactory.Demo</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<AppDesignerFolder>Properties</AppDesignerFolder>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'x86' ">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>bin\Debug\</OutputPath>
<DebugSymbols>True</DebugSymbols>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>bin\Release\</OutputPath>
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainForm.cs" />
<Compile Include="MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">
<Project>{3B2A5653-EC97-4001-BB9B-D90F1AF2C371}</Project>
<Name>ICSharpCode.NRefactory</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="MainForm.resx">
<DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
</Project>
177 changes: 177 additions & 0 deletions ICSharpCode.NRefactory.Demo/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 106 additions & 0 deletions ICSharpCode.NRefactory.Demo/MainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under MIT X11 license (for details please see \doc\license.txt)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.NRefactory.CSharp;

namespace ICSharpCode.NRefactory.Demo
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

CSharpParseButtonClick(null, null);
}

void CSharpParseButtonClick(object sender, EventArgs e)
{
CSharpParser parser = new CSharpParser();
CompilationUnit cu = parser.Parse(new StringReader(csharpCodeTextBox.Text));
csharpTreeView.Nodes.Clear();
foreach (var element in cu.Children) {
csharpTreeView.Nodes.Add(MakeTreeNode(element));
}
}

TreeNode MakeTreeNode(INode node)
{
StringBuilder b = new StringBuilder();
b.Append(DecodeRole(node.Role, node.Parent != null ? node.Parent.GetType() : null));
b.Append(": ");
b.Append(node.GetType().Name);
bool hasProperties = false;
foreach (PropertyInfo p in node.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) {
if (p.PropertyType == typeof(string) || p.PropertyType.IsEnum) {
if (!hasProperties) {
hasProperties = true;
b.Append(" (");
} else {
b.Append(", ");
}
b.Append(p.Name);
b.Append(" = ");
try {
object val = p.GetValue(node, null);
b.Append(val != null ? val.ToString() : "**null**");
} catch (TargetInvocationException ex) {
b.Append("**" + ex.InnerException.GetType().Name + "**");
}
}
}
if (hasProperties)
b.Append(")");
TreeNode t = new TreeNode(b.ToString());
t.Tag = node;
foreach (INode child in node.Children) {
t.Nodes.Add(MakeTreeNode(child));
}
return t;
}

string DecodeRole(int role, Type type)
{
if (type != null) {
foreach (FieldInfo field in type.GetFields(BindingFlags.Public | BindingFlags.Static)) {
if (field.FieldType == typeof(int) && (int)field.GetValue(null) == role)
return field.Name;
}
}
foreach (FieldInfo field in typeof(AbstractNode.Roles).GetFields(BindingFlags.Public | BindingFlags.Static)) {
if (field.FieldType == typeof(int) && (int)field.GetValue(null) == role)
return field.Name;
}

return role.ToString();
}

void CSharpGenerateCodeButtonClick(object sender, EventArgs e)
{
throw new NotImplementedException();
}

void CSharpTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
INode node = e.Node.Tag as INode;
if (node != null) {
int startOffset = csharpCodeTextBox.GetFirstCharIndexFromLine(node.StartLocation.Line - 1) + node.StartLocation.Column - 1;
int endOffset = csharpCodeTextBox.GetFirstCharIndexFromLine(node.EndLocation.Line - 1) + node.EndLocation.Column - 1;
csharpCodeTextBox.Select(startOffset, endOffset - startOffset);
}
}
}
}
Loading

0 comments on commit 716a5f5

Please sign in to comment.