Skip to content

Commit

Permalink
Merge pull request #120 from rubberduck-vba/next
Browse files Browse the repository at this point in the history
sync with main repo
  • Loading branch information
retailcoder committed Jun 7, 2016
2 parents 947ec8c + 7123cac commit 72035d2
Show file tree
Hide file tree
Showing 30 changed files with 393 additions and 166 deletions.
34 changes: 31 additions & 3 deletions RetailCoder.VBE/Common/DeclarationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,40 @@ public static IEnumerable<Declaration> FindEventHandlers(this IEnumerable<Declar

public static IEnumerable<Declaration> FindBuiltInEventHandlers(this IEnumerable<Declaration> declarations)
{
var handlerNames = declarations.Where(declaration => declaration.IsBuiltIn && declaration.DeclarationType == DeclarationType.Event)
var declarationList = declarations.ToList();

var handlerNames = declarationList.Where(declaration => declaration.IsBuiltIn && declaration.DeclarationType == DeclarationType.Event)
.Select(e => e.ParentDeclaration.IdentifierName + "_" + e.IdentifierName);

return declarations.Where(declaration => !declaration.IsBuiltIn
// class module built-in events
var classModuleHandlers = declarationList.Where(item =>
item.DeclarationType == DeclarationType.Procedure &&
item.ParentDeclaration.DeclarationType == DeclarationType.ClassModule &&
(item.IdentifierName == "Class_Initialize" || item.IdentifierName == "Class_Terminate"));

// user form built-in events
var userFormHandlers = declarationList.Where(item =>
item.DeclarationType == DeclarationType.Procedure &&
item.ParentDeclaration.DeclarationType == DeclarationType.ClassModule &&
item.QualifiedName.QualifiedModuleName.Component.Type == vbext_ComponentType.vbext_ct_MSForm &&
new[]
{
"UserForm_Activate", "UserForm_AddControl", "UserForm_BeforeDragOver", "UserForm_BeforeDropOrPaste",
"UserForm_Click", "UserForm_DblClick", "UserForm_Deactivate", "UserForm_Error",
"UserForm_Initialize", "UserForm_KeyDown", "UserForm_KeyPress", "UserForm_KeyUp", "UserForm_Layout",
"UserForm_MouseDown", "UserForm_MouseMove", "UserForm_MouseUp", "UserForm_QueryClose",
"UserForm_RemoveControl", "UserForm_Resize", "UserForm_Scroll", "UserForm_Terminate",
"UserForm_Zoom"
}.Contains(item.IdentifierName));

var handlers = declarationList.Where(declaration => !declaration.IsBuiltIn
&& declaration.DeclarationType == DeclarationType.Procedure
&& handlerNames.Contains(declaration.IdentifierName));
&& handlerNames.Contains(declaration.IdentifierName)).ToList();

handlers.AddRange(classModuleHandlers);
handlers.AddRange(userFormHandlers);

return handlers;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
// ParamArray parameters do not allow an explicit "ByRef" parameter mechanism.
&& !((ParameterDeclaration)item).IsParamArray
&& !interfaceMembers.Select(m => m.Scope).Contains(item.ParentScope)
&& !UserDeclarations.FindBuiltInEventHandlers().Contains(item.ParentDeclaration)
let arg = item.Context as VBAParser.ArgContext
where arg != null && arg.BYREF() == null && arg.BYVAL() == null
select new QualifiedContext<VBAParser.ArgContext>(item.QualifiedName, arg))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA.Nodes;
using Rubberduck.Parsing.VBA;
using Rubberduck.UI;
using Rubberduck.VBEditor;

Expand Down Expand Up @@ -51,16 +54,12 @@ public SetExplicitVariantReturnTypeQuickFix(ParserRuleContext context, Qualified

public override void Fix()
{
// note: turns a multiline signature into a one-liner signature.
// bug: removes all comments.

var node = GetNode(Context as VBAParser.FunctionStmtContext)
?? GetNode(Context as VBAParser.PropertyGetStmtContext);

var signature = node.Signature.TrimEnd();

var procedure = Context.GetText();
var result = procedure.Replace(signature, signature + ' ' + Tokens.As + ' ' + Tokens.Variant);
var indexOfLastClosingParen = procedure.LastIndexOf(')');

var result = indexOfLastClosingParen == procedure.Length
? procedure + ' ' + Tokens.As + ' ' + Tokens.Variant
: procedure.Insert(procedure.LastIndexOf(')') + 1, ' ' + Tokens.As + ' ' + Tokens.Variant);

var module = Selection.QualifiedName.Component.CodeModule;
var selection = Context.GetSelection();
Expand All @@ -69,28 +68,49 @@ public override void Fix()
module.InsertLines(selection.StartLine, result);
}

private ProcedureNode GetNode(VBAParser.FunctionStmtContext context)
private string GetSignature(VBAParser.FunctionStmtContext context)
{
if (context == null)
{
return null;
}

var scope = Selection.QualifiedName.ToString();
var localScope = scope + "." + context.functionName().identifier().GetText();
return new ProcedureNode(context, scope, localScope);
var @static = context.STATIC() == null ? string.Empty : context.STATIC().GetText() + ' ';
var keyword = context.FUNCTION().GetText() + ' ';
var args = context.argList() == null ? "()" : context.argList().GetText() + ' ';
var asTypeClause = context.asTypeClause() == null ? string.Empty : context.asTypeClause().GetText();
var visibility = context.visibility() == null ? string.Empty : context.visibility().GetText() + ' ';

return visibility + @static + keyword + context.functionName().identifier().GetText() + args + asTypeClause;
}

private ProcedureNode GetNode(VBAParser.PropertyGetStmtContext context)
private string GetSignature(VBAParser.PropertyGetStmtContext context)
{
if (context == null)
{
return null;
}

var scope = Selection.QualifiedName.ToString();
var localScope = scope + "." + context.functionName().identifier().GetText();
return new ProcedureNode(context, scope, localScope);
var @static = context.STATIC() == null ? string.Empty : context.STATIC().GetText() + ' ';
var keyword = context.PROPERTY_GET().GetText() + ' ';
var args = context.argList() == null ? "()" : context.argList().GetText() + ' ';
var asTypeClause = context.asTypeClause() == null ? string.Empty : context.asTypeClause().GetText();
var visibility = context.visibility() == null ? string.Empty : context.visibility().GetText() + ' ';

return visibility + @static + keyword + context.functionName().identifier().GetText() + args + asTypeClause;
}

private string GetSignature(VBAParser.DeclareStmtContext context)
{
if (context == null)
{
return null;
}

var args = context.argList() == null ? "()" : context.argList().GetText() + ' ';
var asTypeClause = context.asTypeClause() == null ? string.Empty : context.asTypeClause().GetText();

return args + asTypeClause;
}
}
}
1 change: 0 additions & 1 deletion RetailCoder.VBE/Inspections/InspectionResultBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using Antlr4.Runtime;
using Rubberduck.Parsing;
using Rubberduck.Parsing.Nodes;
using Rubberduck.Parsing.Symbols;
using Rubberduck.UI;
using Rubberduck.UI.Controls;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Collections.Generic;
using Antlr4.Runtime;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Nodes;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.VBEditor;

Expand Down
2 changes: 1 addition & 1 deletion RetailCoder.VBE/Inspections/OptionBaseInspectionResult.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Nodes;
using Rubberduck.Parsing.Symbols;
using Rubberduck.VBEditor;

namespace Rubberduck.Inspections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using Antlr4.Runtime;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Nodes;
using Rubberduck.Parsing.Symbols;
using Rubberduck.VBEditor;

namespace Rubberduck.Inspections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()

var eventScopes = declarations.Where(item =>
!item.IsBuiltIn && item.DeclarationType == DeclarationType.Event)
.Select(e => e.Scope);
.Select(e => e.Scope).Concat(declarations.FindBuiltInEventHandlers().Select(e => e.Scope));

var declareScopes = declarations.Where(item =>
item.DeclarationType == DeclarationType.LibraryFunction
Expand Down
7 changes: 3 additions & 4 deletions RetailCoder.VBE/Inspections/ParameterNotUsedInspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
using System.Linq;
using Microsoft.Vbe.Interop;
using Rubberduck.Common;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Refactorings.RemoveParameters;
using Rubberduck.UI;
using Rubberduck.UI.Refactorings;
using Rubberduck.VBEditor.VBEInterfaces.RubberduckCodePane;

namespace Rubberduck.Inspections
{
Expand Down Expand Up @@ -38,8 +36,9 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
var builtInHandlers = declarations.FindBuiltInEventHandlers();

var parameters = declarations.Where(parameter => parameter.DeclarationType == DeclarationType.Parameter
&& !(parameter.Context.Parent.Parent is VBAParser.EventStmtContext)
&& !(parameter.Context.Parent.Parent is VBAParser.DeclareStmtContext));
&& parameter.ParentDeclaration.DeclarationType != DeclarationType.Event
&& parameter.ParentDeclaration.DeclarationType != DeclarationType.LibraryFunction
&& parameter.ParentDeclaration.DeclarationType != DeclarationType.LibraryProcedure);

var unused = parameters.Where(parameter => !parameter.References.Any()).ToList();
var quickFixRefactoring =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
if (declaration == null) { return false; } // rather be safe than sorry
return UserDeclarations.Where(item => item.IsWithEvents)
.All(withEvents => UserDeclarations.FindEventProcedures(withEvents) == null);
.All(withEvents => UserDeclarations.FindEventProcedures(withEvents) == null) &&
!UserDeclarations.FindBuiltInEventHandlers().Contains(declaration);
});

return ParseTreeResults.ArgListsWithOneByRefParam
Expand Down
2 changes: 2 additions & 0 deletions RetailCoder.VBE/Inspections/ProcedureNotUsedInspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()
handlers.AddRange(forms.SelectMany(form => declarations.FindFormEventHandlers(form)));
}

handlers.AddRange(declarations.FindBuiltInEventHandlers());

var items = declarations
.Where(item => !IsIgnoredDeclaration(declarations, item, handlers, classes, modules)
&& !item.IsInspectionDisabled(AnnotationName)).ToList();
Expand Down
2 changes: 1 addition & 1 deletion RetailCoder.VBE/ToDoItems/ToDoItem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Rubberduck.Parsing.Nodes;
using Rubberduck.Parsing.Symbols;
using Rubberduck.UI;
using Rubberduck.UI.Controls;
using Rubberduck.VBEditor;
Expand Down
2 changes: 1 addition & 1 deletion RetailCoder.VBE/UI/ToDoItems/ToDoExplorerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Rubberduck.Parsing.Nodes;
using Rubberduck.Parsing.VBA;
using Rubberduck.Settings;
using Rubberduck.ToDoItems;
using Rubberduck.UI.Command;
using Rubberduck.UI.Controls;
using Rubberduck.UI.Settings;
using Rubberduck.Common;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.UI.ToDoItems
{
Expand Down
5 changes: 4 additions & 1 deletion RetailCoder.VBE/UnitTesting/TestEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ public void Run(IEnumerable<TestMethod> tests)
var testInitialize = module.Key.FindTestInitializeMethods(_state).ToList();
var testCleanup = module.Key.FindTestCleanupMethods(_state).ToList();

var moduleTestMethods = testMethods
.Where(test => test.QualifiedMemberName.QualifiedModuleName.ComponentName == module.Key.ComponentName);

Run(module.Key.FindModuleInitializeMethods(_state));
foreach (var test in testMethods)
foreach (var test in moduleTestMethods)
{
// no need to run setup/teardown for ignored tests
if (test.Declaration.Annotations.Any(a => a.AnnotationType == AnnotationType.IgnoreTest))
Expand Down
3 changes: 1 addition & 2 deletions Rubberduck.Parsing/Rubberduck.Parsing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
<Compile Include="Preprocessing\VBAConditionalCompilationParserListener.cs" />
<Compile Include="Preprocessing\VBAConditionalCompilationParserVisitor.cs" />
<Compile Include="Symbols\ComInformation.cs" />
<Compile Include="Symbols\CommentNode.cs" />
<Compile Include="Symbols\ComParameter.cs" />
<Compile Include="Symbols\Identifier.cs" />
<Compile Include="Binding\IBindingContext.cs" />
Expand Down Expand Up @@ -256,7 +257,6 @@
<Compile Include="Symbols\ProjectReference.cs" />
<Compile Include="Symbols\ReferencedDeclarationsCollector.cs" />
<Compile Include="Symbols\SyntaxErrorException.cs" />
<Compile Include="Nodes\CommentNode.cs" />
<Compile Include="ParserRuleContextExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="QualifiedContext.cs" />
Expand All @@ -277,7 +277,6 @@
<Compile Include="VBA\ModuleState.cs" />
<Compile Include="VBA\Nodes\INode.cs" />
<Compile Include="VBA\Nodes\Node.cs" />
<Compile Include="VBA\Nodes\ProcedureNode.cs" />
<Compile Include="VBA\ParseErrorEventArgs.cs" />
<Compile Include="VBA\ParserState.cs" />
<Compile Include="VBA\ReferencePriorityMap.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Rubberduck.VBEditor;

namespace Rubberduck.Parsing.Nodes
namespace Rubberduck.Parsing.Symbols
{
/// <summary>
/// Represents a comment.
Expand Down
1 change: 0 additions & 1 deletion Rubberduck.Parsing/Symbols/DeclarationFinder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using NLog;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Nodes;
using Rubberduck.VBEditor;
using System;
using System.Collections.Generic;
Expand Down
3 changes: 1 addition & 2 deletions Rubberduck.Parsing/Symbols/DeclarationSymbolsListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Microsoft.Vbe.Interop.Forms;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Nodes;
using Rubberduck.Parsing.VBA;
using Rubberduck.VBEditor;
using System;
Expand Down Expand Up @@ -247,7 +246,7 @@ private void DeclareControlsAsMembers(VBComponent form)
}
else if (declarationType == DeclarationType.LibraryProcedure || declarationType == DeclarationType.LibraryFunction)
{
result = new ExternalProcedureDeclaration(new QualifiedMemberName(_qualifiedName, identifierName), _parentDeclaration, _currentScopeDeclaration, declarationType, asTypeName, accessibility, context, selection, false, annotations);
result = new ExternalProcedureDeclaration(new QualifiedMemberName(_qualifiedName, identifierName), _parentDeclaration, _currentScopeDeclaration, declarationType, asTypeName, asTypeContext, accessibility, context, selection, false, annotations);
}
else if (declarationType == DeclarationType.PropertyGet)
{
Expand Down
4 changes: 3 additions & 1 deletion Rubberduck.Parsing/Symbols/ExternalProcedureDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Rubberduck.VBEditor;
using System.Collections.Generic;
using System.Linq;
using Rubberduck.Parsing.Grammar;

namespace Rubberduck.Parsing.Symbols
{
Expand All @@ -16,6 +17,7 @@ public sealed class ExternalProcedureDeclaration : Declaration, IDeclarationWith
Declaration parentScope,
DeclarationType declarationType,
string asTypeName,
VBAParser.AsTypeClauseContext asTypeContext,
Accessibility accessibility,
ParserRuleContext context,
Selection selection,
Expand All @@ -34,7 +36,7 @@ public sealed class ExternalProcedureDeclaration : Declaration, IDeclarationWith
context,
selection,
false,
null,
asTypeContext,
isBuiltIn,
annotations,
null)
Expand Down
1 change: 0 additions & 1 deletion Rubberduck.Parsing/VBA/ComponentParseTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using NLog;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Nodes;
using Rubberduck.Parsing.Preprocessing;
using Rubberduck.Parsing.Symbols;
using Rubberduck.VBEditor;
Expand Down
1 change: 0 additions & 1 deletion Rubberduck.Parsing/VBA/ModuleState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Tree;
using Rubberduck.Parsing.Annotations;
using Rubberduck.Parsing.Nodes;
using Rubberduck.Parsing.Symbols;

namespace Rubberduck.Parsing.VBA
Expand Down

0 comments on commit 72035d2

Please sign in to comment.