Is there a way to always insert an empty lines between if-else statements and try-catch blocks with clang-format? - clang-format

I would like to convert
if (true) {
...
} else {
...
}
to
if (true) {
...
}
else {
...
}
The same for try-catch blocks. I couldn't find anything on that in the clang-format docs.
https://clang.llvm.org/docs/ClangFormatStyleOptions.html
Is there a way to do this?

Related

Dart streams error with .listen().onError().onDone()

I have an issue with some code that looks like this. In this form I have an error
The expression here has a type of 'void', and therefore can't be used.
Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.dart(use_of_void_result).
If I remove the .onDone() the error goes away. Why? ELI5 please :-)
I was looking at https://api.dart.dev/stable/2.7.0/dart-async/Stream/listen.html but seem to still be misundertanding something.
I also read https://api.dart.dev/stable/2.7.0/dart-async/StreamSubscription/onDone.html
serviceName.UploadThing(uploadRequest).listen((response) {
uploadMessageOutput = response.message;
if (response.uploadResult) {
showSuccess();
} else {
showError();
}
getUploadFileList(event);
isSaveInProgress = false;
}).onError((error) {
isSaveInProgress = false;
_handleFileUploadError(uploadRequest, error);
}).onDone(() {
isSaveInProgress = false;
});
Your code is almost right, but will only require a simple change to work correctly.
You would be seeing the same error if you swapped the ordering of onError and onDone, so the issue has nothing to do with your stream usage. However, you're attempting to chain together calls to onError and then onDone which won't work since both of these methods return void.
What you're looking for is cascade notation (..), which will allow for you to chain calls to the StreamSubscription returned by listen(). This is what your code should look like:
serviceName.UploadThing(uploadRequest).listen((response) {
uploadMessageOutput = response.message;
if (response.uploadResult) {
showSuccess();
} else {
showError();
}
getUploadFileList(event);
isSaveInProgress = false;
})..onError((error) { // Cascade
isSaveInProgress = false;
_handleFileUploadError(uploadRequest, error);
})..onDone(() { // Cascade
isSaveInProgress = false;
});

check if a resource is locked

I usually lock resources in my declarative pipeline with something like:
lock(resource: "MY_RESOURCE") {
// do something
}
but now I have several different resources I could use, is there a way to check if a resource is locked?
I would like to do something like:
myResources = ["RES1", "RES2", "RES3"]
hasResource = false
for (resource in myResources) {
if (hasresource) {
break
}
if (!isLocked(resource)) {
hasresource = true
lock(resource) {
// do something
}
}
}
(sorry if the syntax is wrong, I don't really program in groovy very often)
according to the sources of lock plugin this should work:
import org.jenkins.plugins.lockableresources.LockableResourcesManager as LRM
def myResources = ["RES1", "RES2", "RES3"]
def notLocked = myResources.find{rName->
LRM.get().forName(rName).with{ r-> !r.isLocked() && !r.isQueued() }
}
if(notLocked){
lock(notLocked){
//do smth
}
}
To Check if a particular resource is locked or not in jenkins
def manager = org.jenkins.plugins.lockableresources.LockableResourcesManager
def myResources = manager.get().fromName("test2")
//def myLabels = manager.get().allLabels // if you want to filter based on labels
def checkLocked = myResources.find { r ->
!r.isLocked() && !r.isQueued() }
if (checkLocked) {
def myResource = checkLocked.toString()
println myResource + "is locked"
}
else {
println "Specified Resource is not Locked"
}
Maybe not an optimal solution, but we managed to achieve this with following approach:
waitUntil {
lock(resource: 'RES1', skipIfLocked: true){
// do something with RES1
return true; // exit the waiting loop
}
lock(resource: 'RES2', skipIfLocked: true){
// do something with RES2
return true; // exit the waiting loop
}
lock(resource: 'RES3', skipIfLocked: true){
// do something with RES3
return true; // exit the waiting loop
}
}
We did it this way due to the following error we received when we tried to use the accepted answer:
Scripts not permitted to use staticMethod org.jenkins.plugins.lockableresources.LockableResourcesManager get

How can I exclude two pages from optimization using Autoptimize snippet given below

I hope someone can help me here. How can I use this Autoptimize snippet to exclude two pages from optimization. The code below excludes the page "https//www.nameofsite/submit-an-article"
add_filter(‘autoptimize_filter_noptimize’,’submit_noptimize’,10,0);
function submit_noptimize() {
if (strpos($_SERVER[‘REQUEST_URI’],’submit-an-article’)!==false) {
return true;
} else {
return false;
}
}
I want to exclude also another page-->"https//:www.nameofsite.com/upload/file" I modified and tried to use the code below but it gave me an error.
add_filter(‘autoptimize_filter_noptimize’,’submit_noptimize’,10,0);
function submit_noptimize() {
if (strpos($_SERVER[‘REQUEST_URI’],’submit-an-article’, 'upload-
file')!==false) {
return true;
} else {
return false;
}
}
Perhaps, I did something wrong with code? Hope someone can help me here. I am just a newbie. Not really familiar in php. Thank you in advanced.
Try this:
// Disable autoptimize on all pages with the words "term1" or "term2" in the URL
add_filter('autoptimize_filter_noptimize','my_ao_noptimize',10,0);
function my_ao_noptimize() {
if (strpos($_SERVER['REQUEST_URI'],'term1')!==false || strpos($_SERVER['REQUEST_URI'],'term2')!==false) {
return true;
} else {
return false;
}
}

How to compare Obj C enum in Swift?

I am using FreeStreamer in Swift and am trying to set the onStateChange block.
audioStream.onStateChange = { (state) in
if state == kFsAudioStreamBuffering {
//blah
}
}
I am getting this error:
Binary operator '==' cannot be applied to operands of type '(FSAudioStreamState)' and 'FSAudioStreamState'
Edit: Still the same error without the parentheses around state in the block params
EDIT: As a temporary fix, state.value == kFsAudioStreamBuffering.value works
try putting a dot (.) before kFsAudioStreamBuffering
something like this:
if state == .kFsAudioStreamBuffering {
//blah
}
UPDATE: Try this instead
audioStream.onStateChange = { state in
if state.value == kFsAudioStreamBuffering.value {
//blah
}
}
It should be something like this to make it work.
self.audioControler?.onStateChange = { (state:FSAudioStreamState) -> Void in
switch state {
case .fsAudioStreamRetrievingURL:

else condition is not executing?

in this following else if ,the last else condtion is not executing ?
Please help me
if (Flag==1)
{
...
}
else if ([totalBooksId containsObject:currentbook])
{
...
}
else if (![totalBooksId containsObject:currentbook])
{
...
} else {
...
}
All variable have some values but still else is not executing.
the above else statement is not executing at all? Please help me
This is will work
if(Flag==1){
if([OwnList containsObject:currentbook]){
if(fileExists) {
[self renameReadButton];
}else if(!fileExists){
[self renameDownloadButton];
}
[self renameLendButton];
}
}
else if([totalBooksId containsObject:currentbook]){
//Checking bought books
if([OwnList containsObject:currentbook]){
if(fileExists){
[self renameReadButton];
}else {
[self renameDownloadButton];
}
} else{
[self renameBuyButton];
}
}
else if(![totalBooksId containsObject:currentbook]) {
if([freeList containsObject:currentbook]){
if(fileExists){
[self renameReadButton];
}else{
[self renameDownloadButton];
}
} else{
[self renameBuyButton];
}
}
If the last else on an else-if block is not hit it usually means either of these:
One of the conditions in the initial if clause or the following else-if clauses was true
One of the evaluated conditions altered the program path (exception, stack corruption, etc.)
The code of the entire if-else block was never reached
In your case it seems to be the first case, because one of the else if conditions is the inverse of another.
Either ([totalBooksId containsObject:currentbook]) is true, or !([totalBooksId containsObject:currentbook]) is true. To hit the final else, both of them must be false.
You have three statement
else if([totalBooksId containsObject:currentbook]) // first
{
}
else if(![totalBooksId containsObject:currentbook]) // second
{
}
else { [self renameBuyButton]; } // last
Last statement is not executed because first and second catch all possible situations.
First catch [totalBooksId containsObject:currentbook] == true , second cath [totalBooksId containsObject:currentbook] == false and there is no third possibility.

Resources