Fix autocomplete text issue on query change (#7392)

* Fix autocomplete text issue on query change

* Update from invariant to ordinal case for exact byte to byte matching

* Add tests for checking when autocomplete should be empty
This commit is contained in:
Divyansh Srivastava 2020-10-20 14:53:32 -07:00 committed by GitHub
parent 0314b570cd
commit 466ed10f3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 3 deletions

View File

@ -336,8 +336,9 @@ namespace PowerLauncher
{ {
var textBox = (TextBox)sender; var textBox = (TextBox)sender;
var text = textBox.Text; var text = textBox.Text;
var autoCompleteText = SearchBox.AutoCompleteTextBlock.Text;
if (string.IsNullOrEmpty(text)) if (MainViewModel.ShouldAutoCompleteTextBeEmpty(text, autoCompleteText))
{ {
SearchBox.AutoCompleteTextBlock.Text = string.Empty; SearchBox.AutoCompleteTextBlock.Text = string.Empty;
} }

View File

@ -871,13 +871,25 @@ namespace PowerLauncher.ViewModel
} }
} }
public static bool ShouldAutoCompleteTextBeEmpty(string queryText, string autoCompleteText)
{
if (string.IsNullOrEmpty(autoCompleteText))
{
return false;
}
else
{
return string.IsNullOrEmpty(queryText) || autoCompleteText.IndexOf(queryText, StringComparison.Ordinal) != 0;
}
}
public static string GetAutoCompleteText(int index, string input, string query) public static string GetAutoCompleteText(int index, string input, string query)
{ {
if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(query)) if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(query))
{ {
if (index == 0) if (index == 0)
{ {
if (input.IndexOf(query, StringComparison.InvariantCultureIgnoreCase) == 0) if (input.IndexOf(query, StringComparison.OrdinalIgnoreCase) == 0)
{ {
// Use the same case as the input query for the matched portion of the string // Use the same case as the input query for the matched portion of the string
return query + input.Substring(query.Length); return query + input.Substring(query.Length);
@ -894,7 +906,7 @@ namespace PowerLauncher.ViewModel
{ {
if (index == 0 && !string.IsNullOrEmpty(query)) if (index == 0 && !string.IsNullOrEmpty(query))
{ {
if (input.IndexOf(query, StringComparison.InvariantCultureIgnoreCase) == 0) if (input.IndexOf(query, StringComparison.OrdinalIgnoreCase) == 0)
{ {
return query + input.Substring(query.Length); return query + input.Substring(query.Length);
} }

View File

@ -207,5 +207,61 @@ namespace Wox.Test
// Assert // Assert
Assert.AreEqual(input, autoCompleteText); Assert.AreEqual(input, autoCompleteText);
} }
[Test]
public void ShouldAutoCompleteTextBeEmpty_ShouldReturnFalse_WhenAutoCompleteTextIsEmpty()
{
// Arrange
string queryText = "Te";
string autoCompleteText = string.Empty;
// Act
bool result = MainViewModel.ShouldAutoCompleteTextBeEmpty(queryText, autoCompleteText);
// Assert
Assert.AreEqual(false, result);
}
[Test]
public void ShouldAutoCompleteTextBeEmpty_ShouldReturnTrue_WhenQueryTextMatchAutoCompleteText()
{
// Arrange
string queryText = "Te";
string autoCompleteText = "Teams";
// Act
bool result = MainViewModel.ShouldAutoCompleteTextBeEmpty(queryText, autoCompleteText);
// Assert
Assert.AreEqual(false, result);
}
[Test]
public void ShouldAutoCompleteTextBeEmpty_ShouldReturnTrue_WhenQueryTextIsEmpty()
{
// Arrange
string queryText = string.Empty;
string autoCompleteText = "Teams";
// Act
bool result = MainViewModel.ShouldAutoCompleteTextBeEmpty(queryText, autoCompleteText);
// Assert
Assert.AreEqual(true, result);
}
[Test]
public void ShouldAutoCompleteTextBeEmpty_ShouldReturnTrue_WhenQueryTextDoesNotMatchAutoCompleteText()
{
// Arrange
string queryText = "TE";
string autoCompleteText = "Teams";
// Act
bool result = MainViewModel.ShouldAutoCompleteTextBeEmpty(queryText, autoCompleteText);
// Assert
Assert.AreEqual(true, result);
}
} }
} }