Skip to content

Commit

Permalink
Changes following PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mansellan committed Apr 19, 2018
1 parent 25ba7ad commit 8ac6a5f
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 47 deletions.
Expand Up @@ -24,6 +24,10 @@ protected SafeRedirectedEventedComWrapper(TSource target, TEventSource eventSour
protected override void Dispose(bool disposing)
{
DetachEvents();

Marshal.ReleaseComObject(_eventSource);
_eventSource = null;

base.Dispose(disposing);
}

Expand Down Expand Up @@ -74,10 +78,8 @@ public void DetachEvents()
_cookie = NotAdvising;
}

Marshal.ReleaseComObject(_icp);
Marshal.ReleaseComObject(_eventSource);
_icp = null;
_eventSource = null;
Marshal.ReleaseComObject(_icp);
_icp = null;
}
}
}
Expand Down
Expand Up @@ -52,18 +52,6 @@ IEnumerator IEnumerable.GetEnumerator()
: ((IEnumerable<ICommandBarControl>) this).GetEnumerator();
}

//public override void Release(bool final = false)
//{
// if (!IsWrappingNullReference)
// {
// for (var i = 1; i <= Count; i++)
// {
// this[i].Release();
// }
// base.Release(final);
// }
//}

public override bool Equals(ISafeComWrapper<MSO.CommandBarControls> other)
{
return IsEqualIfNull(other) || (other != null && ReferenceEquals(other.Target, Target));
Expand Down
21 changes: 8 additions & 13 deletions Rubberduck.VBEditor.VB6/SafeComWrappers/Office/CommandBars.cs
Expand Up @@ -2,11 +2,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using MSO = Office_v8::Office;
using VB = Microsoft.Vbe.Interop.VB6;

// ReSharper disable once CheckNamespace - Special dispensation due to conflicting file vs namespace priorities
namespace Rubberduck.VBEditor.SafeComWrappers.Office8
Expand Down Expand Up @@ -34,22 +31,20 @@ public ICommandBar Add(string name, CommandBarPosition position)
}

private void DeleteExistingCommandBar(string name)
{
try
{
foreach (var commandBar in (IEnumerable<ICommandBar>) this)
{
var existing = Target.Cast<MSO.CommandBar>().FirstOrDefault(bar => bar.Name == name);
if (existing != null)
using (commandBar)
{
existing.Delete();
Marshal.FinalReleaseComObject(existing);
if (commandBar.Name == name)
{
commandBar.Delete();
}
}
}
catch
{
// specified commandbar didn't exist
}
}


public ICommandBarControl FindControl(int id)
{
return new CommandBarControl(IsWrappingNullReference ? null : Target.FindControl(Id: id), _vbe);
Expand Down
4 changes: 3 additions & 1 deletion Rubberduck.VBEditor.VB6/SafeComWrappers/VB/AddIns.cs
Expand Up @@ -44,7 +44,9 @@ public override int GetHashCode()

IEnumerator IEnumerable.GetEnumerator()
{
return IsWrappingNullReference ? new List<IEnumerable>().GetEnumerator() : Target.GetEnumerator();
return IsWrappingNullReference
? (IEnumerator)new List<IEnumerable>().GetEnumerator()
: ((IEnumerable<IAddIn>)this).GetEnumerator();
}

IEnumerator<IAddIn> IEnumerable<IAddIn>.GetEnumerator()
Expand Down
2 changes: 1 addition & 1 deletion Rubberduck.VBEditor.VB6/SafeComWrappers/VB/CodeModule.cs
Expand Up @@ -66,7 +66,7 @@ public void DeleteLines(Selection selection)

using (var codePane = CodePane)
{
if (CodePane.IsWrappingNullReference)
if (codePane.IsWrappingNullReference)
{
return null;
}
Expand Down
4 changes: 3 additions & 1 deletion Rubberduck.VBEditor.VB6/SafeComWrappers/VB/LinkedWindows.cs
Expand Up @@ -43,7 +43,9 @@ public void Add(IWindow window)

IEnumerator IEnumerable.GetEnumerator()
{
return IsWrappingNullReference ? new List<IEnumerable>().GetEnumerator() : Target.GetEnumerator();
return IsWrappingNullReference
? (IEnumerator)new List<IEnumerable>().GetEnumerator()
: ((IEnumerable<IWindow>)this).GetEnumerator();
}

IEnumerator<IWindow> IEnumerable<IWindow>.GetEnumerator()
Expand Down
5 changes: 1 addition & 4 deletions Rubberduck.VBEditor.VB6/SafeComWrappers/VB/VBComponents.cs
Expand Up @@ -124,10 +124,7 @@ public void ImportSourceFile(string path)
component = Import(path);
}

var codeString =
File.ReadAllText(path,
Encoding
.Default); //The VBE uses the current ANSI codepage from the windows settings to export and import.
var codeString = File.ReadAllText(path, Encoding.Default); //The VBE uses the current ANSI codepage from the windows settings to export and import.
var codeLines = codeString.Split(new[] {Environment.NewLine}, StringSplitOptions.None);

var nonAttributeLines = codeLines.TakeWhile(line => !line.StartsWith("Attribute")).Count();
Expand Down
49 changes: 47 additions & 2 deletions Rubberduck.VBEditor.VB6/SafeComWrappers/VB/VBControl.cs
@@ -1,3 +1,4 @@
using System;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using VB = Microsoft.Vbe.Interop.VB6;

Expand All @@ -11,10 +12,54 @@ public VBControl(VB.VBControl target, bool rewrapping = false)
{
}

public IProperties Properties => new Properties(IsWrappingNullReference? null : Target.Properties);

public string Name
{
get => IsWrappingNullReference ? string.Empty : Target.Properties.Item("Name").Value.ToString();
set { if (!IsWrappingNullReference) Target.Properties.Item("Name").Value = value; }
get
{
if (IsWrappingNullReference)
{
return string.Empty;
}

using (var properties = this.Properties)
{
foreach (var property in properties)
{
using (property)
{
if (property.Name == "Name")
{
return (string)property.Value;
}
}
}
}
// Should never happen - all VB controls are required to be named.
return null;
}
set
{
if (!IsWrappingNullReference)
{
using (var properties = this.Properties)
{
foreach (var property in properties)
{
using (property)
{
if (property.Name == "Name")
{
property.Value = value;
return;
}
}
}
}
// Should never happen - all VB controls are required to be named.
}
}
}

public override bool Equals(ISafeComWrapper<VB.VBControl> other)
Expand Down
14 changes: 7 additions & 7 deletions Rubberduck.VBEditor.VB6/SafeComWrappers/VB/VBE.cs
Expand Up @@ -125,19 +125,19 @@ public static void SetSelection(IVBProject vbProject, Selection selection, strin
{
using (var components = vbProject.VBComponents)
{
using (var component = components.SingleOrDefault(c => ComponentHasName(c, name)))
using (var component = components.SingleOrDefault(c => ComponentHasName(c, name)))
{
if (component == null || component.IsWrappingNullReference)
{
return;
}
if (component == null || component.IsWrappingNullReference)
{
return;
}

using (var module = component.CodeModule)
{
using (var pane = module.CodePane)
{
pane.Selection = selection;
}
pane.Selection = selection;
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Rubberduck.VBEditor.VB6/SafeComWrappers/VB/VBProject.cs
Expand Up @@ -43,7 +43,10 @@ public EnvironmentMode Mode
{
get
{
if (IsWrappingNullReference) {return 0; }
if (IsWrappingNullReference)
{
return 0;
}

return (EnvironmentMode) EbMode();
}
Expand Down
4 changes: 3 additions & 1 deletion Rubberduck.VBEditor.VB6/SafeComWrappers/VB/Windows.cs
Expand Up @@ -49,7 +49,9 @@ public void ReleaseDockableHosts()

IEnumerator IEnumerable.GetEnumerator()
{
return IsWrappingNullReference ? new List<IEnumerable>().GetEnumerator() : Target.GetEnumerator();
return IsWrappingNullReference
? (IEnumerator)new List<IEnumerable>().GetEnumerator()
: ((IEnumerable<IWindow>)this).GetEnumerator();
}

IEnumerator<IWindow> IEnumerable<IWindow>.GetEnumerator()
Expand Down

0 comments on commit 8ac6a5f

Please sign in to comment.