private bool SkipWhiteSpaces() { var start = _position; //move to next real character for (; _position < _text.Length && char.IsWhiteSpace(_text, _position); _position++) { } //end of file if (_position >= _text.Length) { return(true); } //if the header has already started and we're not in an open region, check if there was more than one NewLine if (!_started || _regionStarts.Count != 0) { return(false); } var firstNewLine = NewLineManager.NextLineEndPositionInformation(_text, start, _position - start); if (firstNewLine == null) { return(false); } var afterFirstNewLine = firstNewLine.Index + firstNewLine.LineEndLength; var nextNewLine = NewLineManager.NextLineEndPosition(_text, afterFirstNewLine, _position - afterFirstNewLine); //more than one NewLine (= at least one empty line) return(nextNewLine > 0); }
/// <summary> /// Prepares an updated license header before effectively replacing the old one based on the following rules: /// <para> /// If there's a comment right at the beginning of the file, we need to add an empty line so that the comment /// doesn't become a part of the header. /// </para> /// <para>If there already exists an empty line we don't have to add another one.</para> /// </summary> /// <param name="headerText">The new header text.</param> /// <param name="currentHeaderText">The old, already existing, header text.</param> /// <param name="commentParser">An <see cref="ICommentParser" /> instance required for enforcing the described rules.</param> /// <returns></returns> public static string Prepare(string headerText, string currentHeaderText, ICommentParser commentParser) { var lineEndingInDocument = NewLineManager.DetectMostFrequentLineEnd(headerText); var headerWithNewLine = NewLineManager.ReplaceAllLineEnds(headerText, lineEndingInDocument); if (!CurrentFileStartsWithNewLine(currentHeaderText, lineEndingInDocument) && !HeaderEndsWithNewline(headerWithNewLine, lineEndingInDocument) && CurrentFileContainsCommentOnTop(currentHeaderText, commentParser)) { return(headerWithNewLine + lineEndingInDocument); } return(headerWithNewLine); }
private bool HandleToken(string token) { if (token == null) { return(false); } if (_lineComment != null && token.StartsWith(_lineComment)) { SetStarted(); //proceed to end of line _position = NewLineManager.NextLineEndPosition(_text, _position - token.Length + _lineComment.Length); UpdatePositionIfEndOfFile(); return(true); } if (_beginComment != null && token.StartsWith(_beginComment)) { SetStarted(); _position = _text.IndexOf(_endComment, _position - token.Length + _beginComment.Length, StringComparison.Ordinal); if (_position < 0) { throw new ParseException(); } _position += _endComment.Length; return(true); } if (_beginRegion != null && token == _beginRegion) { SetStarted(); _regionStarts.Push(_position - _beginRegion.Length); _position = NewLineManager.NextLineEndPosition(_text, _position); UpdatePositionIfEndOfFile(); return(true); } if (_endRegion != null && token == _endRegion) { SetStarted(); if (_regionStarts.Count == 0) { throw new ParseException(); } _regionStarts.Pop(); _position = NewLineManager.NextLineEndPosition(_text, _position); UpdatePositionIfEndOfFile(); return(true); } if (_endRegion != null && _endRegion.Contains(token)) { SetStarted(); var firstPart = token; token = GetToken(); if (firstPart + " " + token == _endRegion) { if (_regionStarts.Count == 0) { throw new ParseException(); } _regionStarts.Pop(); } _position = NewLineManager.NextLineEndPosition(_text, _position); UpdatePositionIfEndOfFile(); return(true); } _position -= token.Length; return(false); }
private async Task <string> GetLineEndingInDocument() { _lineEndingInDocumentCache ??= NewLineManager.DetectMostFrequentLineEnd(await GetText()); return(_lineEndingInDocumentCache); }