@@ -36,7 +36,7 @@ public class AutocompletePopup extends JPopupMenu {
3636 KeyEvent .VK_HOME ,
3737 KeyEvent .VK_END );
3838 private final JList <String > suggestionList = new JList <>();
39- private final AutocompleteData autocompleteData ;
39+ private final AutocompleteContext autocompleteContext ;
4040
4141 public AutocompletePopup (
4242 @ NotNull JTextComponent parent ,
@@ -45,7 +45,7 @@ public AutocompletePopup(
4545 @ NotNull List <String > suggestions ,
4646 @ NotNull Consumer <String > onCompletion ) {
4747 super ();
48- this .autocompleteData = new AutocompleteData (inputSoFar , suggestions );
48+ this .autocompleteContext = new AutocompleteContext (inputSoFar , suggestions );
4949
5050 int maxVisibleRows = Math .min (suggestions .size (), 10 ); // Limit to 10 rows max
5151 updateSuggestionList ();
@@ -59,7 +59,7 @@ public AutocompletePopup(
5959 public void mouseClicked (MouseEvent e ) {
6060 int index = suggestionList .locationToIndex (e .getPoint ());
6161 if (index >= 0 ) {
62- String selected = autocompleteData .getCompletion (index );
62+ String selected = autocompleteContext .getCompletion (index );
6363 onCompletion .accept (selected );
6464 setVisible (false );
6565 }
@@ -82,10 +82,22 @@ public void mouseMoved(MouseEvent e) {
8282 new KeyAdapter () {
8383 @ Override
8484 public void keyPressed (KeyEvent e ) {
85+ if (e .getKeyCode () == KeyEvent .VK_BACK_SPACE ) {
86+
87+ if (autocompleteContext .canDelete ()) {
88+ autocompleteContext .deleteInput ();
89+ updateSuggestionList ();
90+ } else {
91+ setVisible (false );
92+ }
93+
94+ parent .dispatchEvent (e );
95+ return ;
96+ }
8597 if (e .getKeyCode () == KeyEvent .VK_ENTER ) {
8698 int index = suggestionList .getSelectedIndex ();
8799 if (index >= 0 ) {
88- String selected = autocompleteData .getCompletion (index );
100+ String selected = autocompleteContext .getCompletion (index );
89101 onCompletion .accept (selected );
90102 setVisible (false );
91103 e .consume ();
@@ -115,24 +127,18 @@ public void keyPressed(KeyEvent e) {
115127 // popup
116128 if (CURSOR_MOVEMENT_PASSTHROUGH_KEYS .contains (e .getKeyCode ())) {
117129 setVisible (false );
118- parent .dispatchEvent (e ); // / TODO: is this needed?
130+ parent .dispatchEvent (e );
119131 return ;
120132 }
121133 }
122134
123135 @ Override
124136 public void keyTyped (KeyEvent e ) {
125137 char typedChar = e .getKeyChar ();
126- if (e .getKeyCode () == KeyEvent .VK_BACK_SPACE ) {
127- if (!autocompleteData .canDelete ()) {
128- setVisible (false );
129- return ;
130- }
131- autocompleteData .deleteInput ();
132- } else {
133- autocompleteData .appendInput (typedChar );
138+ if (Character .isDefined (typedChar ) && !Character .isISOControl (typedChar )) {
139+ autocompleteContext .appendInput (typedChar );
134140 }
135- if (autocompleteData .filteredSuggestions ().isEmpty ()) {
141+ if (autocompleteContext .filteredSuggestions ().isEmpty ()) {
136142 // Close the popup if no suggestions match
137143 setVisible (false );
138144 }
@@ -174,7 +180,7 @@ public void keyTyped(KeyEvent e) {
174180 }
175181
176182 private void updateSuggestionList () {
177- suggestionList .setListData (new Vector <>(autocompleteData .filteredSuggestions ()));
183+ suggestionList .setListData (new Vector <>(autocompleteContext .filteredSuggestions ()));
178184 suggestionList .setSelectedIndex (0 );
179185 }
180186
0 commit comments