Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
70a7054
modified quick fix to use a regular expresssion
BZngr Feb 13, 2017
ffb411b
Synced with latest from remote master. Merge branch 'rubberduck-vba/…
BZngr Feb 13, 2017
eb6f24f
removed use of regular expression in PassParameterByReferenceQuickFix
BZngr Feb 14, 2017
47aeed7
Strenghtened the ByRef QuickFix to handle line continuations etc
BZngr Feb 14, 2017
c9f334a
Synced with remote master. Merge branch 'rubberduck-vba/next' into next
BZngr Feb 14, 2017
a3eb575
updated with changes that came from last sync
BZngr Feb 15, 2017
d4a5235
Reworked QuickFix to use ArgContext
BZngr Feb 15, 2017
aade1ca
Change GC suppression on DockableWindowHost. Should be safer.
comintern Feb 16, 2017
5216f76
Get status bar working (partially - CodePane only). First step in bur…
comintern Feb 16, 2017
54b7032
Merge branch 'next' of https://github.com/comintern/Rubberduck.git
comintern Feb 16, 2017
897be10
Merge branch 'next' into next
comintern Feb 16, 2017
4c2e530
Merge branch 'next' of https://github.com/BZngr/Rubberduck into next
BZngr Feb 16, 2017
b48356f
Fix stupid error that left Hwnd property unassigned.
comintern Feb 16, 2017
f9a115d
Can't release the controls from DockableToolwindowPresenter.
comintern Feb 16, 2017
f8272c6
Add initial focus change events - may be a bit unstable ATM.
comintern Feb 16, 2017
a9a5336
Merge branch 'next' of https://github.com/comintern/Rubberduck.git
comintern Feb 16, 2017
0c76eea
Merge pull request #2693 from comintern/next
retailcoder Feb 16, 2017
728a8e4
Merge branch 'next' of https://github.com/BZngr/Rubberduck into next
BZngr Feb 16, 2017
e567400
Recover lost updates for SubstmtContext to ArgListContext
BZngr Feb 16, 2017
b17da1e
Clean-up, renaming
BZngr Feb 16, 2017
7c5f915
more renaming
BZngr Feb 16, 2017
c82459e
Updated per intial review of ByRef QuickFix comments
BZngr Feb 16, 2017
14e24e8
removed the bracketed parameter code and test
BZngr Feb 16, 2017
4f5c05e
Merge pull request #2694 from BZngr/next
retailcoder Feb 16, 2017
6b7f8fc
Fix bracket escaping in end of line comments. Closes #2696
comintern Feb 17, 2017
dc8d33f
Indenter cleanup - commented code, dead file.
comintern Feb 17, 2017
78e4140
Clean up superfluous event code in parser state.
comintern Feb 17, 2017
651ee88
Fixed thread locking in SubclassingWindow - no need to throw in dtor.
comintern Feb 17, 2017
58b650b
Fixes for focus events to catch designer windows. Still needs work in…
comintern Feb 17, 2017
8632df5
Merge branch 'next' into next
retailcoder Feb 17, 2017
1c2a2a7
Merge pull request #2698 from comintern/next
retailcoder Feb 17, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 54 additions & 12 deletions RetailCoder.VBE/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
using System.Windows.Forms;
using Rubberduck.UI.Command;
using Rubberduck.UI.Command.MenuItems.CommandBars;
using Rubberduck.VBEditor.Events;
using Rubberduck.VBEditor.SafeComWrappers;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
using Rubberduck.VBEditor.SafeComWrappers.MSForms;
using Rubberduck.VBEditor.SafeComWrappers.Office.Core.Abstract;
using Rubberduck.VersionCheck;
using Application = System.Windows.Forms.Application;

namespace Rubberduck
{
Expand Down Expand Up @@ -61,7 +63,9 @@ public App(IVBE vbe,
_version = version;
_checkVersionCommand = checkVersionCommand;

_hooks.MessageReceived += _hooks_MessageReceived;
VBEEvents.SelectionChanged += _vbe_SelectionChanged;
VBEEvents.WindowFocusChange += _vbe_FocusChanged;

_configService.SettingsChanged += _configService_SettingsChanged;
_parser.State.StateChanged += Parser_StateChanged;
_parser.State.StatusMessageUpdate += State_StatusMessageUpdate;
Expand All @@ -81,17 +85,57 @@ private void State_StatusMessageUpdate(object sender, RubberduckStatusMessageEve
_stateBar.SetStatusLabelCaption(message, _parser.State.ModuleExceptions.Count);
}

private void _hooks_MessageReceived(object sender, HookEventArgs e)
private void _vbe_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
RefreshSelection();
RefreshSelection(e.CodePane);
}

private void _vbe_FocusChanged(object sender, WindowChangedEventArgs e)
{
if (e.EventType == WindowChangedEventArgs.FocusType.GotFocus)
{
switch (e.Window.Type)
{
case WindowKind.Designer:
RefreshSelection(e.Window);
break;
case WindowKind.CodeWindow:
RefreshSelection(e.CodePane);
break;
}
}
}

private ParserState _lastStatus;
private Declaration _lastSelectedDeclaration;

private void RefreshSelection()
private void RefreshSelection(ICodePane pane)
{
Declaration selectedDeclaration = null;
if (!pane.IsWrappingNullReference)
{
selectedDeclaration = _parser.State.FindSelectedDeclaration(pane);
var refCount = selectedDeclaration == null ? 0 : selectedDeclaration.References.Count();
var caption = _stateBar.GetContextSelectionCaption(_vbe.ActiveCodePane, selectedDeclaration);
_stateBar.SetContextSelectionCaption(caption, refCount);
}

var currentStatus = _parser.State.Status;
if (ShouldEvaluateCanExecute(selectedDeclaration, currentStatus))
{
_appMenus.EvaluateCanExecute(_parser.State);
_stateBar.EvaluateCanExecute(_parser.State);
}

_lastStatus = currentStatus;
_lastSelectedDeclaration = selectedDeclaration;
}

private void RefreshSelection(IWindow window)
{
if (window.IsWrappingNullReference || window.Type != WindowKind.Designer)
{
return;
}
var caption = String.Empty;
var refCount = 0;

Expand All @@ -103,7 +147,7 @@ private void RefreshSelection()

//TODO - I doubt this is the best way to check if the SelectedVBComponent and the ActiveCodePane are the same component.
if (windowKind == WindowKind.CodeWindow || (!_vbe.SelectedVBComponent.IsWrappingNullReference
&& component.ParentProject.ProjectId == pane.CodeModule.Parent.ParentProject.ProjectId
&& component.ParentProject.ProjectId == pane.CodeModule.Parent.ParentProject.ProjectId
&& component.Name == pane.CodeModule.Parent.Name))
{
selectedDeclaration = _parser.State.FindSelectedDeclaration(pane);
Expand All @@ -120,13 +164,13 @@ private void RefreshSelection()
{
//The user might have selected the project node in Project Explorer
//If they've chosen a folder, we'll return the project anyway
caption = !_vbe.ActiveVBProject.IsWrappingNullReference
caption = !_vbe.ActiveVBProject.IsWrappingNullReference
? _vbe.ActiveVBProject.Name
: null;
}
else
{
caption = component.Type == ComponentType.UserForm && component.HasOpenDesigner
caption = component.Type == ComponentType.UserForm && component.HasOpenDesigner
? GetComponentControlsCaption(component)
: String.Format("{0}.{1} ({2})", component.ParentProject.Name, component.Name, component.Type);
}
Expand Down Expand Up @@ -322,10 +366,8 @@ public void Dispose()
_parser.State.StatusMessageUpdate -= State_StatusMessageUpdate;
}

if (_hooks != null)
{
_hooks.MessageReceived -= _hooks_MessageReceived;
}
VBEEvents.SelectionChanged += _vbe_SelectionChanged;
VBEEvents.WindowFocusChange += _vbe_FocusChanged;

if (_configService != null)
{
Expand Down
2 changes: 1 addition & 1 deletion RetailCoder.VBE/Common/WinAPI/RawInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Rubberduck.UI;
using Rubberduck.VBEditor.WindowsApi;

namespace Rubberduck.Common.WinAPI
{
Expand Down
31 changes: 0 additions & 31 deletions RetailCoder.VBE/Common/WinAPI/User32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,36 +190,5 @@ public static class User32
public delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);
[DllImport("user32.dll")]
public static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);

/// <summary>
/// A helper function that returns <c>true</c> when the specified handle is that of the foreground window.
/// </summary>
/// <param name="mainWindowHandle">The handle for the VBE's MainWindow.</param>
/// <returns></returns>
public static bool IsVbeWindowActive(IntPtr mainWindowHandle)
{
uint vbeThread;
GetWindowThreadProcessId(mainWindowHandle, out vbeThread);

uint hThread;
GetWindowThreadProcessId(GetForegroundWindow(), out hThread);

return (IntPtr)hThread == (IntPtr)vbeThread;
}

public enum WindowType
{
Indeterminate,
VbaWindow,
DesignerWindow
}

public static WindowType ToWindowType(this IntPtr hwnd)
{
var name = new StringBuilder(128);
GetClassName(hwnd, name, name.Capacity);
WindowType id;
return Enum.TryParse(name.ToString(), out id) ? id : WindowType.Indeterminate;
}
}
}
8 changes: 6 additions & 2 deletions RetailCoder.VBE/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using NLog;
using Rubberduck.Settings;
using Rubberduck.SettingsProvider;
using Rubberduck.VBEditor.Events;
using Rubberduck.VBEditor.SafeComWrappers.Abstract;

namespace Rubberduck
Expand Down Expand Up @@ -53,8 +54,9 @@ public void OnConnection(object Application, ext_ConnectMode ConnectMode, object
{
if (Application is Microsoft.Vbe.Interop.VBE)
{
var vbe = (Microsoft.Vbe.Interop.VBE) Application;
var vbe = (Microsoft.Vbe.Interop.VBE) Application;
_ide = new VBEditor.SafeComWrappers.VBA.VBE(vbe);
VBEEvents.HookEvents(_ide);

var addin = (Microsoft.Vbe.Interop.AddIn)AddInInst;
_addin = new VBEditor.SafeComWrappers.VBA.AddIn(addin) { Object = this };
Expand Down Expand Up @@ -87,7 +89,7 @@ public void OnConnection(object Application, ext_ConnectMode ConnectMode, object

Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
{
var folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
var assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");
if (!File.Exists(assemblyPath))
{
Expand Down Expand Up @@ -219,6 +221,8 @@ private void Startup()

private void ShutdownAddIn()
{
VBEEvents.UnhookEvents();

var currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyResolve -= LoadFromSameFolder;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using Rubberduck.Inspections.Abstract;
using Rubberduck.Inspections.Resources;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Symbols;
using Rubberduck.VBEditor;
using System.Text.RegularExpressions;
using System.Linq;

namespace Rubberduck.Inspections.QuickFixes
{
Expand All @@ -12,31 +14,75 @@ namespace Rubberduck.Inspections.QuickFixes
/// </summary>
public class PassParameterByReferenceQuickFix : QuickFixBase
{
public PassParameterByReferenceQuickFix(ParserRuleContext context, QualifiedSelection selection)
: base(context, selection, InspectionsUI.PassParameterByReferenceQuickFix)
private Declaration _target;

public PassParameterByReferenceQuickFix(Declaration target, QualifiedSelection selection)
: base(target.Context, selection, InspectionsUI.PassParameterByReferenceQuickFix)
{
_target = target;
}

public override void Fix()
{
var parameter = Context.GetText();
var argCtxt = GetArgContextForIdentifier(Context.Parent.Parent, _target.IdentifierName);

var parts = parameter.Split(new char[]{' '},2);
if (1 != parts.GetUpperBound(0))
{
return;
}
parts[0] = parts[0].Replace(Tokens.ByVal, Tokens.ByRef);
var newContent = parts[0] + " " + parts[1];
var terminalNode = argCtxt.BYVAL();

var selection = Selection.Selection;
var replacementLine = GenerateByRefReplacementLine(terminalNode);

ReplaceModuleLine(terminalNode.Symbol.Line, replacementLine);

}
private VBAParser.ArgContext GetArgContextForIdentifier(RuleContext context, string identifier)
{
var argList = GetArgListForContext(context);
return argList.arg().SingleOrDefault(parameter =>
Identifier.GetName(parameter).Equals(identifier));
}
private string GenerateByRefReplacementLine(ITerminalNode terminalNode)
{
var module = Selection.QualifiedName.Component.CodeModule;
var byValTokenLine = module.GetLines(terminalNode.Symbol.Line, 1);

return ReplaceAtIndex(byValTokenLine, Tokens.ByVal, Tokens.ByRef, terminalNode.Symbol.Column);
}
private void ReplaceModuleLine(int lineNumber, string replacementLine)
{
var module = Selection.QualifiedName.Component.CodeModule;
module.DeleteLines(lineNumber);
module.InsertLines(lineNumber, replacementLine);
}
private string ReplaceAtIndex(string input, string toReplace, string replacement, int startIndex)
{
int stopIndex = startIndex + toReplace.Length;
var prefix = input.Substring(0, startIndex);
var suffix = input.Substring(stopIndex + 1);
var tokenToBeReplaced = input.Substring(startIndex, stopIndex - startIndex + 1);
return prefix + tokenToBeReplaced.Replace(toReplace, replacement) + suffix;
}
private VBAParser.ArgListContext GetArgListForContext(RuleContext context)
{
if (context is VBAParser.SubStmtContext)
{
return ((VBAParser.SubStmtContext)context).argList();
}
else if (context is VBAParser.FunctionStmtContext)
{
return ((VBAParser.FunctionStmtContext)context).argList();
}
else if (context is VBAParser.PropertyLetStmtContext)
{
return ((VBAParser.PropertyLetStmtContext)context).argList();
}
else if (context is VBAParser.PropertyGetStmtContext)
{
return ((VBAParser.PropertyGetStmtContext)context).argList();
}
else if (context is VBAParser.PropertySetStmtContext)
{
var lines = module.GetLines(selection.StartLine, selection.LineCount);
var result = lines.Replace(parameter, newContent);
module.ReplaceLine(selection.StartLine, result);
return ((VBAParser.PropertySetStmtContext)context).argList();
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override IEnumerable<QuickFixBase> QuickFixes
return _quickFixes ?? (_quickFixes = new QuickFixBase[]
{
new AssignedByValParameterQuickFix(Target, QualifiedSelection),
new PassParameterByReferenceQuickFix(Target.Context, QualifiedSelection),
new PassParameterByReferenceQuickFix(Target, QualifiedSelection),
new IgnoreOnceQuickFix(Context, QualifiedSelection, Inspection.AnnotationName)
});
}
Expand Down
2 changes: 0 additions & 2 deletions RetailCoder.VBE/Rubberduck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@
<Compile Include="Common\KeyHookEventArgs.cs" />
<Compile Include="Common\WinAPI\User32.cs" />
<Compile Include="Common\WinAPI\WindowLongFlags.cs" />
<Compile Include="Common\WinAPI\WM.cs" />
<Compile Include="Common\WindowsOperatingSystem.cs" />
<Compile Include="Common\UndocumentedAttribute.cs" />
<Compile Include="Inspections\ApplicationWorksheetFunctionInspection.cs" />
Expand Down Expand Up @@ -500,7 +499,6 @@
<Compile Include="UI\CodeExplorer\Commands\AddClassModuleCommand.cs" />
<Compile Include="UI\CodeExplorer\Commands\AddStdModuleCommand.cs" />
<Compile Include="UI\CodeExplorer\Commands\AddTestModuleCommand.cs" />
<Compile Include="UI\SubclassingWindow.cs" />
<Compile Include="VersionCheck\IVersionCheck.cs" />
<Compile Include="UI\Command\MenuItems\CommandBars\AppCommandBarBase.cs" />
<Compile Include="UI\Command\MenuItems\CommandBars\ContextSelectionLabelMenuItem.cs" />
Expand Down
10 changes: 0 additions & 10 deletions RetailCoder.VBE/UI/DockableToolwindowPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,6 @@ protected virtual void Dispose(bool disposing)
}
if (disposing && _window != null)
{
if (_userControlObject != null)
{
((_DockableWindowHost)_userControlObject).Dispose();
}
_userControlObject = null;

if (_userControl != null)
{
_userControl.Dispose();
}
// cleanup unmanaged resource wrappers
_window.Close();
_window.Release(true);
Expand Down
7 changes: 5 additions & 2 deletions RetailCoder.VBE/UI/DockableWindowHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Windows.Forms;
using Rubberduck.Common.WinAPI;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.WindowsApi;
using User32 = Rubberduck.Common.WinAPI.User32;

namespace Rubberduck.UI
{
Expand Down Expand Up @@ -52,6 +54,7 @@ private struct LParam

private IntPtr _parentHandle;
private ParentWindow _subClassingWindow;
private GCHandle _thisHandle;

internal void AddUserControl(UserControl control, IntPtr vbeHwnd)
{
Expand All @@ -63,7 +66,7 @@ internal void AddUserControl(UserControl control, IntPtr vbeHwnd)
//since we have to inherit from UserControl we don't have to keep handling window messages until the VBE gets
//around to destroying the control's host or it results in an access violation when the base class is disposed.
//We need to manually call base.Dispose() ONLY in response to a WM_DESTROY message.
GC.KeepAlive(this);
_thisHandle = GCHandle.Alloc(this, GCHandleType.Normal);

if (control != null)
{
Expand Down Expand Up @@ -143,7 +146,7 @@ protected override void DefWndProc(ref Message m)
//See the comment in the ctor for why we have to listen for this.
if (m.Msg == (int) WM.DESTROY)
{
base.Dispose(true);
_thisHandle.Free();
return;
}
base.DefWndProc(ref m);
Expand Down
Loading