Skip to content

Commit 9ba3d0f

Browse files
committed
Introduce AnnotateDeclarationRefactoringAction
1 parent 6710770 commit 9ba3d0f

File tree

8 files changed

+505
-4
lines changed

8 files changed

+505
-4
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using Rubberduck.Parsing.Annotations;
3+
using Rubberduck.Parsing.Symbols;
4+
5+
namespace Rubberduck.Refactorings.AnnotateDeclaration
6+
{
7+
//This is introduced in favor of a value tuple in order to ba able to bind to the components in XAML.
8+
public struct TypedAnnotationArgument
9+
{
10+
public AnnotationArgumentType ArgumentType { get; set; }
11+
public string Argument { get; set; }
12+
13+
public TypedAnnotationArgument(AnnotationArgumentType type, string argument)
14+
{
15+
ArgumentType = type;
16+
Argument = argument;
17+
}
18+
}
19+
20+
public class AnnotateDeclarationModel : IRefactoringModel
21+
{
22+
public Declaration Target { get; }
23+
public IAnnotation Annotation { get; set; }
24+
public IList<TypedAnnotationArgument> Arguments { get; }
25+
26+
public AnnotateDeclarationModel(
27+
Declaration target,
28+
IAnnotation annotation = null,
29+
IList<TypedAnnotationArgument> arguments = null)
30+
{
31+
Target = target;
32+
Annotation = annotation;
33+
Arguments = arguments ?? new List<TypedAnnotationArgument>();
34+
}
35+
}
36+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Rubberduck.Parsing.Symbols;
2+
using Rubberduck.Parsing.VBA;
3+
using Rubberduck.Refactorings.Exceptions;
4+
using Rubberduck.VBEditor;
5+
using Rubberduck.VBEditor.Utility;
6+
7+
namespace Rubberduck.Refactorings.AnnotateDeclaration
8+
{
9+
public class AnnotateDeclarationRefactoring : InteractiveRefactoringBase<AnnotateDeclarationModel>
10+
{
11+
private readonly IRefactoringAction<AnnotateDeclarationModel> _annotateDeclarationAction;
12+
private readonly ISelectedDeclarationProvider _selectedDeclarationProvider;
13+
14+
public AnnotateDeclarationRefactoring(
15+
AnnotateDeclarationRefactoringAction annotateDeclarationAction,
16+
ISelectedDeclarationProvider selectedDeclarationProvider,
17+
ISelectionProvider selectionProvider,
18+
RefactoringUserInteraction<IAnnotateDeclarationPresenter, AnnotateDeclarationModel> userInteraction)
19+
: base(selectionProvider, userInteraction)
20+
{
21+
_annotateDeclarationAction = annotateDeclarationAction;
22+
_selectedDeclarationProvider = selectedDeclarationProvider;
23+
}
24+
25+
protected override Declaration FindTargetDeclaration(QualifiedSelection targetSelection)
26+
{
27+
return _selectedDeclarationProvider.SelectedModule(targetSelection);
28+
}
29+
30+
protected override AnnotateDeclarationModel InitializeModel(Declaration target)
31+
{
32+
if (target == null)
33+
{
34+
throw new TargetDeclarationIsNullException();
35+
}
36+
37+
return new AnnotateDeclarationModel(target);
38+
}
39+
40+
protected override void RefactorImpl(AnnotateDeclarationModel model)
41+
{
42+
_annotateDeclarationAction.Refactor(model);
43+
}
44+
}
45+
}
46+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Linq;
3+
using Rubberduck.Common;
4+
using Rubberduck.Parsing.Annotations;
5+
using Rubberduck.Parsing.Grammar;
6+
using Rubberduck.Parsing.Rewriter;
7+
using Rubberduck.Parsing.VBA;
8+
9+
namespace Rubberduck.Refactorings.AnnotateDeclaration
10+
{
11+
public class AnnotateDeclarationRefactoringAction : CodeOnlyRefactoringActionBase<AnnotateDeclarationModel>
12+
{
13+
private readonly IAnnotationUpdater _annotationUpdater;
14+
15+
public AnnotateDeclarationRefactoringAction(
16+
IRewritingManager rewritingManager,
17+
IAnnotationUpdater annotationUpdater)
18+
: base(rewritingManager)
19+
{
20+
_annotationUpdater = annotationUpdater;
21+
}
22+
23+
public override void Refactor(AnnotateDeclarationModel model, IRewriteSession rewriteSession)
24+
{
25+
var arguments = model.Arguments.Select(ToCode).ToList();
26+
_annotationUpdater.AddAnnotation(rewriteSession, model.Target, model.Annotation, arguments);
27+
}
28+
29+
private string ToCode(TypedAnnotationArgument annotationArgument)
30+
{
31+
switch (annotationArgument.ArgumentType)
32+
{
33+
case AnnotationArgumentType.Text:
34+
return annotationArgument.Argument.ToVbaStringLiteral();
35+
case AnnotationArgumentType.Boolean:
36+
return ToBooleanLiteral(annotationArgument.Argument);
37+
default:
38+
return annotationArgument.Argument;
39+
}
40+
}
41+
42+
private const string NotABoolean = "NOT_A_BOOLEAN";
43+
private string ToBooleanLiteral(string booleanText)
44+
{
45+
if (!bool.TryParse(booleanText, out var booleanValue))
46+
{
47+
return NotABoolean;
48+
}
49+
50+
return booleanValue
51+
? Tokens.True
52+
: Tokens.False;
53+
}
54+
}
55+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace Rubberduck.Refactorings.AnnotateDeclaration
2+
{
3+
public interface IAnnotateDeclarationPresenter : IRefactoringPresenter<AnnotateDeclarationModel>
4+
{}
5+
}

Rubberduck.Refactorings/MoveToFolder/MoveToFolderRefactoringAction.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ private void UpdateFolderAnnotation(MoveToFolderModel model, IParseTreeAnnotatio
4747

4848
private static (IAnnotation annotation, IReadOnlyList<string> annotationArguments) NewAnnotation(string targetFolder)
4949
{
50-
var targetFolderLiteral = targetFolder
51-
.Replace("\"", "\"\"")
52-
.EnQuote();
50+
var targetFolderLiteral = targetFolder.ToVbaStringLiteral();
5351

5452
var annotation = new FolderAnnotation();
5553
var annotationValues = new List<string> { targetFolderLiteral };

0 commit comments

Comments
 (0)