@@ -40,7 +40,8 @@ import javax.swing.event.DocumentListener
40
40
import javax.swing.text.*
41
41
42
42
/* *
43
- * todo: description.
43
+ * AutocompleteField is a custom text input field with autocomplete functionality.
44
+ * It extends JTextPane and implements various listener interfaces for handling user input and interactions.
44
45
*
45
46
* @since 0.3.0
46
47
* @author [Brandon Fergerson](mailto:[email protected] )
@@ -255,88 +256,112 @@ class AutocompleteField<T : AutocompleteFieldRow>(
255
256
override fun keyTyped (e : KeyEvent ) = Unit
256
257
257
258
override fun keyPressed (e : KeyEvent ) {
258
- if (e.keyCode == KeyEvent .VK_SPACE && hasControlHeld) {
259
- results.clear()
260
- results.addAll(allLookup
261
- .filter { it.getText().lowercase().contains(text) }
262
- .sortedBy { it.getText() })
263
- model.updateView()
264
- list.visibleRowCount = results.size.coerceAtMost(maxSuggestSize)
265
- autocompleteDropdown?.setCurrentCommandsLabel(list.visibleRowCount)
266
- if (results.size > 0 ) {
267
- list.selectedIndex = 0
268
- }
269
-
270
- if (results.size > 0 ) {
271
- showAutocompletePopup()
272
- } else {
273
- hideAutocompletePopup()
274
- }
275
- } else if (e.keyCode == KeyEvent .VK_CONTROL ) {
276
- hasControlHeld = true
259
+ if (e.keyCode == KeyEvent .VK_CONTROL ) {
260
+ controlPressed()
277
261
} else if (e.keyCode == KeyEvent .VK_UP ) {
278
- val index = list.selectedIndex
279
- if (index > 0 ) {
280
- list.selectedIndex = index - 1
281
- }
282
- scrollListToSelected()
262
+ upPressed()
283
263
} else if (e.keyCode == KeyEvent .VK_DOWN ) {
284
- val index = list.selectedIndex
285
- if (index != - 1 && list.model.size > index + 1 ) {
286
- list.selectedIndex = index + 1
287
- }
288
- scrollListToSelected()
264
+ downPressed()
289
265
} else if (e.keyCode == KeyEvent .VK_TAB ) {
290
- if (text.isBlank() || list.selectedValue == null || (! replaceCommandOnTab && ! autocompleteOnTab)) return
291
- val autocompleteRow = list.selectedValue
292
- if (replaceCommandOnTab) {
293
- if (autocompleteRow is LiveCommandFieldRow && autocompleteRow.liveCommand.params.isNotEmpty()) {
294
- val triggerPrefix = autocompleteRow.liveCommand.getTriggerName().lowercase() + " "
295
- if (text.lowercase().startsWith(triggerPrefix)) {
296
- return // do nothing
297
- }
298
- setText(autocompleteRow.getText() + " " )
299
- } else {
300
- setText(autocompleteRow.getText())
266
+ tabPressed()
267
+ } else if (e.keyCode == KeyEvent .VK_ENTER ) {
268
+ enterPressed()
269
+ } else if (e.keyCode == KeyEvent .VK_SPACE && hasControlHeld) {
270
+ controlSpacePressed()
271
+ }
272
+ }
273
+
274
+ private fun controlPressed () {
275
+ hasControlHeld = true
276
+ }
277
+
278
+ private fun upPressed () {
279
+ val index = list.selectedIndex
280
+ if (index > 0 ) {
281
+ list.selectedIndex = index - 1
282
+ }
283
+ scrollListToSelected()
284
+ }
285
+
286
+ private fun downPressed () {
287
+ val index = list.selectedIndex
288
+ if (index != - 1 && list.model.size > index + 1 ) {
289
+ list.selectedIndex = index + 1
290
+ }
291
+ scrollListToSelected()
292
+ }
293
+
294
+ private fun tabPressed () {
295
+ if (text.isBlank() || list.selectedValue == null || (! replaceCommandOnTab && ! autocompleteOnTab)) return
296
+ val autocompleteRow = list.selectedValue
297
+ if (replaceCommandOnTab) {
298
+ if (autocompleteRow is LiveCommandFieldRow && autocompleteRow.liveCommand.params.isNotEmpty()) {
299
+ val triggerPrefix = autocompleteRow.liveCommand.getTriggerName().lowercase() + " "
300
+ if (text.lowercase().startsWith(triggerPrefix)) {
301
+ return // do nothing
301
302
}
303
+ setText(autocompleteRow.getText() + " " )
302
304
} else {
303
- val userInput = text.substringAfterLast(" " )
304
- setText(text.substring(0 , text.length - userInput.length) + autocompleteRow.getText())
305
- }
306
- caretPosition = text.length
307
- } else if (e.keyCode == KeyEvent .VK_ENTER ) {
308
- if (! autocompleteAndFinishOnEnter) {
309
- ready = true
310
- actualText = text
311
- hideAutocompletePopup()
312
- return
305
+ setText(autocompleteRow.getText())
313
306
}
314
- actualText = text
307
+ } else {
308
+ val userInput = text.substringAfterLast(" " )
309
+ setText(text.substring(0 , text.length - userInput.length) + autocompleteRow.getText())
310
+ }
311
+ caretPosition = text.length
312
+ }
315
313
316
- val text = if (isPopupVisible()) list.selectedValue else null
317
- if (text is LiveCommandFieldRow ) {
318
- val liveCommand = text.liveCommand
319
- if (liveCommand.params.isNotEmpty()) {
320
- if (! getText().lowercase().startsWith(liveCommand.getTriggerName().lowercase() + " " )) {
321
- setText(text.getText() + " " )
314
+ private fun enterPressed () {
315
+ if (! autocompleteAndFinishOnEnter) {
316
+ ready = true
317
+ actualText = text
318
+ hideAutocompletePopup()
319
+ return
320
+ }
321
+ actualText = text
322
+
323
+ val text = if (isPopupVisible()) list.selectedValue else null
324
+ if (text is LiveCommandFieldRow ) {
325
+ val liveCommand = text.liveCommand
326
+ if (liveCommand.params.isNotEmpty()) {
327
+ if (! getText().lowercase().startsWith(liveCommand.getTriggerName().lowercase() + " " )) {
328
+ setText(text.getText() + " " )
329
+ caretPosition = getText().length
330
+ } else {
331
+ val params = substringAfterIgnoreCase(getText(), liveCommand.getTriggerName())
332
+ .split(" " ).filter { it.isNotEmpty() }
333
+ if (params.size < liveCommand.params.size) {
334
+ setText(getText().trimEnd() + " " )
322
335
caretPosition = getText().length
323
336
} else {
324
- val params = substringAfterIgnoreCase(getText(), liveCommand.getTriggerName())
325
- .split(" " ).filter { it.isNotEmpty() }
326
- if (params.size < liveCommand.params.size) {
327
- setText(getText().trimEnd() + " " )
328
- caretPosition = getText().length
329
- } else {
330
- ready = true
331
- }
337
+ ready = true
332
338
}
333
- } else {
334
- ready = true
335
339
}
336
- } else if (text != null ) {
337
- addAutoCompleteToInput(text)
340
+ } else {
338
341
ready = true
339
342
}
343
+ } else if (text != null ) {
344
+ addAutoCompleteToInput(text)
345
+ ready = true
346
+ }
347
+ }
348
+
349
+ private fun controlSpacePressed () {
350
+ results.clear()
351
+ results.addAll(allLookup
352
+ .filter { it.getText().lowercase().contains(text) }
353
+ .sortedBy { it.getText() })
354
+ model.updateView()
355
+ list.visibleRowCount = results.size.coerceAtMost(maxSuggestSize)
356
+ autocompleteDropdown?.setCurrentCommandsLabel(list.visibleRowCount)
357
+ if (results.size > 0 ) {
358
+ list.selectedIndex = 0
359
+ }
360
+
361
+ if (results.size > 0 ) {
362
+ showAutocompletePopup()
363
+ } else {
364
+ hideAutocompletePopup()
340
365
}
341
366
}
342
367
0 commit comments