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

GenerateMethodCodeFixProvider.cs « GenerateMethod « MonoDevelop.CSharp.CodeFixes « CSharpBinding « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c1c8396fa3034fe7585ff1b0a5c5438c1c9c7f45 (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
// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis;
using MonoDevelop.CSharp.CodeFixes.GenerateConstructor;
using ICSharpCode.NRefactory6.CSharp.GenerateMember.GenerateParameterizedMember;
using Microsoft.CodeAnalysis.CSharp;
using ICSharpCode.NRefactory6.CSharp;

namespace MonoDevelop.CSharp.CodeFixes.GenerateMethod
{
	[ExportCodeFixProvider(LanguageNames.CSharp, Name = PredefinedCodeFixProviderNames.GenerateMethod), Shared]
	[ExtensionOrder(After = PredefinedCodeFixProviderNames.GenerateEnumMember)]
	internal class GenerateMethodCodeFixProvider : AbstractGenerateMemberCodeFixProvider
	{
		private const string CS0103 = "CS0103"; // error CS0103: Error The name 'Foo' does not exist in the current context
		private const string CS1061 = "CS1061"; // error CS1061: Error 'Class' does not contain a definition for 'Foo' and no extension method 'Foo' 
		private const string CS0117 = "CS0117"; // error CS0117: 'Class' does not contain a definition for 'Foo'
		private const string CS0122 = "CS0122"; // error CS0122: 'Class' is inaccessible due to its protection level.
		private const string CS0539 = "CS0539"; // error CS0539: 'A.Foo<T>()' in explicit interface declaration is not a member of interface
		private const string CS1501 = "CS1501"; // error CS1501: No overload for method 'M' takes 1 arguments
		private const string CS1503 = "CS1503"; // error CS1503: Argument 1: cannot convert from 'double' to 'int'
		private const string CS0305 = "CS0305"; // error CS0305: Using the generic method 'CA.M<V>()' requires 1 type arguments
		private const string CS0308 = "CS0308"; // error CS0308: The non-generic method 'Program.Foo()' cannot be used with type arguments
		private const string CS1660 = "CS1660"; // error CS1660: Cannot convert lambda expression to type 'string[]' because it is not a delegate type
		private const string CS1739 = "CS1739"; // error CS1739: The best overload for 'M' does not have a parameter named 'x'
		private const string CS7036 = "CS7036"; // error CS7036: There is no argument given that corresponds to the required formal parameter 'x' of 'C.M(int)'

		public override ImmutableArray<string> FixableDiagnosticIds
		{
			get { return ImmutableArray.Create(CS0103, CS1061, CS0117, CS0122, CS0539, CS1501, CS1503, CS0305, CS0308, CS1660, CS1739, CS7036); }
		}

		protected override bool IsCandidate(SyntaxNode node)
		{
			return node.IsKind(SyntaxKind.IdentifierName) ||
				node.IsKind(SyntaxKind.MethodDeclaration) ||
				node.IsKind(SyntaxKind.InvocationExpression) ||
				node.IsKind(SyntaxKind.CastExpression) ||
				node is LiteralExpressionSyntax ||
				node is SimpleNameSyntax ||
				node is ExpressionSyntax;
		}

		protected override SyntaxNode GetTargetNode(SyntaxNode node)
		{
			var invocation = node as InvocationExpressionSyntax;
			if (invocation != null)
			{
				return invocation.Expression.GetRightmostName();
			}

			var memberBindingExpression = node as MemberBindingExpressionSyntax;
			if (memberBindingExpression != null)
			{
				return memberBindingExpression.Name;
			}

			return node;
		}
		static CSharpGenerateMethodService service = new CSharpGenerateMethodService ();

		protected override Task<IEnumerable<CodeAction>> GetCodeActionsAsync(Document document, SyntaxNode node, CancellationToken cancellationToken)
		{
			return service.GenerateMethodAsync(document, node, cancellationToken);
		}
	}
}