Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 17 additions & 10 deletions RetailCoder.VBE/Inspections/ObjectVariableNotSetInspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public SetObjectVariableQuickFix(IdentifierReference reference)
public override void Fix()
{
var codeModule = Selection.QualifiedName.Component.CodeModule;
var codeLine = codeModule.get_Lines(Selection.Selection.StartLine, 1);
var codeLine = codeModule.Lines[Selection.Selection.StartLine, 1];

var letStatementLeftSide = Context.GetText();
var setStatementLeftSide = Tokens.Set + ' ' + letStatementLeftSide;
Expand Down Expand Up @@ -86,19 +86,26 @@ public ObjectVariableNotSetInspection(RubberduckParserState state)

public override IEnumerable<InspectionResultBase> GetInspectionResults()
{
return State.AllUserDeclarations
.Where(item => !ValueTypes.Contains(item.AsTypeName)
&& !item.IsSelfAssigned
&& (item.DeclarationType == DeclarationType.Variable
|| item.DeclarationType == DeclarationType.Parameter))
var interestingDeclarations =
State.AllUserDeclarations.Where(item =>
!item.IsSelfAssigned &&
!ValueTypes.Contains(item.AsTypeName) &&
(item.AsTypeDeclaration == null ||
item.AsTypeDeclaration.DeclarationType != DeclarationType.Enumeration &&
item.AsTypeDeclaration.DeclarationType != DeclarationType.UserDefinedType) &&
(item.DeclarationType == DeclarationType.Variable ||
item.DeclarationType == DeclarationType.Parameter));

var interestingReferences = interestingDeclarations
.SelectMany(declaration =>
declaration.References.Where(reference =>
{
var k = reference.Context.parent.GetType();
var setStmtContext = ParserRuleContextHelper.GetParent<VBAParser.LetStmtContext>(reference.Context);
return setStmtContext != null && setStmtContext.LET() == null;
}))
.Select(reference => new ObjectVariableNotSetInspectionResult(this, reference));
return reference.IsAssignment && setStmtContext != null && setStmtContext.LET() == null;
}));


return interestingReferences.Select(reference => new ObjectVariableNotSetInspectionResult(this, reference));
}
}
}
16 changes: 12 additions & 4 deletions RetailCoder.VBE/Inspections/ObsoleteTypeHintInspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ public override IEnumerable<InspectionResultBase> GetInspectionResults()

var declarations = from item in results
where item.HasTypeHint()
// bug: this inspection result only has one value. Why are we passing two in?
select new ObsoleteTypeHintInspectionResult(this, string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat, InspectionsUI.Inspections_Declaration, item.DeclarationType.ToString().ToLower(), item.IdentifierName), new QualifiedContext(item.QualifiedName, item.Context), item);
// todo: localize this InspectionResultFormat properly
select
new ObsoleteTypeHintInspectionResult(this,
string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat,
InspectionsUI.Inspections_Declaration, item.DeclarationType.ToString().ToLower(),
item.IdentifierName), new QualifiedContext(item.QualifiedName, item.Context), item);

var references = from item in results.SelectMany(d => d.References)
where item.HasTypeHint()
select new ObsoleteTypeHintInspectionResult(this, string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat, InspectionsUI.Inspections_Usage, item.Declaration.DeclarationType.ToString().ToLower(), item.IdentifierName), new QualifiedContext(item.QualifiedModuleName, item.Context), item.Declaration);
select
new ObsoleteTypeHintInspectionResult(this,
string.Format(InspectionsUI.ObsoleteTypeHintInspectionResultFormat,
InspectionsUI.Inspections_Usage, item.Declaration.DeclarationType.ToString().ToLower(),
item.IdentifierName), new QualifiedContext(item.QualifiedModuleName, item.Context),
item.Declaration);

return declarations.Union(references);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Antlr4.Runtime;
using Rubberduck.Parsing.Grammar;
Expand Down Expand Up @@ -72,6 +73,8 @@ public override void Fix()

private static string GetNewSignature(ParserRuleContext context)
{
Debug.Assert(context != null);

return context.children.Aggregate(string.Empty, (current, member) =>
{
var isIdentifierNode = member is VBAParser.IdentifierContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ public override int Compare(CodeExplorerItemViewModel x, CodeExplorerItemViewMod

public class CompareByType : Comparer<CodeExplorerItemViewModel>
{
private static readonly Dictionary<DeclarationType, int> SortOrder = new Dictionary<DeclarationType, int>
{
{DeclarationType.LibraryFunction, 0},
{DeclarationType.LibraryProcedure, 1},
{DeclarationType.UserDefinedType, 2},
{DeclarationType.Enumeration, 3},
{DeclarationType.Event, 4},
{DeclarationType.Constant, 5},
{DeclarationType.Variable, 6},
{DeclarationType.PropertyGet, 7},
{DeclarationType.PropertyLet, 8},
{DeclarationType.PropertySet, 9},
{DeclarationType.Function, 10},
{DeclarationType.Procedure, 11}
};

public override int Compare(CodeExplorerItemViewModel x, CodeExplorerItemViewModel y)
{
if (x == y)
Expand All @@ -54,6 +70,14 @@ public override int Compare(CodeExplorerItemViewModel x, CodeExplorerItemViewMod
// keep separate types separate
if (xNode.Declaration.DeclarationType != yNode.Declaration.DeclarationType)
{
int xValue, yValue;

if (SortOrder.TryGetValue(xNode.Declaration.DeclarationType, out xValue) &&
SortOrder.TryGetValue(yNode.Declaration.DeclarationType, out yValue))
{
return xValue < yValue ? -1 : 1;
}

return xNode.Declaration.DeclarationType < yNode.Declaration.DeclarationType ? -1 : 1;
}

Expand Down
13 changes: 12 additions & 1 deletion RetailCoder.VBE/UI/Command/IndentCurrentProcedureCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Runtime.InteropServices;
using Microsoft.Vbe.Interop;
using Rubberduck.Parsing.VBA;
using Rubberduck.Settings;
using Rubberduck.SmartIndenter;

Expand All @@ -7,13 +9,22 @@ namespace Rubberduck.UI.Command
[ComVisible(false)]
public class IndentCurrentProcedureCommand : CommandBase
{
private readonly VBE _vbe;
private readonly RubberduckParserState _state;
private readonly IIndenter _indenter;

public IndentCurrentProcedureCommand(IIndenter indenter)
public IndentCurrentProcedureCommand(VBE vbe, RubberduckParserState state, IIndenter indenter)
{
_vbe = vbe;
_state = state;
_indenter = indenter;
}

public override bool CanExecute(object parameter)
{
return _state.FindSelectedDeclaration(_vbe.ActiveCodePane, true) != null;
}

public override void Execute(object parameter)
{
_indenter.IndentCurrentProcedure();
Expand Down
2 changes: 1 addition & 1 deletion RetailCoder.VBE/UI/Command/RunAllTestsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected virtual void OnRunCompleted(TestRunEventArgs e)
}
}

public struct TestRunEventArgs
public class TestRunEventArgs
{
public long Duration { get; private set; }

Expand Down
3 changes: 3 additions & 0 deletions Rubberduck.Parsing/Symbols/ReferencedDeclarationsCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public IEnumerable<Declaration> GetDeclarationsForReference(Reference reference)
info.GetTypeAttr(out typeAttributesPointer);

var typeAttributes = (TYPEATTR)Marshal.PtrToStructure(typeAttributesPointer, typeof(TYPEATTR));
info.ReleaseTypeAttr(typeAttributesPointer);

var attributes = new Attributes();
if (typeAttributes.wTypeFlags.HasFlag(TYPEFLAGS.TYPEFLAG_FPREDECLID))
Expand Down Expand Up @@ -243,6 +244,7 @@ private Declaration CreateMemberDeclaration(out FUNCDESC memberDescriptor, TYPEK
IntPtr memberDescriptorPointer;
info.GetFuncDesc(memberIndex, out memberDescriptorPointer);
memberDescriptor = (FUNCDESC)Marshal.PtrToStructure(memberDescriptorPointer, typeof(FUNCDESC));
info.ReleaseFuncDesc(memberDescriptorPointer);

if (memberDescriptor.callconv != CALLCONV.CC_STDCALL)
{
Expand Down Expand Up @@ -390,6 +392,7 @@ private Declaration CreateFieldDeclaration(ITypeInfo info, int fieldIndex, Decla
info.GetVarDesc(fieldIndex, out ppVarDesc);

var varDesc = (VARDESC)Marshal.PtrToStructure(ppVarDesc, typeof(VARDESC));
info.ReleaseVarDesc(ppVarDesc);

var names = new string[255];
int namesArrayLength;
Expand Down
33 changes: 20 additions & 13 deletions Rubberduck.SmartIndenter/Indenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ private void OnReportProgress(string moduleName, int progress, int max)
public void IndentCurrentProcedure()
{
var pane = _vbe.ActiveCodePane;

if (pane == null)
{
return;
}
var module = pane.CodeModule;
var selection = GetSelection(pane);

Expand All @@ -69,6 +74,10 @@ public void IndentCurrentProcedure()
public void IndentCurrentModule()
{
var pane = _vbe.ActiveCodePane;
if (pane == null)
{
return;
}
Indent(pane.CodeModule.Parent);
}

Expand Down Expand Up @@ -288,12 +297,12 @@ public void Indent(string[] codeLines, string moduleName, bool reportProgress =
break;

case ": ":
// a multi-statement line separator => tidy up and continue
if (!currentLine.Substring(0, scan + 1).EndsWith("Then:"))
//a multi-statement line separator => tidy up and continue
if (!currentLine.Substring(0, scan + 1).EndsWith(" Then:"))
{
currentLine = currentLine.Substring(0, scan + 1) + currentLine.Substring(scan + 2);
currentLine = currentLine.Substring(0, scan + 2) + currentLine.Substring(scan + 2);
// check the indenting for the line segment
CheckLine(settings, currentLine, ref noIndent, out ins, out outs, ref atProcedureStart, ref atFirstProcLine, ref isInsideIfBlock);
CheckLine(settings, currentLine.Substring(start - 1), ref noIndent, out ins, out outs, ref atProcedureStart, ref atFirstProcLine, ref isInsideIfBlock);
if (atProcedureStart)
{
atFirstDim = true;
Expand Down Expand Up @@ -455,27 +464,23 @@ public void Indent(string[] codeLines, string moduleName, bool reportProgress =
(settings.EndOfLineCommentStyle == EndOfLineCommentStyle.Absolute ||
settings.EndOfLineCommentStyle == EndOfLineCommentStyle.AlignInColumn))
{
gap -= lineNumber.Length - indents*settings.IndentSpaces - 1;
}
if (gap < 2)
{
gap = settings.IndentSpaces;
gap -= lineNumber.Length - indents * settings.IndentSpaces - 1;
}

commentStart = currentLine.Length + gap;
currentLine += new string(' ', gap) + right;
}

// work out where the text of the comment starts, to align the next line
if (currentLine.Substring(commentStart, 4) == "Rem ")
if (commentStart < currentLine.Length - 4 && currentLine.Substring(commentStart, 4) == "Rem ")
{
commentStart += 3;
}
if (currentLine.Substring(commentStart, 1) == "'")
if (commentStart < currentLine.Length && currentLine.Substring(commentStart, 1) == "'")
{
commentStart += 1;
}
while (currentLine.Substring(commentStart, 1) != " ")
while (commentStart < currentLine.Length && currentLine.Substring(commentStart, 1) != " ")
{
commentStart += 1;
}
Expand Down Expand Up @@ -528,7 +533,9 @@ public void Indent(string[] codeLines, string moduleName, bool reportProgress =
atProcedureStart = false;
}

CheckLine(settings, currentLine.Substring(start - 1, scan - 1), ref noIndent, out ins, out outs, ref atProcedureStart, ref atFirstProcLine, ref isInsideIfBlock);
CheckLine(settings, currentLine.Substring(start - 1, Math.Min(scan - 1, currentLine.Length - start)),
ref noIndent, out ins, out outs, ref atProcedureStart, ref atFirstProcLine,
ref isInsideIfBlock);
if (atProcedureStart)
{
atFirstDim = true;
Expand Down
Loading