public void TestUnderscoreCase1() { var result = WordParser.BreakWords("FIX_1"); Assert.AreEqual(2, result.Count); Assert.AreEqual("FIX", result [0]); Assert.AreEqual("1", result [1]); }
public void TestUnderscore() { var result = WordParser.BreakWords("some_Very_long_NAME"); Assert.AreEqual(4, result.Count); Assert.AreEqual("some", result [0]); Assert.AreEqual("Very", result [1]); Assert.AreEqual("long", result [2]); Assert.AreEqual("NAME", result [3]); }
public void TestCamelCaseWords() { var result = WordParser.BreakWords("someVeryLongName"); Assert.AreEqual(4, result.Count); Assert.AreEqual("some", result [0]); Assert.AreEqual("Very", result [1]); Assert.AreEqual("Long", result [2]); Assert.AreEqual("Name", result [3]); }
public void TestUpperCaseSubWord() { var result = WordParser.BreakWords("someVeryLongXMLName"); Assert.AreEqual(5, result.Count); Assert.AreEqual("some", result [0]); Assert.AreEqual("Very", result [1]); Assert.AreEqual("Long", result [2]); Assert.AreEqual("XML", result [3]); Assert.AreEqual("Name", result [4]); }
/// <summary> /// Applies a suffix to a name and tries to reuse the suffix of the suffix. /// MyArgs + EventArgs -> MyEventArgs instead of MyArgsEventArgs /// </summary> static string ApplySuffix(string name, string suffix) { var words = WordParser.BreakWords(suffix); bool found = false; for (int j = words.Count - 1; j >= 0; j--) { if (name.EndsWith(words[j], StringComparison.Ordinal)) { name = name.Substring(0, name.Length - words[j].Length); found = true; } else { if (found) { return(name + string.Join("", words.ToArray(), j, words.Count - j)); } break; } } return(name + suffix); }
public static IEnumerable <string> GenerateNameProposals(AstType type) { if (type is PrimitiveType) { var pt = (PrimitiveType)type; switch (pt.Keyword) { case "object": yield return("o"); yield return("obj"); break; case "bool": yield return("b"); yield return("pred"); break; case "double": case "float": case "decimal": yield return("d"); yield return("f"); yield return("m"); break; case "char": yield return("c"); break; default: yield return("i"); yield return("j"); yield return("k"); break; } yield break; } string name; if (type is SimpleType) { name = ((SimpleType)type).Identifier; } else if (type is MemberType) { name = ((MemberType)type).MemberName; } else { yield break; } var names = WordParser.BreakWords(name); if (names.Count > 0) { names [0] = Char.ToLower(names [0] [0]) + names [0].Substring(1); } yield return(string.Join("", names)); }
protected async override Task <IEnumerable <CompletionData> > GetItemsWorkerAsync(CompletionResult completionResult, CompletionEngine engine, CompletionContext completionContext, CompletionTriggerInfo info, SyntaxContext ctx, CancellationToken cancellationToken) { var tree = await completionContext.Document.GetCSharpSyntaxTreeAsync(cancellationToken).ConfigureAwait(false); if (tree.IsInNonUserCode(completionContext.Position, cancellationToken) || tree.IsPreProcessorDirectiveContext(completionContext.Position, cancellationToken) || info.CompletionTriggerReason != CompletionTriggerReason.CompletionCommand) { return(Enumerable.Empty <CompletionData>()); } var token = tree.FindTokenOnLeftOfPosition(completionContext.Position, cancellationToken); if (token.Span.End == completionContext.Position) { return(Enumerable.Empty <CompletionData>()); } var parent = token.Parent.AncestorsAndSelf().OfType <GenericNameSyntax> ().FirstOrDefault() ?? token.Parent; if (!parent.Parent.IsKind(SyntaxKind.IncompleteMember) && !IsLocal(parent) && !parent.Parent.IsKind(SyntaxKind.Parameter) && !parent.Parent.IsKind(SyntaxKind.ForEachStatement)) { return(Enumerable.Empty <CompletionData>()); } if (info.TriggerCharacter != ' ' && parent.Parent.IsKind(SyntaxKind.ExpressionStatement)) { return(Enumerable.Empty <CompletionData>()); } var list = new List <CompletionData> (); if (parent.IsKind(SyntaxKind.PredefinedType)) { switch (token.Kind()) { case SyntaxKind.ObjectKeyword: list.Add(engine.Factory.CreateGenericData(this, "o", GenericDataType.NameProposal)); list.Add(engine.Factory.CreateGenericData(this, "obj", GenericDataType.NameProposal)); return(list); case SyntaxKind.BoolKeyword: list.Add(engine.Factory.CreateGenericData(this, "b", GenericDataType.NameProposal)); list.Add(engine.Factory.CreateGenericData(this, "pred", GenericDataType.NameProposal)); return(list); case SyntaxKind.CharKeyword: list.Add(engine.Factory.CreateGenericData(this, "c", GenericDataType.NameProposal)); list.Add(engine.Factory.CreateGenericData(this, "ch", GenericDataType.NameProposal)); return(list); case SyntaxKind.StringKeyword: list.Add(engine.Factory.CreateGenericData(this, "str", GenericDataType.NameProposal)); return(list); case SyntaxKind.DoubleKeyword: case SyntaxKind.FloatKeyword: case SyntaxKind.DecimalKeyword: list.Add(engine.Factory.CreateGenericData(this, "d", GenericDataType.NameProposal)); list.Add(engine.Factory.CreateGenericData(this, "f", GenericDataType.NameProposal)); list.Add(engine.Factory.CreateGenericData(this, "m", GenericDataType.NameProposal)); return(list); default: list.Add(engine.Factory.CreateGenericData(this, "i", GenericDataType.NameProposal)); list.Add(engine.Factory.CreateGenericData(this, "j", GenericDataType.NameProposal)); list.Add(engine.Factory.CreateGenericData(this, "k", GenericDataType.NameProposal)); return(list); } } else { var incompleteMember = parent.Parent as IncompleteMemberSyntax; if (incompleteMember != null) { return(list); } var gns = parent as GenericNameSyntax; var names = WordParser.BreakWords(gns != null ? gns.Identifier.ToString() : token.ToString().Trim()); var possibleName = new StringBuilder(); for (int i = 0; i < names.Count; i++) { possibleName.Length = 0; for (int j = i; j < names.Count; j++) { if (string.IsNullOrEmpty(names [j])) { continue; } if (j == i) { names [j] = Char.ToLower(names [j] [0]) + names [j].Substring(1); } possibleName.Append(names [j]); } list.Add(engine.Factory.CreateGenericData(this, possibleName.ToString(), GenericDataType.NameProposal)); } } return(list); }
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 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."); }
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); }