-
Notifications
You must be signed in to change notification settings - Fork 761
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
Add search widget #1054
Open
jphalip
wants to merge
1
commit into
JetBrains:master
Choose a base branch
from
jphalip:search-widget
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add search widget #1054
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/main/java/com/maddyhome/idea/vim/ui/widgets/search/SearchWidgetFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright 2003-2024 The IdeaVim authors | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE.txt file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
package com.maddyhome.idea.vim.ui.widgets.search | ||
|
||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.project.ProjectManager | ||
import com.intellij.openapi.wm.StatusBarWidget | ||
import com.intellij.openapi.wm.StatusBarWidgetFactory | ||
import com.intellij.openapi.wm.WindowManager | ||
import com.intellij.openapi.wm.impl.status.widget.StatusBarWidgetsManager | ||
import com.maddyhome.idea.vim.VimPlugin | ||
import com.maddyhome.idea.vim.api.VimEditor | ||
import com.maddyhome.idea.vim.api.globalOptions | ||
import com.maddyhome.idea.vim.api.injector | ||
import com.maddyhome.idea.vim.common.SearchListener | ||
import com.maddyhome.idea.vim.helper.vimLastHighlighters | ||
import com.maddyhome.idea.vim.newapi.ij | ||
import com.maddyhome.idea.vim.ui.widgets.VimWidgetListener | ||
import com.maddyhome.idea.vim.ui.widgets.mode.VimStatusBarWidget | ||
import java.awt.Component | ||
|
||
private const val ID = "IdeaVimSearch" | ||
|
||
internal class SearchWidgetFactory : StatusBarWidgetFactory { | ||
|
||
override fun getId(): String { | ||
return ID | ||
} | ||
|
||
override fun getDisplayName(): String { | ||
return "IdeaVim Search" | ||
} | ||
|
||
override fun createWidget(project: Project): StatusBarWidget { | ||
return VimSearchWidget() | ||
} | ||
|
||
override fun isAvailable(project: Project): Boolean { | ||
return VimPlugin.isEnabled() && injector.globalOptions().showmode | ||
} | ||
} | ||
|
||
fun updateSearchWidget() { | ||
val factory = StatusBarWidgetFactory.EP_NAME.findExtension(SearchWidgetFactory::class.java) ?: return | ||
for (project in ProjectManager.getInstance().openProjects) { | ||
val statusBarWidgetsManager = project.service<StatusBarWidgetsManager>() | ||
statusBarWidgetsManager.updateWidget(factory) | ||
} | ||
} | ||
|
||
class VimSearchWidget : StatusBarWidget, VimStatusBarWidget { | ||
var content: String = "" | ||
|
||
override fun ID(): String { | ||
return ID | ||
} | ||
|
||
override fun getPresentation(): StatusBarWidget.WidgetPresentation { | ||
return VimModeWidgetPresentation() | ||
} | ||
|
||
private inner class VimModeWidgetPresentation : StatusBarWidget.TextPresentation { | ||
override fun getAlignment(): Float = Component.CENTER_ALIGNMENT | ||
|
||
override fun getText(): String { | ||
return content | ||
} | ||
|
||
override fun getTooltipText(): String { | ||
return content.ifEmpty { | ||
"No search in progress" | ||
} | ||
} | ||
} | ||
} | ||
|
||
class SearchWidgetListener : SearchListener, VimWidgetListener({ updateSearchWidget() }) { | ||
|
||
override fun searchUpdated(editor: VimEditor, offset: Int) { | ||
val ijEditor = editor.ij | ||
var currentHighlighter = 1 | ||
val numHighlighters = ijEditor.vimLastHighlighters?.size ?: 0 | ||
for (highlighter in ijEditor.vimLastHighlighters!!) { | ||
if (highlighter.startOffset == offset) { | ||
break | ||
} | ||
currentHighlighter++ | ||
} | ||
for (project in ProjectManager.getInstance().openProjects) { | ||
val searchWidget = getWidget(project) ?: continue | ||
searchWidget.content = String.format("Occurrence: %s of %s", currentHighlighter, numHighlighters) | ||
searchWidget.updateWidgetInStatusBar(ID, project) | ||
} | ||
} | ||
|
||
override fun searchStopped() { | ||
for (project in ProjectManager.getInstance().openProjects) { | ||
val searchWidget = getWidget(project) ?: continue | ||
searchWidget.content = "" | ||
searchWidget.updateWidgetInStatusBar(ID, project) | ||
} | ||
} | ||
|
||
private fun getWidget(project: Project): VimSearchWidget? { | ||
val statusBar = WindowManager.getInstance()?.getStatusBar(project) ?: return null | ||
return statusBar.getWidget(ID) as? VimSearchWidget | ||
} | ||
|
||
} | ||
|
||
val searchWidgetOptionListener: VimWidgetListener = VimWidgetListener { updateSearchWidget() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1340,13 +1340,15 @@ abstract class VimSearchGroupBase : VimSearchGroup { | |
} else if (lastPatternTrailing!![ppos + 1] == '?') { | ||
Direction.BACKWARDS | ||
} else { | ||
injector.listenersNotifier.notifySearchUpdated(editor, res) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a good place for running listeners. The |
||
return if (res == -1) null else Pair(res, motionType) | ||
} | ||
if (lastPatternTrailing!!.length - ppos > 2) { | ||
ppos++ | ||
} | ||
res = processSearchCommand(editor, lastPatternTrailing!!.substring(ppos + 1), res, 1, nextDir)?.first ?: -1 | ||
} | ||
injector.listenersNotifier.notifySearchUpdated(editor, res) | ||
return if (res == -1) null else Pair(res, motionType) | ||
} | ||
|
||
|
18 changes: 18 additions & 0 deletions
18
vim-engine/src/main/kotlin/com/maddyhome/idea/vim/common/SearchListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright 2003-2024 The IdeaVim authors | ||
* | ||
* Use of this source code is governed by an MIT-style | ||
* license that can be found in the LICENSE.txt file or at | ||
* https://opensource.org/licenses/MIT. | ||
*/ | ||
|
||
package com.maddyhome.idea.vim.common | ||
|
||
import com.maddyhome.idea.vim.api.VimEditor | ||
|
||
interface SearchListener { | ||
|
||
fun searchUpdated(editor: VimEditor, offset: Int) | ||
fun searchStopped() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Directly accessing the
content
here leaks incapsulation. Let's have special functions likesetSearch(current, total)
andclearSearch()
.(function names are subject to change).