Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.ExtLibs/HeuristicLab.NRefactory/5.5.0/NRefactory.CSharp-5.5.0/Refactoring/FormatStringHelper.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 3.7 KB
Line 
1//
2// FormatStringHelper.cs
3//
4// Author:
5//       Simon Lindgren <simon.n.lindgren@gmail.com>
6//
7// Copyright (c) 2012 Simon Lindgren
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to deal
11// in the Software without restriction, including without limitation the rights
12// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13// copies of the Software, and to permit persons to whom the Software is
14// furnished to do so, subject to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25// THE SOFTWARE.
26using System;
27using System.Collections.Generic;
28using ICSharpCode.NRefactory.CSharp.Resolver;
29using System.Linq;
30using ICSharpCode.NRefactory.TypeSystem;
31
32namespace ICSharpCode.NRefactory.CSharp
33{
34  static class FormatStringHelper
35  {
36    static readonly string[] parameterNames = { "format", "frmt", "fmt" };
37   
38    public static bool TryGetFormattingParameters(CSharpInvocationResolveResult invocationResolveResult, InvocationExpression invocationExpression,
39                                              out Expression formatArgument, out IList<Expression> arguments,
40                                                  Func<IParameter, Expression, bool> argumentFilter)
41    {
42      if (argumentFilter == null)
43        argumentFilter = (p, e) => true;
44
45      formatArgument = null;
46      arguments = new List<Expression>();
47
48      // Serach for method of type: void Name(string format, params object[] args);
49      if (invocationResolveResult.Member.SymbolKind == SymbolKind.Method) {
50        var methods = invocationResolveResult.Member.DeclaringType.GetMethods(m => m.Name == invocationResolveResult.Member.Name).ToList();
51        if (!methods.Any(m => m.Parameters.Count == 2 &&
52          m.Parameters[0].Type.IsKnownType(KnownTypeCode.String) && parameterNames.Contains(m.Parameters[0].Name) &&
53          m.Parameters[1].IsParams))
54          return false;
55      }
56
57      var argumentToParameterMap = invocationResolveResult.GetArgumentToParameterMap();
58      var resolvedParameters = invocationResolveResult.Member.Parameters;
59      var allArguments = invocationExpression.Arguments.ToArray();
60      for (int i = 0; i < allArguments.Length; i++) {
61        var parameterIndex = argumentToParameterMap[i];
62        if (parameterIndex < 0 || parameterIndex >= resolvedParameters.Count) {
63          // No valid mapping for this argument, skip it
64          continue;
65        }
66        var parameter = resolvedParameters[parameterIndex];
67        var argument = allArguments[i];
68        if (i == 0 && parameter.Type.IsKnownType(KnownTypeCode.String) && parameterNames.Contains(parameter.Name)) {
69          formatArgument = argument;
70        } else if (formatArgument != null && parameter.IsParams && !invocationResolveResult.IsExpandedForm) {
71          var ace = argument as ArrayCreateExpression;
72          if (ace == null || ace.Initializer.IsNull)
73            return false;
74          foreach (var element in ace.Initializer.Elements) {
75            if (argumentFilter(parameter, element))
76              arguments.Add(argument);
77          }
78        } else if (formatArgument != null && argumentFilter(parameter, argument)) {
79          arguments.Add(argument);
80        }
81      }
82      return formatArgument != null;
83    }
84  }
85}
86
Note: See TracBrowser for help on using the repository browser.