Bug 456846 - Kate Snippets: not clear how to work with selected text
Summary: Kate Snippets: not clear how to work with selected text
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: plugin-snippets (show other bugs)
Version: 22.04.3
Platform: Other Linux
: NOR minor
Target Milestone: ---
Assignee: Buovjaga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-07-18 06:09 UTC by Buovjaga
Modified: 2022-07-28 21:02 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Buovjaga 2022-07-18 06:09:46 UTC
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."
Comment 1 Buovjaga 2022-07-24 19:32:57 UTC
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.
Comment 2 Thomas Friedrichsmeier 2022-07-24 21:02:11 UTC
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....
Comment 3 Thomas Friedrichsmeier 2022-07-25 19:40:30 UTC
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?
Comment 4 Buovjaga 2022-07-26 05:26:38 UTC
(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.
Comment 5 Buovjaga 2022-07-26 12:59:30 UTC
I created https://invent.kde.org/utilities/kate/-/merge_requests/821