当前位置: 首页>>代码示例>>C#>>正文


C# RefactoringContext.GetLocalNameProposal方法代码示例

本文整理汇总了C#中RefactoringContext.GetLocalNameProposal方法的典型用法代码示例。如果您正苦于以下问题:C# RefactoringContext.GetLocalNameProposal方法的具体用法?C# RefactoringContext.GetLocalNameProposal怎么用?C# RefactoringContext.GetLocalNameProposal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在RefactoringContext的用法示例。


在下文中一共展示了RefactoringContext.GetLocalNameProposal方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetActions

		public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
		{
			if (!context.IsSomethingSelected) {
				yield break;
			}
			var selected = new List<AstNode>(context.GetSelectedNodes());

			if (selected.Count != 1 || !(selected [0] is Expression)) {
				yield break;
			}

			var expr = selected [0] as Expression;
			if (expr is ArrayInitializerExpression) {
				var arr = (ArrayInitializerExpression)expr;
				if (arr.IsSingleElement) {
					expr = arr.Elements.First();
				} else {
					yield break;
				}
			}

			var visitor = new SearchNodeVisitior(expr);
			
			var node = context.GetNode <BlockStatement>();
			if (node != null) {
				node.AcceptVisitor(visitor);
			}

			yield return new CodeAction(context.TranslateString("Declare local variable"), script => {
				var resolveResult = context.Resolve(expr);
				var guessedType = resolveResult.Type;
				if (resolveResult is MethodGroupResolveResult) {
					guessedType = GetDelegateType(context, ((MethodGroupResolveResult)resolveResult).Methods.First(), expr);
				}
				var name = CreateMethodDeclarationAction.CreateBaseName(expr, guessedType);
				name = context.GetLocalNameProposal(name, expr.StartLocation);
				var type = context.UseExplicitTypes ? context.CreateShortType(guessedType) : new SimpleType("var");
				var varDecl = new VariableDeclarationStatement(type, name, expr.Clone());
				var replaceNode = visitor.Matches.First () as Expression;
				if (replaceNode.Parent is ExpressionStatement) {
					script.Replace(replaceNode.Parent, varDecl);
					script.Select(varDecl.Variables.First().NameToken);
				} else {
					var containing = replaceNode.Parent;
					while (!(containing.Parent is BlockStatement)) {
						containing = containing.Parent;
					}

					script.InsertBefore(containing, varDecl);
					var identifierExpression = new IdentifierExpression(name);
					script.Replace(replaceNode, identifierExpression);
					script.Link(varDecl.Variables.First().NameToken, identifierExpression);
				}
			}, expr);

			if (visitor.Matches.Count > 1) {
				yield return new CodeAction(string.Format(context.TranslateString("Declare local variable (replace '{0}' occurrences)"), visitor.Matches.Count), script => {
					var resolveResult = context.Resolve(expr);
					var guessedType = resolveResult.Type;
					if (resolveResult is MethodGroupResolveResult) {
						guessedType = GetDelegateType(context, ((MethodGroupResolveResult)resolveResult).Methods.First(), expr);
					}
					var linkedNodes = new List<AstNode>();
					var name = CreateMethodDeclarationAction.CreateBaseName(expr, guessedType);
					name = context.GetLocalNameProposal(name, expr.StartLocation);
					var type = context.UseExplicitTypes ? context.CreateShortType(guessedType) : new SimpleType("var");
					var varDecl = new VariableDeclarationStatement(type, name, expr.Clone());
					linkedNodes.Add(varDecl.Variables.First().NameToken);
					var first = visitor.Matches [0];
					if (first.Parent is ExpressionStatement) {
						script.Replace(first.Parent, varDecl);
					} else {
						var containing = first.Parent;
						while (!(containing.Parent is BlockStatement)) {
							containing = containing.Parent;
						}

						script.InsertBefore(containing, varDecl);
						var identifierExpression = new IdentifierExpression(name);
						linkedNodes.Add(identifierExpression);
						script.Replace(first, identifierExpression);
					}
					for (int i = 1; i < visitor.Matches.Count; i++) {
						var identifierExpression = new IdentifierExpression(name);
						linkedNodes.Add(identifierExpression);
						script.Replace(visitor.Matches [i], identifierExpression);
					}
					script.Link(linkedNodes.ToArray ());
				}, expr);
			}
		}
开发者ID:0xb1dd1e,项目名称:NRefactory,代码行数:91,代码来源:DeclareLocalVariableAction.cs


注:本文中的RefactoringContext.GetLocalNameProposal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。