The docs for Snippets could use some love: https://docs.kde.org/trunk5/en/kate/kate/kate-application-plugin-snippets.html I am looking for a way to wrap the selected text in tags, for example to help with MediaWiki Translate extension. Searching all over the web, I found many others looking for the same information. There is an old blog post with relevant discussion in the comments: https://milianw.de/blog/snippets-in-kdevelop-kate.html It is said that at some point this used to work: <strong>%{selection}</strong> There is a function in the view API view.selectedText() but it does not seem to be applying or maybe I am missing something. https://docs.kde.org/trunk5/en/kate/katepart/dev-scripting.html#dev-scripting-api Given this in the Snippet tab <translate>${text}</translate> and in the Scripts tab text = view.selectedText(); the variable will be simply be replaced by "text". I also tried with editor.clipboardText() but the result was still "text", which I don't understand at all. If I use the percentage sign instead of dollar sign, like %{text}, the result will just be %{text}. I found this doc from 2010 that explains the difference between $ and % variables: https://kate-editor.org/2010/02/03/snippets-in-kdevelop-kate/ "snippet gets inserted (properly indented) and potential placeholders/variables get expanded. A variable is something like %{date} or ${email}. Also take a look at the API documentation. variables that get inserted via “${…}” will be “selectable”, meaning you can jump from one var to the other by hitting TAB / Shift TAB the %{…} vars will only get expanded and inserted, without getting selectable."
Thomas: excuse me for adding you into the Cc, but I noticed your recent commit to Kate Snippets docs: https://invent.kde.org/utilities/kate/-/commit/bbdb00c136290021d642b6d9b20c0e04a8ecc9bf Maybe you have some insight on this problem.
In RKWard we use the following in the snippet tab (example): ${rangeCommand("**%%1**", "Bold")} where rangeCommand is defined as (in the scripts tab): function rangeCommand(command, def) { if (view.selectedText().length > 0) { return command.replace("%%1", view.selectedText()); } else { return command.replace("%%1", def); } } This puts the selected text inside "**" markup, or inserts "**Bold**", in case nothing is selected. That took quite some experimenting to figure out, indeed....
But in fact, if you do not care about adding a default value, in case there is no selection, you can simply write (snippet tab): <translate>${view.selectedText()}</translate> Would you like to create a merge request adding some illustrative examples?
(In reply to Thomas Friedrichsmeier from comment #3) > But in fact, if you do not care about adding a default value, in case there > is no selection, you can simply write (snippet tab): > > <translate>${view.selectedText()}</translate> > > Would you like to create a merge request adding some illustrative examples? Thanks a lot for these! Yes, I will look into creating a merge request for the docs.
I created https://invent.kde.org/utilities/kate/-/merge_requests/821