Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RemoveUnnecessaryCastCodeFixProvider.cs « RemoveUnnecessaryCast « MonoDevelop.CSharp.CodeFixes « CSharpBinding « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 63770abbdf045417599856de88b3dd7f684ce6da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.

using System;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Extensions;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis;
using MonoDevelop.CSharp.Diagnostics;
using ICSharpCode.NRefactory6.CSharp;
using MonoDevelop.Core;
using ICSharpCode.NRefactory6.CSharp.Refactoring;
using Microsoft.CodeAnalysis.CSharp;

namespace MonoDevelop.CSharp.CodeFixes.RemoveUnnecessaryCast
{
	[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.RemoveUnnecessaryCast), Shared]
	[ExtensionOrder(After = PredefinedCodeFixProviderNames.ImplementInterface)]
	internal partial class RemoveUnnecessaryCastCodeFixProvider : CodeFixProvider
	{
		public sealed override ImmutableArray<string> FixableDiagnosticIds
		{
			get { return ImmutableArray.Create(IDEDiagnosticIds.RemoveUnnecessaryCastDiagnosticId); }
		}

//		public sealed override FixAllProvider GetFixAllProvider()
//		{
//			return RemoveUnnecessaryCastFixAllProvider.Instance;
//		}
//
		private static CastExpressionSyntax GetCastNode(SyntaxNode root, SemanticModel model, TextSpan span, CancellationToken cancellationToken)
		{
			var token = root.FindToken(span.Start);
			if (!token.Span.IntersectsWith(span))
			{
				return null;
			}

			return token.GetAncestors<CastExpressionSyntax>()
				.FirstOrDefault(c => c.Span.IntersectsWith(span) && c.IsUnnecessaryCast(model, cancellationToken));
		}

		public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
		{
			var document = context.Document;
			var span = context.Span;
			var cancellationToken = context.CancellationToken;

			var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
			var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
			var node = GetCastNode(root, model, span, cancellationToken);
			if (node == null)
			{
				return;
			}

			context.RegisterCodeFix(
				new DocumentChangeAction(node.Span, DiagnosticSeverity.Warning,
					GettextCatalog.GetString ("Remove Unnecessary Cast"),
					(c) => RemoveUnnecessaryCastAsync(document, node, c)),
				context.Diagnostics);
		}

		private static async Task<Document> RemoveUnnecessaryCastAsync(Document document, CastExpressionSyntax cast, CancellationToken cancellationToken)
		{
			var annotatedCast = cast.WithAdditionalAnnotations(Simplifier.Annotation);

			if (annotatedCast.Expression is ParenthesizedExpressionSyntax)
			{
				annotatedCast = annotatedCast.WithExpression(
					annotatedCast.Expression.WithAdditionalAnnotations(Simplifier.Annotation));
			}
			else
			{
				annotatedCast = annotatedCast.WithExpression(
					annotatedCast.Expression.Parenthesize());
			}

			ExpressionSyntax oldNode = cast;
			ExpressionSyntax newNode = annotatedCast;

			// Ensure that we simplify any parenting parenthesized expressions not just on the syntax tree level but also on Token based
			// Case 1:
			//  In the syntax, (((Task<Action>)x).Result)() 
			//                 oldNode = (Task<Action>)x
			//                 newNode = (Task<Action>)(x)
			//                 Final newNode will be (((Task<Action>)(x)).Result)
			while (oldNode.Parent.IsKind(SyntaxKind.ParenthesizedExpression) || oldNode.GetFirstToken().GetPreviousToken().Parent.IsKind(SyntaxKind.ParenthesizedExpression))
			{
				var parenthesizedExpression = (ParenthesizedExpressionSyntax)oldNode.GetFirstToken().GetPreviousToken().Parent;
				newNode = parenthesizedExpression.ReplaceNode(oldNode, newNode)
					.WithAdditionalAnnotations(Simplifier.Annotation);
				oldNode = parenthesizedExpression;
			}

			newNode = newNode.WithAdditionalAnnotations(Formatter.Annotation);

			var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
			var newRoot = root.ReplaceNode(oldNode, newNode);

			return document.WithSyntaxRoot(newRoot);
		}
//
//		private class MyCodeAction : CodeAction.DocumentChangeAction
//		{
//			public MyCodeAction(string title, Func<CancellationToken, Task<Document>> createChangedDocument) :
//			base(title, createChangedDocument)
//			{
//			}
//		}
	}
}