API to highlight search results in editor?
I have a python script that finds the most used words in a text file.
I have a working algorithm for finding the most used words. From this I create a listview of the words and phrases which I can click on.
I currently have a text view (which shows the markdown file) and use attributed text to highlight the clicked on word.
However, this reimplements the wheel, that is Pythonista's editor.
So, I would love to be able to click on the items and highlight the occurrences (case-insensitive) in a Pythonista editor tab (so that I can quickly edit some of the frequently used words with alternatives)
Does anyone have an example of how to highlight search results in the editor? I've experimented with editorView, but end up in a mess either setting up NSRegularExpression or getting the results to highlight.
Have you tried using on_main_thread?
Thanks for the on_main_thread idea - I gave it a go, but the search term didn't change and it just cycled through the old search results.
I'll keep playing with it.
@cvp that issue is due to the sandbox security limitations of the uidocumentpicker -- in this case the search bar is hosted by pythonista.
@JonB Ok, sorry, I said that due to the same try to set searchfield text...Bad thinking 😢
import editor, objc_util @objc_util.on_main_thread def setSearchTerm(txt): t=editor._get_editor_tab() v=t.editorView() v.searchController().searchView().searchBar().text=txt v.searchController().searchBar_textDidChange_(v.searchController().searchView().searchBar(),txt) def highlighNext(): t=editor._get_editor_tab() v=t.editorView() v.highlightNextSearchResult() setSearchTerm('tab')
Setting the searchbar text seems necessary.
import editor from objc_util import * t = editor._get_editor_tab() v = t.editorView() v.searchController().searchView().subviews().setText_('test')
My example above used
Which is probably more robust than using subviews. Sorry I wasnt clear -- the above example is fully functional. (You need to also call the delegate method for the little arrows to show up)
@JonB That is fantastic. It took a second to realise I needed to open the search bar first, but now it works perfectly.
ps. Sorry to only just reply, I wasn't getting alerts on your posts