<commit_before>﻿using System.Linq;
using JetBrains.Annotations;
using JetBrains.ReSharper.Feature.Services.Lookup;
using JetBrains.ReSharper.PostfixTemplates.LookupItems;
using JetBrains.ReSharper.Psi.CSharp;
using JetBrains.ReSharper.Psi.CSharp.Tree;

namespace JetBrains.ReSharper.PostfixTemplates.Templates
{
  // todo: (Bar) foo.par - available in auto?

  [PostfixTemplate(
    templateName: "par",
    description: "Parenthesizes current expression",
    example: "(expr)")]
  public class ParenthesizedExpressionTemplate : IPostfixTemplate
  {
    public ILookupItem CreateItem(PostfixTemplateContext context)
    {
      if (context.IsAutoCompletion) return null;

      PrefixExpressionContext bestContext = null;
      foreach (var expressionContext in context.Expressions.Reverse())
      {
        if (CommonUtils.IsNiceExpression(expressionContext.Expression))
        {
          bestContext = expressionContext;
          break;
        }
      }

      return new ParenthesesItem(bestContext ?? context.OuterExpression);
    }

    private sealed class ParenthesesItem : ExpressionPostfixLookupItem<ICSharpExpression>
    {
      public ParenthesesItem([NotNull] PrefixExpressionContext context) : base("par", context) { }

      protected override ICSharpExpression CreateExpression(CSharpElementFactory factory,
                                                            ICSharpExpression expression)
      {
        return factory.CreateExpression("($0)", expression);
      }
    }
  }
}<commit_msg>Allow .par in auto in case of '(T) expr.par'<commit_after>﻿using System.Linq;
using JetBrains.Annotations;
using JetBrains.ReSharper.Feature.Services.Lookup;
using JetBrains.ReSharper.PostfixTemplates.LookupItems;
using JetBrains.ReSharper.Psi.CSharp;
using JetBrains.ReSharper.Psi.CSharp.Tree;

namespace JetBrains.ReSharper.PostfixTemplates.Templates
{
  [PostfixTemplate(
    templateName: "par",
    description: "Parenthesizes current expression",
    example: "(expr)")]
  public class ParenthesizedExpressionTemplate : IPostfixTemplate
  {
    public ILookupItem CreateItem(PostfixTemplateContext context)
    {
      PrefixExpressionContext bestContext = null;
      foreach (var expressionContext in context.Expressions.Reverse())
      {
        if (CommonUtils.IsNiceExpression(expressionContext.Expression))
        {
          bestContext = expressionContext;
          break;
        }
      }

      // available in auto over cast expressions
      var targetContext = bestContext ?? context.OuterExpression;
      var insideCastExpression = CastExpressionNavigator.GetByOp(targetContext.Expression) != null;

      if (!insideCastExpression && context.IsAutoCompletion) return null;

      return new ParenthesesItem(targetContext);
    }

    private sealed class ParenthesesItem : ExpressionPostfixLookupItem<ICSharpExpression>
    {
      public ParenthesesItem([NotNull] PrefixExpressionContext context) : base("par", context) { }

      protected override ICSharpExpression CreateExpression(CSharpElementFactory factory,
                                                            ICSharpExpression expression)
      {
        return factory.CreateExpression("($0)", expression);
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.08179012345679013, 'fraction_numerical': 0.0006172839506172839, 'mean_word_length': 3.3213333333333335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2548324', 'n_tokens_mistral': 867, 'n_tokens_neox': 834, 'n_words': 174}