Add text to search box

Answered

I am currently opening a new editor like this:

FileEditorManager.getInstance(psiElement.getProject()).openFile(virtualFile, true);

Is there a way to set some text in the search box for the newly opened editor so that the editor will automatically navigate to the first instance of the search text?

5 comments
Comment actions Permalink

You can use com.intellij.find.FindManager

0
Comment actions Permalink

I spent 2 hours looking for something that might work but was unsuccessful? A little more help possible? Thanks

0
Comment actions Permalink

Here is what I use to get the search text and options so I can duplicate search highlights in Markdown Navigator preview:

val searchSession = EditorSearchSession.get(myEditor)
if (searchSession != null) {
    val findModel = searchSession.findModel
    val searchText = searchSession.component.searchTextComponent.text
    val isRegex = findModel.isRegularExpressions
    val isCaseSensitive = findModel.isCaseSensitive
    val isWord = findModel.isWholeWordsOnly
    if (searchText.isNotEmpty()) {
        try {
            val pattern = if (isRegex) {
                Pattern.compile(searchText, if (isCaseSensitive) 0 else Pattern.CASE_INSENSITIVE)
            } else {
                if (isWord) {
                    val wordStart = searchText[0].isJavaIdentifierPart() && searchText[0] != '$'
                    val wordEnd = searchText[searchText.length - 1].isJavaIdentifierPart() && searchText[searchText.length - 1] != '$'
                    Pattern.compile("${if (wordStart) "\\b" else ""}\\Q$searchText\\E${if (wordEnd) "\\b" else ""}", if (isCaseSensitive) 0 else Pattern.CASE_INSENSITIVE)
                } else {
                    Pattern.compile("\\Q$searchText\\E", if (isCaseSensitive) 0 else Pattern.CASE_INSENSITIVE)
                }
            }

            highlightRanges = pattern
        } catch (e: PatternSyntaxException) {

        }
    }
}
0
Comment actions Permalink

Thanks. For some reason

EditorSearchSession.get(editor) is always null for me.
0
Comment actions Permalink

I think it will be null if no search is showing (or was showing) for the editor instance.

If you get null then you probably want to start a search. Try the static EditorSearchSession.start methods. I have not used them but they look promising to start a new search session for the editor.

You also need to look at the implementation for actions and use of API classes to make sense of what is going on. I would look for the search action and see how it starts a new search session for the editor, then do the same thing.

The best way I found is to have the intellij-community project locally. Then the IDE can help to find usages, implementations and to navigate the actual source not decompiled class giberish.

The IDE implementation can be hard enough to absorb with original source and comments in tact. Having to deal with decompiled symbol placeholders and no comments is too much for my taste.

0

Please sign in to leave a comment.