Skip to content

Commit c7cecf6

Browse files
committed
Merge branch 'next' of https://github.com/comintern/Rubberduck into next
2 parents 11dce9d + b4fb9d6 commit c7cecf6

33 files changed

+455
-104
lines changed

Rubberduck.Core/AutoComplete/AutoCompleteBase.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,19 @@ public virtual bool Execute(AutoCompleteEventArgs e, AutoCompleteSettings settin
4040
var nextChar = zSelection.StartColumn == original.Length ? string.Empty : original.Substring(zSelection.StartColumn, 1);
4141
if (input == InputToken && (input != OutputToken || nextChar != OutputToken))
4242
{
43-
var code = original.Insert(Math.Max(0, zSelection.StartColumn), InputToken + OutputToken);
43+
string code;
44+
if (!StripExplicitCallStatement(ref original, ref pSelection))
45+
{
46+
code = original.Insert(Math.Max(0, zSelection.StartColumn), InputToken + OutputToken);
47+
}
48+
else
49+
{
50+
code = original;
51+
}
4452
module.ReplaceLine(pSelection.StartLine, code);
4553

4654
var newCode = module.GetLines(pSelection);
47-
if (newCode == code)
55+
if (newCode.Equals(code, StringComparison.OrdinalIgnoreCase))
4856
{
4957
pane.Selection = new Selection(pSelection.StartLine, pSelection.StartColumn + 1);
5058
}
@@ -67,6 +75,21 @@ public virtual bool Execute(AutoCompleteEventArgs e, AutoCompleteSettings settin
6775
}
6876
}
6977

78+
private bool StripExplicitCallStatement(ref string code, ref Selection pSelection)
79+
{
80+
// VBE will "helpfully" strip empty parentheses in 'Call Something()'
81+
// ...and there's no way around it. since Call statement is optional and obsolete,
82+
// this function strips it
83+
var pattern = @"\bCall\b\s+";
84+
if (Regex.IsMatch(code, pattern, RegexOptions.IgnoreCase))
85+
{
86+
pSelection = new Selection(pSelection.StartLine, pSelection.StartColumn - "Call ".Length);
87+
code = Regex.Replace(code, pattern, string.Empty, RegexOptions.IgnoreCase);
88+
return true;
89+
}
90+
return false;
91+
}
92+
7093
private int GetPrettifiedCaretPosition(Selection pSelection, string insertedCode, string prettifiedCode)
7194
{
7295
var zSelection = pSelection.ToZeroBased();
@@ -80,7 +103,10 @@ private int GetPrettifiedCaretPosition(Selection pSelection, string insertedCode
80103
outputTokenIndices.Add(i);
81104
}
82105
}
83-
106+
if (!outputTokenIndices.Any())
107+
{
108+
return pSelection.EndColumn;
109+
}
84110
var firstAfterCaret = outputTokenIndices.Where(i => i > zSelection.StartColumn).Min();
85111

86112
var prettifiedTokenIndices = new List<int>();
@@ -93,7 +119,9 @@ private int GetPrettifiedCaretPosition(Selection pSelection, string insertedCode
93119
}
94120
}
95121

96-
return prettifiedTokenIndices[outputTokenIndices.IndexOf(firstAfterCaret)] + 1;
122+
return prettifiedTokenIndices.Any()
123+
? prettifiedTokenIndices[outputTokenIndices.IndexOf(firstAfterCaret)] + 1
124+
: prettifiedCode.Length + 2;
97125
}
98126

99127
public virtual bool IsMatch(string input) =>

Rubberduck.Core/UI/CodeExplorer/Commands/AddComponentCommand.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace Rubberduck.UI.CodeExplorer.Commands
99
public class AddComponentCommand
1010
{
1111
private readonly IVBE _vbe;
12-
private const string DefaultFolder = "VBAProject";
1312

1413
public AddComponentCommand(IVBE vbe)
1514
{
@@ -69,16 +68,22 @@ private Declaration GetDeclaration(CodeExplorerItemViewModel node)
6968

7069
return (node as ICodeExplorerDeclarationViewModel)?.Declaration;
7170
}
72-
71+
private string GetActiveProjectName()
72+
{
73+
using (var activeProject = _vbe.ActiveVBProject)
74+
{
75+
return activeProject.Name;
76+
}
77+
}
7378
private string GetFolder(CodeExplorerItemViewModel node)
7479
{
7580
switch (node)
7681
{
7782
case null:
78-
return DefaultFolder;
83+
return GetActiveProjectName();
7984
case ICodeExplorerDeclarationViewModel declarationNode:
8085
return string.IsNullOrEmpty(declarationNode.Declaration.CustomFolder)
81-
? DefaultFolder
86+
? GetActiveProjectName()
8287
: declarationNode.Declaration.CustomFolder.Replace("\"", string.Empty);
8388
default:
8489
return ((CodeExplorerCustomFolderViewModel)node).FullPath;

Rubberduck.Deployment/BuildRegistryScript.ps1

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,32 @@ function Restore-Environment {
6969
foreach-object { set-item Env:$($_.Name) $_.Value }
7070
}
7171

72+
# Remove older imported registry scripts for debug builds.
73+
function Clean-OldImports
74+
{
75+
param(
76+
[String] $dir
77+
)
78+
$i = 0;
79+
Get-ChildItem $dir -Filter DebugRegistryEntries.reg.imported_*.txt |
80+
Sort-Object Name -Descending |
81+
Foreach-Object {
82+
if($i -ge 10) {
83+
$_.Delete();
84+
}
85+
$i++;
86+
}
87+
}
88+
7289
Set-StrictMode -Version latest;
7390
$ErrorActionPreference = "Stop";
7491
$DebugUnregisterRun = $false;
7592

7693
try
7794
{
95+
# Clean imports older than 10 builds
96+
Clean-OldImports ((Get-ScriptDirectory) + "\LocalRegistryEntries");;
97+
7898
# Allow multiple DLL files to be registered if necessary
7999
$separator = "|";
80100
$option = [System.StringSplitOptions]::RemoveEmptyEntries;
@@ -137,6 +157,22 @@ try
137157
$dllXml = $targetDll + ".xml";
138158
$tlbXml = $targetTlb32 + ".xml";
139159

160+
# Write-Host "Variable printout:"
161+
# Write-Host "dllFile = $dllFile";
162+
# Write-Host "idlFile = $idlFile";
163+
# Write-Host "tlb32File = $tlb32File";
164+
# Write-Host "tlb64File = $tlb64File";
165+
# Write-Host "sourceDll = $sourceDll";
166+
# Write-Host "targetDll = $targetDll";
167+
# Write-Host "sourceTlb32 = $sourceTlb32";
168+
# Write-Host "targetTlb32 = $targetTlb32";
169+
# Write-Host "sourceTlb64 = $sourceTlb64";
170+
# Write-Host "targetTlb64 = $targetTlb64";
171+
# Write-Host "dllXml = $dllXml";
172+
# Write-Host "tlbXml = $tlbXml";
173+
# Write-Host "targetDir = $targetDir";
174+
# Write-Host "";
175+
140176
# Use for debugging issues with passing parameters to the external programs
141177
# Note that it is not legal to have syntax like `& $cmdIncludingArguments` or `& $cmd $args`
142178
# For simplicity, the arguments are pass in literally.
@@ -151,12 +187,21 @@ try
151187
$encoding = New-Object System.Text.UTF8Encoding $true;
152188
[System.IO.File]::WriteAllLines($idlFile, $idl, $encoding);
153189

154-
$origEnv = Get-Environment
190+
$origEnv = Get-Environment;
155191
try {
156192
Invoke-CmdScript "$devPath";
157-
158-
& "midl.exe" ""$idlFile"" /win32 /out ""$targetDir"" /tlb ""$tlb32File"";
159-
& "midl.exe" ""$idlFile"" /amd64 /out ""$targetDir"" /tlb ""$tlb64File"";
193+
194+
if($targetDir.EndsWith("\"))
195+
{
196+
$targetDirWithoutSlash = $targetDir.Substring(0,$targetDir.Length-1);
197+
}
198+
else
199+
{
200+
$targetDirWithoutSlash = $targetDir;
201+
}
202+
203+
& midl.exe /win32 /tlb ""$tlb32File"" ""$idlFile"" /out ""$targetDirWithoutSlash"";
204+
& midl.exe /amd64 /tlb ""$tlb64File"" ""$idlFile"" /out ""$targetDirWithoutSlash"";
160205
} catch {
161206
throw;
162207
} finally {

Rubberduck.Deployment/Rubberduck.Deployment.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@
116116
</ItemGroup>
117117
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
118118
<PropertyGroup>
119-
<PostBuildEvent>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -command "$(ProjectDir)BuildRegistryScript.ps1 -config '$(ConfigurationName)' -builderAssemblyPath '$(TargetPath)' -netToolsDir '$(FrameworkSDKDir)bin\NETFX 4.6.1 Tools\' -wixToolsDir '$(ProjectDir)WixToolset\' -sourceDir '$(TargetDir)' -targetDir '$(TargetDir)' -projectDir '$(ProjectDir)' -includeDir '$(ProjectDir)InnoSetup\Includes\' -filesToExtract 'Rubberduck.dll|Rubberduck.API.dll'"</PostBuildEvent>
119+
<PostBuildEvent>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -command "&amp; '$(ProjectDir)BuildRegistryScript.ps1' -config '$(ConfigurationName)' -builderAssemblyPath '$(TargetPath)' -netToolsDir '$(FrameworkSDKDir)bin\NETFX 4.6.1 Tools\' -wixToolsDir '$(ProjectDir)WixToolset\' -sourceDir '$(TargetDir)' -targetDir '$(TargetDir)' -projectDir '$(ProjectDir)' -includeDir '$(ProjectDir)InnoSetup\Includes\' -filesToExtract 'Rubberduck.dll|Rubberduck.API.dll'"</PostBuildEvent>
120120
</PropertyGroup>
121121
<PropertyGroup>
122-
<PreBuildEvent>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -command "$(ProjectDir)PreInnoSetupConfiguration.ps1 -WorkingDir $(ProjectDir)</PreBuildEvent>
122+
<PreBuildEvent>C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -command "&amp; '$(ProjectDir)PreInnoSetupConfiguration.ps1' -WorkingDir '$(ProjectDir)'"</PreBuildEvent>
123123
</PropertyGroup>
124124
</Project>

Rubberduck.Interaction/Rubberduck.Interaction.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@
4646
<Compile Include="IMessageBox.cs" />
4747
<Compile Include="Properties\AssemblyInfo.cs" />
4848
</ItemGroup>
49+
<ItemGroup>
50+
<Analyzer Include="..\RubberduckCodeAnalysis\bin\Release\RubberduckCodeAnalysis.dll" />
51+
</ItemGroup>
4952
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5053
</Project>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using Antlr4.Runtime;
2+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
23
using System.Threading;
34

45
namespace Rubberduck.Parsing.PreProcessing
56
{
67
public interface IVBAPreprocessor
78
{
8-
void PreprocessTokenStream(string moduleName, CommonTokenStream unprocessedTokenStream, BaseErrorListener errorListener, CancellationToken token);
9+
void PreprocessTokenStream(IVBProject project, string moduleName, CommonTokenStream unprocessedTokenStream, BaseErrorListener errorListener, CancellationToken token);
910
}
1011
}

Rubberduck.Parsing/Preprocessing/VBAPredefinedCompilationConstants.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ public bool VBA7
2222
{
2323
get
2424
{
25-
return _vbVersion < 8;
25+
return _vbVersion >= 7;
2626
}
2727
}
2828

2929
public bool VBA6
3030
{
3131
get
3232
{
33-
return _vbVersion < 7;
33+
return _vbVersion >= 6;
3434
}
3535
}
3636

3737
public bool Win64
3838
{
3939
get
4040
{
41-
return Environment.Is64BitOperatingSystem;
41+
return IntPtr.Size >= 8;
4242
}
4343
}
4444

Rubberduck.Parsing/Preprocessing/VBAPreprocessor.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
using System.Threading;
33
using Rubberduck.Parsing.Symbols;
44
using Rubberduck.Parsing.VBA;
5+
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
6+
using System.Collections.Generic;
7+
using Rubberduck.VBEditor.ComManagement.TypeLibs;
8+
using Rubberduck.VBEditor.Utility;
9+
using Rubberduck.Parsing.UIContext;
510

611
namespace Rubberduck.Parsing.PreProcessing
712
{
@@ -16,17 +21,34 @@ public VBAPreprocessor(double vbaVersion)
1621
_parser = new VBAPrecompilationParser();
1722
}
1823

19-
public void PreprocessTokenStream(string moduleName, CommonTokenStream tokenStream, BaseErrorListener errorListener, CancellationToken token)
24+
public void PreprocessTokenStream(IVBProject project, string moduleName, CommonTokenStream tokenStream, BaseErrorListener errorListener, CancellationToken token)
2025
{
2126
token.ThrowIfCancellationRequested();
2227
var symbolTable = new SymbolTable<string, IValue>();
2328
var tree = _parser.Parse(moduleName, tokenStream, errorListener);
2429
token.ThrowIfCancellationRequested();
2530
var stream = tokenStream.TokenSource.InputStream;
26-
var evaluator = new VBAPreprocessorVisitor(symbolTable, new VBAPredefinedCompilationConstants(_vbaVersion), stream, tokenStream);
31+
var evaluator = new VBAPreprocessorVisitor(symbolTable, new VBAPredefinedCompilationConstants(_vbaVersion), GetUserDefinedCompilationArguments(project), stream, tokenStream);
2732
var expr = evaluator.Visit(tree);
2833
var processedTokens = expr.Evaluate(); //This does the actual preprocessing of the token stream as a side effect.
2934
tokenStream.Reset();
3035
}
36+
37+
public Dictionary<string, short> GetUserDefinedCompilationArguments(IVBProject project)
38+
{
39+
// for the mocks, just return an empty dictionary for now
40+
if (project == null) return new Dictionary<string, short>();
41+
42+
// use the TypeLib API to grab the user defined compilation arguments. must be obtained on the main thread.
43+
var providerInst = UiContextProvider.Instance();
44+
var task = (new UiDispatcher(providerInst)).StartTask(delegate () {
45+
using (var typeLib = TypeLibWrapper.FromVBProject(project))
46+
{
47+
return typeLib.ConditionalCompilationArguments;
48+
}
49+
});
50+
task.Wait();
51+
return task.Result;
52+
}
3153
}
3254
}

Rubberduck.Parsing/Preprocessing/VBAPreprocessorVisitor.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Antlr4.Runtime;
44
using Antlr4.Runtime.Misc;
55
using Rubberduck.Parsing.Symbols;
6+
using System.Collections.Generic;
67

78
namespace Rubberduck.Parsing.PreProcessing
89
{
@@ -15,6 +16,7 @@ public sealed class VBAPreprocessorVisitor : VBAConditionalCompilationParserBase
1516
public VBAPreprocessorVisitor(
1617
SymbolTable<string, IValue> symbolTable,
1718
VBAPredefinedCompilationConstants predefinedConstants,
19+
Dictionary<string, short> userDefinedConstants,
1820
ICharStream stream,
1921
CommonTokenStream tokenStream)
2022
{
@@ -39,16 +41,25 @@ public VBAPreprocessorVisitor(
3941
_tokenStream = tokenStream;
4042
_symbolTable = symbolTable;
4143
AddPredefinedConstantsToSymbolTable(predefinedConstants);
44+
AddUserDefinedConstantsToSymbolTable(userDefinedConstants);
4245
}
4346

4447
private void AddPredefinedConstantsToSymbolTable(VBAPredefinedCompilationConstants predefinedConstants)
4548
{
46-
_symbolTable.Add(VBAPredefinedCompilationConstants.VBA6_NAME, new BoolValue(predefinedConstants.VBA6));
47-
_symbolTable.Add(VBAPredefinedCompilationConstants.VBA7_NAME, new BoolValue(predefinedConstants.VBA7));
48-
_symbolTable.Add(VBAPredefinedCompilationConstants.WIN64_NAME, new BoolValue(predefinedConstants.Win64));
49-
_symbolTable.Add(VBAPredefinedCompilationConstants.WIN32_NAME, new BoolValue(predefinedConstants.Win32));
50-
_symbolTable.Add(VBAPredefinedCompilationConstants.WIN16_NAME, new BoolValue(predefinedConstants.Win16));
51-
_symbolTable.Add(VBAPredefinedCompilationConstants.MAC_NAME, new BoolValue(predefinedConstants.Mac));
49+
_symbolTable.Add(VBAPredefinedCompilationConstants.VBA6_NAME, new DecimalValue(predefinedConstants.VBA6 ? 1 : 0));
50+
_symbolTable.Add(VBAPredefinedCompilationConstants.VBA7_NAME, new DecimalValue(predefinedConstants.VBA7 ? 1 : 0));
51+
_symbolTable.Add(VBAPredefinedCompilationConstants.WIN64_NAME, new DecimalValue(predefinedConstants.Win64 ? 1 : 0));
52+
_symbolTable.Add(VBAPredefinedCompilationConstants.WIN32_NAME, new DecimalValue(predefinedConstants.Win32 ? 1 : 0));
53+
_symbolTable.Add(VBAPredefinedCompilationConstants.WIN16_NAME, new DecimalValue(predefinedConstants.Win16 ? 1 : 0));
54+
_symbolTable.Add(VBAPredefinedCompilationConstants.MAC_NAME, new DecimalValue(predefinedConstants.Mac ? 1 : 0));
55+
}
56+
57+
private void AddUserDefinedConstantsToSymbolTable(Dictionary<string, short> userDefinedConstants)
58+
{
59+
foreach (var constant in userDefinedConstants)
60+
{
61+
_symbolTable.Add(constant.Key, new DecimalValue(constant.Value));
62+
}
5263
}
5364

5465
public override IExpression VisitCompilationUnit([NotNull] VBAConditionalCompilationParser.CompilationUnitContext context)

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@
408408
<Compile Include="VBA\ParseCoordinator.cs" />
409409
<Compile Include="VBA\RubberduckParserState.cs" />
410410
<Compile Include="VBA\StringExtensions.cs" />
411+
<Compile Include="VBA\VBACodeStringParser.cs" />
411412
<Compile Include="VBA\VBALikePatternParser.cs" />
412413
<Compile Include="VBA\VBADateLiteralParser.cs" />
413414
<Compile Include="VBA\VBAExpressionParser.cs" />

0 commit comments

Comments
 (0)