Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 2a0cb43

Browse files
committed
Implement EnvDTE.CodeInterface.AddFunction()
1 parent c0b7089 commit 2a0cb43

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/AddIns/BackendBindings/CSharpBinding/Project/Src/Refactoring/CSharpCodeGenerator.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ public override void AddFieldAtStart(ITypeDefinition declaringType, Accessibilit
159159
}
160160
}
161161

162+
public override void AddMethodAtStart(ITypeDefinition declaringType, Accessibility accessibility, IType returnType, string name)
163+
{
164+
SDRefactoringContext context = declaringType.CreateRefactoringContext();
165+
var typeDecl = context.GetNode<TypeDeclaration>();
166+
using (var script = context.StartScript()) {
167+
var astBuilder = context.CreateTypeSystemAstBuilder(typeDecl.FirstChild);
168+
var methodDecl = new MethodDeclaration();
169+
methodDecl.Name = name;
170+
methodDecl.ReturnType = astBuilder.ConvertType(context.Compilation.Import(returnType));
171+
172+
script.AddTo(typeDecl, methodDecl);
173+
}
174+
}
175+
162176
public override void ChangeAccessibility(IEntity entity, Accessibility newAccessiblity)
163177
{
164178
// TODO script.ChangeModifiers(...)

src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeInterface.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.Linq;
2121
using System.Text;
2222
using ICSharpCode.NRefactory.TypeSystem;
23+
using ICSharpCode.NRefactory.TypeSystem.Implementation;
2324
using ICSharpCode.SharpDevelop.Dom;
2425

2526
namespace ICSharpCode.PackageManagement.EnvDTE
@@ -37,9 +38,39 @@ public CodeInterface(CodeModelContext context, ITypeDefinition typeDefinition, p
3738

3839
public global::EnvDTE.CodeFunction AddFunction(string name, global::EnvDTE.vsCMFunction kind, object type, object Position = null, global::EnvDTE.vsCMAccess Access = global::EnvDTE.vsCMAccess.vsCMAccessPublic)
3940
{
40-
// var codeGenerator = new ClassCodeGenerator(Class);
41-
// return codeGenerator.AddPublicMethod(name, (string)type);
41+
IType returnType = GetMethodReturnType((string)type);
42+
43+
context.CodeGenerator.AddMethodAtStart(typeDefinition, Access.ToAccessibility(), returnType, name);
44+
45+
ReloadTypeDefinition();
46+
47+
IMethod method = typeDefinition.Methods.FirstOrDefault(f => f.Name == name);
48+
if (method != null) {
49+
return new CodeFunction(context, method);
50+
}
4251
return null;
4352
}
53+
54+
IType GetMethodReturnType(string typeName)
55+
{
56+
var fullTypeName = new FullTypeName(typeName);
57+
58+
IType type = typeDefinition.Compilation.FindType(fullTypeName);
59+
if (type != null) {
60+
return type;
61+
}
62+
63+
return new UnknownType(fullTypeName);
64+
}
65+
66+
void ReloadTypeDefinition()
67+
{
68+
ICompilation compilation = context.DteProject.GetCompilationUnit(typeDefinition.BodyRegion.FileName);
69+
70+
ITypeDefinition matchedTypeDefinition = compilation.MainAssembly.GetTypeDefinition(typeDefinition.FullTypeName);
71+
if (matchedTypeDefinition != null) {
72+
typeDefinition = matchedTypeDefinition;
73+
}
74+
}
4475
}
4576
}

src/AddIns/Misc/PackageManagement/Project/Src/ICodeGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ public interface ICodeGenerator
2727
void AddImport(FileName fileName, string name);
2828
void MakePartial(ITypeDefinition typeDefinition);
2929
void AddFieldAtStart(ITypeDefinition typeDefinition, Accessibility accessibility, IType fieldType, string name);
30+
void AddMethodAtStart(ITypeDefinition typeDefinition, Accessibility accessibility, IType returnType, string name);
3031
}
3132
}

src/AddIns/Misc/PackageManagement/Project/Src/ThreadSafeCodeGenerator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,10 @@ public void AddFieldAtStart(ITypeDefinition typeDefinition, Accessibility access
5454
{
5555
InvokeIfRequired(() => codeGenerator.AddFieldAtStart(typeDefinition, accessibility, fieldType, name));
5656
}
57+
58+
public void AddMethodAtStart(ITypeDefinition typeDefinition, Accessibility accessibility, IType returnType, string name)
59+
{
60+
InvokeIfRequired(() => codeGenerator.AddMethodAtStart(typeDefinition, accessibility, returnType, name));
61+
}
5762
}
5863
}

src/Main/Base/Project/Refactoring/CodeGenerator.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public virtual void AddFieldAtStart(ITypeDefinition typeDefinition, Accessibilit
9393
throw new NotSupportedException("Feature not supported!");
9494
}
9595

96+
public virtual void AddMethodAtStart(ITypeDefinition declaringType, Accessibility accessibility, IType returnType, string name)
97+
{
98+
throw new NotSupportedException("Feature not supported!");
99+
}
100+
96101
public virtual void ChangeAccessibility(IEntity entity, Accessibility newAccessiblity)
97102
{
98103
throw new NotSupportedException("Feature not supported!");

0 commit comments

Comments
 (0)