Revision: 8408
Author:
v...@rsdn.ru
Date: Fri Oct 23 08:14:53 2009
Log: Working on Integration: Implementing smart-tags (interlace members
implementation).
http://code.google.com/p/nemerle/source/detail?r=8408
Added:
/nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/InterfaceMemberImplSourceGenerator.n
Modified:
/nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Nemerle.Compiler.Utils.csproj
/nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/SourceGenerator.n
/nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Utils.n
/nemerle/trunk/VsIntegration/Nemerle.VisualStudio/GUI/ImplementMembersForm.cs
=======================================
--- /dev/null
+++
/nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/InterfaceMemberImplSourceGenerator.n
Fri Oct 23 08:14:53 2009
@@ -0,0 +1,134 @@
+using Nemerle.Collections;
+using Nemerle.Compiler;
+using Nemerle.Compiler.Typedtree;
+using Nemerle.Utility;
+using Nemerle.Assertions;
+using System;
+using System.Diagnostics;
+using System.Globalization;
+using System.IO;
+using System.Text;
+using SR = System.Reflection;
+
+namespace Nemerle.Completion2
+{
+ public class InterfaceMemberImplSourceGenerator : SourceGenerator
+ {
+ _explicit : bool;
+ _accessMods : string;
+ _implName : string;
+ _ty : MType.Class;
+
+ public this(fileIndex : int, writer : TextWriter, ty : MType.Class,
explicit : bool, accessMods : string, implName : string)
+ {
+ base(fileIndex, writer, null);
+
+ assert(!explicit || implName != null);
+
+ _explicit = explicit;
+ _accessMods = accessMods;
+ _implName = implName;
+ _ty = ty;
+ }
+
+ public override WriteParameterDeclarations([NotNull] value :
IMethod) : void
+ {
+ def (from, to) =
+ match (_ty.TypeOfMember(value).Hint)
+ {
+ | Some(MType.Fun(MType.Tuple(from), to)) => (from, to)
+ | Some(MType.Fun(from, to)) => ([from], to)
+ | Some(MType.Fun(MType.Void, to)) => ([], to)
+ | _ => assert(false);
+ };
+
+ def parms = value.GetParameters().ZipLazy(from);
+
+ if (value.IsVarArgs)
+ {
+ def (till_last, last) = value.GetParameters().DivideLast();
+
+ WriteList(till_last, WriteParameterDeclaration);
+ Write(", params ");
+ WriteParameterDeclaration(last);
+ }
+ else
+ WriteList(value.GetParameters(), WriteParameterDeclaration);
+ }
+
+ public override WriteName(value : IMember) : void
+ {
+ if (_explicit)
+ Write(_implName);
+ else
+ base.WriteName(value);
+ }
+
+ public override WriteAttributes(_member : IMember) : void
+ {
+ if (_explicit)
+ when (_accessMods != null)
+ Write(_accessMods + " ");
+ else
+ Write("public ");
+ }
+
+ WriteNoImplBlock([NotNull] value : IMethod) : void
+ {
+ BeginBlock();
+ def parms = value.GetParameters();
+ unless (parms.IsEmpty)
+ {
+ def init = value.GetParameters().Map(p => $"_ = $(p.Name)");
+ WriteLine($<#..$(init; "; ");#>);
+ }
+
+ WriteLine("throw System.NotImplementedException()");
+ EndBlock();
+ }
+
+ WriteImplements(member : IMember) : void
+ {
+ when(_explicit)
+ {
+ WriteLine();
+ Write($" implements $(
member.DeclaringType.Name).$(member.Name)");
+ }
+ }
+
+ public override WriteMethodBody([NotNull] value : IMethod) : void
+ {
+ WriteImplements(value);
+ WriteNoImplBlock(value);
+ }
+
+ public override WritePropertyBoby(value : IProperty) : void
+ {
+ WriteImplements(value);
+
+ def getMethod = value.GetGetter();
+ def setMethod = value.GetSetter();
+
+ BeginBlock();
+
+ unless (getMethod == null)
+ WritePropertyGetter(value, getMethod);
+ unless (setMethod == null)
+ WritePropertySetter(value, setMethod);
+
+ EndBlock();
+ }
+
+ public override WritePropertyGetter(_property : IProperty, getMethod :
IMethod) : void
+ {
+ Write("get");
+ WriteNoImplBlock(getMethod);
+ }
+
+ public override WritePropertySetter(_property : IProperty, setMethod :
IMethod) : void
+ {
+ Write("set");
+ WriteNoImplBlock(setMethod);
+ }
+ }
+}
=======================================
---
/nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Nemerle.Compiler.Utils.csproj
Mon Oct 19 04:51:16 2009
+++
/nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Nemerle.Compiler.Utils.csproj
Fri Oct 23 08:14:53 2009
@@ -285,6 +285,9 @@
<ItemGroup>
<Compile
Include="Nemerle.Completion2\Engine\BackgroundWorks\Engine-FindUnimplementedMembers.n"
/>
</ItemGroup>
+ <ItemGroup>
+ <Compile
Include="Nemerle.Completion2\CodeModel\InterfaceMemberImplSourceGenerator.n"
/>
+ </ItemGroup>
<Import Project="$(Nemerle)\Nemerle.MSBuild.targets" />
<!-- To modify your build process, add your task inside one of the
targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
=======================================
---
/nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/SourceGenerator.n
Wed Oct 21 13:57:22 2009
+++
/nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Nemerle.Completion2/CodeModel/SourceGenerator.n
Fri Oct 23 08:14:53 2009
@@ -1,6 +1,7 @@
using Nemerle.Compiler;
using Nemerle.Compiler.Typedtree;
using Nemerle.Utility;
+using Nemerle.Assertions;
using System;
using System.Diagnostics;
using System.Globalization;
@@ -13,16 +14,17 @@
// Description of SourceGenerator
public class SourceGenerator
{
- [Accessor] mutable _location : Location;
-
- mutable _fileIndex : int;
- mutable _line : int;
- mutable _column : int;
- mutable _indentSize : int;
- mutable _applyIndent : bool;
- mutable _ns : NamespaceTree.Node;
- private _writer : TextWriter;
- private _target : SR.MemberInfo;
+ [Accessor]
+ protected mutable _location : Location;
+
+ protected mutable _fileIndex : int;
+ protected mutable _line : int;
+ protected mutable _column : int;
+ protected mutable _indentSize : int;
+ protected mutable _applyIndent : bool;
+ protected mutable _ns : NamespaceTree.Node;
+ protected _writer : TextWriter;
+ protected _target : SR.MemberInfo;
public this(fileIndex : int, writer : TextWriter, target :
SR.MemberInfo)
{
@@ -37,10 +39,10 @@
IsNeedMakeLocation(other : SR.MemberInfo) : bool
{
- _target != null && _target.Equals(other)
+ _target.Equals(other)
}
- public WriteEventDeclaration(value : IEvent) : void
+ public virtual WriteEventDeclaration(value : IEvent) : void
{
when (value == null)
throw ArgumentNullException("value");
@@ -56,7 +58,7 @@
Write(";");
}
- public WriteFieldDeclaration(value : IField) : void
+ public virtual WriteFieldDeclaration(value : IField) : void
{
when (value == null)
throw ArgumentNullException("value");
@@ -70,7 +72,7 @@
Write(";");
}
- public WriteConstructorDeclaration(value : IMethod) : void
+ public virtual WriteConstructorDeclaration(value : IMethod) : void
{
when (value == null)
throw ArgumentNullException("value");
@@ -94,11 +96,10 @@
Write(");");
}
- public WriteMethodDeclaration(value : IMethod) : void
- {
- when (value == null)
- throw ArgumentNullException("value");
-
+ #region Method
+
+ public virtual WriteMethodDeclaration([NotNull] value : IMethod) : void
+ {
WriteDocumentation(value);
WriteAttributes(value);
@@ -111,6 +112,18 @@
Write(" ");
}
+ WriteParameterDeclarations(value);
+
+ Write(")");
+
+ Write(" : ");
+ WriteType(value.ReturnType.FixedValue);
+
+ WriteMethodBody(value);
+ }
+
+ public virtual WriteParameterDeclarations([NotNull] value : IMethod) :
void
+ {
if (value.IsVarArgs)
{
def (till_last, last) = value.GetParameters().DivideLast();
@@ -121,24 +134,24 @@
}
else
WriteList(value.GetParameters(), WriteParameterDeclaration);
-
- Write(")");
-
- Write(" : ");
- WriteType(value.ReturnType.FixedValue);
+ }
+
+ public virtual WriteMethodBody([NotNull] _value : IMethod) : void
+ {
Write(";");
}
- public WritePropertyDeclaration(value : IProperty) : void
+ #endregion
+
+ #region Property
+
+ public virtual WritePropertyDeclaration(value : IProperty) : void
{
when (value == null)
throw ArgumentNullException("value");
WriteDocumentation(value);
- def getMethod = value.GetGetter();
- def setMethod = value.GetSetter();
-
WriteAttributes(value);
WriteName(value);
@@ -147,15 +160,37 @@
Write(" : ");
WriteType(value.GetMemType());
+ WritePropertyBoby(value);
+ }
+
+ public virtual WritePropertyBoby(value : IProperty) : void
+ {
+ def getMethod = value.GetGetter();
+ def setMethod = value.GetSetter();
+
Write(" {");
+
unless (getMethod == null)
- Write(" get;");
+ WritePropertyGetter(value, getMethod);
unless (setMethod == null)
- Write(" set;");
+ WritePropertyGetter(value, setMethod);
+
Write(" }");
}
- public WriteTypeDeclaration(value : TypeInfo) : void
+ public virtual WritePropertyGetter(_property : IProperty, _value :
IMethod) : void
+ {
+ Write(" get;");
+ }
+
+ public virtual WritePropertySetter(_property : IProperty, _value :
IMethod) : void
+ {
+ Write(" set;");
+ }
+
+ #endregion
+
+ public virtual WriteTypeDeclaration(value : TypeInfo) : void
{
when (value == null)
throw ArgumentNullException("value");
@@ -209,79 +244,7 @@
EndBlock();
}
- #region Implementation
-
- private Write(value : string) : void
- {
- when (_applyIndent)
- {
- repeat (_indentSize)
- _writer.Write('\t');
- _column += _indentSize;
- _applyIndent = false;
- }
-
- _writer.Write(value);
- _column += value.Length;
- }
-
- private WriteLine() : void
- {
- Write(Environment.NewLine);
- _applyIndent = true;
- _line++;
- _column = 1;
- }
-
- private BeginBlock() : void
- {
- WriteLine();
- Write("{");
- ++ _indentSize;
- WriteLine();
- }
-
- private EndBlock() : void
- {
- -- _indentSize;
- Write("}");
- WriteLine();
- }
-
- private WriteList['a](lst : list['a], writer : 'a -> void) : void
- {
- WriteList(lst, null, null, ", ", writer)
- }
-
- private WriteList['a](lst : list['a], start : string, stop : string,
writer : 'a -> void) : void
- {
- WriteList(lst, start, stop, ", ", writer)
- }
-
- private WriteList['a](lst : list['a], start : string, stop : string,
delemiter : string, writer : 'a -> void) : void
- {
- def loop(_)
- {
- | x :: Nil =>
- writer(x);
- | x :: tail =>
- writer(x);
- Write(delemiter);
- loop(tail)
- | [] => ()
- }
-
- unless (lst.IsEmpty)
- {
- unless (string.IsNullOrEmpty(start))
- Write(start);
- loop(lst);
- unless (string.IsNullOrEmpty(stop))
- Write(stop);
- }
- }
-
- private WriteName(value : IMember) : void
+ public virtual WriteName(value : IMember) : void
{
def name = if (value.MemberKind == MemberKinds.Constructor) "this"
else value.Name;
@@ -294,7 +257,7 @@
_location = Location(_fileIndex, _location.Line, _location.Column,
_line, _column);
}
- private WriteType(ty : TypeInfo) : void
+ public virtual WriteType(ty : TypeInfo) : void
{
mutable path = [ty.Name];
mutable node = ty.NamespaceNode.Parent;
@@ -315,7 +278,7 @@
}
}
- private WriteType(ty : TyVar) : void
+ public virtual WriteType(ty : TyVar) : void
{
match (ty.Fix())
{
@@ -345,21 +308,21 @@
}
}
- private WriteGenericConstraintList(_ : list[StaticTyVar]) : void
+ public virtual WriteGenericConstraintList(_ : list[StaticTyVar]) : void
{
}
- private WriteDocumentation(member : IMember) : void
+ public virtual WriteDocumentation(member : IMember) : void
{
WriteDocumentation(XmlDocReader.GetContent(member, member.Location));
}
- private WriteDocumentation(ns : NamespaceTree.Node, location :
Location) : void
+ public virtual WriteDocumentation(ns : NamespaceTree.Node, location :
Location) : void
{
WriteDocumentation(XmlDocReader.GetContent(ns, location));
}
- private WriteDocumentation(content : string) : void
+ public virtual WriteDocumentation(content : string) : void
{
unless (content == null)
{
@@ -375,13 +338,13 @@
}
}
- private WriteAttributes(member : IMember) : void
+ public virtual WriteAttributes(member : IMember) : void
{
unless (member.DeclaringType.IsInterface)
Write(GetAttributeString(member.Attributes));
}
- private WriteParameterDeclaration(value : TParameter) : void
+ public virtual WriteParameterDeclaration(value : TParameter) : void
{
Write(value.Name);
Write(" : ");
@@ -390,13 +353,13 @@
#region Types
- private WriteAliasDeclaration(to : TypeInfo) : void
+ public virtual WriteAliasDeclaration(to : TypeInfo) : void
{
Write("= ");
WriteType(to);
}
- private WriteClassDeclaration(value : TypeInfo) : void
+ public virtual WriteClassDeclaration(value : TypeInfo) : void
{
def superTypes =
match (value.GetDirectSuperTypes().Map(cls => cls.tycon))
@@ -476,7 +439,7 @@
EndBlock();
}
- private WriteDelegateDeclaration(ty : TypeInfo) : void
+ public virtual WriteDelegateDeclaration(ty : TypeInfo) : void
{
match (ty.LookupMember("Invoke"))
{
@@ -497,7 +460,7 @@
Write(";");
}
- private WriteEnumDeclaration(value : TypeInfo) : void
+ public virtual WriteEnumDeclaration(value : TypeInfo) : void
{
unless (value.InternalType.Int32.tycon.Equals(value.UnderlyingType))
{
@@ -529,11 +492,91 @@
}
#endregion
+
+ #region Implementation
+
+ protected Write(value : string) : void
+ {
+ when (_applyIndent)
+ {
+ repeat (_indentSize)
+ _writer.Write('\t');
+ _column += _indentSize;
+ _applyIndent = false;
+ }
+
+ _writer.Write(value);
+ _column += value.Length;
+ }
+
+ protected WriteLine(value : string) : void
+ {
+ Write(value);
+ WriteLine();
+ }
+
+ protected WriteLine() : void
+ {
+ Write(Environment.NewLine);
+ _applyIndent = true;
+ _line++;
+ _column = 1;
+ }
+
+ protected BeginBlock() : void
+ {
+ WriteLine();
+ Write("{");
+ ++ _indentSize;
+ WriteLine();
+ }
+
+ protected EndBlock() : void
+ {
+ -- _indentSize;
+ Write("}");
+ WriteLine();
+ }
+
+ protected WriteList['a](lst : list['a], writer : 'a -> void) : void
+ {
+ WriteList(lst, null, null, ", ", writer)
+ }
+
+ protected WriteList['a](lst : list['a], start : string, stop : string,
writer : 'a -> void) : void
+ {
+ WriteList(lst, start, stop, ", ", writer)
+ }
+
+ protected WriteList['a](lst : list['a], start : string, stop : string,
delemiter : string, writer : 'a -> void) : void
+ {
+ def loop(_)
+ {
+ | x :: Nil =>
+ writer(x);
+ | x :: tail =>
+ writer(x);
+ Write(delemiter);
+ loop(tail)
+ | [] => ()
+ }
+
+ unless (lst.IsEmpty)
+ {
+ unless (string.IsNullOrEmpty(start))
+ Write(start);
+ loop(lst);
+ unless (string.IsNullOrEmpty(stop))
+ Write(stop);
+ }
+ }
#endregion
+ #region static helpers
+
// TODO: Move these methods to Utils
- //
+
public static AccessibilityString(value : Accessibility) : string
{
| Public => "public";
@@ -607,5 +650,7 @@
text.ToString();
}
+
+ #endregion
}
}
=======================================
--- /nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Utils.n Wed Oct 21
13:57:22 2009
+++ /nemerle/trunk/VsIntegration/Nemerle.Compiler.Utils/Utils.n Fri Oct 23
08:14:53 2009
@@ -21,25 +21,22 @@
{
InvariantCultureCompareInfo : CompareInfo =
CultureInfo.InvariantCulture.CompareInfo;
- public GenerateMemberImplementation(fileIndex : int, member : IMember,
explicit : bool, _accessMods : string, _implName : string) : string
- {
- def writer = IO.StringWriter();
- def generator = SourceGenerator(fileIndex, writer, null);
-
- if (explicit)
- {
- "not impl"
- }
- else
- {
- match (member)
- {
- | method is IMethod =>
generator.WriteMethodDeclaration(method);
- | prop is IProperty =>
generator.WritePropertyDeclaration(prop);
- | _ => throw ArgumentException($"Unsupported member type
($member.GetType().Name)", "member");
- }
-
- writer.ToString()
+ public GenerateMemberImplementation(
+ writer : IO.TextWriter,
+ fileIndex : int,
+ ty : MType.Class,
+ member : IMember,
+ explicit : bool,
+ accessMods : string,
+ implName : string) : void
+ {
+ def generator = InterfaceMemberImplSourceGenerator(fileIndex,
writer, ty, explicit, accessMods, implName);
+
+ match (member)
+ {
+ | method is IMethod => generator.WriteMethodDeclaration(method);
+ | prop is IProperty => generator.WritePropertyDeclaration(prop);
+ | _ => throw ArgumentException($"Unsupported member type
($member.GetType().Name)", "member");
}
}
=======================================
---
/nemerle/trunk/VsIntegration/Nemerle.VisualStudio/GUI/ImplementMembersForm.cs
Wed Oct 21 13:57:22 2009
+++
/nemerle/trunk/VsIntegration/Nemerle.VisualStudio/GUI/ImplementMembersForm.cs
Fri Oct 23 08:14:53 2009
@@ -9,6 +9,7 @@
using Nemerle.VisualStudio.LanguageService;
using Nemerle.Compiler;
using System.Diagnostics;
+using TypeMembers =
System.Collections.Generic.KeyValuePair<Nemerle.Compiler.MType.Class,
Nemerle.Compiler.IMember>;
namespace Nemerle.VisualStudio.GUI
{
@@ -34,11 +35,19 @@
if (_unimplementedMembers == null)
return;
+ //var _ty.GetDirectSuperTypes().GroupJoin(_unimplementedMembers, itf
=> itf.tycon, m => m.DeclaringType,
+ // (t, ms) => new { Ty = t, Members = ms });
+ var implItfs = _ty.GetDirectSuperTypes().Where(t => t.IsInterface);
var itfs = _unimplementedMembers.GroupBy(m => m.DeclaringType);
- FillTable(itfs);
- }
-
- void FillTable(IEnumerable<IGrouping<TypeInfo, IMember>> itfs)
+ var res = implItfs.Join(itfs, t => t.tycon, itf => itf.Key, (t, itf)
=> new { Group = itf, Ty = t });
+ var ht = new Dictionary<MType.Class, IMember[]>();
+ foreach (var item in res)
+ ht[item.Ty] = ReplaceGettersAndSettersByProperties(item.Group);
+
+ FillTable(ht);
+ }
+
+ void FillTable(Dictionary<MType.Class, IMember[]> itfs)
{
_grid.CellPainting += CellPainting;
_grid.CellValueChanged += CellValueChanged;
@@ -57,9 +66,7 @@
var row = _grid.Rows[rowIndex];
row.Cells[0].Style.Font = new Font(_grid.DefaultCellStyle.Font,
FontStyle.Bold);
- var mems = ReplaceGettersAndSettersByProperties(item);
-
- foreach (var m in mems)
+ foreach (var m in item.Value)
{
var name = m.Name;
@@ -72,7 +79,10 @@
row.Cells["AccessMods"].Style.ForeColor = gray;
row.Cells["ImplName"].Style.ForeColor = gray;
row.Cells["Signature"].Style.ForeColor = gray;
- row.Tag = m;
+ //var x0 = item.Key.TypeOfMember(item.Value[0]);
+ //var x1 = item.Key.TypeOfMember(item.Value[1]);
+ //var x2 = item.Key.TypeOfMember(item.Value[2]);
+ row.Tag = item;
}
}
}
@@ -134,7 +144,7 @@
if (e.ColumnIndex == 0 && row.Tag != null)
{
- var member = (IMember)row.Tag;
+ var member = ((TypeMembers)row.Tag).Value;
var imgIndex = Nemerle.Compiler.Utils.Utils.GetGlyphIndex(member);
e.Paint(r, e.PaintParts);
@@ -159,59 +169,33 @@
private void pbImplement_Click(object sender, EventArgs e)
{
var res = _grid.Rows.Cast<DataGridViewRow>().Where(r => r.Tag !=
null)
- .GroupBy(r => ((IMember)r.Tag).DeclaringType, r =>
+ .GroupBy(r => ((TypeMembers)r.Tag).Key, r =>
new
- {
- Member = ((IMember)r.Tag),
+ {
+ Type = ((TypeMembers)r.Tag).Key,
+ Member = ((TypeMembers)r.Tag).Value,
Explicit = (bool)r.Cells["Explicit"].Value,
AccessMods = (string)r.Cells["AccessMods"].Value,
ImplName = (string)r.Cells["ImplName"].Value
});
- var res2 = res.ToArray();
- var sb = new StringBuilder();
+ var res2 = res.ToArray();
+ var writer = new System.IO.StringWriter();
foreach (var item in res2)
{
- sb.AppendLine("//" + item.Key + ":");
+ writer.WriteLine("//" + item.Key + ":");
foreach (var item2 in item)
{
-
sb.AppendLine(Nemerle.Compiler.Utils.Utils.GenerateMemberImplementation(
- _source.FileIndex, item2.Member, item2.Explicit,
item2.AccessMods, item2.ImplName));
-
+ Nemerle.Compiler.Utils.Utils.GenerateMemberImplementation(writer,
+ _source.FileIndex, item.Key, item2.Member, item2.Explicit,
item2.AccessMods, item2.ImplName);
+
+ writer.WriteLine();
}
}
- sb.Replace(" { get; set; }", @"
-{
- get
- {
- System.NotImplementedException()
- }
- set
- {
- _ = value~
- System.NotImplementedException()
- }
-}").Replace(" { get; }", @"
-{
- get
- {
- System.NotImplementedException()
- }
-}").Replace(" { set; }", @"
-{
- set
- {
- System.NotImplementedException()
- }
-}").Replace(";", @"
-{
- System.NotImplementedException()
-}").Replace("~", ";");
-
- Debug.WriteLine(sb.ToString());
+
Debug.WriteLine(writer.GetStringBuilder().Replace("\t", " ").ToString());
}
}
}