private string[] ExpandToSingleWord(string shortForm, SingleWordPattern pattern, XElement methodXml, string methodComment, string classComment)
{
//construct the regular expression for the pattern
string patternRegex;
if (pattern == SingleWordPattern.Prefix)
{
if (shortForm[0] == 'x')
{
patternRegex = string.Format(@"\be?{0}\w+\b", shortForm);
}
else
{
patternRegex = string.Format(@"\b{0}\w+\b", shortForm);
}
}
else if (pattern == SingleWordPattern.DroppedLetter)
{
StringBuilder sb = new StringBuilder(@"\b");
for (int i = 0; i < shortForm.Length; i++)
{
sb.AppendFormat(@"{0}\w*", shortForm[i]);
}
sb.Append(@"\b");
patternRegex = sb.ToString();
}
else
{
throw new ArgumentException("Pattern must be a valid member of SingleWordPattern.", "pattern");
}
//look for potential long forms
if ((pattern == SingleWordPattern.Prefix
|| Regex.IsMatch(shortForm, @"^\w[^aeiou]+$", RegexOptions.IgnoreCase) //first letter may be a vowel, the rest are consonents
|| shortForm.Length > 3)
&& !Regex.IsMatch(shortForm, @"^\w[aeiou][aeiou]+$", RegexOptions.IgnoreCase)) //don't want words that are mostly consecutive vowels, e.g. GUI
{
List<string> matches = new List<string>();
bool foundMatch = false;
//1. Search JavaDoc comments. We don't have these, so skip this step.
//2. Search Type Names and variable names for "pattern sf"
var decls = from decl in methodXml.Descendants(SRC.Declaration)
select decl;
foreach (var decl in decls)
{
//Console.WriteLine("Decl found: {0}", decl.Value);
Match m = Regex.Match(decl.Value, string.Format(@"({0})\s+{1}", patternRegex, shortForm), RegexOptions.IgnoreCase);
if(m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
//3. Search Method Name for pattern
if (!foundMatch)
{
Match m = Regex.Match(methodXml.Element(SRC.Name).Value, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
//4. Search method text for "pattern sf" and "sf pattern"
//Emily says to search within each statement. However, it is equivalent to search within the whole method text at once because if pattern and sf
//are separated only by whitespace then they must be within the same statement.
if (!foundMatch)
{
string methodText = methodXml.Value;
Match m = Regex.Match(methodText, string.Format(@"({0})\s+{1}", patternRegex, shortForm), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
m = Regex.Match(methodText, string.Format(@"{0}\s+({1})", shortForm, patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
//5. Search method words for "pattern"
if (!foundMatch && shortForm.Length != 2)
{
Match m = Regex.Match(methodXml.Value, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
//6. Search method comment words
if (!foundMatch && shortForm.Length != 2)
{
Match m = Regex.Match(methodComment, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
//7. Search class comment words
if (!foundMatch && pattern == SingleWordPattern.Prefix && shortForm.Length > 1)
{
Match m = Regex.Match(classComment, string.Format("({0})", patternRegex), RegexOptions.IgnoreCase);
if (m.Success)
{
matches.Add(m.Groups[1].Value);
foundMatch = true;
}
}
return matches.ToArray();
}
else
{
return new string[0];
}
}