Resharper naming convention rule for void methods - naming-conventions

Does anyone know how (or if it's possible) to create a Resharper naming convention rule that doesn't allow void methods to start with "Get"?
So this would be fine:
public string GetFoo();
But the rule would complain about this:
public void GetFoo();

You can't really add this as a naming convention, but you could create a search pattern that will look for void methods starting with Get, and flag them with a Warning or Error.
For this, go to ReSharper's Options, then Code Inspection → Custom Patterns:
Click Add Pattern
In the new dialog, select Find
Type in the following pattern: void $method$($args$){ $stmt$ }, and press Add Placeholder → Extract from pattern. This should create 3 placeholders in the list.
Double click the method placehoder, and add the following RegEx: ^Get.*
In the Pattern Severity combobox select Show as Warning or Show as Error, depending on your preference.
In the description, write something like Void methods should not begin with Get.
Click Add, then Save (or Save To → Team Shared, to have this pattern stored in the team-shared settings, available to all your teammates).
ReSharper will now flag all void methods that start with Get:

Related

AST and Language Service Plugin

I would like to create a vscode extension which can help with unit testing and "live" debugging.
Lets say you have the following code:
/*1*/ export class Test {
/*2*/ public add(a: any, b: any): any {
/*3*/ return a + b;
/*4*/ }
/*5*/ }
Live debugging:
New option in the context menu to test the add function (e.g. something similar to current Peek Definition) where you can provide the function input and check the output in a new smaller editor.
Unit test generation:
After you wrote an input and checked the output you can simply generate a new unit test from it.
Now I'm only at the beginning of this project but it seems like getting type information is much more complicated than I expected.
First I would like to get some simple type information (based on cursor position) e.g.:
Line 1 is a class
Line 2-4 is a function
Line 5 is a class
After hours of Google search I think what I have to do is to implement my own TypeScript Language Service Plugin and use it somehow in my extension.
My questions:
Is there any extension similar to my idea which I could use?
Is there an easier way to get the type information I need?
Am I on a right track at all?
Do you have some material about how to use this language service plugin from extension?
P.S. I know that it won't be easy (e.g. several states with different visibility), but good practice for a home project.

How to replace all `public void` "Test"-methods with just "void" (with SSR and IntelliJ)

I've recently joined a codebase that no one seemed to ever run SonarLint against, and now every test class I open in IntelliJ is highlighted like a Christmas tree - all test methods are public, even though JUnit5 is used.
Tired of removing those public modifiers manually, I decided to implement an SSR template to do that for me. However, I can't seem to make it work with method parameter annotations! (which seem to be rather usual thing with JMockit)
The best I can have thus far is this:
Open Edit->Find->Replace structurally
Paste this into Search field:
#$MethodAnnotation$
public void $MethodName$(#mockit.Injectable $ParameterType$ $Parameter$) {
$statement$;
}
Choose file type: Java - Class Member
Add filter for MethodAnnotation variable: Text=org.junit.jupiter.api.Test
Add Min=0 for statement and Parameter variables
Paste this into Replace field:
#$MethodAnnotation$
void $MethodName$(#Injectable $ParameterType$ $Parameter$) {
$statement$;
}
(mind the indentation, otherwise it will be problematic!)
Select Complete match value for Search target
Find
As you can see, the annotation is hard-coded, which of course limits the applicability of this snippet seriously.
Is this a problem with IntelliJ or rather my bad knowledge of SSR? What would be a proposed solution?

stop code formatting through modifying AST in Eclipse CDT

I am editing a cod pragmatically through editing the AST in Eclipse CDT, put after perform the refactor using Change#perform method, I found the code to be formatted, example here I am just cast a function call to be void
//Old code
Publish(Author_id, Content);
//New code
(void) Publish(
Author_id,
Content);
As you see the method has been formatted to be in 3 lines, how to stop this action?
My code sample that do refactoring
INodeFactory factory = ast.getASTNodeFactory();
IASTNode newNode = rewriter.createLiteralNode("( void )"+selectedNode.getRawSignature());
rewriter.replace(selectedNode, newNode, null);
Change change = rewriter.rewriteAST();
change = change.perform(new NullProgressMonitor());
I believe you have two options:
Configure CDT's built-in formatter to format the code the way you like it. In this case, based on the appearance of the "New code", it looks like Line Wrapping -> Function calls -> Arguments -> Line wrapping policy is set to one of the "Wrap all elements" options. You can change it to something you like better.
Implement the org.eclipse.cdt.core.CodeFormatter extension point to provide an alternative formatter that behaves differently from the default code formatter (for example, it could do nothing).
UPDATE: I don't know where to find an example of this, but assuming you're familiar with implementing Eclipse extension points in general, here is the reference for this one. The steps would be:
Write a class that extends org.eclipse.cdt.core.formatter.CodeFormatter and implements the format() method
Ad an <extension ...> tag to your plugin's plugin.xml referencing your class

How to search for constructor / method invocation of given signature in IntelliJ?

Is it possible to search for constructor / method invocation of given signature in IntelliJ?
For example, suppose I have two methods
void myMethod(int x, int y);
void myMethod(double x, double y);
How to search second one invocation?
Simplest way is the following:
Just invoke Find Usages (Alt+F7) on the second method. If the search results contain both methods, click on the cog and spanner Settings icon in the Find tool window. Deselect the Include overloaded methods checkbox and click on the Find button.
This settings dialog can also be reached directly via the menu Edit | Search | Find Usages Settings...
For reference I'm giving another option - structural search (Edit > Find > Search > Search Structurally).
The template you need is something like this:
$Instance$.$MethodCall$($Parameter$)
In "Edit Variables" dialog you need to adjust "MethodCall" to be "myMethod", "Parameter" count to be 2 and its "Expression type" to be "double"

How to generate a void method in IntelliJ IDEA?

In Eclipse, when I type main ctr+space, it will generate a static void main method for me. And when I type methodName ctr+space, smart code completion will suggest generating the method named methodName.
How can I auto-generate a void method in IntelliJ?
To create a new method from usage in the code like:
...
someMethodName()
...
AltEnter on the red code:
It's also possible to type void methodName() and use Complete Statement (CtrlShiftEnter), it will become:
void methodName() {
|
}
You could create your own Live Template as #Makoto answered, but programming by intention seems to be more natural. When you don't have a method, you write code that will use it, then create the method from the intention action - this way IDEA will generate the method signature automatically according to the parameters and return type in the not yet existing method usage, like String result = someMethod(stringParam);.
Finally, it is worth nothing that in IntelliJ IDEA main() method can be generated using psvmTab.
IntelliJ IDEA 15
Generate a main method
Default:
Type psvm (public static void main) > press Tab
Use the template from Eclipse (main instead of psvm)
File > Settings or press Ctrl + Alt + S
Editor > Live Templates
From the right side, click on the "+" sign > Live Template
Add the following details:
Abbreviation: main
Description: main() method declaration
Template text:
public static void main(String[] args){
$END$
}
You will see the new template added in Others.
Click on Define
Select Java > Press on OK
Type main in your Java code > press Tab
Generate a void method
Type your method name followed by parentheses (+ the arguments, if you use them) - E.g.: m() or m(1,2) > Press Alt + Enter > Click on "Create method ..." (or press Enter if it is already selected)
Type the abbreviation of the main() method template:
Press the template invocation key. By default, it is Tab. The abbreviation expands to the main() method.
IntelliJ makes use of Live Templates to do its code completion. It's then a matter of deciding what shorthand name you wish to use to name your void method.
Here's an example. Create a live template in Settings > Live Templates, then select the "Other" box. Hit the + on the right, then give your template a shorthand keystroke name.
Here's the one that I typed up. With the two different variables $NAME$ and $ARGS$, I can tab between them when I need to fill them in. $END$ is where the cursor ends when I'm done tabbing through the other two variables.
void $NAME$ ($ARGS$) {
$END$
}
The shorthand name I used is pmeth. So, every time I type pmeth into IntelliJ in a Java file, then hit Tab, this method is filled in, and my cursor automatically starts at $NAME$.
type psvm on the Java class and then Cntrl+Period key
Just type main and a suggestion will pop up. Press enter.
Simply Type the abbreviation of the main() method template:
psvm then Enter