How do I transform a string with IDEA Live Template? - intellij-idea

Given something like:
org.jboss:jboss-remote-naming
...it would be useful to be able to transform it to:
exclude group: "org.jboss", module: "jboss-remote-naming"
Use case is exclusions for Gradle dependencies. Have no idea how to do this though. Could use some tips. I've been to the documentation often enough to know this probably isn't in there, but I will double check.
"exclude group: \""+ "org.jboss.logging:jboss-logging".replaceAll(':','\", module: \"') +"\""
...OK I've figured that you can do the above in the Groovy shell which results in the below:
groovy:000> "exclude group: \""+ "org.jboss.logging:jboss-logging".replaceAll(':','\", module: \"') +"\""
===> exclude group: "org.jboss.logging", module: "jboss-logging"
Now, how to get IDEA to use it?

Well, nobody has responded and I've found the answer anyway.
So the problem is that I've got strings like this on my clip board:
org.jboss.logging:jboss-logging
I'm using Gradle and IntelliJ IDEA. What I'm doing is fixing a big bunch of dependencies and I want to be able to paste exclusions in in the right form which is:
exclude group: "org.jboss.logging", module: "jboss-logging"
What I did was:
CTRL+ALT+S | Edit | Live Templates. Note that I've previously created a "User" group herein to put my personal Live Templates. I click "+" to add a new one to the User group.
Set Template Text to: $VAR$$END$
Select the Edit Variables button and set the Expression field to:
groovyScript("'exclude group: \"'+ _1.replaceAll(':','\", module: \"') +'\"'",clipboard())
Set the Abbreviation field to 'xld'.
Note the following regarding the text above:
A. groovyScript executes a short one line Groovy script (or Groovy file for a bigger script). See the docs here: Predefined Functions
B. Everything is wrapped in "" and then inside those double quotes, every time you want to use a double quote like so: ", you have to escape it with a \. The output has double quotes but aside from that I tried to stick to single quotes.
C. _1 is a string that represents the result returned by the clipboard() method.
How to use it: Copy any dependency of the form "org.jboss:jboss-remote-naming" then at the appropriate location in IntelliJ IDEA type xld and then [TAB]. It should expand using the proper syntax for a Gradle exclusion.

Related

Intellij Idea Live Templates

I faced with the problem of writing my vcs current branch name each time I have written 'todo' comment.
Recently I learned about Intellij's 'Live Templates' which is quite comfortable to use. I tried to apply it to my problem but there's no templates to take out a branch name.
So the question is could I actually take out the name of my branch to code comments somehow?
It is possible to use the groovyScript predefined function and a script to extract the branch name. For example create the following live template:
$COMMENT$ todo [$BRANCH$]: $END$
with abbreviation "todo" and description "Inserts todo comment with branch name". Click Edit variables and give the variables the following definitions:
COMMENT:
lineCommentStart()
BRANCH (updated for 2020.2 and newer)
groovyScript("com.intellij.dvcs.repo.VcsRepositoryManager.getInstance(_editor.project).getRepositoryForFileQuick(com.intellij.openapi.fileEditor.FileDocumentManager.getInstance().getFile(_editor.document)).getCurrentBranchName()")
Skip if defined checked for both variables. The Groovy script is (unfortunately) all one line. Set applicable contexts to Everywhere.
With this live template it is now possible to type todoTab somewhere in a source file and a line comment with the branch name will be inserted. This will insert the proper line comment depending on the language of the file, or nothing in case of languages without a line comment like HTML. And should extract the branch name no matter the type of version control used (I tested with Git).
For live templates you can use predefined functions. Unfortunately there is no function to detect the current VCS branch.
But you can create a template to make work a little easier:
// TODO [$branch_name$]: $comment$
With this template, you still have to fill branch name, but you should not type symbols like [ and caret will be placed automatically.
You can also create a feature request for a new predefined function.

Scripts connected to an Image in EggPlant

I have LoginButton.png that is being used across the whole suite in different scripts. I want to edit the name or move it to a new folder, without breaking all the scripts. So is there a way to list all the scripts that are using this image or to refactor the name/path across the whole suite?
Find option (Edit>Find) it's the closest to what I want, but it only looks at the open script, not the whole suite, and replaces anything with similar naming convention eg: Find "LoginButton" and replace with "NewButton", if you have "LoginButton", "LoginButton1", "LoginButton2" after the replace you will end up with "NewButton", "NewButton1", "NewButton2", and i just want to change "LoginButton" not "LoginButton1" and "LoginButton2".
Thanks
Unfortunately, you've found the closest solution. One of Eggplant's major drawbacks is that you're locked into a single, (rather feature-less) IDE. If you're having issues matching other words with your search query, you can try including spaces, i.e. " LoginButton ", on the outside of the word.

funcList Visual Studio Code regex for custom language

I'm trying to get the funcList visual studio code extension plugin to work with a proprietary language that I use and I'm having a problem getting the regex to work. The plugin documentation can be found here: https://marketplace.visualstudio.com/items?itemName=qrti.funclist and describes creating a settings.json file inside the .vscode folder of your project. My problems arise when trying to modify the regex expressions for the funcList.nativeFilter and funcList.displayFilter configuration values. Here is my current settings.json file:
{
"funcList.nativeFilter": "/(?:^|\\s)Function\\s+\\w+\\(/mg",
"funcList.displayFilter": "/\\s*Function\\s+(.*)/1",
"funcList.sortList": 1,
"funcList.doubleSpacing": false
}
I believe the main problem is the part of \\w+ in the nativeFilter property. This seems to only match on characters/numbers but not any special characters. Here is a snippet of a piece of code that I would like to work with this Function List:
Function Do.Something(paramOne, paramTwo)
'...
End Function
Method Do.Something_Else(paramOne, paramTwo)
'...
End Function
Ideally, the nativeFilter would capture Function Do.Something(paramOne, paramTwo) .. until End Function and then the displayFilter would only capture the first line (ie. Do.Something(paramOne, paramTwo))
Note: according to the docs, nativeFilter does not allow regex groups, but displayFilter allows groups 0-9.
I use this for javascript and the funcList extension. It is similar to what you are trying to do:
// so that "function someName(arg1, arg2) is captured
"funcList.nativeFilter": "/^[a-z]+\\s+\\w+\\s*\\(.*\\)/mgi",
// now display "someName(arg1, arg2)"
"funcList.displayFilter": "/\\S* +(\\w+\\s*\\(.*\\))/1",
You obviously have capital letters, periods and underscores to worry about but hopefully this helps you to some degree. [I see I did not need to actually put the function keyword in to make it work...]
[EDIT] Try this, it seems to work:
"funcList.nativeFilter": "/^Function \\w+.\\w+\\(.*\\)?/mg",
"funcList.displayFilter": "/\\S* +(\\w+.\\w+\\(.*\\))/1",
You just needed the . as it is not included in \w
This seems to be working for my needs:
{
"funcList.nativeFilter": "/^(Function|Method|Macro)\\s+[a-zA-Z0-9.+(){}\\/\\\\[\\],_\\-=:;!##$%^&*|,.<>? ]*\\).*$/mg",
"funcList.displayFilter": "/\\s*((Function|Method|Macro)\\s+[a-zA-Z0-9.+(){}\\/\\\\[\\],_\\-=:;!##$%^&*|,.<>? ]+)/1"
}
With this plugin, you need to capture the entire line in order to get the navigation to work when clicking on a function list.

How do I write a robust structural search template to report Mockito times(1)/Times(1) passed to verify in IntelliJ IDEA?

In my project Mockito.times(1) is often used when verifying mocks:
verify(mock, times(1)).call();
This is redundant since Mockito uses implicit times(1) for verify(Object), thus the following code does exactly what the code above does:
verify(mock).call();
So I'm going to write an a structural search drive inspection to report such cases (let's say, named something like Mockito.times(1) is redundant). As I'm not an expert in IntelliJ IDEA structural search, my first attempt was:
Mockito.times(1)
Obviously, this is not a good seach template because it ignores the call-site. Let's say, I find it useful for the following code and I would not like the inspection to trigger:
VerificationMode times = Mockito.times(1);
// ^ unwanted "Mockito.times(1) is redundant"
So now I would like to define the context where I would like the inspection to trigger. Now the inspection search template becomes:
Mockito.verify($mock$, Mockito.times(1))
Great! Now code like verify(mock, times(1)).call() is reported fine (if times was statically imported from org.mockito.Mockito). But there is also one thing. Mockito.times actually comes from its VerificationModeFactory class where such verification modes are grouped, so the following line is ignored by the inspection:
verify(mockSupplier, VerificationModeFactory.times(1)).get();
My another attempt to fix this one was something like:
Mockito.verify($mock$, $times$(1))
where:
$mock$ is still a default template variable;
$times$ is a variable with Text/regexp set to times, Whole words only and Value is read are set to true, and Expression type (regexp) is set to (Times|VerificationMode) -- at least this is the way I believed it should work.
Can't make it work. Why is Times also included to the regexp? This is the real implementation of *.times(int), so, ideally, the following line should be reported too:
verify(mockSupplier, new Times(1)).get();
Of course, I could create all three inspection templates, but is it possible to create such a template using single search template and what am I missing when configuring the $times$ variable?
(I'm using IntelliJ IDEA Community Edition 2016.1.1)
Try the following search query:
Mockito.verify($mock$, $Qualifier$.times(1))
With $Qualifier$ text/regexp VerificationModeFactory|Mockito and occurrences count 0,1 (to find it when statically imported also).
To also match new Times(1) you can use the following query:
Mockito.verify($mock$, $times$)
With $times$ text/regexp .*times\s*\(\s*1\s*\) and uncheck the Case sensitive checkbox.

cvs2svn include single branch and head?

I'm using cvs2svn to convert my repository. I've done it with success in one repository, and now my new problem is a second repository.
In my new conversion, I want to convert only the HEAD and one branch. cvs2svn only have "exclude" function for branches, but not "include". I have many many branches and excluding each and every one of them will take A LOT of work....
is there any way to convert only the trunk (HEAD) and only one branch?
thanks,
Oded
If you only want to retain the one branch and no tags, then this is easy. The first rule that matches a symbol is used, so specify the branch that you want to be included then exclude everything else:
cvs2svn --force-branch=mybranch --exclude='.*' ...
If you want to include not only the branch but also as many tags as possible, then it is a little bit trickier. Not only don't you necessarily know the names of all of the tags, but you also cannot include tags that are dependent on excluded branches. In this case, it is easiest to work with the --write-symbol-info and --symbol-hints options:
cvs2svn --write-symbol-info=symbol-info.out --passes=1:3 ...
This will create a file called "symbol-info.out" containing information about all CVS symbols. In your editor, open this file, find all of the lines corresponding to branches that you want to exclude, and change the third column of those lines to the word "exclude". Make sure that the third column of the line for the branch that you want to include contains the word "branch" and its fourth column is the path where you want it to end up.
Now run cvs2svn again, starting at pass 3, and using the edited symbol-info file as a symbol hints file:
cvs2svn --symbol-hints=symbol-info.out --passes=3 ...
you will get a lot of errors like:
ERROR: ExcludedSymbol('FOO_BRANCH') cannot be excluded because the following symbols depend on it:
BAR_TAG
BAZ_TAG
Now go back into the editor and change the listed tags (BAR_TAG and BAZ_TAG in the example) to be excluded too, then try running pass3 again. This procedure might need to be iterated a couple of times, but it should not be cumbersome because pass3 runs very quickly.
When you have gotten pass3 to complete without errors, run the rest of the conversion:
cvs2svn --symbol-hints=symbol-info.out --passes=4: ...
One problem is that cvs2svn not only needs to determine whether to include a branch or not, but (simultaneously) whether a symbol is a branch or a tag in the first place. So if you want to include that one branch, and also some tags, it's more difficult than just saying "include only that branch" - doing so would kill all tags.
IOW, cvs2svn doesn't really support that. You can work around by editing its source code. In cvs2svn_lib.symbol_strategy.BranchIfCommits, change the case where it returns Branch(symbol) to
if symbol.name == 'my_branch':
return Branch(symbol)
else:
return ExcludedSymbol(symbol)
IIUC, BranchIfCommits should be used by default.
Personally, I would use a different strategy:
1. convert the repository once, with all branches.
2. do a "svn ls" on branches, and redirect that into a file.
3. edit the file to construct an exclude regex out of it, of the form `b1|b2|...|bn`
I wouldn't call that a LOT of work...