提交 355d6958 编写于 作者: S shyamn

Add syntax viuslizer (changeset 1211735)

上级 d6e99da9
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis;
namespace Roslyn.SyntaxVisualizer.Control
{
public static class SyntaxKindHelper
{
// Helpers that return the language-sepcific (C# / VB) SyntaxKind of a language-agnostic
// SyntaxNode / SyntaxToken / SyntaxTrivia.
public static string GetKind(this SyntaxNodeOrToken nodeOrToken)
{
var kind = string.Empty;
if (nodeOrToken.IsNode)
{
kind = nodeOrToken.AsNode().GetKind();
}
else
{
kind = nodeOrToken.AsToken().GetKind();
}
return kind;
}
public static string GetKind(this SyntaxNode node)
{
var kind = string.Empty;
if (node.Language == LanguageNames.CSharp)
{
kind = node.CSharpKind().ToString();
}
else
{
kind = node.VisualBasicKind().ToString();
}
return kind;
}
public static string GetKind(this SyntaxToken token)
{
var kind = string.Empty;
if (token.Language == LanguageNames.CSharp)
{
kind = token.CSharpKind().ToString();
}
else
{
kind = token.VisualBasicKind().ToString();
}
return kind;
}
public static string GetKind(this SyntaxTrivia trivia)
{
var kind = string.Empty;
if (trivia.Language == LanguageNames.CSharp)
{
kind = trivia.CSharpKind().ToString();
}
else
{
kind = trivia.VisualBasicKind().ToString();
}
return kind;
}
}
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<OutDir>..\..\..\..\..\Binaries\$(Configuration)\</OutDir>
</PropertyGroup>
<ItemGroup Label="Project References">
<ProjectReference Include="..\..\..\..\Compilers\CSharp\Source\CSharpCodeAnalysis.csproj">
<Project>{B501A547-C911-4A05-AC6E-274A50DFF30E}</Project>
<Name>CSharpCodeAnalysis</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Compilers\Core\Source\CodeAnalysis.csproj">
<Project>{1EE8CAD3-55F9-4D91-96B2-084641DA9A6C}</Project>
<Name>CodeAnalysis</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Compilers\VisualBasic\Source\BasicCodeAnalysis.vbproj">
<Project>{2523D0E6-DF32-4A3E-8AE0-A19BFFAE2EF6}</Project>
<Name>BasicCodeAnalysis</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>{AFE45E23-E7EE-48C5-8143-EBE2FF67070F}</ProjectGuid>
<OutputType>library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Roslyn.SyntaxVisualizer.Control</RootNamespace>
<AssemblyName>Roslyn.SyntaxVisualizer.Control</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<DefineDebug>true</DefineDebug>
<DefineTrace>true</DefineTrace>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DefineDebug>false</DefineDebug>
<DefineTrace>true</DefineTrace>
<Optimize>true</Optimize>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<Page Include="SyntaxVisualizerControl.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="SyntaxKindHelper.cs" />
<Compile Include="SyntaxVisualizerControl.xaml.cs">
<DependentUpon>SyntaxVisualizerControl.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>
<Resource Include="SyntaxGraph.ico" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
<!-- Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<UserControl x:Class="Roslyn.SyntaxVisualizer.Control.SyntaxVisualizerControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="600" d:DesignWidth="300">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderThickness="1,1,1,0" BorderBrush="#FF828790" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" VerticalAlignment="Center" Content="Syntax Tree" FontWeight="Bold"/>
<Button Grid.Column="2" Name="legendButton" Click="LegendButton_Click" Visibility="Hidden" Margin="3" Padding="2" >
<StackPanel>
<TextBlock>Legend</TextBlock>
<Popup StaysOpen="False" Name="legendPopup">
<Border BorderThickness="1" BorderBrush="#FF828790">
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Blue" Foreground="Blue" Margin="5,5,0,0"/>
<Label Grid.Row="0" Grid.Column="1" Content="SyntaxNode" Margin="0,5,5,0"/>
<Label Grid.Row="1" Grid.Column="0" Content="Green" Foreground="DarkGreen" Margin="5,0,0,0"/>
<Label Grid.Row="1" Grid.Column="1" Content="SyntaxToken" Margin="0,0,5,0"/>
<Label Grid.Row="2" Grid.Column="0" Content="Lead:Maroon" Foreground="Maroon" Margin="5,0,0,0"/>
<Label Grid.Row="2" Grid.Column="1" Content="Leading SyntaxTrivia" Margin="0,0,5,0"/>
<Label Grid.Row="3" Grid.Column="0" Content="Trail:Maroon" Foreground="Maroon" Margin="5,0,0,0"/>
<Label Grid.Row="3" Grid.Column="1" Content="Trailing SyntaxTrivia" Margin="0,0,5,0"/>
<Label Grid.Row="4" Grid.Column="0" Content="Pink" Background="Pink" Margin="10,0,0,5" Padding="5,1" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Label Grid.Row="4" Grid.Column="1" Content="Has Diagnostics" Margin="0,0,5,5"/>
<Label Name="highlightLegendTextLabel" Visibility="Collapsed" Grid.Row="5" Grid.Column="0" Content="Yellow" BorderThickness="1" BorderBrush="Black" Background="Yellow" Margin="10,-5,0,5" Padding="5,1" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Label Name="highlightLegendDescriptionLabel" Visibility="Collapsed" Grid.Row="5" Grid.Column="1" Content="Of Interest" Margin="0,-5,5,5"/>
</Grid>
</Border>
</Popup>
</StackPanel>
</Button>
</Grid>
</Border>
<TreeView Grid.Row="1" Name="treeView" SelectedItemChanged="TreeView_SelectedItemChanged" PreviewMouseRightButtonDown="TreeView_PreviewMouseRightButtonDown" ContextMenuOpening="TreeView_ContextMenuOpening" Padding="5">
<TreeView.Resources>
<ContextMenu x:Key="treeViewItemContextMenu">
<MenuItem Name="directedSyntaxGraphMenuItem" Header="Directed Syntax Graph" Click="DirectedSyntaxGraphMenuItem_Click">
<MenuItem.Icon>
<Image Height="16" Width="16" Source="/Roslyn.SyntaxVisualizer.Control;component/SyntaxGraph.ico" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Style.Resources>
<SolidColorBrush
x:Key="{x:Static SystemColors.ControlBrushKey}"
Color="{x:Static SystemColors.HighlightColor}"/>
</Style.Resources>
<Setter Property="ContextMenu" Value="{StaticResource ResourceKey=treeViewItemContextMenu}"/>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
<GridSplitter Grid.Row="2" ResizeDirection="Rows" HorizontalAlignment="Stretch" Height="5"/>
<Grid Grid.Row="3">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderThickness="1,1,1,0" BorderBrush="#FF828790" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.ColumnSpan="2" Content="Properties" FontWeight="Bold"/>
<Label Grid.Row="1" Grid.Column="0" Name="typeTextLabel" Visibility="Hidden" Content="Type" Padding="5,2,5,2" Margin="20,0,0,0"/>
<Label Grid.Row="1" Grid.Column="1" Name="typeValueLabel" Padding="5,2,5,2"/>
<Label Grid.Row="2" Grid.Column="0" Name="kindTextLabel" Visibility="Hidden" Content="Kind" Padding="5,2,5,2" Margin="20,0,0,0"/>
<Label Grid.Row="2" Grid.Column="1" Name="kindValueLabel" Padding="5,2,5,2"/>
</Grid>
</Border>
<WindowsFormsHost Grid.Row="1" Name="windowsFormsHost" Padding="5" />
</Grid>
</Grid>
</UserControl>
\ No newline at end of file
' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports Microsoft.CodeAnalysis
Friend Module ObjectInfoHelper
'Helpers that use Reflection to return details pertaining to an object including
'its type + the types, names and values of properties on this object.
#Region "GetObjectInfo"
Friend Function GetObjectInfo(nodeOrToken As SyntaxNodeOrToken) As ObjectInfo
Dim info As ObjectInfo = Nothing
If nodeOrToken.IsNode Then
info = GetObjectInfo(nodeOrToken.AsNode)
Else
info = GetObjectInfo(nodeOrToken.AsToken)
End If
Return info
End Function
Friend Function GetObjectInfo(node As SyntaxNode) As ObjectInfo
Dim type = node.GetType()
Dim properties = type.GetProperties(System.Reflection.BindingFlags.Instance Or
System.Reflection.BindingFlags.Public)
Dim propertyInfos = (From p In properties Where IsSimpleProperty(p)
Select GetPropertyInfo(p, node)).ToList()
Return New ObjectInfo(type.Name, propertyInfos)
End Function
Friend Function GetObjectInfo(token As SyntaxToken) As ObjectInfo
Dim type = token.GetType()
Dim properties = type.GetProperties(System.Reflection.BindingFlags.Instance Or
System.Reflection.BindingFlags.Public)
Dim propertyInfos = (From p In properties Where IsSimpleProperty(p)
Select GetPropertyInfo(p, token)).ToList()
Return New ObjectInfo(type.Name, propertyInfos)
End Function
Friend Function GetObjectInfo(trivia As SyntaxTrivia) As ObjectInfo
Dim type = trivia.GetType()
Dim properties = type.GetProperties(System.Reflection.BindingFlags.Instance Or
System.Reflection.BindingFlags.Public)
Dim propertyInfos = (From p In properties Where IsSimpleProperty(p)
Select GetPropertyInfo(p, trivia)).ToList()
Return New ObjectInfo(type.Name, propertyInfos)
End Function
#End Region
#Region "GetPropertyInfo"
Private Function IsSimpleProperty(prop As System.Reflection.PropertyInfo) As Boolean
Dim type = prop.PropertyType
If type Is GetType(Char) OrElse
type Is GetType(Boolean) OrElse
type Is GetType(Short) OrElse
type Is GetType(UShort) OrElse
type Is GetType(Integer) OrElse
type Is GetType(UInteger) OrElse
type Is GetType(Long) OrElse
type Is GetType(ULong) OrElse
type Is GetType(Single) OrElse
type Is GetType(Double) OrElse
type Is GetType(Date) OrElse
type Is GetType(Decimal) OrElse
type Is GetType(String) OrElse
type.IsEnum Then
Return True
Else
Return False
End If
End Function
'Only called if IsSimpleProperty returns true.
Private Function GetPropertyInfo(prop As System.Reflection.PropertyInfo,
node As SyntaxNode) As ObjectInfo.PropertyInfo
Return New ObjectInfo.PropertyInfo(prop.Name, prop.PropertyType,
prop.GetValue(node, Nothing))
End Function
'Only called if IsSimpleProperty returns true.
Private Function GetPropertyInfo(prop As System.Reflection.PropertyInfo,
token As SyntaxToken) As ObjectInfo.PropertyInfo
Return New ObjectInfo.PropertyInfo(prop.Name, prop.PropertyType,
prop.GetValue(token, Nothing))
End Function
'Only called if IsSimpleProperty returns true.
Private Function GetPropertyInfo(prop As System.Reflection.PropertyInfo,
trivia As SyntaxTrivia) As ObjectInfo.PropertyInfo
Return New ObjectInfo.PropertyInfo(prop.Name, prop.PropertyType,
prop.GetValue(trivia, Nothing))
End Function
#End Region
End Module
'Encapsulates details pertaining to an object including its type + the types, names
'and values of properties on this object.
Friend Class ObjectInfo
Private ReadOnly _typeName As String
Private ReadOnly _propertyInfos As IEnumerable(Of PropertyInfo)
Private Shared ReadOnly _emptyPropertyInfos As IEnumerable(Of PropertyInfo) = {}
Friend ReadOnly Property TypeName As String
Get
Return _typeName
End Get
End Property
Friend ReadOnly Property PropertyInfos As IEnumerable(Of PropertyInfo)
Get
If _propertyInfos Is Nothing Then
Return _emptyPropertyInfos
Else
Return _propertyInfos
End If
End Get
End Property
Friend Sub New(typeName As String, propertyInfos As IEnumerable(Of PropertyInfo))
_typeName = typeName
_propertyInfos = propertyInfos
End Sub
'Encapsulates the name, type and value of a property on an object.
Friend Class PropertyInfo
Private ReadOnly _name As String
Private ReadOnly _type As Type
Private ReadOnly _value As Object
Friend ReadOnly Property Name As String
Get
Return _name
End Get
End Property
Friend ReadOnly Property Type As Type
Get
Return _type
End Get
End Property
Friend ReadOnly Property Value As Object
Get
Return _value
End Get
End Property
Friend Sub New(name As String, type As Type, value As Object)
_name = name
_type = type
_value = value
End Sub
End Class
End Class
\ No newline at end of file
' Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Runtime.CompilerServices
Imports Microsoft.CodeAnalysis
Friend Module SyntaxKindHelper
'Helpers that return the language-sepcific (C# / VB) SyntaxKind of a language-agnostic
'SyntaxNode / SyntaxToken / SyntaxTrivia.
<Extension()>
Friend Function GetKind(nodeOrToken As SyntaxNodeOrToken) As String
Dim kind = String.Empty
If nodeOrToken.IsNode Then
kind = nodeOrToken.AsNode().GetKind()
Else
kind = nodeOrToken.AsToken().GetKind()
End If
Return kind
End Function
<Extension()>
Friend Function GetKind(node As SyntaxNode) As String
Dim kind = String.Empty
If node.Language = LanguageNames.CSharp Then
kind = node.CSharpKind().ToString()
Else
kind = node.VisualBasicKind().ToString()
End If
Return kind
End Function
<Extension()>
Friend Function GetKind(token As SyntaxToken) As String
Dim kind = String.Empty
If token.Language = LanguageNames.CSharp Then
kind = token.CSharpKind().ToString()
Else
kind = token.VisualBasicKind().ToString()
End If
Return kind
End Function
<Extension()>
Friend Function GetKind(trivia As SyntaxTrivia) As String
Dim kind = String.Empty
If trivia.Language = LanguageNames.CSharp Then
kind = trivia.CSharpKind().ToString()
Else
kind = trivia.VisualBasicKind().ToString()
End If
Return kind
End Function
End Module
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<OutDir>..\..\..\..\..\Binaries\$(Configuration)\</OutDir>
</PropertyGroup>
<ItemGroup Label="Project References">
<ProjectReference Include="..\..\..\..\Compilers\CSharp\Source\CSharpCodeAnalysis.csproj">
<Project>{B501A547-C911-4A05-AC6E-274A50DFF30E}</Project>
<Name>CSharpCodeAnalysis</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Compilers\Core\Source\CodeAnalysis.csproj">
<Project>{1EE8CAD3-55F9-4D91-96B2-084641DA9A6C}</Project>
<Name>CodeAnalysis</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\..\Compilers\VisualBasic\Source\BasicCodeAnalysis.vbproj">
<Project>{2523D0E6-DF32-4A3E-8AE0-A19BFFAE2EF6}</Project>
<Name>BasicCodeAnalysis</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DA4F74AF-2694-4AC9-A8CC-18382DE8215E}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>Roslyn.SyntaxVisualizer.DgmlHelper</RootNamespace>
<AssemblyName>Roslyn.SyntaxVisualizer.DgmlHelper</AssemblyName>
<MyType>Windows</MyType>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<DefineDebug>true</DefineDebug>
<DefineTrace>true</DefineTrace>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<DefineDebug>false</DefineDebug>
<DefineTrace>true</DefineTrace>
<Optimize>true</Optimize>
</PropertyGroup>
<PropertyGroup>
<OptionExplicit>On</OptionExplicit>
</PropertyGroup>
<PropertyGroup>
<OptionCompare>Binary</OptionCompare>
</PropertyGroup>
<PropertyGroup>
<OptionStrict>Off</OptionStrict>
</PropertyGroup>
<PropertyGroup>
<OptionInfer>On</OptionInfer>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="ObjectInfoHelper.vb" />
<Compile Include="SyntaxDgmlHelper.vb" />
<Compile Include="SyntaxKindHelper.vb" />
</ItemGroup>
<ItemGroup>
<Folder Include="My Project\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
</Project>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册