Skip to content

Commit 68685cf

Browse files
authored
Add list rollover (#176)
* Add list rollover * Update readme * Remove unnecessary file * Refactor to a more readable function and make this a minor bump
1 parent d5a6435 commit 68685cf

5 files changed

Lines changed: 85 additions & 16 deletions

File tree

build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2727
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2828

2929
// Use the same version and group for the jar and the plugin
30-
val currentVersion = "2.4.0"
30+
val currentVersion = "2.4.1"
3131
val myGroup = "com.mituuz"
3232
version = currentVersion
3333
group = myGroup
@@ -39,6 +39,12 @@ intellijPlatform {
3939

4040
changeNotes = """
4141
<h2>Version $currentVersion</h2>
42+
<ul>
43+
<li>
44+
Add list rollover support
45+
</li>
46+
</ul>
47+
<h2>Version 2.4.0</h2>
4248
<ul>
4349
<li>
4450
Add file recency scoring

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Version 2.4.1
4+
5+
- Add list rollover support
6+
37
## Version 2.4.0
48

59
- Add file recency scoring

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ List movement can be remapped from settings -> keymaps, but do not support chord
4545
- Fuzzy file search
4646
- Search all except excluded files
4747
- Search only from VCS-tracked files
48-
- Text search leveraging [ripgrep](https://github.com/BurntSushi/ripgrep "Link to GitHub - ripgrep"), grep or findstr
48+
- Text search leveraging [ripgrep](https://github.com/BurntSushi/ripgrep "Link to GitHub - ripgrep") or a native
49+
implementation
4950
- Support for searching from the whole project, within open tabs or the current buffer
5051
- With file extension support for ripgrep in the secondary search field
5152
- File mover

src/main/kotlin/com/mituuz/fuzzier/actions/FuzzyAction.kt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import java.awt.Font
5959
import java.awt.event.ActionEvent
6060
import java.util.concurrent.ConcurrentHashMap
6161
import javax.swing.*
62+
import kotlin.time.Duration.Companion.milliseconds
6263

6364
abstract class FuzzyAction : AnAction() {
6465
companion object {
@@ -160,7 +161,7 @@ abstract class FuzzyAction : AnAction() {
160161
debounceJob?.cancel()
161162
val debouncePeriod = globalState.debouncePeriod
162163
debounceJob = actionScope?.launch {
163-
delay(debouncePeriod.toLong())
164+
delay(debouncePeriod.toLong().milliseconds)
164165
updateListContents(project, component.searchField.text)
165166
}
166167
}
@@ -225,20 +226,37 @@ abstract class FuzzyAction : AnAction() {
225226
}
226227

227228
fun moveListUp() {
228-
val selectedIndex = component.fileList.selectedIndex
229-
if (selectedIndex > 0) {
230-
component.fileList.selectedIndex = selectedIndex - 1
231-
component.fileList.ensureIndexIsVisible(selectedIndex - 1)
229+
val fileList = component.fileList
230+
val listSize = fileList.model.size
231+
232+
if (listSize == 0) return
233+
234+
val currentIndex = fileList.selectedIndex
235+
val newIndex = if (currentIndex in 1 until listSize) {
236+
currentIndex - 1
237+
} else {
238+
listSize - 1
232239
}
240+
241+
fileList.selectedIndex = newIndex
242+
fileList.ensureIndexIsVisible(newIndex)
233243
}
234244

235245
fun moveListDown() {
236-
val selectedIndex = component.fileList.selectedIndex
237-
val length = component.fileList.model.size
238-
if (selectedIndex < length - 1) {
239-
component.fileList.selectedIndex = selectedIndex + 1
240-
component.fileList.ensureIndexIsVisible(selectedIndex + 1)
246+
val fileList = component.fileList
247+
val listSize = fileList.model.size
248+
249+
if (listSize == 0) return
250+
251+
val currentIndex = fileList.selectedIndex
252+
val newIndex = if (currentIndex < listSize - 1) {
253+
currentIndex + 1
254+
} else {
255+
0
241256
}
257+
258+
fileList.selectedIndex = newIndex
259+
fileList.ensureIndexIsVisible(newIndex)
242260
}
243261

244262
fun getCellRenderer(): ListCellRenderer<Any?> {

src/test/kotlin/com/mituuz/fuzzier/FuzzyActionTest.kt

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import com.intellij.testFramework.TestApplicationManager
3333
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
3434
import com.mituuz.fuzzier.actions.FuzzyAction
3535
import com.mituuz.fuzzier.components.SimpleFinderComponent
36+
import com.mituuz.fuzzier.entities.FuzzyContainer
3637
import com.mituuz.fuzzier.entities.FuzzyContainer.FilenameType.*
3738
import com.mituuz.fuzzier.entities.FuzzyMatchContainer
3839
import com.mituuz.fuzzier.entities.FuzzyMatchContainer.FileType.FILE
@@ -41,10 +42,7 @@ import org.junit.jupiter.api.Assertions.*
4142
import org.junit.jupiter.api.Test
4243
import java.awt.event.InputEvent
4344
import java.awt.event.KeyEvent
44-
import javax.swing.JComponent
45-
import javax.swing.JLabel
46-
import javax.swing.JList
47-
import javax.swing.KeyStroke
45+
import javax.swing.*
4846

4947
class FuzzyActionTest {
5048
@Suppress("unused")
@@ -185,6 +183,48 @@ class FuzzyActionTest {
185183
assertEquals(expectedIcon, component.icon)
186184
}
187185

186+
@Test
187+
fun `Test moveListUp rollover`() {
188+
val action = getAction()
189+
action.component = SimpleFinderComponent()
190+
val model = DefaultListModel<FuzzyContainer>()
191+
model.addElement(FuzzyMatchContainer(FuzzyScore(), "/src/asd1", "asd1", "", FILE))
192+
model.addElement(FuzzyMatchContainer(FuzzyScore(), "/src/asd2", "asd2", "", FILE))
193+
model.addElement(FuzzyMatchContainer(FuzzyScore(), "/src/asd3", "asd3", "", FILE))
194+
action.component.fileList.model = model
195+
action.component.fileList.selectedIndex = 0
196+
197+
action.moveListUp()
198+
assertEquals(2, action.component.fileList.selectedIndex)
199+
200+
action.moveListUp()
201+
assertEquals(1, action.component.fileList.selectedIndex)
202+
203+
action.moveListUp()
204+
assertEquals(0, action.component.fileList.selectedIndex)
205+
}
206+
207+
@Test
208+
fun `Test moveListDown rollover`() {
209+
val action = getAction()
210+
action.component = SimpleFinderComponent()
211+
val model = DefaultListModel<FuzzyContainer>()
212+
model.addElement(FuzzyMatchContainer(FuzzyScore(), "/src/asd1", "asd1", "", FILE))
213+
model.addElement(FuzzyMatchContainer(FuzzyScore(), "/src/asd2", "asd2", "", FILE))
214+
model.addElement(FuzzyMatchContainer(FuzzyScore(), "/src/asd3", "asd3", "", FILE))
215+
action.component.fileList.model = model
216+
action.component.fileList.selectedIndex = 2
217+
218+
action.moveListDown()
219+
assertEquals(0, action.component.fileList.selectedIndex)
220+
221+
action.moveListDown()
222+
assertEquals(1, action.component.fileList.selectedIndex)
223+
224+
action.moveListDown()
225+
assertEquals(2, action.component.fileList.selectedIndex)
226+
}
227+
188228
private fun getAction(): FuzzyAction {
189229
return object : FuzzyAction() {
190230
override fun actionPerformed(actionEvent: AnActionEvent) {

0 commit comments

Comments
 (0)