IntelliJ IDEA auto insert space after choosing statement from suggested variants - intellij-idea

I noticed that every time I made a choice in code completion suggested list I have to manually add a space after every statement, it's annoying. Want to automate this and try to find a solution in settings but nothing.
]

It works just fine here and behaviour depends on where return is used (tested in PhpStorm 2020.1.4 on Windows 10).
1. The space will inserted automatically after return completion in functions where IDE knowns that they can return non empty values, e.g.
where return type is declared via PHPDoc or native PHP type;
based on other/already present non-empty returns in function body.
2. If a function already declare that it will return void then return; will be inserted on completion (notice ; at the end).
3. If no function return type is known in advance (e.g. you are writing first return and no return type declared) or other cases (e.g. return in include file -- a typical case is PHP config files (e.g. Laravel)) then no space will be inserted.
refs: WI-51343, WI-45793, WI-45803.
Here are examples when IDE will insert a space after return keyword completion via completion popup:
A short demo video for one of the cases: https://recordit.co/U90Sx6PPTO
function some1($a)
{
if ($a) {
return 22;
}
ret[COMPLETION]
}
function some2($a): string
{
ret[COMPLETION]
}
/**
* Some func.
*
* #param $a
* #return string
*/
function some3($a)
{
ret[COMPLETION]
}
Here IDE will insert return; (notice ; at the end):
/**
* #param $a
* #return void
*/
function some4($a)
{
ret[COMPLETION]
}
Possible workaround if you ALWAYS want to have a space after return statement: just make custom Live Template that will expand r[TAB] into return [CARET].
For example:

Related

Create empty IFutureEnumerable instance

I have a method which performs an NHibernate query, and returns the values as an IEnumerable<long>. It is running a future query so the result is actually of type IFutureEnumerable<long>.
public static IEnumerable<long> GetQueryResults(IEnumerable<long> idsToFilterOn)
{
if((idsToFilterOn == null) || !(idsToFilterOn.Any()))
{
return Enumerable.Empty<long>();
}
else
{
IQueryOver<MyTable> query = GenerateTheBigQuery(idsToFilterOn);
return query.Future<long>();
}
}
I want this result to return IFutureEnumerable<long>, but I still want to first check the parameters, and if I know the result will be empty I want to just return an empty value without running a query.
If I just change the return type to IFutureEnumerable<long>, the line of code that returns Enumerable.Empty<long>() generates a compiler error (Cannot implicitly convert type...)
Is there some static method like FutureEnumerable.Empty<long>() which generates an IFutureEnumerable that returns an empty list?
Looking at the code, there doesn't appear to be any native support for that concept. IFutureEnumerable is implemented by two types, one of which is deprecated and neither offer the notion of emptiness.
I suppose that leaves it up to you to create a type that implements IFutureEnumerable<T> that supports emptiness.

How to repeat Mono while not empty

I have a method which returns like this!
Mono<Integer> getNumberFromSomewhere();
I need to keep calling this until it has no more items to emit. That is I need to make this as Flux<Integer>.
One option is to add repeat. the point is - I want to stop when the above method emits the first empty signal.
Is there any way to do this? I am looking for a clean way.
A built-in operator that does that (although it is intended for "deeper" nesting) is expand.
expand naturally stops expansion when the returned Publisher completes empty.
You could apply it to your use-case like this:
//this changes each time one subscribes to it
Mono<Integer> monoWithUnderlyingState;
Flux<Integer> repeated = monoWithUnderlyingState
.expand(i -> monoWithUnderlyingState);
I'm not aware of a built-in operator which would do the job straightaway. However, it can be done using a wrapper class and a mix of operators:
Flux<Integer> repeatUntilEmpty() {
return getNumberFromSomewhere()
.map(ResultWrapper::new)
.defaultIfEmpty(ResultWrapper.EMPTY)
.repeat()
.takeWhile(ResultWrapper::isNotEmpty)
}
// helper class, not necessarily needs to be Java record
record ResultWrapper(Integer value) {
public static final ResultWrapper EMPTY = new ResultWrapper(null);
public boolean isNotEmpty() {
return value != null;
}
}

Intellisense JSDoc for Mixins and/or Factory Functions

My Problem
I'm currently using Visual Code Studio, the lastest version. I'm trying to get the intellisense to show up for the methods on the instance created by a factory function. The methods are going to be applied via object composition (so added directly as properties on the object).
so the function that acts as a constructore basically returns:
function makeWrappedObj() {
var obj = { /* random data */ }; // Then add methods to obj
Object.keys(methods).forEach(key => a[key] = methods[key]; );
return obj;
}
var methods = {
/**
* Yay documentation
* #returns {Object}
*/
method1: function() { return null; }
};
var instance = makeWrappedObj();
instance.method1( // What documentation to show up here
instance. // and here
Is the basic idea. Here's someone doing something similar. I'll address that in the third possible solution.
Solutions I've tried
1) #class on makeWrappedObj I think only works if you attach methods on to makeWrappedObj.prototype which is not what I'm doing so that doesn't work. Unless I'm misunderstanding something.
2) #namespace solution
/* #namespace ClassName */
/* #returns {ClassName}
function createWrappedObj() {
var obj = { /* random data */ }; // Then add methods to obj
Object.keys(methods).forEach(key => a[key] = methods[key]; );
return obj;
}
var methods = {
/**
* Currently the soultion I'm using
* #memberof ClassName
* #param {number} a
**/
method1: function (a) {}
};
var instance = makeWrapperObj();
instance.method1( // Only shows documentation here
So this sort of works. The two problems are that:
methods on instances only get documentation when you type out the full instance.method( and not in the instance. case--not really a big deal
if you have a method name that is the same as an in-built function, eg. toString, instance.toString( will block any documentation you have from showing and show the native toString default documentation instaed.
3) #typedef like the link above.
/** Can be placed anywhere
* #typedef {ClassName}
* #property {function} method1
* dunno how to document arguments with this method
* but it gets the intellisense menu to pop up for "instance." after dot
*/
/* #returns {ClassName} */
function createdWrappedObject() {
var obj = { /* random data */ }; // Then add methods to obj
Object.keys(methods).forEach(key => a[key] = methods[key]; );
return obj;
}
var methods = {
method1: function (a) {}
};
var instance = makeWrappedObj();
instance. // Yay
instance.method1( // Doesn't work
Has the benefit of showing up when one types instance. however has a few disadvantages
Do not know how to specify arguments passed to method1. Maybe it's not possible.
#typedef requires all documentations to be specified in its own comment block. So I have to be redundant between specific documentation on the methods and the typedef
loses the docummentation once you write out the entire method up until the open paren. Sort of workable...
Note: I am willing to just assign all the methods manually one by one instead of doing a foreach on all the keys of the methods mixin.
Also I do not know how to specify the parameters for a function passed as a. This should be possible since Array.map( specifies three arguments for its function call.
The goal is to get documentation when someone imports this as a library with require, or just includes it as a library in browser side script.

Bypassing functions that do not exist

how would it be possible to bypass functions that are not existing in DM
such that the main code would still run? Try/catch does not seem to work, e..g
image doSomething(number a,number b)
{
try
{
whateverfunction(a,b)
}
catch
{
continue
}
}
number a,b
doSomething(a,b)
Also conditioning wont work, e.g..
image doSomething(number a,number b)
{
if(doesfunctionexist("whateverfunction"))
{
whateverfunction(a,b)
}
}
number a,b
doSomething(a,b)
thanks in advance!
As "unknown" commands are caught by the script-interpreter, there is no easy way to do this. However, you can construct a workaround by using ExecuteScriptCommand().
There is an example tutorial to be found in this e-book, but in short, you want to do something like the following:
String scriptCallStr = "beep();\n"
scriptCallStr = "MyUnsaveFunctionCall();\n"
number exitVal
Try { exitVal = ExecuteScriptString(scriptCallStr ); }
Catch { exitVal = -1; break; }
if ( -1 == exitVal )
{
OKDialog("Sorry, couldn't do:\n" + scriptCallStr )
}
else
{
OKDialog( "All worked. Exit value: " + exitVal )
}
This works nicely and easy for simple commands and if your task is only to "verify" that a script could run.
It becomes clumsy, when you need to pass around parameters. But even then there are ways to do so. (The 'outer' script could create an object and pass the object-ID per string. Similarly, the 'inner' script can do the same and return the script-object ID as exit-value.)
Note: You can of course also put doesfunctionexist inside the test-script, if you do only want to have a "safe test", but don't actually want to execute the command.
Depending on what you need there might also be another workaround solution: Wrapper-functions in a library. This can be useful if you want to run the same script on different PCs with some of which having the functionality - most likely some microscope - while others don't.
You can make your main-script use wrapper methods and then you install different versions of the wrapper method script scripts as libraries.
void My_SpecialFunction( )
{
SpecialFunction() // use this line on PCs which have the SpecialFunction()
DoNothing() // use alternative line on PCs which don't have the SpecialFunction()
}
My_SpecialFunction( )
I have used this in the past where the same functionality (-stage movement-) required different commands on different machines.

Check for project errors before performing action

I have an eclipse plug-in which provides a menu item which can be selected to run a command on the currently active file. I would like the plug-in to display a warning message if the currently active file has any errors on it (as reported in the Problems view), similar to how Eclipse acts when you try to run a java project with errors.
I know it's an old question, but I found a solution similar to the one proposed. The code that does what you descrive is in org.eclipse.debug.core.model.LaunchConfigurationDelegate. It checks if the project has errors and show the dialog if needed. Here is the relevant code, from Eclipse Luna:
/**
* Returns whether the given project contains any problem markers of the
* specified severity.
*
* #param proj the project to search
* #return whether the given project contains any problems that should
* stop it from launching
* #throws CoreException if an error occurs while searching for
* problem markers
*/
protected boolean existsProblems(IProject proj) throws CoreException {
IMarker[] markers = proj.findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
if (markers.length > 0) {
for (int i = 0; i < markers.length; i++) {
if (isLaunchProblem(markers[i])) {
return true;
}
}
}
return false;
}
/**
* Returns whether the given problem should potentially abort the launch.
* By default if the problem has an error severity, the problem is considered
* a potential launch problem. Subclasses may override to specialize error
* detection.
*
* #param problemMarker candidate problem
* #return whether the given problem should potentially abort the launch
* #throws CoreException if any exceptions occur while accessing marker attributes
*/
protected boolean isLaunchProblem(IMarker problemMarker) throws CoreException {
Integer severity = (Integer)problemMarker.getAttribute(IMarker.SEVERITY);
if (severity != null) {
return severity.intValue() >= IMarker.SEVERITY_ERROR;
}
return false;
}
The same code can run on any IResource instead of an IProject.
I managed to find it easily by suspending from the debugger when the dialog was shown and setting a breakpoint on the relevant class and tracing back from there.
Errors are usually saved as IMarkers on a resource (IFile in your case), so you can query the IFile for the markers you are looking for.
You'll need to know the type of the markers before the look-up (either by debugging and get all the current markers, or by looking at the code the contributed them in the validation process of the file).
Hope that helps.