public string GetErrorMessage(BaseRefactoringContext ctx, string name, out IList <string> suggestedNames)
        {
            suggestedNames = new List <string>();
            string id = name;

            string errorMessage = null;

            bool   missingRequiredPrefix = false;
            bool   missingRequiredSuffix = false;
            string requiredPrefix        = null;
            string allowedPrefix         = null;
            string suffix = null;

            if (AllowedPrefixes != null && AllowedPrefixes.Length > 0)
            {
                allowedPrefix = AllowedPrefixes.FirstOrDefault(p => id.StartsWith(p));
                if (allowedPrefix != null)
                {
                    id = id.Substring(allowedPrefix.Length);
                }
            }

            if (RequiredPrefixes != null && RequiredPrefixes.Length > 0)
            {
                requiredPrefix = RequiredPrefixes.FirstOrDefault(p => id.StartsWith(p));
                if (requiredPrefix == null)
                {
                    errorMessage          = string.Format(ctx.TranslateString("Name should have prefix '{0}'."), RequiredPrefixes [0]);
                    missingRequiredPrefix = true;
                }
                else
                {
                    id = id.Substring(requiredPrefix.Length);
                }
            }
            else if (ForbiddenPrefixes != null && ForbiddenPrefixes.Length > 0)
            {
                requiredPrefix = ForbiddenPrefixes.FirstOrDefault(p => id.StartsWith(p));
                if (requiredPrefix != null)
                {
                    errorMessage = string.Format(ctx.TranslateString("Name has forbidden prefix '{0}'."), requiredPrefix);
                    id           = id.Substring(requiredPrefix.Length);
                }
            }

            if (RequiredSuffixes != null && RequiredSuffixes.Length > 0)
            {
                suffix = RequiredSuffixes.FirstOrDefault(s => id.EndsWith(s));
                if (suffix == null)
                {
                    errorMessage          = string.Format(ctx.TranslateString("Name should have suffix '{0}'."), RequiredSuffixes [0]);
                    missingRequiredSuffix = true;
                }
                else
                {
                    id = id.Substring(0, id.Length - suffix.Length);
                }
            }
            else if (ForbiddenSuffixes != null && ForbiddenSuffixes.Length > 0)
            {
                suffix = ForbiddenSuffixes.FirstOrDefault(p => id.EndsWith(p));
                if (suffix != null)
                {
                    errorMessage = string.Format(ctx.TranslateString("Name has forbidden suffix '{0}'."), suffix);
                    id           = id.Substring(0, id.Length - suffix.Length);
                }
            }

            switch (NamingStyle)
            {
            case NamingStyle.AllLower:
                if (id.Any(ch => char.IsLetter(ch) && char.IsUpper(ch)))
                {
                    errorMessage = string.Format(ctx.TranslateString("'{0}' contains upper case letters."), name);
                    suggestedNames.Add(LowerCaseIdentifier(WordParser.BreakWords(id)));
                }
                else
                {
                    suggestedNames.Add(id);
                }
                break;

            case NamingStyle.AllUpper:
                if (id.Any(ch => char.IsLetter(ch) && char.IsLower(ch)))
                {
                    errorMessage = string.Format(ctx.TranslateString("'{0}' contains lower case letters."), name);
                    suggestedNames.Add(UpperCaseIdentifier(WordParser.BreakWords(id)));
                }
                else
                {
                    suggestedNames.Add(id);
                }
                break;

            case NamingStyle.CamelCase:
                if (id.Length > 0 && char.IsUpper(id [0]))
                {
                    errorMessage = string.Format(ctx.TranslateString("'{0}' should start with a lower case letter."), name);
                }
                else if (!NoUnderscoreWithoutNumber(id))
                {
                    errorMessage = string.Format(ctx.TranslateString("'{0}' should not separate words with an underscore."), name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(CamelCaseIdentifier(WordParser.BreakWords(id)));
                break;

            case NamingStyle.PascalCase:
                if (id.Length > 0 && char.IsLower(id [0]))
                {
                    errorMessage = string.Format(ctx.TranslateString("'{0}' should start with an upper case letter."), name);
                }
                else if (!NoUnderscoreWithoutNumber(id))
                {
                    errorMessage = string.Format(ctx.TranslateString("'{0}' should not separate words with an underscore."), name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(PascalCaseIdentifier(WordParser.BreakWords(id)));
                break;

            case NamingStyle.FirstUpper:
                if (id.Length > 0 && char.IsLower(id [0]))
                {
                    errorMessage = string.Format(ctx.TranslateString("'{0}' should start with an upper case letter."), name);
                }
                else if (id.Take(1).Any(ch => char.IsLetter(ch) && char.IsUpper(ch)))
                {
                    errorMessage = string.Format(ctx.TranslateString("'{0}' contains an upper case letter after the first."), name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(FirstUpperIdentifier(WordParser.BreakWords(id)));
                break;
            }

            if (requiredPrefix != null)
            {
                for (int i = 0; i < suggestedNames.Count; i++)
                {
                    suggestedNames [i] = requiredPrefix + suggestedNames [i];
                }
            }
            else if (allowedPrefix != null)
            {
                int count = suggestedNames.Count;
                for (int i = 0; i < count; i++)
                {
                    suggestedNames.Add(suggestedNames [i]);
                    suggestedNames [i] = allowedPrefix + suggestedNames [i];
                }
            }
            else if (missingRequiredPrefix)
            {
                for (int i = 0; i < suggestedNames.Count; i++)
                {
                    var  n     = suggestedNames [i];
                    bool first = true;
                    foreach (var p in RequiredPrefixes)
                    {
                        if (first)
                        {
                            first = false;
                            suggestedNames [i] = p + n;
                        }
                        else
                        {
                            suggestedNames.Add(p + n);
                        }
                    }
                }
            }

            if (suffix != null)
            {
                for (int i = 0; i < suggestedNames.Count; i++)
                {
                    suggestedNames [i] = suggestedNames [i] + suffix;
                }
            }
            else if (missingRequiredSuffix)
            {
                for (int i = 0; i < suggestedNames.Count; i++)
                {
                    var  n     = suggestedNames [i];
                    bool first = true;
                    foreach (var s in RequiredSuffixes)
                    {
                        if (first)
                        {
                            first = false;
                            suggestedNames [i] = n + s;
                        }
                        else
                        {
                            suggestedNames.Add(n + s);
                        }
                    }
                }
            }

            return(errorMessage
                   // should never happen.
                   ?? "no known errors.");
        }
Esempio n. 2
0
        public string GetErrorMessage(SemanticModel ctx, string name, out IList <string> suggestedNames)
        {
            suggestedNames = new List <string>();
            string id = name;

            string errorMessage = null;

            bool   missingRequiredPrefix = false;
            bool   missingRequiredSuffix = false;
            string requiredPrefix        = null;
            string allowedPrefix         = null;
            string suffix = null;

            if (AllowedPrefixes != null && AllowedPrefixes.Length > 0)
            {
                allowedPrefix = AllowedPrefixes.FirstOrDefault(p => id.StartsWith(p, StringComparison.Ordinal));
                if (allowedPrefix != null)
                {
                    id = id.Substring(allowedPrefix.Length);
                }
            }

            if (RequiredPrefixes != null && RequiredPrefixes.Length > 0)
            {
                requiredPrefix = RequiredPrefixes.FirstOrDefault(p => id.StartsWith(p, StringComparison.Ordinal));
                if (requiredPrefix == null)
                {
                    errorMessage          = string.Format(TranslateString("Name should have prefix '{0}'. (Rule '{1}')."), RequiredPrefixes[0], Name);
                    missingRequiredPrefix = true;
                }
                else
                {
                    id = id.Substring(requiredPrefix.Length);
                }
            }
            else if (ForbiddenPrefixes != null && ForbiddenPrefixes.Length > 0)
            {
                requiredPrefix = ForbiddenPrefixes.FirstOrDefault(p => id.StartsWith(p, StringComparison.Ordinal));
                if (requiredPrefix != null)
                {
                    errorMessage = string.Format(TranslateString("Name has forbidden prefix '{0}'. (Rule '{1}')"), requiredPrefix, Name);
                    id           = id.Substring(requiredPrefix.Length);
                }
            }

            if (RequiredSuffixes != null && RequiredSuffixes.Length > 0)
            {
                suffix = RequiredSuffixes.FirstOrDefault(s => id.EndsWith(s, StringComparison.Ordinal));
                if (suffix == null)
                {
                    errorMessage          = string.Format(TranslateString("Name should have suffix '{0}'. (Rule '{1}')"), RequiredSuffixes[0], Name);
                    missingRequiredSuffix = true;
                }
                else
                {
                    id = id.Substring(0, id.Length - suffix.Length);
                }
            }
            else if (ForbiddenSuffixes != null && ForbiddenSuffixes.Length > 0)
            {
                suffix = ForbiddenSuffixes.FirstOrDefault(p => id.EndsWith(p, StringComparison.Ordinal));
                if (suffix != null)
                {
                    errorMessage = string.Format(TranslateString("Name has forbidden suffix '{0}'. (Rule '{1}')"), suffix, Name);
                    id           = id.Substring(0, id.Length - suffix.Length);
                }
            }

            switch (NamingStyle)
            {
            case NamingStyle.AllLower:
                if (id.Any(ch => char.IsLetter(ch) && char.IsUpper(ch)))
                {
                    errorMessage = string.Format(TranslateString("'{0}' contains upper case letters. (Rule '{1}')"), name, Name);
                    suggestedNames.Add(LowerCaseIdentifier(WordParser.BreakWords(id)));
                }
                else
                {
                    suggestedNames.Add(id);
                }
                break;

            case NamingStyle.AllUpper:
                if (id.Any(ch => char.IsLetter(ch) && char.IsLower(ch)))
                {
                    errorMessage = string.Format(TranslateString("'{0}' contains lower case letters. (Rule '{1}')"), name, Name);
                    suggestedNames.Add(UpperCaseIdentifier(WordParser.BreakWords(id)));
                }
                else
                {
                    suggestedNames.Add(id);
                }
                break;

            case NamingStyle.CamelCase:
                if (id.Length > 0 && char.IsUpper(id[0]))
                {
                    errorMessage = string.Format(TranslateString("'{0}' should start with a lower case letter. (Rule '{1}')"), name, Name);
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.Forbid))
                {
                    errorMessage = string.Format(TranslateString("'{0}' should not separate words with an underscore. (Rule '{1}')"), name, Name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(CamelCaseIdentifier(id));
                break;

            case NamingStyle.CamelCaseWithLowerLetterUnderscore:
                if (id.Length > 0 && char.IsUpper(id[0]))
                {
                    errorMessage = string.Format(TranslateString("'{0}' should start with a lower case letter. (Rule '{1}')"), name, Name);
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.AllowWithLowerStartingLetter))
                {
                    errorMessage = string.Format(TranslateString("after '_' a lower letter should follow. (Rule '{0}')"), Name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(CamelCaseWithLowerLetterUnderscore(id));
                break;

            case NamingStyle.CamelCaseWithUpperLetterUnderscore:
                if (id.Length > 0 && char.IsUpper(id[0]))
                {
                    errorMessage = string.Format(TranslateString("'{0}' should start with a lower case letter. (Rule '{1}')"), name, Name);
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.AllowWithUpperStartingLetter))
                {
                    errorMessage = string.Format(TranslateString("after '_' an upper letter should follow. (Rule '{0}')"), Name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(CamelCaseWithUpperLetterUnderscore(id));
                break;

            case NamingStyle.PascalCase:
                if (id.Length > 0 && char.IsLower(id[0]))
                {
                    errorMessage = string.Format(TranslateString("'{0}' should start with an upper case letter. (Rule '{1}')"), name, Name);
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.Forbid))
                {
                    errorMessage = string.Format(TranslateString("'{0}' should not separate words with an underscore. (Rule '{1}')"), name, Name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(PascalCaseIdentifier(id));
                break;

            case NamingStyle.PascalCaseWithLowerLetterUnderscore:
                if (id.Length > 0 && char.IsLower(id[0]))
                {
                    errorMessage = string.Format(TranslateString("'{0}' should start with an upper case letter. (Rule '{1}')"), name, Name);
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.AllowWithLowerStartingLetter))
                {
                    errorMessage = string.Format(TranslateString("after '_' a lower letter should follow. (Rule '{0}')"), Name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(PascalCaseWithLowerLetterUnderscore(id));
                break;

            case NamingStyle.PascalCaseWithUpperLetterUnderscore:
                if (id.Length > 0 && char.IsLower(id[0]))
                {
                    errorMessage = string.Format(TranslateString("'{0}' should start with an upper case letter. (Rule '{1}')"), name, Name);
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.AllowWithUpperStartingLetter))
                {
                    errorMessage = string.Format(TranslateString("after '_' an upper letter should follow. (Rule '{0}')"), Name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(PascalCaseWithUpperLetterUnderscore(id));
                break;

            case NamingStyle.FirstUpper:
                if (id.Length > 0 && char.IsLower(id[0]))
                {
                    errorMessage = string.Format(TranslateString("'{0}' should start with an upper case letter. (Rule '{1}')"), name, Name);
                }
                else if (id.Take(1).Any(ch => char.IsLetter(ch) && char.IsUpper(ch)))
                {
                    errorMessage = string.Format(TranslateString("'{0}' contains an upper case letter after the first. (Rule '{1}')"), name, Name);
                }
                else
                {
                    suggestedNames.Add(id);
                    break;
                }
                suggestedNames.Add(FirstUpperIdentifier(WordParser.BreakWords(id)));
                break;
            }

            if (requiredPrefix != null)
            {
                for (int i = 0; i < suggestedNames.Count; i++)
                {
                    suggestedNames[i] = requiredPrefix + suggestedNames[i];
                }
            }
            else if (allowedPrefix != null)
            {
                int count = suggestedNames.Count;
                for (int i = 0; i < count; i++)
                {
                    suggestedNames.Add(suggestedNames[i]);
                    suggestedNames[i] = allowedPrefix + suggestedNames[i];
                }
            }
            else if (missingRequiredPrefix)
            {
                for (int i = 0; i < suggestedNames.Count; i++)
                {
                    var  n     = suggestedNames[i];
                    bool first = true;
                    foreach (var p in RequiredPrefixes)
                    {
                        if (first)
                        {
                            first             = false;
                            suggestedNames[i] = p + n;
                        }
                        else
                        {
                            suggestedNames.Add(p + n);
                        }
                    }
                }
            }

            if (suffix != null)
            {
                for (int i = 0; i < suggestedNames.Count; i++)
                {
                    suggestedNames[i] = ApplySuffix(suggestedNames[i], suffix);
                }
            }
            else if (missingRequiredSuffix)
            {
                for (int i = 0; i < suggestedNames.Count; i++)
                {
                    var  n     = suggestedNames[i];
                    bool first = true;
                    foreach (var s in RequiredSuffixes)
                    {
                        if (first)
                        {
                            first             = false;
                            suggestedNames[i] = ApplySuffix(n, s);
                        }
                        else
                        {
                            suggestedNames.Add(ApplySuffix(n, s));
                        }
                    }
                }
            }

            return(errorMessage
                   // should never happen.
                   ?? "no known errors.");
        }
        public string CorrectName(string name)
        {
            string prefix = null, suffix = null;
            string realName = name;
            string id       = name;

            // check prefix
            if (AllowedPrefixes != null && AllowedPrefixes.Length > 0)
            {
                var allowedPrefix = AllowedPrefixes.FirstOrDefault(p => id.StartsWith(p, StringComparison.Ordinal));
                if (allowedPrefix != null)
                {
                    prefix = allowedPrefix;
                    id     = id.Substring(allowedPrefix.Length);
                }
            }

            if (prefix == null && RequiredPrefixes != null && RequiredPrefixes.Length > 0)
            {
                var requiredPrefix = RequiredPrefixes.FirstOrDefault(p => id.StartsWith(p, StringComparison.Ordinal));
                if (requiredPrefix == null)
                {
                    prefix = RequiredPrefixes[0];
                }
                else
                {
                    prefix = requiredPrefix;
                    id     = id.Substring(requiredPrefix.Length);
                }
            }

            if (prefix == null && ForbiddenPrefixes != null && ForbiddenPrefixes.Length > 0)
            {
                var forbiddenPrefix = ForbiddenPrefixes.FirstOrDefault(p => id.StartsWith(p, StringComparison.Ordinal));
                if (forbiddenPrefix != null)
                {
                    id = id.Substring(forbiddenPrefix.Length);
                }
            }

            // check suffix
            if (RequiredSuffixes != null && RequiredSuffixes.Length > 0)
            {
                var requiredSuffix = RequiredSuffixes.FirstOrDefault(s => id.EndsWith(s, StringComparison.Ordinal));
                if (requiredSuffix == null)
                {
                    suffix = RequiredSuffixes[0];
                }
                else
                {
                    suffix = requiredSuffix;
                    id     = id.Substring(0, id.Length - requiredSuffix.Length);
                }
            }

            if (suffix == null && ForbiddenSuffixes != null && ForbiddenSuffixes.Length > 0)
            {
                var forbiddenSuffix = ForbiddenSuffixes.FirstOrDefault(p => id.EndsWith(p, StringComparison.Ordinal));
                if (forbiddenSuffix != null)
                {
                    id = id.Substring(0, id.Length - forbiddenSuffix.Length);
                }
            }
            Console.WriteLine("style: " + NamingStyle);
            switch (NamingStyle)
            {
            case NamingStyle.AllLower:
                if (id.Any(ch => char.IsLetter(ch) && char.IsUpper(ch)))
                {
                    realName = LowerCaseIdentifier(WordParser.BreakWords(id));
                }
                else
                {
                    realName = id;
                }
                break;

            case NamingStyle.AllUpper:
                if (id.Any(ch => char.IsLetter(ch) && char.IsLower(ch)))
                {
                    realName = UpperCaseIdentifier(WordParser.BreakWords(id));
                }
                else
                {
                    realName = id;
                }
                break;

            case NamingStyle.CamelCase:
                if (id.Length > 0 && !char.IsLower(id [0]))
                {
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.Forbid))
                {
                }
                else
                {
                    realName = id;
                    break;
                }
                realName = CamelCaseIdentifier(id);
                break;

            case NamingStyle.CamelCaseWithLowerLetterUnderscore:
                if (id.Length > 0 && !char.IsLower(id [0]))
                {
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.AllowWithLowerStartingLetter))
                {
                }
                else
                {
                    realName = id;
                    break;
                }
                realName = CamelCaseWithLowerLetterUnderscore(id);
                break;

            case NamingStyle.CamelCaseWithUpperLetterUnderscore:
                if (id.Length > 0 && !char.IsLower(id [0]))
                {
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.AllowWithUpperStartingLetter))
                {
                }
                else
                {
                    realName = id;
                    break;
                }
                realName = CamelCaseWithUpperLetterUnderscore(id);
                break;

            case NamingStyle.PascalCase:
                if (id.Length > 0 && !char.IsUpper(id [0]))
                {
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.Forbid))
                {
                }
                else
                {
                    realName = id;
                    break;
                }
                realName = PascalCaseIdentifier(id);
                break;

            case NamingStyle.PascalCaseWithLowerLetterUnderscore:
                if (id.Length > 0 && !char.IsUpper(id [0]))
                {
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.AllowWithLowerStartingLetter))
                {
                }
                else
                {
                    realName = id;
                    break;
                }
                realName = PascalCaseWithLowerLetterUnderscore(id);
                break;

            case NamingStyle.PascalCaseWithUpperLetterUnderscore:
                if (id.Length > 0 && !char.IsUpper(id [0]))
                {
                }
                else if (!CheckUnderscore(id, UnderscoreHandling.AllowWithUpperStartingLetter))
                {
                }
                else
                {
                    realName = id;
                    break;
                }
                realName = PascalCaseWithUpperLetterUnderscore(id);
                break;

            case NamingStyle.FirstUpper:
                if (id.Length > 0 && !char.IsUpper(id [0]))
                {
                }
                else if (id.Take(1).Any(ch => char.IsLetter(ch) && char.IsUpper(ch)))
                {
                }
                else
                {
                    realName = id;
                    break;
                }
                realName = FirstUpperIdentifier(WordParser.BreakWords(id));
                break;
            }

            return(prefix + realName + suffix);
        }