Uncrustify: Trim linebreaks before braces - objective-c

What is the right config for uncrustify so that it removes empty lines before curly braces so that:
}
}
else
{
// foo
}
[bar tend];
}
becomes:
}
}
else
{
// foo
}
[bar tend];
}
I scourged the docs but couldn't find the right setting, maybe it has an unusual name.

Found it. The following will take care of spurious blank lines.
eat_blanks_after_open_brace = TRUE
eat_blanks_before_close_brace = TRUE
nl_max = 2

Related

Better way to replace nested if else in Kotlin

Is there any better way to replace below if else to more cleaner in Kotlin. I tried to replace with when statement but i couldn't match the logic.
if (!reached)
{
if (!info)
{
d.sinfo = extractinfo()
}
else
{
parserMessage("print something")
return d
}
info = true
}
else
{
if (d.media.isEmpty()){
parserMessage("print something")
return d
}
else{
if (d.media.at(d.media.size() - 1).media_information.isEmpty())
{d.media[d.media.size() - 1].minfo = extractinfo()}
else{
parserMessage("print something")
return d
}
}
}
Unless the code you have left out have some weird side effects, this code should be semantically equal:
when {
!reached && !info -> {
d.sinfo = extractinfo()
info = true
}
!reached && info -> {
parserMessage("print something")
return d
}
d.media.isEmpty() -> {
parserMessage("print something")
return d
}
d.media.at(d.media.size() - 1).media_information.isEmpty() -> {
d.media[d.media.size() - 1].minfo = extractinfo()
}
else -> {
parserMessage("print something")
return d
}
}
However, to say this, I had to fill in the gaps in the code you have presented myself, so I can't state this very confidently. It really helps your chances of getting a good answer if the code you want help with is runnable/understandable as presented.
By the way. This refactoring was partly done by pasting the code into IntelliJ and hitting Alt+Enter and choosing "Replace 'if' with 'when'" and "Flatten when"

Conditional mixin based on parameter existence

Any suggestion how to create a conditional mixin based on parameter existence?
For example I need to to verify that all the parameters are passed in order to perform something or not, for example:
.margin (#margintop:0,#marginbottom:0,#marginright:0,#marginleft:0) {
// if #marginright:0 or #marginleft:0 are passed do that...
// else...
}
1:
In general, when you need to generate different things for different number of arguments passed you don't need to use default argument values at all, e.g.:
.margin(#top, #bottom, #right, #left) {
/* right and left are passed */
}
.margin(#top, #bottom) {
/* right and left are not passed */
}
.margin() {
/* no arguments passed */
}
// etc.
Note that each of these mixins can reuse the others, for example .margin(#top, #bottom) can do something special for the "no right and left case" and then call .margin(#top, #bottom, 0, 0) to perform the main job.
2:
But if you still need these defaults for some reason you can use some special default value that can't be a valid margin, e.g. something like this:
.margin(#top: undefined, #bottom: undefined, #right: undefined, #left: undefined) {
.test-args();
.test-args() when (#right = undefined) {
/* right is not passed */
}
.test-args() when (#left = undefined) {
/* left is not passed */
}
.test-args()
when not(#right = undefined)
and not(#left = undefined) {
/* right and left are passed */
}
// etc.
}
3:
And the third option would be to use variadic arguments and test their count, but this one is the most verbose and dumb I guess:
.margin(#args...) {
.eval-args(length(#args)); // requires LESS 1.5.+
.eval-args(#nargs) {
// default values:
#top: not passed;
#bottom: not passed;
#right: not passed;
#left: not passed;
}
.eval-args(#nargs) when (#nargs > 0) {
#top: extract(#args, 1);
}
.eval-args(#nargs) when (#nargs > 1) {
#bottom: extract(#args, 2);
}
.eval-args(#nargs) when (#nargs > 2) {
#right: extract(#args, 3);
}
.eval-args(#nargs) when (#nargs > 3) {
#left: extract(#args, 4);
}
args: #top, #bottom, #right, #left;
}
Though it may probably have its pros in some special use-cases.

isEqualToString always returns true

for (Annotation *ann in annotaionArray) // annotationArray contains annotations added to map
{
NSString *fetchedtitle = ann.title;
if([fetchedtitle isEqualToString:oldTitle]); // oldTitle = textfield.text
{
ann.title = appDelegate.pinTitle;
break;
}
}
But the comparison is always true. What could be the error please?
fetched const char from sqlite is casted to stringWithUTF8String.
Everything has been done to cast perfectly to string but still why is the error in comparison?
Remove the trailing semi-colon!
if ([fetchedtitle isEqualToString:oldTitle])
{
ann.title = appDelegate.pinTitle;
break;
}
With the semi-colon, your code is the same as:
if ([fetchedtitle isEqualToString:oldTitle])
;
{
ann.title = appDelegate.pinTitle;
break;
}
Since you have a semi colon at the end of your IF statement your code is :
if( [fetchedtitle isEqualToString:oldTitle])
{
// Do nothing
}
// this will always run
ann.title = appDelegate.pinTitle;
break;
replace the following line
if([fetchedtitle isEqualToString:oldTitle]);
with
if([fetchedtitle isEqualToString:oldTitle])
Just remove semicolon(;) from the following part of your code...
if([fetchedtitle isEqualToString:oldTitle]);

How to switch cases with ( - ) and ( + ) keys presses?

I am working with AutoHotKey. I know I have tagged C also, I think someone with enough C programming knowledge can also help here.
Code below is working for me.
It will read two keyboard input from user and based on what user pressed it will run code for that case.
1::
Input Key, L1
if Key=1
{
;your code
}
if Key=2
{
;your code
}
2::
Input Key, L1
if Key=1
{
;your code
}
if Key=2
{
;your code
}
I would like to know if I can add a loop or something if user presses + or - key it will go do one case at a time,
for example if user presses + for first time it will do
1 1 if user presses + again it will do
1 2 if user presses - it will do
1 1
and so on.
I am not sure if this is do able or not.
I am new to programming. please help :)
You can use global variables. A global variable can be accessed anywhere in the program, unlike a normal variable which exists only inside the function.
Example:
#NoEnv
#Persistent
SetBatchLines, -1
global myVar = 0
h::
myVar := myVar + 1
execute()
return
g::
myVar := myVar - 1
execute()
return
execute()
{
if(myVar == 1)
{
;do stuff
tooltip, myVar: %myVar%
}
else if (myVar == 2)
{
;do stuff
tooltip, myVar: %myVar%
}
else if (myVar == 3)
{
;do stuff
tooltip, myVar: %myVar%
}
else if (myVar == 4)
{
;do stuff
tooltip, myVar: %myVar%
}
else if (myVar == 5)
{
;do stuff
tooltip, myVar: %myVar%
}
else
{
; nothing
tooltip,
}
return
}
I hope this is what you were asking, i wasn't quite sure from the question.
; Some of this is what's called Pseudo code. (not sure if you're familiar). It gives you needs to be turned into actual code...
; Written for AHK...
CurrentNumber = 1
(plus key)::
CurrentNumber += 1
send %CurrentNumber%
return
(minus key)::
CurrentNumber -= 1
send %CurrentNumber%
return
; Not sure if this is what you were looking for or not.. if you want a loop it will be different.
; either way, good luck to you, i'm out..
+::
keywait, +, u
{
If var =
var = 11
Else
var++
}
Return
-::
keywait, -, u
{
If var =
var = 11
Else
var--
}
Return
"var" should have same name with the variable, which has two or one digit number, in your code.
You may use this too
NumpadAdd::
keywait, NumpadAdd, u
{
If var =
var = 11
Else
var++
}
Return
NumpadSub::
keywait, NumpadSub, u
{
If var =
var = 11
Else
var--
}
Return

Comment Line Dissapears After Rewriting a Node

I was writing simple refactoring and noticed a strange thing. The comment line before the node I am rewriting disappears after refactoring. Also comments after the node in question are transferred inside the node and break the indentation in the new place. This is very strange and I want to ask if it is a bug in jdt or I did something wrong and oblivious.
For example my code suppose to refactor if-else statements in a way that the shortest branch would appear first.
when I try to refactor this:
// pre
if(a==6) {
a = 5;
return false;
} else {
a++;
}
//post
I get this:
if (!(a==6)) {
a++;
}
//post
else {
a = 5;
return false;
}
The relevant snippet where the refactoring is done:
protected ASTRewrite createRewrite(CompilationUnit cu, SubProgressMonitor pm) {
pm.beginTask("Creating rewrite operation...", 1);
final AST ast = cu.getAST();
final ASTRewrite rewrite = ASTRewrite.create(ast);
cu.accept(new ASTVisitor() {
public boolean visit(IfStatement node) {
if (node.getStartPosition() > selection.getOffset() + selection.getLength() || node.getStartPosition() < selection.getOffset())
return true;
if (node.getElseStatement() == null)
return true;
int thenCount = countNodes(node.getThenStatement());
int elseCount = countNodes(node.getElseStatement());
if(thenCount <= elseCount)
return true;
IfStatement newnode = ast.newIfStatement();
PrefixExpression neg = negateExpression(ast, rewrite, node.getExpression());
newnode.setExpression(neg);
newnode.setThenStatement((org.eclipse.jdt.core.dom.Statement) rewrite.createMoveTarget(node.getElseStatement()));
newnode.setElseStatement((org.eclipse.jdt.core.dom.Statement) rewrite.createMoveTarget(node.getThenStatement()));
rewrite.replace(node, newnode, null);
return true;
}
});
pm.done();
return rewrite;
}
The // pre comment goes away because the parser considers it to be part of the next statement (represented by node), which you replace with newNode. When node goes away, so does the attached comment.
still thinking about why the // post ends up where it does... Try replacing the newNode before setting its then and else statements