Skip to content

Commit dd31347

Browse files
committed
Split the remaining refactorings, except EncapsulateField
1 parent a4ae6e5 commit dd31347

File tree

51 files changed

+1458
-1095
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1458
-1095
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Runtime.ExceptionServices;
2+
using NLog;
3+
using Rubberduck.Parsing.Rewriter;
4+
using Rubberduck.Parsing.VBA;
5+
using Rubberduck.Refactorings.Exceptions;
6+
7+
namespace Rubberduck.Refactorings
8+
{
9+
public abstract class BaseRefactoringWithSuspensionBase<TModel> : BaseRefactoringBase<TModel>
10+
where TModel : class, IRefactoringModel
11+
{
12+
private readonly IParseManager _parseManager;
13+
14+
private readonly Logger _logger;
15+
16+
protected BaseRefactoringWithSuspensionBase(IParseManager parseManager, IRewritingManager rewritingManager)
17+
: base(rewritingManager)
18+
{
19+
_parseManager = parseManager;
20+
_logger = LogManager.GetLogger(GetType().FullName);
21+
}
22+
23+
protected abstract bool RequiresSuspension(TModel model);
24+
25+
public override void Refactor(TModel model)
26+
{
27+
if (RequiresSuspension(model))
28+
{
29+
RefactorWithSuspendedParser(model);
30+
}
31+
else
32+
{
33+
base.Refactor(model);
34+
}
35+
}
36+
37+
private void RefactorWithSuspendedParser(TModel model)
38+
{
39+
var suspendResult = _parseManager.OnSuspendParser(this, new[] { ParserState.Ready }, () => base.Refactor(model));
40+
var suspendOutcome = suspendResult.Outcome;
41+
if (suspendOutcome != SuspensionOutcome.Completed)
42+
{
43+
if ((suspendOutcome == SuspensionOutcome.UnexpectedError || suspendOutcome == SuspensionOutcome.Canceled)
44+
&& suspendResult.EncounteredException != null)
45+
{
46+
ExceptionDispatchInfo.Capture(suspendResult.EncounteredException).Throw();
47+
return;
48+
}
49+
50+
_logger.Warn($"{GetType().Name} failed because a parser suspension request could not be fulfilled. The request's result was '{suspendResult.ToString()}'.");
51+
throw new SuspendParserFailureException();
52+
}
53+
}
54+
}
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Rubberduck.Parsing.Rewriter;
2+
using Rubberduck.Parsing.VBA.Parsing;
3+
using Rubberduck.Refactorings.Exceptions;
4+
5+
namespace Rubberduck.Refactorings
6+
{
7+
public abstract class CodeOnlyBaseRefactoringBase<TModel> : ICodeOnlyBaseRefactoring<TModel>
8+
where TModel : class, IRefactoringModel
9+
{
10+
private readonly IRewritingManager _rewritingManager;
11+
12+
protected CodeOnlyBaseRefactoringBase(IRewritingManager rewritingManager)
13+
{
14+
_rewritingManager = rewritingManager;
15+
}
16+
17+
public abstract void Refactor(TModel model, IRewriteSession rewriteSession);
18+
19+
public virtual void Refactor(TModel model)
20+
{
21+
var rewriteSession = RewriteSession(RewriteSessionCodeKind);
22+
23+
Refactor(model, rewriteSession);
24+
25+
if (!rewriteSession.TryRewrite())
26+
{
27+
throw new RewriteFailedException(rewriteSession);
28+
}
29+
}
30+
31+
private IExecutableRewriteSession RewriteSession(CodeKind codeKind)
32+
{
33+
return codeKind == CodeKind.AttributesCode
34+
? _rewritingManager.CheckOutAttributesSession()
35+
: _rewritingManager.CheckOutCodePaneSession();
36+
}
37+
38+
protected virtual CodeKind RewriteSessionCodeKind => CodeKind.CodePaneCode;
39+
}
40+
}

Rubberduck.Refactorings/IBaseRefactoring.cs renamed to Rubberduck.Refactorings/Abstract/IBaseRefactoring.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Rubberduck.Refactorings
1+
using Rubberduck.Parsing.Rewriter;
2+
3+
namespace Rubberduck.Refactorings
24
{
35
/// <summary>
46
/// The heart of a refactoring: this part of the refactoring performs the actual transformation of the code once all necessary information has been gathered.
@@ -13,7 +15,7 @@ public interface IBaseRefactoring<in TModel> where TModel : class, IRefactoringM
1315
void Refactor(TModel model);
1416
}
1517

16-
public interface IBaseRefactoringWithPreview<in TModel> : IBaseRefactoring<TModel>
18+
public interface IRefactoringPreviewProvider<in TModel>
1719
where TModel : class, IRefactoringModel
1820
{
1921
/// <summary>
@@ -23,4 +25,15 @@ public interface IBaseRefactoringWithPreview<in TModel> : IBaseRefactoring<TMode
2325
/// <returns>Preview of the refactored code</returns>
2426
string Preview(TModel model);
2527
}
28+
29+
public interface ICodeOnlyBaseRefactoring<in TModel> : IBaseRefactoring<TModel>
30+
where TModel : class, IRefactoringModel
31+
{
32+
/// <summary>
33+
/// Performs the refactoring according to the model and using the provided rewrite session.
34+
/// </summary>
35+
/// <param name="model">The model specifying all parameters of the refactoring</param>
36+
/// <param name="rewriteSession">Rewrite session used to manipulate the code (Does not get executed.)</param>
37+
void Refactor(TModel model, IRewriteSession rewriteSession);
38+
}
2639
}

0 commit comments

Comments
 (0)