Name suggestions

Hi!

Can you explain how to properely get name suggestions for a variable i.e. catch clause variable?
I did something like his below but I'm not sure if this take advantage of all settings and if it is right.

        public string SuggestVariableName()
        {
            var namesCollection = NameSuggestionUtil.CreateEmptyCollection(
                this.CatchClauseNode.Language, PluralityKinds.Single, true, this.CatchClauseNode.GetSolution());
            namesCollection.Add(this.GetCatchedException(), PluralityKinds.Single);

            var roots = new List<NameRoot>(namesCollection.GetRoots("e", PredefinedPrefixPolicy.Preserve));
            var newNames = new List<string>(NamingManager.SuggestUniqueShortNames(roots, this.CatchClauseNode, NamedElementKinds.Locals, ScopeKind.LocalSelfScoped));

            return newNames.Count > 0 ? newNames.GetLast() : "exception";
        }

Regards,
Bartek.

1 comment

   // Am I right in assumption that you're trying to suggest name for 'catch' variable having expression? If I am here is a slightly corrected version:

    public string SuggestVariableName()
    {
      var namesCollection = NameSuggestionUtil.CreateEmptyCollection(CatchClauseNode.Language, PluralityKinds.Single, true, CatchClauseNode.GetSolution());
      namesCollection.Add(GetCatchedException(), PluralityKinds.Single);
      var roots = new List<NameRoot>(namesCollection.GetRoots("e", PredefinedPrefixPolicy.Preserve));
      // It's always better to have a 'declared element' you trying to make out a name for...
      // ...
      var specificCatch = CatchClauseNode as ISpecificCatchClause;
      IDeclaredElement element = null;
      if (specificCatch != null)
      {
        element = specificCatch.ExceptionDeclaration.DeclaredElement;
      } else
      {
        // transform to specific...?
      }
      // having declared element we can precisely choose naming rule.
      var namingRule = NamingManager.GetRulesProvider(CatchClauseNode.Language).GetPolicy(element).NamingRule;
      // ScopeKind.LocalSelfScoped should be used for lambda parameter names))...
      return NamingManager.GetUniqueShortName(roots, CatchClauseNode.ToTreeNode(), ScopeKind.Common, namingRule);
    }

0

Please sign in to leave a comment.