diff --git a/ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs b/ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs index 4113dec53d..af887d2731 100644 --- a/ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs +++ b/ICSharpCode.NRefactory.Tests/Utils/CompositeFormatStringParser/CompositeFormatStringParserTests.cs @@ -37,7 +37,7 @@ namespace ICSharpCode.NRefactory.Utils IList ParseTest(string format, params IFormatStringSegment[] expectedFormatSegments) { var parser = new CompositeFormatStringParser(); - var actualFormatSegments = parser.Parse(format).ToList(); + var actualFormatSegments = parser.Parse(format).Segments; Console.WriteLine("Expected format segments:"); foreach (var item in expectedFormatSegments) { diff --git a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs index 31c63b310b..bd3fc38f76 100644 --- a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs +++ b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/CompositeFormatStringParser.cs @@ -26,6 +26,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Linq; namespace ICSharpCode.NRefactory.Utils { @@ -50,11 +51,13 @@ namespace ICSharpCode.NRefactory.Utils /// /// The format string. /// - public IEnumerable Parse (string format) + public FormatStringParseResult Parse (string format) { if (format == null) throw new ArgumentNullException ("format"); + var result = new FormatStringParseResult(); + // Format string syntax: http://msdn.microsoft.com/en-us/library/txafckwd.aspx int start = 0; var length = format.Length; @@ -73,8 +76,8 @@ namespace ICSharpCode.NRefactory.Utils } } }; - yield return textSegment; - yield break; + result.Segments.Add(textSegment); + return result; } else if (format [i + 1] == '{') { // Escape sequence; we're still in a text segment // Skip ahead to the char after the escape sequence @@ -83,7 +86,7 @@ namespace ICSharpCode.NRefactory.Utils } else { // This is the end of the text segment and the start of a FormatItem if (i - start > 0) { - yield return new TextSegment (UnEscape (format.Substring (start, i - start))); + result.Segments.Add(new TextSegment (UnEscape (format.Substring (start, i - start)))); start = i; } } @@ -108,11 +111,11 @@ namespace ICSharpCode.NRefactory.Utils // i may actually point outside of format; if that happens, we want the last position var endLocation = Math.Min (length, i + 1); var errors = GetErrors (); - yield return new FormatItem (index, alignment, argumentFormat) { + result.Segments.Add(new FormatItem (index, alignment, argumentFormat) { StartLocation = start, EndLocation = endLocation, Errors = errors - }; + }); ClearErrors (); // The next potential text segment starts after this format item @@ -121,8 +124,9 @@ namespace ICSharpCode.NRefactory.Utils } // Handle remaining text if (start < length) { - yield return new TextSegment (UnEscape (format.Substring (start)), start); + result.Segments.Add(new TextSegment (UnEscape (format.Substring (start)), start)); } + return result; } int ParseIndex (string format, ref int i) @@ -301,5 +305,22 @@ namespace ICSharpCode.NRefactory.Utils errors = new List (); } } + + public class FormatStringParseResult + { + public FormatStringParseResult() + { + Segments = new List(); + } + + public IList Segments { get; private set; } + + public bool HasErrors + { + get { + return Segments.SelectMany(segment => segment.Errors).Any(); + } + } + } } diff --git a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/IFormatStringError.cs b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/IFormatStringError.cs index 71cfb7f4e8..b9f3919f81 100644 --- a/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/IFormatStringError.cs +++ b/ICSharpCode.NRefactory/Utils/CompositeFormatStringParser/IFormatStringError.cs @@ -39,7 +39,7 @@ namespace ICSharpCode.NRefactory.Utils string SuggestedReplacementText { get; } } - class DefaultFormatStringError : IFormatStringError + public class DefaultFormatStringError : IFormatStringError { public DefaultFormatStringError() {