Skip to content

Commit

Permalink
Merge pull request #6175 from rubberduck-vba/next
Browse files Browse the repository at this point in the history
v2.5.9.1
  • Loading branch information
retailcoder authored Nov 27, 2023
2 parents 9686494 + 22b5a72 commit 4ec0ddb
Show file tree
Hide file tree
Showing 61 changed files with 896 additions and 555 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,31 @@ public override void Fix(IInspectionResult result, IRewriteSession rewriteSessio
var arg = parameterizedDeclaration.Parameters.First(p => p.IsByRef || p.IsImplicitByRef);
var argIndex = parameterizedDeclaration.Parameters.IndexOf(arg);

UpdateSignature(result.Target, arg, rewriteSession);
UpdateProcedure(result.Target, arg, rewriteSession);
foreach (var reference in result.Target.References.Where(reference => !reference.IsDefaultMemberAccess))
{
UpdateCall(reference, argIndex, rewriteSession);
}
}

private void UpdateSignature(Declaration target, ParameterDeclaration arg, IRewriteSession rewriteSession)
private void UpdateProcedure(Declaration target, ParameterDeclaration arg, IRewriteSession rewriteSession)
{
var subStmt = (VBAParser.SubStmtContext) target.Context;
var argContext = (VBAParser.ArgContext)arg.Context;

var argName = argContext.unrestrictedIdentifier().GetText();
var rewriter = rewriteSession.CheckOutModuleRewriter(target.QualifiedModuleName);

UpdateSignature(subStmt, arg, rewriter);
AddReturnStatement(subStmt, argName, rewriter);
ReplaceExitSubs(subStmt, argName, rewriter);
}

private void UpdateSignature(VBAParser.SubStmtContext subStmt, ParameterDeclaration arg, IModuleRewriter rewriter)
{
rewriter.Replace(subStmt.SUB(), Tokens.Function);
rewriter.Replace(subStmt.END_SUB(), "End Function");

var argContext = (VBAParser.ArgContext)arg.Context;
rewriter.InsertAfter(subStmt.argList().Stop.TokenIndex, $" As {arg.AsTypeName}");

if (arg.IsByRef)
Expand All @@ -86,11 +94,26 @@ private void UpdateSignature(Declaration target, ParameterDeclaration arg, IRewr
{
rewriter.InsertBefore(argContext.unrestrictedIdentifier().Start.TokenIndex, Tokens.ByVal);
}
}

var returnStmt = $" {subStmt.subroutineName().GetText()} = {argContext.unrestrictedIdentifier().GetText()}{Environment.NewLine}";
private void AddReturnStatement(VBAParser.SubStmtContext subStmt, string argName, IModuleRewriter rewriter)
{
var returnStmt = $" {subStmt.subroutineName().GetText()} = {argName}{Environment.NewLine}";
// This exploits that the VBE will realign the End Function statement automatically.
rewriter.InsertBefore(subStmt.END_SUB().Symbol.TokenIndex, returnStmt);
}

private void ReplaceExitSubs(VBAParser.SubStmtContext subStmt, string argName, IModuleRewriter rewriter)
{
// We use a statement separator here to be able to deal with single line if statments without too much issues.
var exitFunctionCode = $"{subStmt.subroutineName().GetText()} = {argName}: Exit Function";
foreach (var exitSub in subStmt.GetDescendents<VBAParser.ExitStmtContext>())
{
rewriter.Replace(exitSub, exitFunctionCode);
}
}


private void UpdateCall(IdentifierReference reference, int argIndex, IRewriteSession rewriteSession)
{
var rewriter = rewriteSession.CheckOutModuleRewriter(reference.QualifiedModuleName);
Expand Down
2 changes: 1 addition & 1 deletion Rubberduck.Core/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Rubberduck.Core/Rubberduck.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,17 @@
<Version>2.0.20525</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
</Project>
5 changes: 4 additions & 1 deletion Rubberduck.Core/Settings/GeneralSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IGeneralSettings
DisplayLanguageSetting Language { get; set; }
bool CanShowSplash { get; set; }
bool CanCheckVersion { get; set; }
string ApiBaseUrl { get; set; }
bool IncludePreRelease { get; set; }
bool CompileBeforeParse { get; set; }
bool IsSmartIndenterPrompted { get; set; }
Expand Down Expand Up @@ -45,6 +46,7 @@ public DisplayLanguageSetting Language

public bool CanShowSplash { get; set; }
public bool CanCheckVersion { get; set; }
public string ApiBaseUrl { get; set; }
public bool IncludePreRelease { get; set; }
public bool CompileBeforeParse { get; set; }
public bool IsSmartIndenterPrompted { get; set; }
Expand Down Expand Up @@ -103,7 +105,8 @@ public bool Equals(GeneralSettings other)
EnableExperimentalFeatures.Count == other.EnableExperimentalFeatures.Count &&
EnableExperimentalFeatures.All(other.EnableExperimentalFeatures.Contains) &&
SetDpiUnaware == other.SetDpiUnaware &&
EnableFolderDragAndDrop == other.EnableFolderDragAndDrop;
EnableFolderDragAndDrop == other.EnableFolderDragAndDrop &&
ApiBaseUrl == other.ApiBaseUrl;
}
}
}
Loading

0 comments on commit 4ec0ddb

Please sign in to comment.