Skip to content

Commit b664cab

Browse files
committed
created treeview and detailview
1 parent cf215dd commit b664cab

20 files changed

+1498
-95
lines changed
Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
1-
using Microsoft.CodeAnalysis;
2-
using Microsoft.CodeAnalysis.CSharp;
3-
using Microsoft.CodeAnalysis.CSharp.Syntax;
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
using System.Runtime.Versioning;
4+
5+
using Microsoft.CodeAnalysis;
6+
using RoslynSyntaxTool.Process;
7+
8+
[assembly: TargetFramework("net6.0", FrameworkDisplayName = "net6.0")]
9+
[assembly: AssemblyTitle("RoslynSyntaxTool.Proxy.ConvertToCSharpProxy")]
10+
[assembly: AssemblyProduct("RoslynSyntaxTool.Proxy.ConvertToCSharpProxy")]
11+
[assembly: AssemblyCopyright("Copyright © MatoApp")]
12+
[assembly: ComVisible(false)]
13+
[assembly: Guid("9b38af72-b313-4b5b-b893-bd102c131dc6")]
14+
[assembly: AssemblyVersion("0.0.0.1")]
15+
[assembly: AssemblyFileVersion("0.0.0.1")]
16+
417

518
static SyntaxTree GenerateProxyTree()
619
{
7-
return SyntaxFactory.CompilationUnit()
8-
.WithMembers(
9-
SyntaxFactory.SingletonList<MemberDeclarationSyntax>(
10-
SyntaxFactory.GlobalStatement(
11-
SyntaxFactory.LocalFunctionStatement(
12-
SyntaxFactory.IdentifierName("opt"),
13-
SyntaxFactory.MissingToken(SyntaxKind.IdentifierToken))
14-
.WithModifiers(
15-
SyntaxFactory.TokenList(
16-
new[]{
17-
SyntaxFactory.Token(SyntaxKind.PublicKeyword),
18-
SyntaxFactory.Token(SyntaxKind.StaticKeyword)}))
19-
.WithBody(
20-
SyntaxFactory.Block(
21-
SyntaxFactory.SingletonList<StatementSyntax>(
22-
SyntaxFactory.ReturnStatement(
23-
SyntaxFactory.LiteralExpression(
24-
SyntaxKind.StringLiteralExpression,
25-
SyntaxFactory.Literal("a")))))))))
26-
.NormalizeWhitespace().SyntaxTree;
20+
return new ProxyTreeGen().Process();
2721
}
2822

2923
Console.WriteLine(GenerateProxyTree().ToString());
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
5+
namespace RoslynSyntaxTool.Process
6+
{
7+
public class ProxyTreeGen
8+
{
9+
public SyntaxTree Process()
10+
{
11+
return SyntaxFactory.CompilationUnit()
12+
.WithMembers(
13+
SyntaxFactory.SingletonList<MemberDeclarationSyntax>(
14+
SyntaxFactory.GlobalStatement(
15+
SyntaxFactory.LocalFunctionStatement(
16+
SyntaxFactory.IdentifierName("opt"),
17+
SyntaxFactory.MissingToken(SyntaxKind.IdentifierToken))
18+
.WithModifiers(
19+
SyntaxFactory.TokenList(
20+
new[]{
21+
SyntaxFactory.Token(SyntaxKind.PublicKeyword),
22+
SyntaxFactory.Token(SyntaxKind.StaticKeyword)}))
23+
.WithBody(
24+
SyntaxFactory.Block(
25+
SyntaxFactory.SingletonList<StatementSyntax>(
26+
SyntaxFactory.ReturnStatement(
27+
SyntaxFactory.LiteralExpression(
28+
SyntaxKind.StringLiteralExpression,
29+
SyntaxFactory.Literal("a")))))))))
30+
.NormalizeWhitespace().SyntaxTree;
31+
}
32+
}
33+
}
34+

RoslynSyntaxTool/App.xaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public App()
3434
.AddSingleton<MainViewModel>()
3535
.AddSingleton<IndexPageViewModel>()
3636
.AddSingleton<SettingPageViewModel>()
37+
.AddSingleton<ConvertToCSharpPageViewModel>()
38+
.AddSingleton<SyntaxTreeViewerViewModel>()
3739
.BuildServiceProvider());
3840
}
3941

RoslynSyntaxTool/Control/ProgressWindow.xaml.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,21 @@ public static void StaticUnShowDialog()
5050
public ProgressWindow()
5151
{
5252
InitializeComponent();
53-
WeakReferenceMessenger.Default.Register<string>( MessengerToken.UPDATEPROGRESS, HandleMessage);
54-
WeakReferenceMessenger.Default.Register<string>( MessengerToken.CLOSEPROGRESS, HandleClose);
53+
Register(MessengerToken.UPDATEPROGRESS, HandleMessage);
54+
Register(MessengerToken.CLOSEPROGRESS, HandleClose);
5555
this.Unloaded += (sender, e) => WeakReferenceMessenger.Default.UnregisterAll(MessengerToken.UPDATEPROGRESS);
5656
this.Unloaded += (sender, e) => WeakReferenceMessenger.Default.UnregisterAll(MessengerToken.CLOSEPROGRESS);
5757

5858
}
5959

60+
private void Register(object recipient, MessageHandler<object, string> handler)
61+
{
62+
if (!WeakReferenceMessenger.Default.IsRegistered<string>(recipient))
63+
{
64+
WeakReferenceMessenger.Default.Register<string>(recipient, handler);
65+
66+
}
67+
}
6068

6169
public void Show(string title)
6270
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<UserControl x:Class="Workshop.Control.SyntaxDetailViewer"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="clr-namespace:Workshop.Control"
7+
mc:Ignorable="d"
8+
d:DesignHeight="450" d:DesignWidth="800"
9+
DataContext="{Binding Source={StaticResource Locator},Path=SyntaxDetailViewer}">
10+
11+
<Grid>
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="Auto"></RowDefinition>
14+
<RowDefinition Height="*"></RowDefinition>
15+
</Grid.RowDefinitions>
16+
<StackPanel>
17+
<TextBlock ></TextBlock>
18+
<TextBlock ></TextBlock>
19+
</StackPanel>
20+
<TreeView Grid.Row="1" ItemsSource="{Binding RootItems}">
21+
<TreeView.ItemTemplate>
22+
<HierarchicalDataTemplate
23+
ItemsSource="{Binding Items}">
24+
<TextBlock Text="{Binding Header}" ></TextBlock>
25+
</HierarchicalDataTemplate>
26+
</TreeView.ItemTemplate>
27+
</TreeView>
28+
</Grid>
29+
</UserControl>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.Classification;
3+
using Microsoft.CodeAnalysis.CSharp;
4+
using Microsoft.CodeAnalysis.Text;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Collections.Immutable;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
using System.Windows;
13+
using System.Windows.Controls;
14+
using System.Windows.Data;
15+
using System.Windows.Documents;
16+
using System.Windows.Input;
17+
using System.Windows.Media;
18+
using System.Windows.Media.Imaging;
19+
using System.Windows.Navigation;
20+
using System.Windows.Shapes;
21+
using Microsoft.CodeAnalysis.CSharp;
22+
using CommunityToolkit.Mvvm.Messaging;
23+
using Workshop.Common;
24+
using CommunityToolkit.Mvvm.DependencyInjection;
25+
using Workshop.ViewModel;
26+
using Workshop.Model;
27+
28+
namespace Workshop.Control
29+
{
30+
/// <summary>
31+
/// SyntaxTreeViewer.xaml 的交互逻辑
32+
/// </summary>
33+
public partial class SyntaxTreeViewer : UserControl
34+
{
35+
public SyntaxTreeViewer()
36+
{
37+
InitializeComponent();
38+
39+
40+
}
41+
42+
43+
44+
}
45+
46+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<UserControl x:Class="Workshop.Control.SyntaxTreeViewer"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="clr-namespace:Workshop.Control"
7+
mc:Ignorable="d"
8+
d:DesignHeight="450" d:DesignWidth="800"
9+
DataContext="{Binding Source={StaticResource Locator},Path=SyntaxTreeViewer}">
10+
11+
<Grid>
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="Auto"></RowDefinition>
14+
<RowDefinition Height="*"></RowDefinition>
15+
</Grid.RowDefinitions>
16+
<StackPanel>
17+
<TextBlock ></TextBlock>
18+
<TextBlock ></TextBlock>
19+
</StackPanel>
20+
<TreeView Grid.Row="1" ItemsSource="{Binding RootItems}">
21+
<TreeView.ItemTemplate>
22+
<HierarchicalDataTemplate
23+
ItemsSource="{Binding Items}">
24+
<TextBlock Text="{Binding Header}" ></TextBlock>
25+
</HierarchicalDataTemplate>
26+
</TreeView.ItemTemplate>
27+
</TreeView>
28+
</Grid>
29+
</UserControl>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.Classification;
3+
using Microsoft.CodeAnalysis.CSharp;
4+
using Microsoft.CodeAnalysis.Text;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Collections.Immutable;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
using System.Windows;
13+
using System.Windows.Controls;
14+
using System.Windows.Data;
15+
using System.Windows.Documents;
16+
using System.Windows.Input;
17+
using System.Windows.Media;
18+
using System.Windows.Media.Imaging;
19+
using System.Windows.Navigation;
20+
using System.Windows.Shapes;
21+
using Microsoft.CodeAnalysis.CSharp;
22+
using CommunityToolkit.Mvvm.Messaging;
23+
using Workshop.Common;
24+
using CommunityToolkit.Mvvm.DependencyInjection;
25+
using Workshop.ViewModel;
26+
using Workshop.Model;
27+
28+
namespace Workshop.Control
29+
{
30+
/// <summary>
31+
/// SyntaxTreeViewer.xaml 的交互逻辑
32+
/// </summary>
33+
public partial class SyntaxTreeViewer : UserControl
34+
{
35+
public SyntaxTreeViewer()
36+
{
37+
InitializeComponent();
38+
39+
40+
}
41+
42+
43+
44+
}
45+
46+
}

RoslynSyntaxTool/MainWindow.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@
112112
<!-- Items -->
113113
<mah:HamburgerMenu.ItemsSource>
114114
<mah:HamburgerMenuItemCollection>
115-
<mah:HamburgerMenuIconItem Label="转换" Tag="./View/IndexPage.xaml">
115+
<mah:HamburgerMenuIconItem Label="C# 转 Syntax" Tag="./View/IndexPage.xaml">
116+
</mah:HamburgerMenuIconItem>
117+
<mah:HamburgerMenuIconItem Label="Syntax 转 C#" Tag="./View/ConvertToCSharpPage.xaml">
116118
</mah:HamburgerMenuIconItem>
117-
118119
<mah:HamburgerMenuIconItem Label="设置" Tag="./View/SettingPage.xaml">
119120
</mah:HamburgerMenuIconItem>
120121
<mah:HamburgerMenuIconItem Label="示例" Tag="./View/ProductPage.xaml">

RoslynSyntaxTool/Manager/CompilationUtilitys.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Reflection;
@@ -13,25 +14,21 @@ public static class CompilationUtilitys
1314
{
1415
#region Public Method
1516

16-
public static MemoryStream CompileClientProxy(IEnumerable<SyntaxTree> trees, IEnumerable<MetadataReference> references)
17+
public static MemoryStream CompileClientProxy(IEnumerable<SyntaxTree> trees)
1718
{
18-
#if !NET
19-
var assemblys = new[]
20-
{
21-
"System.Runtime",
22-
"mscorlib",
23-
"System.Threading.Tasks",
24-
"System.Collections"
25-
};
26-
references = assemblys.Select(i => MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName(i)).Location)).Concat(references).Distinct();
27-
#endif
28-
references = new[]
29-
{
19+
20+
var references = new[]
21+
{
3022
MetadataReference.CreateFromFile(typeof(SyntaxTree).GetTypeInfo().Assembly.Location),
3123
MetadataReference.CreateFromFile(typeof(StatementSyntax).GetTypeInfo().Assembly.Location),
3224
MetadataReference.CreateFromFile(typeof(SyntaxFactory).GetTypeInfo().Assembly.Location)
33-
}.Concat(references).Distinct();
34-
return Compile(AssemblyInfo.Create("RoslynSyntaxTool.Proxy.ConvertToCSharpProxy"), trees, references);
25+
};
26+
var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToList();
27+
var allReferences = assemblies.Where(c => !c.IsDynamic).Select(x => {
28+
var f = MetadataReference.CreateFromFile(x.Location);
29+
return f;
30+
}).Concat(references);
31+
return Compile(AssemblyInfo.Create("RoslynSyntaxTool.Proxy.ConvertToCSharpProxy"), trees, allReferences);
3532
}
3633

3734
public static MemoryStream Compile(AssemblyInfo assemblyInfo, IEnumerable<SyntaxTree> trees, IEnumerable<MetadataReference> references)

0 commit comments

Comments
 (0)