Skip to content

Commit 0eca734

Browse files
committed
style: detekt
1 parent 52247c2 commit 0eca734

File tree

1 file changed

+95
-70
lines changed

1 file changed

+95
-70
lines changed

plugin/src/main/kotlin/spp/jetbrains/sourcemarker/command/util/AutocompleteField.kt

Lines changed: 95 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ import javax.swing.event.DocumentListener
4040
import javax.swing.text.*
4141

4242
/**
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.
4445
*
4546
* @since 0.3.0
4647
* @author [Brandon Fergerson](mailto:[email protected])
@@ -255,88 +256,112 @@ class AutocompleteField<T : AutocompleteFieldRow>(
255256
override fun keyTyped(e: KeyEvent) = Unit
256257

257258
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()
277261
} 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()
283263
} 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()
289265
} 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
301302
}
303+
setText(autocompleteRow.getText() + " ")
302304
} 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())
313306
}
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+
}
315313

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() + " ")
322335
caretPosition = getText().length
323336
} 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
332338
}
333-
} else {
334-
ready = true
335339
}
336-
} else if (text != null) {
337-
addAutoCompleteToInput(text)
340+
} else {
338341
ready = true
339342
}
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()
340365
}
341366
}
342367

0 commit comments

Comments
 (0)