Browse Source

Simplify Demo app.

newNRvisualizers
Daniel Grunwald 13 years ago
parent
commit
2b41cda674
  1. 2
      ICSharpCode.NRefactory.Demo/CSDemo.Designer.cs
  2. 84
      ICSharpCode.NRefactory.Demo/CSDemo.cs
  3. 10
      README

2
ICSharpCode.NRefactory.Demo/CSDemo.Designer.cs generated

@ -142,7 +142,7 @@ namespace ICSharpCode.NRefactory.Demo @@ -142,7 +142,7 @@ namespace ICSharpCode.NRefactory.Demo
this.csharpGenerateCodeButton.Name = "csharpGenerateCodeButton";
this.csharpGenerateCodeButton.Size = new System.Drawing.Size(100, 23);
this.csharpGenerateCodeButton.TabIndex = 1;
this.csharpGenerateCodeButton.Text = "Generate";
this.csharpGenerateCodeButton.Text = "\u2191 Generate \u2191";
this.csharpGenerateCodeButton.UseVisualStyleBackColor = true;
this.csharpGenerateCodeButton.Click += new System.EventHandler(this.CSharpGenerateCodeButtonClick);
//

84
ICSharpCode.NRefactory.Demo/CSDemo.cs

@ -152,9 +152,17 @@ namespace ICSharpCode.NRefactory.Demo @@ -152,9 +152,17 @@ namespace ICSharpCode.NRefactory.Demo
int GetOffset(TextBox textBox, TextLocation location)
{
// TextBox uses 0-based coordinates, TextLocation is 1-based
return textBox.GetFirstCharIndexFromLine(location.Line - 1) + location.Column - 1;
}
TextLocation GetTextLocation(TextBox textBox, int offset)
{
int line = textBox.GetLineFromCharIndex(offset);
int col = offset - textBox.GetFirstCharIndexFromLine(line);
return new TextLocation(line + 1, col + 1);
}
void CSharpTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
AstNode node = e.Node.Tag as AstNode;
@ -203,74 +211,22 @@ namespace ICSharpCode.NRefactory.Demo @@ -203,74 +211,22 @@ namespace ICSharpCode.NRefactory.Demo
ICompilation compilation = project.CreateCompilation();
AstNode selectedNode = null;
StoreInDictionaryNavigator navigator;
ResolveResult result;
if (csharpTreeView.SelectedNode != null) {
selectedNode = (AstNode)csharpTreeView.SelectedNode.Tag;
navigator = new StoreInDictionaryNavigator(selectedNode);
var selectedNode = (AstNode)csharpTreeView.SelectedNode.Tag;
CSharpAstResolver resolver = new CSharpAstResolver(compilation, compilationUnit, parsedFile);
result = resolver.Resolve(selectedNode);
// CSharpAstResolver.Resolve() never returns null
} else {
navigator = new StoreInDictionaryNavigator();
}
CSharpAstResolver resolver = new CSharpAstResolver(compilation, compilationUnit, parsedFile);
resolver.ApplyNavigator(navigator);
csharpTreeView.BeginUpdate();
ShowResolveResultsInTree(csharpTreeView.Nodes, navigator);
csharpTreeView.EndUpdate();
if (csharpTreeView.SelectedNode != null) {
using (var dlg = new SemanticTreeDialog(resolver.Resolve(selectedNode)))
dlg.ShowDialog();
}
}
sealed class StoreInDictionaryNavigator : NodeListResolveVisitorNavigator
{
internal Dictionary<AstNode, ResolveResult> resolveResults = new Dictionary<AstNode, ResolveResult>();
internal Dictionary<AstNode, Conversion> conversions = new Dictionary<AstNode, Conversion>();
internal Dictionary<AstNode, IType> conversionTargetTypes = new Dictionary<AstNode, IType>();
bool resolveAll;
public StoreInDictionaryNavigator(params AstNode[] nodes)
: base(nodes)
{
resolveAll = (nodes.Length == 0);
}
public override ResolveVisitorNavigationMode Scan(AstNode node)
{
if (resolveAll)
return ResolveVisitorNavigationMode.Resolve;
else
return base.Scan(node);
}
public override void Resolved(AstNode node, ResolveResult result)
{
resolveResults.Add(node, result);
}
public override void ProcessConversion(Expression expression, ResolveResult result, Conversion conversion, IType targetType)
{
conversions.Add(expression, conversion);
conversionTargetTypes.Add(expression, targetType);
}
}
void ShowResolveResultsInTree(TreeNodeCollection c, StoreInDictionaryNavigator n)
{
foreach (TreeNode t in c) {
AstNode node = t.Tag as AstNode;
if (node != null) {
ResolveResult rr;
string text = GetNodeTitle(node);
if (n.resolveResults.TryGetValue(node, out rr))
text += " " + rr.ToString();
if (n.conversions.ContainsKey(node)) {
text += " [" + n.conversions[node] + " to " + n.conversionTargetTypes[node] + "]";
}
t.Text = text;
TextLocation location = GetTextLocation(csharpCodeTextBox, csharpCodeTextBox.SelectionStart);
result = ResolveAtLocation.Resolve(compilation, parsedFile, compilationUnit, location);
if (result == null) {
MessageBox.Show("Could not find a resolvable node at the caret location.");
return;
}
ShowResolveResultsInTree(t.Nodes, n);
}
using (var dlg = new SemanticTreeDialog(result))
dlg.ShowDialog();
}
void CSharpCodeTextBoxKeyDown(object sender, KeyEventArgs e)

10
README

@ -7,7 +7,10 @@ Features: @@ -7,7 +7,10 @@ Features:
- Code Completion for C#
- Pretty Printer for C#
- Lots of C# refactorings
Non-Features:
- VB support is not implemented yet.
- NRefactory cannot generate IL code
How to download:
- Binaries are provided as a NuGet package (http://nuget.org/packages/ICSharpCode.NRefactory)
@ -110,6 +113,13 @@ Null-Object pattern: @@ -110,6 +113,13 @@ Null-Object pattern:
Null nodes are not considered to be part of the AST (e.g. they don't have a parent).
FAQ:
Q: What is the difference between NRefactory and Roslyn?
- NRefactory is ready and stable, Roslyn is only a CTP at this point of time.
- NRefactory does not have VB support.
- NRefactory cannot compile code to IL; it's not a full compiler, just a frontend.
- NRefactory C# AST is mutable and supports pattern matching; Roslyn is immutable and does not have built-in pattern matching.
- NRefactory is Open Source, if you somehow can't get at the information you need, you can modify the code.
Q: What is the difference between types and type definitions?
A: Basically, a type (IType) is any type in the .NET type system:

Loading…
Cancel
Save