Fixed multiline comment saving

This commit is contained in:
Ben Wallis
2017-12-07 22:22:26 +00:00
parent 523a9553d9
commit 9cf4d86cb0
7 changed files with 52 additions and 24 deletions

View File

@@ -471,11 +471,15 @@ namespace Filtration.Parser.Services
public string TranslateItemFilterBlockBaseToString(IItemFilterBlockBase itemFilterBlockBase)
{
var itemFilterBlock = itemFilterBlockBase as IItemFilterBlock;
if (itemFilterBlock != null) return TranslateItemFilterBlockToString(itemFilterBlock);
if (itemFilterBlockBase is IItemFilterBlock itemFilterBlock)
{
return TranslateItemFilterBlockToString(itemFilterBlock);
}
var itemFilterCommentBlock = itemFilterBlockBase as IItemFilterCommentBlock;
if (itemFilterCommentBlock != null) return TranslateItemFilterCommentBlockToString(itemFilterCommentBlock);
if (itemFilterBlockBase is IItemFilterCommentBlock itemFilterCommentBlock)
{
return TranslateItemFilterCommentBlockToString(itemFilterCommentBlock);
}
throw new InvalidOperationException("Unable to translate unknown ItemFilterBlock type");
}
@@ -483,10 +487,18 @@ namespace Filtration.Parser.Services
// TODO: Private
public string TranslateItemFilterCommentBlockToString(IItemFilterCommentBlock itemFilterCommentBlock)
{
// TODO: Handle multi-line
// TODO: Tests
// TODO: # Section: text?
return $"#{itemFilterCommentBlock.Comment}";
var commentWithHashes = string.Empty;
// Add "# " to the beginning of each line of the comment before saving it
foreach (var line in new LineReader(() => new StringReader(itemFilterCommentBlock.Comment)))
{
commentWithHashes += $"# {line.TrimStart(' ')}{Environment.NewLine}";
}
// Remove trailing newline
return commentWithHashes.TrimEnd('\r', '\n');
}
// This method converts an ItemFilterBlock object into a string. This is used for copying a ItemFilterBlock

View File

@@ -126,7 +126,11 @@ namespace Filtration.Parser.Services
// Process the script header
for (var i = 0; i < conditionBoundaries.Skip(1).First().StartLine; i++)
{
if (lines[i].StartsWith("#"))
if (lines[i].StartsWith("# EnableBlockGroups"))
{
script.ItemFilterScriptSettings.BlockGroupsEnabled = true;
}
else if (lines[i].StartsWith("#"))
{
script.Description += lines[i].Substring(1).Trim(' ') + Environment.NewLine;
}
@@ -235,6 +239,11 @@ namespace Filtration.Parser.Services
outputString += "# Script edited with Filtration - https://github.com/ben-wallis/Filtration" +
Environment.NewLine;
if (script.ItemFilterScriptSettings.BlockGroupsEnabled)
{
outputString += "# EnableBlockGroups" + Environment.NewLine;
}
if (!string.IsNullOrEmpty(script.Description))
{
// ReSharper disable once LoopCanBeConvertedToQuery
@@ -251,7 +260,7 @@ namespace Filtration.Parser.Services
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var block in script.ItemFilterBlocks)
{
outputString += _blockTranslator.TranslateItemFilterBlockToString(block as ItemFilterBlock) + Environment.NewLine;
outputString += _blockTranslator.TranslateItemFilterBlockBaseToString(block) + Environment.NewLine;
if (Settings.Default.ExtraLineBetweenBlocks)
{