Skip to content

Commit 603de48

Browse files
authored
Merge pull request #4792 from retailcoder/scp-bugfix
SCP Fixes
2 parents d9e4a3c + fbf5689 commit 603de48

16 files changed

+104
-63
lines changed

Rubberduck.Core/AutoComplete/AutoCompleteHandlerBase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace Rubberduck.AutoComplete
77
{
8+
/// <summary>
9+
/// A base class/interface for AC services / "handlers".
10+
/// </summary>
811
public abstract class AutoCompleteHandlerBase
912
{
1013
protected AutoCompleteHandlerBase(ICodePaneHandler pane)
@@ -14,6 +17,13 @@ protected AutoCompleteHandlerBase(ICodePaneHandler pane)
1417

1518
protected ICodePaneHandler CodePaneHandler { get; }
1619

20+
/// <summary>
21+
/// A method that returns <c>false</c> if the input isn't handled, <c>true</c> if it is.
22+
/// </summary>
23+
/// <param name="e">The autocompletion event info</param>
24+
/// <param name="settings">The current AC settings</param>
25+
/// <param name="result">If handled, the resulting <c>CodeString</c></param>
26+
/// <returns></returns>
1727
public abstract bool Handle(AutoCompleteEventArgs e, AutoCompleteSettings settings, out CodeString result);
1828
}
1929
}

Rubberduck.Core/AutoComplete/Service/AutoCompleteService.cs renamed to Rubberduck.Core/AutoComplete/AutoCompleteService.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
using Rubberduck.Settings;
66
using Rubberduck.VBEditor.Events;
77

8-
namespace Rubberduck.AutoComplete.Service
8+
namespace Rubberduck.AutoComplete
99
{
10+
/// <summary>
11+
/// A service responsible for dispatching CodePane work to more specialized autocompletion services.
12+
/// Handles changes in configuration settings.
13+
/// </summary>
1014
public class AutoCompleteService : IDisposable
1115
{
1216
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
@@ -151,6 +155,7 @@ private bool TryHandle(AutoCompleteEventArgs e, AutoCompleteHandlerBase handler)
151155
return false;
152156
}
153157

158+
Logger.Debug($"Keypress was handled by {handler.GetType().Name}.");
154159
e.Handled = true;
155160
return true;
156161

@@ -169,7 +174,8 @@ public void Dispose()
169174
}
170175

171176
private bool _isDisposed;
172-
protected virtual void Dispose(bool disposing)
177+
178+
private void Dispose(bool disposing)
173179
{
174180
if (_isDisposed || !disposing)
175181
{

Rubberduck.Core/AutoComplete/Service/SelfClosingPair.cs renamed to Rubberduck.Core/AutoComplete/SelfClosingPairs/SelfClosingPair.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using Rubberduck.VBEditor;
2-
using System;
1+
using System;
2+
using Rubberduck.VBEditor;
33

4-
namespace Rubberduck.AutoComplete.Service
4+
namespace Rubberduck.AutoComplete.SelfClosingPairs
55
{
66
public class SelfClosingPair : IEquatable<SelfClosingPair>
77
{

Rubberduck.Core/AutoComplete/Service/SelfClosingPairCompletionService.cs renamed to Rubberduck.Core/AutoComplete/SelfClosingPairs/SelfClosingPairCompletionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using Rubberduck.Parsing.VBA.Parsing;
99
using Rubberduck.VBEditor;
1010

11-
namespace Rubberduck.AutoComplete.Service
11+
namespace Rubberduck.AutoComplete.SelfClosingPairs
1212
{
1313
public class SelfClosingPairCompletionService
1414
{

Rubberduck.Core/AutoComplete/Service/SelfClosingPairHandler.cs renamed to Rubberduck.Core/AutoComplete/SelfClosingPairs/SelfClosingPairHandler.cs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
using System.Collections.Generic;
2-
using System.Diagnostics;
1+
using System;
2+
using System.Collections.Generic;
33
using System.Linq;
44
using Rubberduck.Settings;
55
using Rubberduck.VBEditor;
66
using Rubberduck.VBEditor.Events;
77
using Rubberduck.VBEditor.SourceCodeHandling;
88

9-
namespace Rubberduck.AutoComplete.Service
9+
namespace Rubberduck.AutoComplete.SelfClosingPairs
1010
{
11+
/// <summary>
12+
/// An AC handler that automatically closes certain specific "pairs" of characters, e.g. double quotes, or parentheses.
13+
/// </summary>
1114
public class SelfClosingPairHandler : AutoCompleteHandlerBase
1215
{
1316
private const int MaximumLines = 25;
@@ -39,6 +42,7 @@ public override bool Handle(AutoCompleteEventArgs e, AutoCompleteSettings settin
3942
result = null;
4043
if (!_scpInputLookup.TryGetValue(e.Character, out var pair) && e.Character != '\b')
4144
{
45+
// not an interesting keypress.
4246
return false;
4347
}
4448

@@ -50,15 +54,25 @@ public override bool Handle(AutoCompleteEventArgs e, AutoCompleteSettings settin
5054
return false;
5155
}
5256

57+
if (!original.CaretPosition.IsSingleCharacter)
58+
{
59+
// here would be an opportunity to "wrap selection" with a SCP.
60+
// todo: WrapSelectionWith(pair)?
61+
result = null;
62+
return false;
63+
}
64+
5365
if (pair != null)
5466
{
67+
// found a SCP for the input key; see if we should handle it:
5568
if (!HandleInternal(e, original, pair, out result))
5669
{
5770
return false;
5871
}
5972
}
6073
else if (e.Character == '\b')
6174
{
75+
// backspace - see if SCP logic needs to intervene:
6276
foreach (var scp in _selfClosingPairs)
6377
{
6478
if (HandleInternal(e, original, scp, out result))
@@ -70,9 +84,11 @@ public override bool Handle(AutoCompleteEventArgs e, AutoCompleteSettings settin
7084

7185
if (result == null)
7286
{
87+
// no meaningful output; let the input be handled by another handler, maybe.
7388
return false;
7489
}
7590

91+
// 1-based selection span in the code pane starts at column 1 but really encompasses the entire line.
7692
var snippetPosition = new Selection(result.SnippetPosition.StartLine, 1, result.SnippetPosition.EndLine, 1);
7793
result = new CodeString(result.Code, result.CaretPosition, snippetPosition);
7894

@@ -82,13 +98,6 @@ public override bool Handle(AutoCompleteEventArgs e, AutoCompleteSettings settin
8298

8399
private bool HandleInternal(AutoCompleteEventArgs e, CodeString original, SelfClosingPair pair, out CodeString result)
84100
{
85-
if (!original.CaretPosition.IsSingleCharacter)
86-
{
87-
// todo: WrapSelectionWith(pair)?
88-
result = null;
89-
return false;
90-
}
91-
92101
// if executing the SCP against the original code yields no result, we need to bail out.
93102
if (!_scpService.Execute(pair, original, e.Character, out result))
94103
{
@@ -115,21 +124,34 @@ private bool HandleInternal(AutoCompleteEventArgs e, CodeString original, SelfCl
115124
);
116125
}
117126

127+
if (original.CaretLine.EndsWith(" ") &&
128+
string.Equals(original.CaretLine, prettified.CaretLine + " ", StringComparison.InvariantCultureIgnoreCase))
129+
{
130+
prettified = original;
131+
}
132+
118133
// if executing the SCP against the prettified code yields no result, we need to bail out.
119134
if (!_scpService.Execute(pair, prettified, e.Character, out result))
120135
{
121136
return false;
122137
}
123138

124139
var reprettified = CodePaneHandler.Prettify(e.Module, result);
125-
if (pair.OpeningChar == '(' && e.Character == pair.OpeningChar && !reprettified.Equals(result))
140+
if (pair.OpeningChar == '(' && e.Character == pair.OpeningChar)
126141
{
142+
if (string.Equals(reprettified.Code, result.Code, StringComparison.InvariantCultureIgnoreCase))
143+
{
144+
e.Handled = true;
145+
result = reprettified;
146+
return true;
147+
}
148+
127149
// VBE eats it. bail out but don't swallow the keypress.
128150
e.Handled = false;
129151
result = null;
130152
return false;
131153
}
132-
154+
133155
var currentLine = reprettified.Lines[reprettified.CaretPosition.StartLine];
134156
if (!string.IsNullOrWhiteSpace(currentLine) &&
135157
currentLine.EndsWith(" ") &&

Rubberduck.Core/AutoComplete/Service/ShowIntelliSenseCommand.cs renamed to Rubberduck.Core/AutoComplete/ShowIntelliSenseCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Rubberduck.UI.Command;
44
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
55

6-
namespace Rubberduck.AutoComplete.Service
6+
namespace Rubberduck.AutoComplete
77
{
88
public interface IShowIntelliSenseCommand
99
{

Rubberduck.Core/AutoComplete/Service/SmartConcatenationHandler.cs renamed to Rubberduck.Core/AutoComplete/SmartConcat/SmartConcatenationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Rubberduck.VBEditor.Events;
55
using Rubberduck.VBEditor.SourceCodeHandling;
66

7-
namespace Rubberduck.AutoComplete.Service
7+
namespace Rubberduck.AutoComplete.SmartConcat
88
{
99
/// <summary>
1010
/// Adds a line continuation when {ENTER} is pressed when inside a string literal.

Rubberduck.Core/Common/RubberduckHooks.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
99
using Rubberduck.VBEditor.WindowsApi;
1010
using Rubberduck.AutoComplete;
11-
using Rubberduck.AutoComplete.Service;
1211

1312
namespace Rubberduck.Common
1413
{

Rubberduck.Core/UI/Settings/AutoCompleteSettings.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
MaxNumber="{Binding Path=DataContext.ConcatMaxLinesMaxValue, RelativeSource={RelativeSource Self}}">
131131
132132
</controls:NumberPicker>-->
133+
<!--
133134
<Label Margin="10"
134135
Content="{Resx ResxName=Rubberduck.Resources.Settings.AutoCompletesPage, Key=BlockCompletion}"
135136
FontWeight="Bold" />
@@ -145,6 +146,7 @@
145146
IsEnabled="{Binding EnableBlockCompletion}"
146147
IsChecked="{Binding CompleteBlockOnTab}"
147148
Content="{Resx ResxName=Rubberduck.Resources.Settings.AutoCompletesPage, Key=CompleteBlockOnTab}" />
149+
-->
148150
</StackPanel>
149151
</ScrollViewer>
150152
</Grid>

Rubberduck.Main/Root/RubberduckIoCInstaller.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
using Rubberduck.VBEditor.Events;
4242
using Rubberduck.VBEditor.Utility;
4343
using Rubberduck.AutoComplete;
44-
using Rubberduck.AutoComplete.Service;
4544
using Rubberduck.CodeAnalysis.CodeMetrics;
4645
using Rubberduck.Parsing.Rewriter;
4746
using Rubberduck.Parsing.VBA.ComReferenceLoading;

0 commit comments

Comments
 (0)