Browse Source

Support for CloseKey in autocomplete append

Thomas 2 years ago
parent
commit
1eb6db9538

+ 20 - 0
Terminal.Gui/Core/Autocomplete/AppendAutocomplete.cs

@@ -39,9 +39,29 @@ namespace Terminal.Gui {
 			if (key == Key.CursorDown) {
 				return this.CycleSuggestion (-1);
 			}
+			else if(key == CloseKey && Suggestions.Any())
+			{
+				ClearSuggestions();
+				_suspendSuggestions = true;
+				return true;
+			}
+
+			if(char.IsLetterOrDigit((char)kb.KeyValue))
+			{
+				_suspendSuggestions = false;
+			}
 
 			return false;
 		}
+		bool _suspendSuggestions = false;
+		public override void GenerateSuggestions (AutocompleteContext context)
+		{
+			if(_suspendSuggestions)
+			{
+				return;
+			}
+			base.GenerateSuggestions (context);
+		}
 
 		public override void RenderOverlay (Point renderAt)
 		{

+ 47 - 2
UnitTests/Views/AppendAutocompleteTests.cs

@@ -16,11 +16,10 @@ namespace Terminal.Gui.ViewTests {
 		}
 
         [Fact, AutoInitShutdown]
-        public void TestAutocomplete_ShowThenAccept_MatchCase()
+        public void TestAutoAppend_ShowThenAccept_MatchCase()
         {
 			var tf = GetTextFieldsInView();
 
-
 			tf.Autocomplete = new AppendAutocomplete(tf);
 			var generator = (SingleWordSuggestionGenerator)tf.Autocomplete.SuggestionGenerator;
 			generator.AllSuggestions = new List<string>{"fish"};
@@ -49,6 +48,52 @@ namespace Terminal.Gui.ViewTests {
 			Assert.NotSame(tf,Application.Top.Focused);
         }
 
+		        [Fact, AutoInitShutdown]
+        public void TestAutoAppend_AfterCloseKey_NoAutocomplete()
+        {
+			var tf = GetTextFieldsInViewSuggestingFish();
+
+			// f is typed and suggestion is "fish"
+			tf.Redraw(tf.Bounds);
+			TestHelpers.AssertDriverContentsAre("fish",output);
+			Assert.Equal("f",tf.Text.ToString());
+
+			// When cancelling autocomplete
+			Application.Driver.SendKeys('e',ConsoleKey.Escape,false,false,false);
+
+			// Suggestion should disapear
+			tf.Redraw(tf.Bounds);
+			TestHelpers.AssertDriverContentsAre("f",output);
+			Assert.Equal("f",tf.Text.ToString());
+
+			// Still has focus though
+			Assert.Same(tf,Application.Top.Focused);
+
+			// But can tab away
+			Application.Driver.SendKeys('\t',ConsoleKey.Tab,false,false,false);
+			Assert.NotSame(tf,Application.Top.Focused);
+        }
+
+		private TextField GetTextFieldsInViewSuggestingFish ()
+		{
+			var tf = GetTextFieldsInView();
+			
+			tf.Autocomplete = new AppendAutocomplete(tf);
+			var generator = (SingleWordSuggestionGenerator)tf.Autocomplete.SuggestionGenerator;
+			generator.AllSuggestions = new List<string>{"fish"};
+
+			tf.Redraw(tf.Bounds);
+			TestHelpers.AssertDriverContentsAre("",output);
+
+			tf.ProcessKey(new KeyEvent(Key.f,new KeyModifiers()));
+
+			tf.Redraw(tf.Bounds);
+			TestHelpers.AssertDriverContentsAre("fish",output);
+			Assert.Equal("f",tf.Text.ToString());
+
+			return tf;
+		}
+
 		private TextField GetTextFieldsInView ()
 		{
             var tf = new TextField{