272
public IMember AddMember (IType cls, CodeTypeMember member)
274
RefactorerContext gctx = GetGeneratorContext (cls);
275
IRefactorer gen = GetGeneratorForClass (cls);
276
IMember m = gen.AddMember (gctx, cls, member);
281
public IType AddMembers (IType cls, IEnumerable<CodeTypeMember> members, string foldingRegionName)
283
RefactorerContext gctx = GetGeneratorContext (cls);
284
IRefactorer gen = GetGeneratorForClass (cls);
285
gen.AddMembers (gctx, cls, members, foldingRegionName);
287
return GetUpdatedClass (gctx, cls);
290
public IMember ImplementMember (IType cls, IMember member, IReturnType privateReturnType)
293
RefactorerContext gctx = GetGeneratorContext (cls);
294
IRefactorer gen = GetGeneratorForClass (cls);
295
IMember m = gen.ImplementMember (gctx, cls, member, privateReturnType);
272
// public IMember AddMember (IType cls, CodeTypeMember member)
274
// RefactorerContext gctx = GetGeneratorContext (cls);
275
// IRefactorer gen = GetGeneratorForClass (cls);
276
// IMember m = gen.AddMember (gctx, cls, member);
281
// public IType AddMembers (IType cls, IEnumerable<CodeTypeMember> members, string foldingRegionName)
283
// RefactorerContext gctx = GetGeneratorContext (cls);
284
// IRefactorer gen = GetGeneratorForClass (cls);
285
// gen.AddMembers (gctx, cls, members, foldingRegionName);
287
// return GetUpdatedClass (gctx, cls);
290
// public IMember ImplementMember (IType cls, IMember member, IReturnType privateReturnType)
292
// RefactorerContext gctx = GetGeneratorContext (cls);
293
// IRefactorer gen = GetGeneratorForClass (cls);
294
// IMember m = gen.ImplementMember (gctx, cls, member, privateReturnType);
300
299
public void AddGlobalNamespaceImport (ProjectDom dom, string fileName, string nsName)
316
315
return refactorer.CompleteStatement (new RefactorerContext (dom, fileProvider, null), fileName, caretLocation);
319
public IType ImplementMembers (IType cls, IEnumerable<KeyValuePair<IMember,IReturnType>> members,
320
string foldingRegionName)
322
RefactorerContext gctx = GetGeneratorContext (cls);
323
cls = GetUpdatedClass (gctx, cls);
324
IRefactorer gen = GetGeneratorForClass (cls);
325
gen.ImplementMembers (gctx, cls, members, foldingRegionName);
327
return GetUpdatedClass (gctx, cls);
318
// public IType ImplementMembers (IType cls, IEnumerable<KeyValuePair<IMember,IReturnType>> members,
319
// string foldingRegionName)
321
// RefactorerContext gctx = GetGeneratorContext (cls);
322
// cls = GetUpdatedClass (gctx, cls);
323
// IRefactorer gen = GetGeneratorForClass (cls);
324
// gen.ImplementMembers (gctx, cls, members, foldingRegionName);
326
// return GetUpdatedClass (gctx, cls);
330
329
string GenerateGenerics (IRefactorer gen, IType iface, IReturnType hintReturnType)
378
public IType ImplementInterface (ICompilationUnit pinfo, IType klass, IType iface, bool explicitly, IType declaringClass, IReturnType hintReturnType)
381
throw new ArgumentNullException ("klass");
383
throw new ArgumentNullException ("iface");
384
RefactorerContext gctx = GetGeneratorContext (klass);
385
klass = GetUpdatedClass (gctx, klass);
387
bool alreadyImplemented;
388
IReturnType prefix = null;
390
List<KeyValuePair<IMember,IReturnType>> toImplement = new List<KeyValuePair<IMember,IReturnType>> ();
392
prefix = new DomReturnType (iface);
394
// Stub out non-implemented events defined by @iface
395
foreach (IEvent ev in iface.Events) {
396
if (ev.IsSpecialName)
398
bool needsExplicitly = explicitly;
400
alreadyImplemented = gctx.ParserContext.GetInheritanceTree (klass).Any (x => x.ClassType != ClassType.Interface && x.Events.Any (y => y.Name == ev.Name));
402
if (!alreadyImplemented)
403
toImplement.Add (new KeyValuePair<IMember,IReturnType> (ev, needsExplicitly ? prefix : null));
406
// Stub out non-implemented methods defined by @iface
407
foreach (IMethod method in iface.Methods) {
408
if (method.IsSpecialName)
410
bool needsExplicitly = explicitly;
411
alreadyImplemented = false;
412
foreach (IType t in gctx.ParserContext.GetInheritanceTree (klass)) {
413
if (t.ClassType == ClassType.Interface)
415
foreach (IMethod cmet in t.Methods) {
416
if (cmet.Name == method.Name && Equals (cmet.Parameters, method.Parameters)) {
417
if (!needsExplicitly && !cmet.ReturnType.Equals (method.ReturnType))
418
needsExplicitly = true;
420
alreadyImplemented |= !needsExplicitly || (iface.FullName == GetExplicitPrefix (cmet.ExplicitInterfaces));
425
if (!alreadyImplemented)
426
toImplement.Add (new KeyValuePair<IMember,IReturnType> (method, needsExplicitly ? prefix : null));
429
// Stub out non-implemented properties defined by @iface
430
foreach (IProperty prop in iface.Properties) {
431
if (prop.IsSpecialName)
433
bool needsExplicitly = explicitly;
434
alreadyImplemented = false;
435
foreach (IType t in gctx.ParserContext.GetInheritanceTree (klass)) {
436
if (t.ClassType == ClassType.Interface)
438
foreach (IProperty cprop in t.Properties) {
439
if (cprop.Name == prop.Name) {
440
if (!needsExplicitly && !cprop.ReturnType.Equals (prop.ReturnType))
441
needsExplicitly = true;
443
alreadyImplemented |= !needsExplicitly || (iface.FullName == GetExplicitPrefix (cprop.ExplicitInterfaces));
447
if (!alreadyImplemented)
448
toImplement.Add (new KeyValuePair<IMember,IReturnType> (prop, needsExplicitly ? prefix : null)); }
450
Ambience ambience = AmbienceService.GetAmbienceForFile (klass.CompilationUnit.FileName);
452
ImplementMembers (klass, toImplement, ambience.GetString (iface, OutputFlags.ClassBrowserEntries | OutputFlags.IncludeGenerics | OutputFlags.IncludeParameters) + " implementation");
455
klass = GetUpdatedClass (gctx, klass);
456
foreach (IType baseClass in iface.SourceProjectDom.GetInheritanceTree (iface)) {
457
if (baseClass.Equals (iface) || baseClass.FullName == "System.Object")
459
klass = ImplementInterface (pinfo, klass, baseClass, explicitly, declaringClass, hintReturnType);
377
// public IType ImplementInterface (ICompilationUnit pinfo, IType klass, IType iface, bool explicitly, IType declaringClass, IReturnType hintReturnType)
379
// if (klass == null)
380
// throw new ArgumentNullException ("klass");
381
// if (iface == null)
382
// throw new ArgumentNullException ("iface");
383
// RefactorerContext gctx = GetGeneratorContext (klass);
384
// klass = GetUpdatedClass (gctx, klass);
386
// bool alreadyImplemented;
387
// IReturnType prefix = null;
389
// List<KeyValuePair<IMember,IReturnType>> toImplement = new List<KeyValuePair<IMember,IReturnType>> ();
391
// prefix = new DomReturnType (iface);
393
// // Stub out non-implemented events defined by @iface
394
// foreach (IEvent ev in iface.Events) {
395
// if (ev.IsSpecialName)
397
// bool needsExplicitly = explicitly;
399
// alreadyImplemented = gctx.ParserContext.GetInheritanceTree (klass).Any (x => x.ClassType != ClassType.Interface && x.Events.Any (y => y.Name == ev.Name));
401
// if (!alreadyImplemented)
402
// toImplement.Add (new KeyValuePair<IMember,IReturnType> (ev, needsExplicitly ? prefix : null));
405
// // Stub out non-implemented methods defined by @iface
406
// foreach (IMethod method in iface.Methods) {
407
// if (method.IsSpecialName)
409
// bool needsExplicitly = explicitly;
410
// alreadyImplemented = false;
411
// foreach (IType t in gctx.ParserContext.GetInheritanceTree (klass)) {
412
// if (t.ClassType == ClassType.Interface)
414
// foreach (IMethod cmet in t.Methods) {
415
// if (cmet.Name == method.Name && Equals (cmet.Parameters, method.Parameters)) {
416
// if (!needsExplicitly && !cmet.ReturnType.Equals (method.ReturnType))
417
// needsExplicitly = true;
419
// alreadyImplemented |= !needsExplicitly || (iface.FullName == GetExplicitPrefix (cmet.ExplicitInterfaces));
424
// if (!alreadyImplemented)
425
// toImplement.Add (new KeyValuePair<IMember,IReturnType> (method, needsExplicitly ? prefix : null));
428
// // Stub out non-implemented properties defined by @iface
429
// foreach (IProperty prop in iface.Properties) {
430
// if (prop.IsSpecialName)
432
// bool needsExplicitly = explicitly;
433
// alreadyImplemented = false;
434
// foreach (IType t in gctx.ParserContext.GetInheritanceTree (klass)) {
435
// if (t.ClassType == ClassType.Interface)
437
// foreach (IProperty cprop in t.Properties) {
438
// if (cprop.Name == prop.Name) {
439
// if (!needsExplicitly && !cprop.ReturnType.Equals (prop.ReturnType))
440
// needsExplicitly = true;
442
// alreadyImplemented |= !needsExplicitly || (iface.FullName == GetExplicitPrefix (cprop.ExplicitInterfaces));
446
// if (!alreadyImplemented)
447
// toImplement.Add (new KeyValuePair<IMember,IReturnType> (prop, needsExplicitly ? prefix : null)); }
449
// Ambience ambience = AmbienceService.GetAmbienceForFile (klass.CompilationUnit.FileName);
450
// //implement members
451
// ImplementMembers (klass, toImplement, ambience.GetString (iface, OutputFlags.ClassBrowserEntries | OutputFlags.IncludeGenerics | OutputFlags.IncludeParameters) + " implementation");
454
// klass = GetUpdatedClass (gctx, klass);
455
// foreach (IType baseClass in iface.SourceProjectDom.GetInheritanceTree (iface)) {
456
// if (baseClass.Equals (iface) || baseClass.FullName == "System.Object")
458
// klass = ImplementInterface (pinfo, klass, baseClass, explicitly, declaringClass, hintReturnType);
466
465
IType GetUpdatedClass (RefactorerContext gctx, IType klass)
468
467
IEditableTextFile file = gctx.GetFile (klass.CompilationUnit.FileName);
469
ParsedDocument doc = ProjectDomService.Parse (gctx.ParserContext.Project, file.Name, null, delegate () { return file.Text; });
468
ParsedDocument doc = ProjectDomService.Parse (gctx.ParserContext.Project, file.Name, delegate () { return file.Text; });
470
469
IType result = gctx.ParserContext.GetType (klass.FullName, klass.TypeParameters.Count, true);
471
470
if (result is CompoundType) {
472
471
IType hintType = doc.CompilationUnit.GetType (klass.FullName, klass.TypeParameters.Count);
574
public bool RenameVariable (IProgressMonitor monitor, LocalVariable var, string newName)
577
MemberReferenceCollection refs = new MemberReferenceCollection ();
578
Refactor (monitor, var, new RefactorDelegate (new RefactorFindVariableReferences (var, refs).Refactor));
579
refs.RenameAll (newName);
581
RefactorerContext gctx = GetGeneratorContext (var);
582
IRefactorer r = GetGeneratorForVariable (var);
583
bool rv = r.RenameVariable (gctx, var, newName);
587
} catch (Exception e) {
588
LoggingService.LogError (GettextCatalog.GetString ("Error while renaming {0} to {1}: {2}", var, newName, e.ToString ()));
593
public bool RenameParameter (IProgressMonitor monitor, IParameter param, string newName)
596
MemberReferenceCollection refs = new MemberReferenceCollection ();
597
Refactor (monitor, param, new RefactorDelegate (new RefactorFindParameterReferences (param, refs, false).Refactor));
598
refs.RenameAll (newName);
600
IMember member = param.DeclaringMember;
601
RefactorerContext gctx = GetGeneratorContext (member.DeclaringType);
602
IRefactorer r = GetGeneratorForClass (member.DeclaringType);
603
bool rv = r.RenameParameter (gctx, param, newName);
607
} catch (Exception e) {
608
LoggingService.LogError (GettextCatalog.GetString ("Error while renaming {0} to {1}: {2}", param, newName, e.ToString ()));
613
public IMember EncapsulateField (IProgressMonitor monitor, IType cls, IField field, string propName, MemberAttributes attr, bool generateSetter, bool updateInternalRefs)
615
RefactoryScope scope = GetScope (field);
617
MemberReferenceCollection refs = new MemberReferenceCollection ();
618
Refactor (monitor, cls, scope, new RefactorDelegate (new RefactorFindMemberReferences (cls, field, refs, false).Refactor));
620
if (!updateInternalRefs) {
621
ArrayList list = new ArrayList ();
622
list.AddRange (refs);
623
list.Sort (new MemberReferenceCollection.MemberComparer ());
625
foreach (MemberReference mref in list) {
627
foreach (IType part in field.DeclaringType.Parts) {
628
if (mref.FileName == part.CompilationUnit.FileName) {
629
DomRegion region = part.BodyRegion;
631
// check if the reference is internal to the class
632
if ((mref.Line > region.Start.Line ||
633
(mref.Line == region.Start.Line && mref.Column >= region.Start.Column)) &&
634
(mref.Line < region.End.Line ||
635
(mref.Line == region.End.Line && mref.Column <= region.End.Column))) {
636
// Internal to the class, don't rename
644
mref.Rename (propName);
647
refs.RenameAll (propName);
650
RefactorerContext gctx = GetGeneratorContext (cls);
651
IRefactorer r = GetGeneratorForClass (cls);
652
IMember m = r.EncapsulateField (gctx, cls, field, propName, attr, generateSetter);
573
// public bool RenameVariable (IProgressMonitor monitor, LocalVariable var, string newName)
576
// MemberReferenceCollection refs = new MemberReferenceCollection ();
577
// Refactor (monitor, var, new RefactorDelegate (new RefactorFindVariableReferences (var, refs).Refactor));
578
// refs.RenameAll (newName);
580
// RefactorerContext gctx = GetGeneratorContext (var);
581
// IRefactorer r = GetGeneratorForVariable (var);
582
// bool rv = r.RenameVariable (gctx, var, newName);
586
// } catch (Exception e) {
587
// LoggingService.LogError (GettextCatalog.GetString ("Error while renaming {0} to {1}: {2}", var, newName, e.ToString ()));
592
// public bool RenameParameter (IProgressMonitor monitor, IParameter param, string newName)
595
// MemberReferenceCollection refs = new MemberReferenceCollection ();
596
// Refactor (monitor, param, new RefactorDelegate (new RefactorFindParameterReferences (param, refs, false).Refactor));
597
// refs.RenameAll (newName);
599
// IMember member = param.DeclaringMember;
600
// RefactorerContext gctx = GetGeneratorContext (member.DeclaringType);
601
// IRefactorer r = GetGeneratorForClass (member.DeclaringType);
602
// bool rv = r.RenameParameter (gctx, param, newName);
606
// } catch (Exception e) {
607
// LoggingService.LogError (GettextCatalog.GetString ("Error while renaming {0} to {1}: {2}", param, newName, e.ToString ()));
658
612
public IType[] FindDerivedClasses (IType baseClass)