private string[] EditCommentInternal(string[] lines, NamedComment namedComment, EditCommentMode mode) { List <string> newLines = new List <string>(lines.Length); bool isUncommenting = false; foreach (string line in lines) { if (isUncommenting) { if (line.Contains(namedComment.End)) { isUncommenting = false; } else if (mode != EditCommentMode.DeleteCode) { string newLine = line; if (mode == EditCommentMode.UncommentCode) { if (newLine.TrimStart().StartsWith(namedComment.Comment.Start)) { int commentIndex = newLine.IndexOf(namedComment.Comment.Start); newLine = newLine.Substring(0, commentIndex) + (newLine[commentIndex + namedComment.Comment.Start.Length] == ' ' ? newLine.Substring(commentIndex + namedComment.Comment.Start.Length + 1) : newLine.Substring(commentIndex + namedComment.Comment.Start.Length)); } if (namedComment.Comment.HasEnd && newLine.TrimEnd().EndsWith(namedComment.Comment.End)) { int commentIndex = newLine.LastIndexOf(namedComment.Comment.End); newLine = newLine.Substring(0, commentIndex) + newLine.Substring(commentIndex + namedComment.Comment.End.Length); } } newLines.Add(newLine); } } else if (line.Contains(namedComment.Start)) { isUncommenting = true; } else { newLines.Add(line); } } if (isUncommenting) { throw new InvalidOperationException($"No end comment was found. End:<{namedComment.End}>."); } return(newLines.ToArray()); }
private async Task EditCommentInternal(string commentName, EditCommentMode mode, string filePath) { string fileExtension = Path.GetExtension(filePath); Comment[] comments = Comment.GetComments(fileExtension); if (comments.Length > 0) { string[] lines = await this.fileSystemService.FileReadAllLines(filePath); foreach (Comment comment in comments) { NamedComment namedComment = new NamedComment(commentName, comment); lines = EditCommentInternal(lines, namedComment, mode); } this.fileSystemService.FileWriteAllLines(filePath, lines); } }
private string[] EditCommentInternal(string[] lines, NamedComment namedComment, EditCommentMode mode) { List<string> newLines = new List<string>(lines.Length); bool isUncommenting = false; foreach (string line in lines) { if (isUncommenting) { if (line.Contains(namedComment.End)) { isUncommenting = false; } else if (mode != EditCommentMode.DeleteCode) { string newLine = line; if (mode == EditCommentMode.UncommentCode) { if (newLine.TrimStart().StartsWith(namedComment.Comment.Start)) { int commentIndex = newLine.IndexOf(namedComment.Comment.Start); newLine = newLine.Substring(0, commentIndex) + (newLine[commentIndex + namedComment.Comment.Start.Length] == ' ' ? newLine.Substring(commentIndex + namedComment.Comment.Start.Length + 1) : newLine.Substring(commentIndex + namedComment.Comment.Start.Length)); } if (namedComment.Comment.HasEnd && newLine.TrimEnd().EndsWith(namedComment.Comment.End)) { int commentIndex = newLine.LastIndexOf(namedComment.Comment.End); newLine = newLine.Substring(0, commentIndex) + newLine.Substring(commentIndex + namedComment.Comment.End.Length); } } newLines.Add(newLine); } } else if (line.Contains(namedComment.Start)) { isUncommenting = true; } else { newLines.Add(line); } } return newLines.ToArray(); }
private async Task EditCommentInternal(string commentName, EditCommentMode mode, string filePath) { string fileExtension = Path.GetExtension(filePath); Comment[] comments = Comment.GetComments(fileExtension); if (comments.Length > 0) { string[] lines = await this.fileSystemService.FileReadAllLines(filePath); foreach (Comment comment in comments) { NamedComment namedComment = new NamedComment(commentName, comment); lines = EditCommentInternal(lines, namedComment, mode); } await this.fileSystemService.FileWriteAllLines(filePath, lines); } }
private async Task DeleteCommentInternal(string commentName, string filePath, DeleteCommentMode mode) { string fileExtension = Path.GetExtension(filePath); Comment comment = Comment.GetComment(fileExtension); if (comment == null) { // We don't support this file extension. return; } NamedComment namedComment = new NamedComment(commentName, comment); string[] lines = await this.fileSystemService.FileReadAllLines(filePath); List <string> newLines = new List <string>(lines.Length); bool isUncommenting = false; foreach (string line in lines) { if (isUncommenting) { if (line.Contains(namedComment.End)) { isUncommenting = false; } else if (mode != DeleteCommentMode.StartEndCommentAndCode) { string newLine = line; if (mode == DeleteCommentMode.StartEndCommentAndUncommentCode) { if (newLine.TrimStart().StartsWith(comment.Start)) { int commentIndex = newLine.IndexOf(comment.Start); newLine = newLine.Substring(0, commentIndex) + new string(' ', comment.Start.Length) + newLine.Substring(commentIndex + comment.Start.Length); } if (comment.HasEnd && newLine.TrimEnd().EndsWith(comment.End)) { int commentIndex = newLine.LastIndexOf(comment.End); newLine = newLine.Substring(0, commentIndex) + newLine.Substring(commentIndex + comment.End.Length); } } newLines.Add(newLine); } } else if (line.Contains(namedComment.Start)) { isUncommenting = true; } else { newLines.Add(line); } } await this.fileSystemService.FileWriteAllLines(filePath, newLines); }
private async Task EditCommentInternal(string commentName, EditCommentMode mode, string filePath) { string fileExtension = Path.GetExtension(filePath); Comment comment = Comment.GetComment(fileExtension); if (comment == null) { // We don't support this file extension. return; } NamedComment namedComment = new NamedComment(commentName, comment); string[] lines = await this.fileSystemService.FileReadAllLines(filePath); List<string> newLines = new List<string>(lines.Length); bool isUncommenting = false; foreach (string line in lines) { if (isUncommenting) { if (line.Contains(namedComment.End)) { isUncommenting = false; } else if (mode != EditCommentMode.DeleteCode) { string newLine = line; if (mode == EditCommentMode.UncommentCode) { if (newLine.TrimStart().StartsWith(comment.Start)) { int commentIndex = newLine.IndexOf(comment.Start); newLine = newLine.Substring(0, commentIndex) + (newLine[commentIndex + comment.Start.Length] == ' ' ? newLine.Substring(commentIndex + comment.Start.Length + 1) : newLine.Substring(commentIndex + comment.Start.Length)); } if (comment.HasEnd && newLine.TrimEnd().EndsWith(comment.End)) { int commentIndex = newLine.LastIndexOf(comment.End); newLine = newLine.Substring(0, commentIndex) + newLine.Substring(commentIndex + comment.End.Length); } } newLines.Add(newLine); } } else if (line.Contains(namedComment.Start)) { isUncommenting = true; } else { newLines.Add(line); } } await this.fileSystemService.FileWriteAllLines(filePath, newLines); }