diff --git a/RetailCoder.VBE/API/ParserState.cs b/RetailCoder.VBE/API/ParserState.cs index f5b6647efe..72007d90f3 100644 --- a/RetailCoder.VBE/API/ParserState.cs +++ b/RetailCoder.VBE/API/ParserState.cs @@ -9,6 +9,7 @@ using Rubberduck.Parsing.Preprocessing; using System.Globalization; using Rubberduck.Parsing.Symbols; +using Rubberduck.VBEditor.SafeComWrappers; using Rubberduck.VBEditor.SafeComWrappers.VBA; namespace Rubberduck.API @@ -16,7 +17,7 @@ namespace Rubberduck.API [ComVisible(true)] public interface IParserState { - void Initialize(VBE vbe); + void Initialize(Microsoft.Vbe.Interop.VBE vbe); void Parse(); void BeginParse(); @@ -57,20 +58,20 @@ public ParserState() UiDispatcher.Initialize(); } - public void Initialize(VBE vbe) + public void Initialize(Microsoft.Vbe.Interop.VBE vbe) { if (_parser != null) { throw new InvalidOperationException("ParserState is already initialized."); } - _vbe = vbe; + _vbe = new VBE(vbe); _state = new RubberduckParserState(null); _state.StateChanged += _state_StateChanged; - Func preprocessorFactory = () => new VBAPreprocessor(double.Parse(vbe.Version, CultureInfo.InvariantCulture)); + Func preprocessorFactory = () => new VBAPreprocessor(double.Parse(_vbe.Version, CultureInfo.InvariantCulture)); _attributeParser = new AttributeParser(new ModuleExporter(), preprocessorFactory); - _parser = new ParseCoordinator(vbe, _state, _attributeParser, preprocessorFactory, + _parser = new ParseCoordinator(_vbe, _state, _attributeParser, preprocessorFactory, new List { new DebugDeclarations(_state), new SpecialFormDeclarations(_state), new FormEventDeclarations(_state), new AliasDeclarations(_state) }); } diff --git a/RetailCoder.VBE/Inspections/QuickFixes/IgnoreOnceQuickFix.cs b/RetailCoder.VBE/Inspections/QuickFixes/IgnoreOnceQuickFix.cs index 5c709e2595..db83309267 100644 --- a/RetailCoder.VBE/Inspections/QuickFixes/IgnoreOnceQuickFix.cs +++ b/RetailCoder.VBE/Inspections/QuickFixes/IgnoreOnceQuickFix.cs @@ -1,6 +1,7 @@ using Antlr4.Runtime; using Rubberduck.Inspections.Abstract; using Rubberduck.Inspections.Resources; +using Rubberduck.Parsing.Grammar; using Rubberduck.Parsing.VBA; using Rubberduck.VBEditor; @@ -15,8 +16,8 @@ public IgnoreOnceQuickFix(ParserRuleContext context, QualifiedSelection selectio : base(context, selection, InspectionsUI.IgnoreOnce) { _inspectionName = inspectionName; - _annotationText = "'" + Parsing.Grammar.Annotations.AnnotationMarker + - Parsing.Grammar.Annotations.IgnoreInspection + ' ' + inspectionName; + _annotationText = "'" + Annotations.AnnotationMarker + + Annotations.IgnoreInspection + ' ' + inspectionName; } public override bool CanFixInModule { get { return false; } } // not quite "once" if applied to entire module @@ -27,10 +28,13 @@ public override void Fix() var module = Selection.QualifiedName.Component.CodeModule; { var insertLine = Selection.Selection.StartLine; - + while (insertLine != 1 && module.GetLines(insertLine - 1, 1).EndsWith(" _")) + { + insertLine--; + } var codeLine = insertLine == 1 ? string.Empty : module.GetLines(insertLine - 1, 1); var annotationText = _annotationText; - var ignoreAnnotation = "'" + Parsing.Grammar.Annotations.AnnotationMarker + Parsing.Grammar.Annotations.IgnoreInspection; + var ignoreAnnotation = "'" + Annotations.AnnotationMarker + Annotations.IgnoreInspection; int commentStart; if (codeLine.HasComment(out commentStart) && codeLine.Substring(commentStart).StartsWith(ignoreAnnotation)) diff --git a/RetailCoder.VBE/UI/Command/FindAllImplementationsCommand.cs b/RetailCoder.VBE/UI/Command/FindAllImplementationsCommand.cs index b79c133821..a7158b582e 100644 --- a/RetailCoder.VBE/UI/Command/FindAllImplementationsCommand.cs +++ b/RetailCoder.VBE/UI/Command/FindAllImplementationsCommand.cs @@ -232,7 +232,7 @@ public void Dispose() { if (_state != null) { - _state.StateChanged += _state_StateChanged; + _state.StateChanged -= _state_StateChanged; } } } diff --git a/RetailCoder.VBE/UI/Command/FindAllReferencesCommand.cs b/RetailCoder.VBE/UI/Command/FindAllReferencesCommand.cs index ebe89ea456..0fc4213fd7 100644 --- a/RetailCoder.VBE/UI/Command/FindAllReferencesCommand.cs +++ b/RetailCoder.VBE/UI/Command/FindAllReferencesCommand.cs @@ -167,7 +167,7 @@ public void Dispose() { if (_state != null) { - _state.StateChanged += _state_StateChanged; + _state.StateChanged -= _state_StateChanged; } } } diff --git a/RetailCoder.VBE/UI/Command/ShowParserErrorsCommand.cs b/RetailCoder.VBE/UI/Command/ShowParserErrorsCommand.cs index 090cd32b73..95ff14fea2 100644 --- a/RetailCoder.VBE/UI/Command/ShowParserErrorsCommand.cs +++ b/RetailCoder.VBE/UI/Command/ShowParserErrorsCommand.cs @@ -129,7 +129,7 @@ public void Dispose() { if (_state != null) { - _state.StateChanged += _state_StateChanged; + _state.StateChanged -= _state_StateChanged; } } } diff --git a/RetailCoder.VBE/UI/UnitTesting/TestExplorerModel.cs b/RetailCoder.VBE/UI/UnitTesting/TestExplorerModel.cs index cb43655e8b..d12c8b50bd 100644 --- a/RetailCoder.VBE/UI/UnitTesting/TestExplorerModel.cs +++ b/RetailCoder.VBE/UI/UnitTesting/TestExplorerModel.cs @@ -10,7 +10,7 @@ namespace Rubberduck.UI.UnitTesting { - public class TestExplorerModel : ViewModelBase + public class TestExplorerModel : ViewModelBase, IDisposable { private readonly IVBE _vbe; private readonly RubberduckParserState _state; @@ -168,5 +168,13 @@ private void OnTestsRefreshed() handler.Invoke(this, EventArgs.Empty); } } + + public void Dispose() + { + if (_state != null) + { + _state.StateChanged -= State_StateChanged; + } + } } } \ No newline at end of file diff --git a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBar.cs b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBar.cs index 67ac8b3cf5..946131ded2 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBar.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBar.cs @@ -29,13 +29,13 @@ public ICommandBarControls Controls public bool IsEnabled { get { return !IsWrappingNullReference && Target.Enabled; } - set { Target.Enabled = value; } + set { if (!IsWrappingNullReference) Target.Enabled = value; } } public int Height { get { return IsWrappingNullReference ? 0 : Target.Height; } - set { Target.Height = value; } + set { if (!IsWrappingNullReference) Target.Height = value; } } public int Index @@ -46,25 +46,25 @@ public int Index public int Left { get { return IsWrappingNullReference ? 0 : Target.Left; } - set { Target.Left = value; } + set { if (!IsWrappingNullReference) Target.Left = value; } } public string Name { get { return IsWrappingNullReference ? string.Empty : Target.Name; } - set { Target.Name = value; } + set { if (!IsWrappingNullReference) Target.Name = value; } } public CommandBarPosition Position { get { return IsWrappingNullReference ? 0 : (CommandBarPosition)Target.Position; } - set { Target.Position = (Microsoft.Office.Core.MsoBarPosition)value; } + set { if (!IsWrappingNullReference) Target.Position = (Microsoft.Office.Core.MsoBarPosition)value; } } public int Top { get { return IsWrappingNullReference ? 0 : Target.Top; } - set { Target.Top = value; } + set { if (!IsWrappingNullReference) Target.Top = value; } } public CommandBarType Type @@ -75,28 +75,28 @@ public CommandBarType Type public bool IsVisible { get { return !IsWrappingNullReference && Target.Visible; } - set { Target.Visible = value; } + set { if (!IsWrappingNullReference) Target.Visible = value; } } public int Width { get { return IsWrappingNullReference ? 0 : Target.Width; } - set { Target.Width = value; } + set { if (!IsWrappingNullReference) Target.Width = value; } } public ICommandBarControl FindControl(int id) { - return new CommandBarControl(Target.FindControl(Id: id)); + return new CommandBarControl(IsWrappingNullReference ? null : Target.FindControl(Id: id)); } public ICommandBarControl FindControl(ControlType type, int id) { - return new CommandBarControl(Target.FindControl(type, id)); + return new CommandBarControl(IsWrappingNullReference ? null : Target.FindControl(type, id)); } public void Delete() { - Target.Delete(); + if (!IsWrappingNullReference) Target.Delete(); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarButton.cs b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarButton.cs index 9dbfc4470e..c2ebb80db1 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarButton.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarButton.cs @@ -52,7 +52,7 @@ public event EventHandler Click private void Target_Click(Microsoft.Office.Core.CommandBarButton ctrl, ref bool cancelDefault) { var handler = _clickHandler; - if (handler == null) + if (handler == null || IsWrappingNullReference) { return; } @@ -72,31 +72,31 @@ private void Target_Click(Microsoft.Office.Core.CommandBarButton ctrl, ref bool public bool IsBuiltInFace { get { return !IsWrappingNullReference && Button.BuiltInFace; } - set { Button.BuiltInFace = value; } + set { if (!IsWrappingNullReference) Button.BuiltInFace = value; } } public int FaceId { get { return IsWrappingNullReference ? 0 : Button.FaceId; } - set { Button.FaceId = value; } + set { if (!IsWrappingNullReference) Button.FaceId = value; } } public string ShortcutText { get { return IsWrappingNullReference ? string.Empty : Button.ShortcutText; } - set { Button.ShortcutText = value; } + set { if (!IsWrappingNullReference) Button.ShortcutText = value; } } public ButtonState State { get { return IsWrappingNullReference ? 0 : (ButtonState)Button.State; } - set { Button.State = (Microsoft.Office.Core.MsoButtonState)value; } + set { if (!IsWrappingNullReference) Button.State = (Microsoft.Office.Core.MsoButtonState)value; } } public ButtonStyle Style { get { return IsWrappingNullReference ? 0 : (ButtonStyle)Button.Style; } - set { Button.Style = (Microsoft.Office.Core.MsoButtonStyle)value; } + set { if (!IsWrappingNullReference) Button.Style = (Microsoft.Office.Core.MsoButtonStyle)value; } } public Image Picture { get; set; } @@ -104,6 +104,8 @@ public ButtonStyle Style public void ApplyIcon() { + if (IsWrappingNullReference) return; + Button.FaceId = 0; if (Picture == null || Mask == null) { diff --git a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarControl.cs b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarControl.cs index e18d8b0926..2f3851d822 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarControl.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarControl.cs @@ -14,7 +14,7 @@ public CommandBarControl(Microsoft.Office.Core.CommandBarControl target) public bool BeginsGroup { get { return !IsWrappingNullReference && Target.BeginGroup; } - set { Target.BeginGroup = value; } + set { if (!IsWrappingNullReference) Target.BeginGroup = value; } } public bool IsBuiltIn @@ -25,25 +25,25 @@ public bool IsBuiltIn public string Caption { get { return IsWrappingNullReference ? string.Empty : Target.Caption; } - set { Target.Caption = value; } + set { if (!IsWrappingNullReference) Target.Caption = value; } } public string DescriptionText { get { return IsWrappingNullReference ? string.Empty : Target.DescriptionText; } - set { Target.DescriptionText = value; } + set { if (!IsWrappingNullReference) Target.DescriptionText = value; } } public bool IsEnabled { get { return !IsWrappingNullReference && Target.Enabled; } - set { Target.Enabled = value; } + set { if (!IsWrappingNullReference) Target.Enabled = value; } } public int Height { get { return Target.Height; } - set { Target.Height = value; } + set { if (!IsWrappingNullReference) Target.Height = value; } } public int Id @@ -64,7 +64,7 @@ public int Left public string OnAction { get { return IsWrappingNullReference ? string.Empty : Target.OnAction; } - set { Target.OnAction = value; } + set { if (!IsWrappingNullReference) Target.OnAction = value; } } public ICommandBar Parent @@ -75,25 +75,25 @@ public ICommandBar Parent public string Parameter { get { return IsWrappingNullReference ? string.Empty : Target.Parameter; } - set { Target.Parameter = value; } + set { if (!IsWrappingNullReference) Target.Parameter = value; } } public int Priority { get { return IsWrappingNullReference ? 0 : Target.Priority; } - set { Target.Priority = value; } + set { if (!IsWrappingNullReference) Target.Priority = value; } } public string Tag { get { return Target.Tag; } - set { Target.Tag = value; } + set { if (!IsWrappingNullReference) Target.Tag = value; } } public string TooltipText { get { return IsWrappingNullReference ? string.Empty : Target.TooltipText; } - set { Target.TooltipText = value; } + set { if (!IsWrappingNullReference) Target.TooltipText = value; } } public int Top @@ -109,28 +109,28 @@ public ControlType Type public bool IsVisible { get { return !IsWrappingNullReference && Target.Visible; } - set { Target.Visible = value; } + set { if (!IsWrappingNullReference) Target.Visible = value; } } public int Width { get { return IsWrappingNullReference ? 0 : Target.Width; } - set { Target.Width = value; } + set { if (!IsWrappingNullReference) Target.Width = value; } } public bool IsPriorityDropped { - get { return Target.IsPriorityDropped; } + get { return (!IsWrappingNullReference) && Target.IsPriorityDropped; } } public void Delete() { - Target.Delete(true); + if (!IsWrappingNullReference) Target.Delete(true); } public void Execute() { - Target.Execute(); + if (!IsWrappingNullReference) Target.Execute(); } public override bool Equals(ISafeComWrapper other) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarControls.cs b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarControls.cs index 3b1ed9f858..71c0175c68 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarControls.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarControls.cs @@ -25,27 +25,32 @@ public ICommandBar Parent public ICommandBarControl this[object index] { - get { return new CommandBarControl(Target[index]); } + get { return new CommandBarControl(!IsWrappingNullReference ? Target[index] : null); } } public ICommandBarControl Add(ControlType type) { - return new CommandBarControl(Target.Add(type, Temporary:true)); + return new CommandBarControl(IsWrappingNullReference ? null : Target.Add(type, Temporary:true)); } public ICommandBarControl Add(ControlType type, int before) { - return new CommandBarControl(Target.Add(type, Before:before, Temporary:true)); + return new CommandBarControl(IsWrappingNullReference ? null : Target.Add(type, Before: before, Temporary: true)); } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new CommandBarControl((Microsoft.Office.Core.CommandBarControl)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new CommandBarControl(null)) + : new ComWrapperEnumerator(Target, + o => new CommandBarControl((Microsoft.Office.Core.CommandBarControl) o)); } IEnumerator IEnumerable.GetEnumerator() { - return ((IEnumerable)this).GetEnumerator(); + return IsWrappingNullReference + ? new List().GetEnumerator() + : ((IEnumerable) this).GetEnumerator(); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarPopup.cs b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarPopup.cs index 291a82ce2c..3d12898e4f 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarPopup.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBarPopup.cs @@ -11,12 +11,12 @@ public CommandBarPopup(Microsoft.Office.Core.CommandBarPopup target) public static ICommandBarPopup FromCommandBarControl(ICommandBarControl control) { - return new CommandBarPopup((Microsoft.Office.Core.CommandBarPopup)control.Target); + return new CommandBarPopup(control.Target as Microsoft.Office.Core.CommandBarPopup); } private Microsoft.Office.Core.CommandBarPopup Popup { - get { return (Microsoft.Office.Core.CommandBarPopup)Target; } + get { return Target as Microsoft.Office.Core.CommandBarPopup; } } public ICommandBar CommandBar diff --git a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBars.cs b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBars.cs index f1c1585d2b..72935468f7 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBars.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/Office.Core/CommandBars.cs @@ -18,13 +18,13 @@ public CommandBars(Microsoft.Office.Core.CommandBars target) public ICommandBar Add(string name) { DeleteExistingCommandBar(name); - return new CommandBar(Target.Add(name, Temporary:true)); + return new CommandBar(IsWrappingNullReference ? null : Target.Add(name, Temporary: true)); } public ICommandBar Add(string name, CommandBarPosition position) { DeleteExistingCommandBar(name); - return new CommandBar(Target.Add(name, position, Temporary: true)); + return new CommandBar(IsWrappingNullReference ? null : Target.Add(name, position, Temporary: true)); } private void DeleteExistingCommandBar(string name) @@ -46,17 +46,20 @@ private void DeleteExistingCommandBar(string name) public ICommandBarControl FindControl(int id) { - return new CommandBarControl(Target.FindControl(Id:id)); + return new CommandBarControl(IsWrappingNullReference ? null : Target.FindControl(Id: id)); } public ICommandBarControl FindControl(ControlType type, int id) { - return new CommandBarControl(Target.FindControl(type, id)); + return new CommandBarControl(IsWrappingNullReference ? null : Target.FindControl(type, id)); } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new CommandBar((Microsoft.Office.Core.CommandBar)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new CommandBar(null)) + : new ComWrapperEnumerator(Target, + o => new CommandBar((Microsoft.Office.Core.CommandBar) o)); } public IEnumerator GetEnumerator() @@ -71,7 +74,7 @@ public int Count public ICommandBar this[object index] { - get { return new CommandBar(Target[index]); } + get { return new CommandBar(IsWrappingNullReference ? null : Target[index]); } } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/AddIn.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/AddIn.cs index 66b621b010..d24794419e 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/AddIn.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/AddIn.cs @@ -32,20 +32,20 @@ public IAddIns Collection public string Description { - get { return IsWrappingNullReference ? string.Empty : Target.Description; } - set { Target.Description = value; } + get { return IsWrappingNullReference ? string.Empty : Target.Description; } + set { if (!IsWrappingNullReference) Target.Description = value; } } public bool Connect { get { return !IsWrappingNullReference && Target.Connect; } - set { Target.Connect = value; } + set { if (!IsWrappingNullReference) Target.Connect = value; } } public object Object // definitely leaks a COM object { get { return IsWrappingNullReference ? null : Target.Object; } - set { Target.Object = value; } + set { if (!IsWrappingNullReference) Target.Object = value; } } public override bool Equals(ISafeComWrapper other) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/AddIns.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/AddIns.cs index 5bd92626d1..cbdae6b1b7 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/AddIns.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/AddIns.cs @@ -66,12 +66,14 @@ public override int GetHashCode() IEnumerator IEnumerable.GetEnumerator() { - return Target.GetEnumerator(); + return IsWrappingNullReference ? new List().GetEnumerator() : Target.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new AddIn((VB.AddIn)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new AddIn(null)) + : new ComWrapperEnumerator(Target, o => new AddIn((VB.AddIn) o)); } } } \ No newline at end of file diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodeModule.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodeModule.cs index ba04730d83..301e0a23b4 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodeModule.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodeModule.cs @@ -44,12 +44,12 @@ public int CountOfLines public string Name { get { return IsWrappingNullReference ? string.Empty : Target.Name; } - set { Target.Name = value; } + set { if (!IsWrappingNullReference) Target.Name = value; } } public string GetLines(int startLine, int count) { - return Target.get_Lines(startLine, count); + return IsWrappingNullReference ? string.Empty : Target.get_Lines(startLine, count); } /// @@ -59,7 +59,7 @@ public string GetLines(int startLine, int count) /// public string GetLines(Selection selection) { - return GetLines(selection.StartLine, selection.LineCount); + return IsWrappingNullReference ? string.Empty : GetLines(selection.StartLine, selection.LineCount); } /// @@ -68,6 +68,7 @@ public string GetLines(Selection selection) /// public void DeleteLines(Selection selection) { + if (IsWrappingNullReference) return; DeleteLines(selection.StartLine, selection.LineCount); } @@ -82,11 +83,12 @@ public void DeleteLines(Selection selection) public string Content() { - return Target.CountOfLines == 0 ? string.Empty : GetLines(1, CountOfLines); + return IsWrappingNullReference || Target.CountOfLines == 0 ? string.Empty : GetLines(1, CountOfLines); } public void Clear() { + if (IsWrappingNullReference) return; if (Target.CountOfLines > 0) { Target.DeleteLines(1, CountOfLines); @@ -107,31 +109,38 @@ public string ContentHash() public void AddFromString(string content) { + if (IsWrappingNullReference) return; Target.AddFromString(content); } public void AddFromFile(string path) { + if (IsWrappingNullReference) return; Target.AddFromFile(path); } public void InsertLines(int line, string content) { + if (IsWrappingNullReference) return; Target.InsertLines(line, content); } public void DeleteLines(int startLine, int count = 1) { + if (IsWrappingNullReference) return; Target.DeleteLines(startLine, count); } public void ReplaceLine(int line, string content) { + if (IsWrappingNullReference) return; Target.ReplaceLine(line, content); } public Selection? Find(string target, bool wholeWord = false, bool matchCase = false, bool patternSearch = false) { + if (IsWrappingNullReference) return null; + var startLine = 0; var startColumn = 0; var endLine = 0; @@ -144,27 +153,29 @@ public void ReplaceLine(int line, string content) public int GetProcStartLine(string procName, ProcKind procKind) { - return Target.get_ProcStartLine(procName, (vbext_ProcKind)procKind); + return IsWrappingNullReference ? 0 : Target.get_ProcStartLine(procName, (vbext_ProcKind)procKind); } public int GetProcBodyStartLine(string procName, ProcKind procKind) { - return Target.get_ProcBodyLine(procName, (vbext_ProcKind)procKind); + return IsWrappingNullReference ? 0 : Target.get_ProcBodyLine(procName, (vbext_ProcKind)procKind); } public int GetProcCountLines(string procName, ProcKind procKind) { - return Target.get_ProcCountLines(procName, (vbext_ProcKind)procKind); + return IsWrappingNullReference ? 0 : Target.get_ProcCountLines(procName, (vbext_ProcKind)procKind); } public string GetProcOfLine(int line) { + if (IsWrappingNullReference) return string.Empty; vbext_ProcKind procKind; return Target.get_ProcOfLine(line, out procKind); } public ProcKind GetProcKindOfLine(int line) { + if (IsWrappingNullReference) return 0; vbext_ProcKind procKind; Target.get_ProcOfLine(line, out procKind); return (ProcKind)procKind; diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodePane.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodePane.cs index a48e9de9aa..ec9ab7c663 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodePane.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodePane.cs @@ -29,7 +29,7 @@ public IWindow Window public int TopLine { get { return IsWrappingNullReference ? 0 : Target.TopLine; } - set { Target.TopLine = value; } + set { if (!IsWrappingNullReference) Target.TopLine = value; } } public int CountOfVisibleLines @@ -50,11 +50,13 @@ public CodePaneView CodePaneView public Selection Selection { get { return GetSelection(); } - set { SetSelection(value.StartLine, value.StartColumn, value.EndLine, value.EndColumn); } + set { if (!IsWrappingNullReference) SetSelection(value.StartLine, value.StartColumn, value.EndLine, value.EndColumn); } } private Selection GetSelection() { + if (IsWrappingNullReference) return new Selection(0, 0, 0, 0); + int startLine; int startColumn; int endLine; @@ -90,12 +92,14 @@ private Selection GetSelection() private void SetSelection(int startLine, int startColumn, int endLine, int endColumn) { + if (IsWrappingNullReference) return; Target.SetSelection(startLine, startColumn, endLine, endColumn); ForceFocus(); } private void ForceFocus() { + if (IsWrappingNullReference) return; Show(); var window = VBE.MainWindow; @@ -118,6 +122,7 @@ private void ForceFocus() public void Show() { + if (IsWrappingNullReference) return; Target.Show(); } diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodePanes.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodePanes.cs index e09b554bf8..e21e38bf17 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodePanes.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/CodePanes.cs @@ -30,22 +30,26 @@ public IVBE VBE public ICodePane Current { get { return new CodePane(IsWrappingNullReference ? null : Target.Current); } - set { Target.Current = (VB.CodePane)value.Target;} + set { if (!IsWrappingNullReference) Target.Current = (VB.CodePane)value.Target; } } public ICodePane this[object index] { - get { return new CodePane(Target.Item(index)); } + get { return new CodePane(IsWrappingNullReference ? null : Target.Item(index)); } } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new CodePane((VB.CodePane)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new CodePane(null)) + : new ComWrapperEnumerator(Target, o => new CodePane((VB.CodePane) o)); } IEnumerator IEnumerable.GetEnumerator() { - return ((IEnumerable)this).GetEnumerator(); + return IsWrappingNullReference + ? (IEnumerator) new List().GetEnumerator() + : ((IEnumerable) this).GetEnumerator(); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Control.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Control.cs index 905be2e238..c6dbdb159d 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Control.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Control.cs @@ -14,7 +14,7 @@ public Control(VB.Forms.Control target) public string Name { get { return IsWrappingNullReference ? string.Empty : Target.Name; } - set { Target.Name = value; } + set { if (!IsWrappingNullReference) Target.Name = value; } } public override bool Equals(ISafeComWrapper other) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Controls.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Controls.cs index 0c9e5eccfb..ab859f2fa6 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Controls.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Controls.cs @@ -20,18 +20,22 @@ public int Count public IControl this[object index] { - get { return new Control((VB.Forms.Control) Target.Item(index)); } + get { return IsWrappingNullReference ? new Control(null) : new Control((VB.Forms.Control) Target.Item(index)); } } IEnumerator IEnumerable.GetEnumerator() { // soft-casting because ImageClass doesn't implement IControl - return new ComWrapperEnumerator(Target, o => new Control(o as VB.Forms.Control)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new Control(null)) + : new ComWrapperEnumerator(Target, o => new Control(o as VB.Forms.Control)); } IEnumerator IEnumerable.GetEnumerator() { - return ((IEnumerable)this).GetEnumerator(); + return IsWrappingNullReference + ? (IEnumerator) new List().GetEnumerator() + : ((IEnumerable) this).GetEnumerator(); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/LinkedWindows.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/LinkedWindows.cs index a4151ede1d..2eef526758 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/LinkedWindows.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/LinkedWindows.cs @@ -29,27 +29,31 @@ public IWindow Parent public IWindow this[object index] { - get { return new Window(Target.Item(index)); } + get { return new Window(IsWrappingNullReference ? null : Target.Item(index)); } } public void Remove(IWindow window) { + if (IsWrappingNullReference) return; Target.Remove(((Window)window).Target); } public void Add(IWindow window) { + if (IsWrappingNullReference) return; Target.Add(((Window)window).Target); } IEnumerator IEnumerable.GetEnumerator() { - return Target.GetEnumerator(); + return IsWrappingNullReference ? new List().GetEnumerator() : Target.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new Window((VB.Window)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new Window(null)) + : new ComWrapperEnumerator(Target, o => new Window((VB.Window) o)); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Properties.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Properties.cs index 10e3de0271..eb2e8968a7 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Properties.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Properties.cs @@ -34,17 +34,21 @@ public object Parent public IProperty this[object index] { - get { return new Property(Target.Item(index)); } + get { return new Property(IsWrappingNullReference ? null : Target.Item(index)); } } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new Property((VB.Property)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new Property(null)) + : new ComWrapperEnumerator(Target, o => new Property((VB.Property) o)); } IEnumerator IEnumerable.GetEnumerator() { - return ((IEnumerable)this).GetEnumerator(); + return IsWrappingNullReference + ? (IEnumerator) new List().GetEnumerator() + : ((IEnumerable) this).GetEnumerator(); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Property.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Property.cs index 42942ce5d1..4fa9c9fdac 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Property.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Property.cs @@ -45,7 +45,7 @@ public IVBE VBE public object Value { get { return IsWrappingNullReference ? null : Target.Value; } - set { Target.Value = value; } + set { if (!IsWrappingNullReference) Target.Value = value; } } /// @@ -53,12 +53,12 @@ public object Value /// public object GetIndexedValue(object index1, object index2 = null, object index3 = null, object index4 = null) { - return Target.get_IndexedValue(index1, index2, index3, index4); + return IsWrappingNullReference ? null : Target.get_IndexedValue(index1, index2, index3, index4); } public void SetIndexedValue(object value, object index1, object index2 = null, object index3 = null, object index4 = null) { - Target.set_IndexedValue(index1, index2, index3, index4, value); + if (!IsWrappingNullReference) Target.set_IndexedValue(index1, index2, index3, index4, value); } /// @@ -67,7 +67,7 @@ public void SetIndexedValue(object value, object index1, object index2 = null, o public object Object { get { return IsWrappingNullReference ? null : Target.Object; } - set { Target.Object = value; } + set { if (!IsWrappingNullReference) Target.Object = value; } } public override bool Equals(ISafeComWrapper other) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/References.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/References.cs index cf34302595..268fd28797 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/References.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/References.cs @@ -77,32 +77,37 @@ private void Target_ItemAdded(Microsoft.Vbe.Interop.Reference reference) public IReference this[object index] { - get { return new Reference(Target.Item(index)); } + get { return new Reference(IsWrappingNullReference ? null : Target.Item(index)); } } public IReference AddFromGuid(string guid, int major, int minor) { - return new Reference(Target.AddFromGuid(guid, major, minor)); + return new Reference(IsWrappingNullReference ? null : Target.AddFromGuid(guid, major, minor)); } public IReference AddFromFile(string path) { - return new Reference(Target.AddFromFile(path)); + return new Reference(IsWrappingNullReference ? null : Target.AddFromFile(path)); } public void Remove(IReference reference) { + if (IsWrappingNullReference) return; Target.Remove(((ISafeComWrapper)reference).Target); } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new Reference((VB.Reference)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new Reference(null)) + : new ComWrapperEnumerator(Target, o => new Reference((VB.Reference) o)); } IEnumerator IEnumerable.GetEnumerator() { - return ((IEnumerable)this).GetEnumerator(); + return IsWrappingNullReference + ? (IEnumerator) new List().GetEnumerator() + : ((IEnumerable) this).GetEnumerator(); } private bool _isReleased; diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponent.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponent.cs index 18b98bb0d9..e97ab44b1e 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponent.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponent.cs @@ -53,7 +53,7 @@ public string DesignerId public string Name { get { return IsWrappingNullReference ? string.Empty : Target.Name; } - set { Target.Name = value; } + set { if (!IsWrappingNullReference) Target.Name = value; } } private string SafeName diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponents.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponents.cs index ffa2e82fe7..17a0ecfa28 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponents.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBComponents.cs @@ -42,37 +42,41 @@ public IVBComponent this[object index] public void Remove(IVBComponent item) { - Target.Remove((VB.VBComponent) item.Target); + if (!IsWrappingNullReference) Target.Remove((VB.VBComponent)item.Target); } public IVBComponent Add(ComponentType type) { - return new VBComponent(Target.Add((VB.vbext_ComponentType) type)); + return new VBComponent(IsWrappingNullReference ? null : Target.Add((VB.vbext_ComponentType)type)); } public IVBComponent Import(string path) { - return new VBComponent(Target.Import(path)); + return new VBComponent(IsWrappingNullReference ? null : Target.Import(path)); } public IVBComponent AddCustom(string progId) { - return new VBComponent(Target.AddCustom(progId)); + return new VBComponent(IsWrappingNullReference ? null : Target.AddCustom(progId)); } public IVBComponent AddMTDesigner(int index = 0) { - return new VBComponent(Target.AddMTDesigner(index)); + return new VBComponent(IsWrappingNullReference ? null : Target.AddMTDesigner(index)); } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new VBComponent((VB.VBComponent)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new VBComponent(null)) + : new ComWrapperEnumerator(Target, o => new VBComponent((VB.VBComponent) o)); } IEnumerator IEnumerable.GetEnumerator() { - return ((IEnumerable)this).GetEnumerator(); + return IsWrappingNullReference + ? (IEnumerator) new List().GetEnumerator() + : ((IEnumerable) this).GetEnumerator(); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBE.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBE.cs index 5d8ae4bbee..b6eaa779c9 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBE.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBE.cs @@ -23,13 +23,13 @@ public string Version public ICodePane ActiveCodePane { get { return new CodePane(IsWrappingNullReference ? null : Target.ActiveCodePane); } - set { Target.ActiveCodePane = (VB.CodePane)value.Target; } + set { if (!IsWrappingNullReference) Target.ActiveCodePane = (VB.CodePane)value.Target; } } public IVBProject ActiveVBProject { get { return new VBProject(IsWrappingNullReference ? null : Target.ActiveVBProject); } - set { Target.ActiveVBProject = (VB.VBProject)value.Target; } + set { if (!IsWrappingNullReference) Target.ActiveVBProject = (VB.VBProject)value.Target; } } public IWindow ActiveWindow diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBProject.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBProject.cs index 4ca5d9717e..4ea33e995d 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBProject.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBProject.cs @@ -27,19 +27,19 @@ public IApplication Parent public string HelpFile { get { return IsWrappingNullReference ? string.Empty : Target.HelpFile; } - set { Target.HelpFile = value; } + set { if (!IsWrappingNullReference) Target.HelpFile = value; } } public string Description { get { return IsWrappingNullReference ? string.Empty : Target.Description; } - set { Target.Description = value; } + set { if (!IsWrappingNullReference) Target.Description = value; } } public string Name { get { return IsWrappingNullReference ? string.Empty : Target.Name; } - set { Target.Name = value; } + set { if (!IsWrappingNullReference) Target.Name = value; } } public EnvironmentMode Mode @@ -105,12 +105,12 @@ public IVBE VBE public void SaveAs(string fileName) { - Target.SaveAs(fileName); + if (!IsWrappingNullReference) Target.SaveAs(fileName); } public void MakeCompiledFile() { - Target.MakeCompiledFile(); + if (!IsWrappingNullReference) Target.MakeCompiledFile(); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBProjects.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBProjects.cs index 7730ece782..4c39d42247 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBProjects.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/VBProjects.cs @@ -34,11 +34,12 @@ public IVBE Parent public IVBProject Add(ProjectType type) { - return new VBProject(Target.Add((VB.vbext_ProjectType)type)); + return new VBProject(IsWrappingNullReference ? null : Target.Add((VB.vbext_ProjectType)type)); } public void Remove(IVBProject project) { + if (IsWrappingNullReference) return; Target.Remove((VB.VBProject) project.Target); } @@ -65,22 +66,26 @@ public IConnectionPoint ConnectionPoint public IVBProject Open(string path) { - return new VBProject(Target.Open(path)); + return new VBProject(IsWrappingNullReference ? null : Target.Open(path)); } public IVBProject this[object index] { - get { return new VBProject(Target.Item(index)); } + get { return new VBProject(IsWrappingNullReference ? null : Target.Item(index)); } } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new VBProject((VB.VBProject)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new VBProject(null)) + : new ComWrapperEnumerator(Target, o => new VBProject((VB.VBProject) o)); } IEnumerator IEnumerable.GetEnumerator() { - return ((IEnumerable)this).GetEnumerator(); + return IsWrappingNullReference + ? (IEnumerator) new List().GetEnumerator() + : ((IEnumerable) this).GetEnumerator(); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Window.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Window.cs index 569bd76580..ed68b9541e 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Window.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Window.cs @@ -41,31 +41,31 @@ public string Caption public bool IsVisible { get { return !IsWrappingNullReference && Target.Visible; } - set { Target.Visible = value; } + set { if (!IsWrappingNullReference) Target.Visible = value; } } public int Left { get { return IsWrappingNullReference ? 0 : Target.Left; } - set { Target.Left = value; } + set { if (!IsWrappingNullReference) Target.Left = value; } } public int Top { get { return IsWrappingNullReference ? 0 : Target.Top; } - set { Target.Top = value; } + set { if (!IsWrappingNullReference) Target.Top = value; } } public int Width { get { return IsWrappingNullReference ? 0 : Target.Width; } - set { Target.Width = value; } + set { if (!IsWrappingNullReference) Target.Width = value; } } public int Height { get { return IsWrappingNullReference ? 0 : Target.Height; } - set { Target.Height = value; } + set { if (!IsWrappingNullReference) Target.Height = value; } } public WindowState WindowState @@ -90,27 +90,27 @@ public IWindow LinkedWindowFrame public void Close() { - Target.Close(); + if (!IsWrappingNullReference) Target.Close(); } public void SetFocus() { - Target.SetFocus(); + if (!IsWrappingNullReference) Target.SetFocus(); } public void SetKind(WindowKind eKind) { - Target.SetKind((vbext_WindowType)eKind); + if (!IsWrappingNullReference) Target.SetKind((vbext_WindowType)eKind); } public void Detach() { - Target.Detach(); + if (!IsWrappingNullReference) Target.Detach(); } public void Attach(int lWindowHandle) { - Target.Attach(lWindowHandle); + if (!IsWrappingNullReference) Target.Attach(lWindowHandle); } public override void Release(bool final = false) diff --git a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Windows.cs b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Windows.cs index a452d48e51..2096d9bf94 100644 --- a/Rubberduck.VBEEditor/SafeComWrappers/VBA/Windows.cs +++ b/Rubberduck.VBEEditor/SafeComWrappers/VBA/Windows.cs @@ -14,7 +14,7 @@ public Windows(VB.Windows windows) public int Count { - get { return Target.Count; } + get { return IsWrappingNullReference ? 0 : Target.Count; } } public IVBE VBE @@ -29,11 +29,12 @@ public IApplication Parent public IWindow this[object index] { - get { return new Window(Target.Item(index)); } + get { return new Window(IsWrappingNullReference ? null : Target.Item(index)); } } public ToolWindowInfo CreateToolWindow(IAddIn addInInst, string progId, string caption, string guidPosition) { + if (IsWrappingNullReference) return new ToolWindowInfo(null, null); object control = null; var window = new Window(Target.CreateToolWindow((VB.AddIn)addInInst.Target, progId, caption, guidPosition, ref control)); return new ToolWindowInfo(window, control); @@ -41,12 +42,14 @@ public ToolWindowInfo CreateToolWindow(IAddIn addInInst, string progId, string c IEnumerator IEnumerable.GetEnumerator() { - return Target.GetEnumerator(); + return IsWrappingNullReference ? new List().GetEnumerator() : Target.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { - return new ComWrapperEnumerator(Target, o => new Window((VB.Window)o)); + return IsWrappingNullReference + ? new ComWrapperEnumerator(null, o => new Window(null)) + : new ComWrapperEnumerator(Target, o => new Window((VB.Window) o)); } public override void Release(bool final = false)