Skip to content

Commit

Permalink
Fix issue with extra spaces in commands (#366) (#369)
Browse files Browse the repository at this point in the history
(cherry picked from commit d0a5110)
  • Loading branch information
tlmii authored Jul 31, 2020
1 parent ea4856b commit e8c4510
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Microsoft.Repl/Parsing/CoreParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public ICoreParseResult Parse(string commandText, int caretPosition)
commandText = commandText ?? throw new ArgumentNullException(nameof(commandText));

List<string> sections = commandText.Split(' ').ToList();
// We can't use StringSplitOptions.RemoveEmptyEntries because it
// is more aggressive than we need, so we need to do it ourselves.
RemoveEmptyEntries(sections);
Dictionary<int, int> sectionStartLookup = new Dictionary<int, int>();
HashSet<int> quotedSections = new HashSet<int>();
int runningIndex = 0;
Expand Down Expand Up @@ -131,5 +134,23 @@ public ICoreParseResult Parse(string commandText, int caretPosition)

return new CoreParseResult(caretPosition, caretPositionWithinSelectedSection, commandText, sections, selectedSection, sectionStartLookup, quotedSections);
}

private static void RemoveEmptyEntries(List<string> sections)
{
if (sections.Count < 2)
{
return;
}

// We want to remove empty spaces from the beginning, and from the middle
// but not from the end.
for (int index = 0; index < sections.Count - 1; index++)
{
if (sections[index].Length == 0)
{
sections.RemoveAt(index);
}
}
}
}
}
15 changes: 15 additions & 0 deletions test/Microsoft.Repl.Tests/Parsing/CoreParseResultTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,20 @@ public void Slice_WithSelectedSection_SelectedSectionMoves(string commandText, i

Assert.Equal(expectedSelectedSection, result.SelectedSection);
}

[Theory]
[InlineData("set base https://localhost", 3)]
[InlineData("run c:\\script.txt", 2)]
[InlineData("help connect", 2)]
[InlineData("set base ", 3)]
[InlineData("", 1)]
[InlineData(" set base https://localhost", 3)]
public void Parse_WithDoubleSpace_RemovesEmptySections(string commandText, int expectedSectionCount)
{
CoreParser parser = new CoreParser();
ICoreParseResult parseResult = parser.Parse(commandText, commandText.Length + 1);

Assert.Equal(expectedSectionCount, parseResult.Sections.Count);
}
}
}

0 comments on commit e8c4510

Please sign in to comment.