Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VIM-268 Completion for :e command #848

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/main/java/com/maddyhome/idea/vim/ui/ex/ExActions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ package com.maddyhome.idea.vim.ui.ex

import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.textarea.TextComponentEditorImpl
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.containers.CollectionFactory
import com.maddyhome.idea.vim.KeyHandler
import com.maddyhome.idea.vim.VimPlugin
import com.maddyhome.idea.vim.api.LocalOptionInitialisationScenario
Expand Down Expand Up @@ -293,3 +296,65 @@ internal class ToggleInsertReplaceAction : TextAction(ExEditorKit.ToggleInsertRe
private val logger = logger<ToggleInsertReplaceAction>()
}
}

internal class AutocompleteAction: TextAction(ExEditorKit.Autocomplete){
override fun actionPerformed(e: ActionEvent) {
val currentEditor = ExEntryPanel.getInstance().entry.editor
val target = getTextComponent(e) as ExTextField
val text = target.actualText
// autocomplete for edit
if(text.startsWith("e ") || text.startsWith("edit ")){
// split after first space to get filename to autocomplete
val fileStart = text.substring(text.indexOf(' ', 0) + 1)
// find potential files
val project = currentEditor!!.project!!

// find all files in the folders that are open
val editors = FileEditorManager.getInstance(project).getAllEditors()
val names = CollectionFactory.createSmallMemoryFootprintSet<String>()
val parents = CollectionFactory.createSmallMemoryFootprintSet<VirtualFile>()
for(editor in editors){
val parent = editor.file.parent
// we need to check a parent file only one time
if(parent in parents){
continue
}
parents.add(parent)
val childrens = parent.children
if (childrens != null){
for(child in childrens){
if(!child.isDirectory()) {
val name = child.name
if (name.startsWith(fileStart)) {
names.add(name)
}
}
}
}
}

// have we found a match?
if(names.size > 0){
val first = names.first()
// find the common starting string
var common = ""
for((i,c) in first.withIndex()){
var allSame = true
for(name in names){
if(name[i] != c) {
allSame = false
break
}
}
if(allSame){
common += c
} else {
break
}
}
// append common starting string
target.text += common.substring(fileStart.length)
}
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/maddyhome/idea/vim/ui/ex/ExEditorKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ internal object ExEditorKit : DefaultEditorKit() {
@NonNls
val StartLiteral: String = "start-literal"

@NonNls
val Autocomplete: String = "autocomplete"

private val logger = logger<ExEditorKit>()

/**
Expand Down Expand Up @@ -109,6 +112,7 @@ internal object ExEditorKit : DefaultEditorKit() {
HistoryDownFilterAction(),
ToggleInsertReplaceAction(),
InsertRegisterAction(),
AutocompleteAction(),
)

class DefaultExKeyHandler : DefaultKeyTypedAction() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/maddyhome/idea/vim/ui/ex/ExKeyBindings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ internal object ExKeyBindings {

KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.CTRL_DOWN_MASK), ExEditorKit.InsertRegister),

KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), ExEditorKit.Autocomplete),

// These appear to be non-Vim shortcuts
KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_V, KeyEvent.META_DOWN_MASK), DefaultEditorKit.pasteAction),
KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_INSERT, KeyEvent.SHIFT_DOWN_MASK), DefaultEditorKit.pasteAction),
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/jetbrains/plugins/ideavim/ui/ShowCmdTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package org.jetbrains.plugins.ideavim.ui

import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.ui.ShowCmd
import com.maddyhome.idea.vim.ui.ex.ExEntryPanel
import org.jetbrains.plugins.ideavim.SkipNeovimReason
import org.jetbrains.plugins.ideavim.TestWithoutNeovim
import org.jetbrains.plugins.ideavim.VimBehaviorDiffers
Expand Down Expand Up @@ -244,7 +245,29 @@ class ShowCmdTest : VimTestCase() {
assertEquals("22\"a22\"a22\"a22\"a22d22", getShowCmdTooltipText())
}

@TestWithoutNeovim(reason = SkipNeovimReason.SHOW_CMD)
@Test
fun `test edit autocomplete`() {
// the test document is called aaa.txt, so we can use it to autocomplete to that filename
typeText(injector.parser.parseKeys(":edit aa\t"))

// check if the text of the panel got autocompleted
assertEquals("edit aaa.txt", getExEntryPanelText())
}

@TestWithoutNeovim(reason = SkipNeovimReason.SHOW_CMD)
@Test
fun `test e autocomplete`() {
// the test document is called aaa.txt, so we can use it to autocomplete to that filename
typeText(injector.parser.parseKeys(":e aa\t"))

// check if the text of the panel got autocompleted
assertEquals("e aaa.txt", getExEntryPanelText())
}

private fun getShowCmdText() = ShowCmd.getWidgetText(fixture.editor!!)

private fun getShowCmdTooltipText() = ShowCmd.getFullText(fixture.editor!!)

private fun getExEntryPanelText() = ExEntryPanel.getInstance().text
}
Loading