Skip to content

Commit a4ae6e5

Browse files
committed
Introduce IBaseRefactoring<TModel> and split ReorderParameterRefactoring using it
1 parent dc732ef commit a4ae6e5

File tree

6 files changed

+499
-411
lines changed

6 files changed

+499
-411
lines changed
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 BaseRefactoringBase<TModel> : IBaseRefactoring<TModel>
8+
where TModel : class, IRefactoringModel
9+
{
10+
private readonly IRewritingManager _rewritingManager;
11+
12+
protected BaseRefactoringBase(IRewritingManager rewritingManager)
13+
{
14+
_rewritingManager = rewritingManager;
15+
}
16+
17+
protected 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+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Rubberduck.Refactorings
2+
{
3+
/// <summary>
4+
/// The heart of a refactoring: this part of the refactoring performs the actual transformation of the code once all necessary information has been gathered.
5+
/// </summary>
6+
/// <typeparam name="TModel">The model used by the refactoring containing all information needed to specify what to do.</typeparam>
7+
public interface IBaseRefactoring<in TModel> where TModel : class, IRefactoringModel
8+
{
9+
/// <summary>
10+
/// Performs the actual refactoring based on the parameters specified in the model.
11+
/// </summary>
12+
/// <param name="model">The model specifying all parameters of the refactoring</param>
13+
void Refactor(TModel model);
14+
}
15+
16+
public interface IBaseRefactoringWithPreview<in TModel> : IBaseRefactoring<TModel>
17+
where TModel : class, IRefactoringModel
18+
{
19+
/// <summary>
20+
/// Returns some preview of the refactored code.
21+
/// </summary>
22+
/// <param name="model">The model used by the refactoring containing all information needed to specify what to do.</param>
23+
/// <returns>Preview of the refactored code</returns>
24+
string Preview(TModel model);
25+
}
26+
}

0 commit comments

Comments
 (0)