static PrimitiveExpression CreateFormatString(RefactoringContext context, PrimitiveExpression pExpr, int argumentNumber)
        {
            var start  = context.GetOffset(pExpr.StartLocation);
            var end    = context.GetOffset(pExpr.EndLocation);
            var sStart = context.GetOffset(context.SelectionStart);
            var sEnd   = context.GetOffset(context.SelectionEnd);

            return(new PrimitiveExpression("", context.GetText(start, sStart - start) + "{" + argumentNumber + "}" + context.GetText(sEnd, end - sEnd)));
        }
Exemplo n.º 2
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            if (context.IsSomethingSelected)
            {
                yield break;
            }
            var pexpr = context.GetNode <PrimitiveExpression>();

            if (pexpr == null || !(pexpr.Value is string))
            {
                yield break;
            }
            if (pexpr.LiteralValue.StartsWith("@", StringComparison.Ordinal))
            {
                if (!(pexpr.StartLocation < new TextLocation(context.Location.Line, context.Location.Column - 2) &&
                      new TextLocation(context.Location.Line, context.Location.Column + 2) < pexpr.EndLocation))
                {
                    yield break;
                }
            }
            else
            {
                if (!(pexpr.StartLocation < new TextLocation(context.Location.Line, context.Location.Column - 1) && new TextLocation(context.Location.Line, context.Location.Column + 1) < pexpr.EndLocation))
                {
                    yield break;
                }
            }

            yield return(new CodeAction(context.TranslateString("Split string literal"), script => {
                int offset = context.GetOffset(context.Location);
                script.InsertText(offset, pexpr.LiteralValue.StartsWith("@", StringComparison.Ordinal) ? "\" + @\"" : "\" + \"");
            }));
        }
Exemplo n.º 3
0
        public IEnumerable <CodeAction> GetActions(RefactoringContext context)
        {
            IType type;
            var   anonymousMethodExpression = GetAnonymousMethodExpression(context, out type);

            if (anonymousMethodExpression == null)
            {
                yield break;
            }
            yield return(new CodeAction(context.TranslateString("Insert anonymous method signature"), script => {
                var delegateMethod = type.GetDelegateInvokeMethod();

                var sb = new StringBuilder("(");
                for (int k = 0; k < delegateMethod.Parameters.Count; k++)
                {
                    if (k > 0)
                    {
                        sb.Append(", ");
                    }

                    var paramType = delegateMethod.Parameters [k].Type;

                    sb.Append(context.CreateShortType(paramType));
                    sb.Append(" ");
                    sb.Append(delegateMethod.Parameters [k].Name);
                }
                sb.Append(")");

                script.InsertText(context.GetOffset(anonymousMethodExpression.DelegateToken.EndLocation), sb.ToString());
            }, anonymousMethodExpression));
        }