break function call arguments into new lines if crosses a boundary length - uncrustify

I am using this uncrustify config. It formats the following code block
void Load(int /*_argc*/, char ** /*_argv*/)
{
this->connections.push_back(
event::Events::ConnectPreRender(
std::bind(&SystemGUI::Update, this)));
}
into this:
void Load(int /*_argc*/, char ** /*_argv*/)
{
this->connections.push_back(event::Events::ConnectPreRender(std::bind(&SystemGUI::Update,
this)));
}
I want the behavior to be like this when it fits into a single line, within 80 columns. Otherwise, if greater than 80 columns, I want the arguments to break over multiple lines. Is that possible within uncrustify?

Related

IntelliJ IDEA auto insert space after choosing statement from suggested variants

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:

Bytebuddy - Arguments for agent premain

I have a premain as below for attaching an agent to a remote process.
public static void premain(String args, Instrumentation instrumentation) {
System.out.println("Premain");
File file ;
try {
file = (new File("Agent.jar"));
ByteBuddyAgent.attach(file,"18467");
}
catch (Exception e)
{
e.printStackTrace();
}
}
Here, I want to pass the process id and couple of other arguments. Is there anyway to do this. Looks like it takes one single argument.
java -javaagent:/path/to/agent.jar -cp jar-under-test.jar Foo.Main
How can i pass the argument here?
You would need to encode the argument in any manner you find appropriate. Instead of spaces as used for the arguments to main, use commas for example but escape all existing commas, for example by doubling them. In the agent, split by single commas and reverse the escaping for the resulting segments.

how to judge there is content in QTextEdit in QT?

I have written a notepad which looks like notepad in Windows. How to set to make the find action disabled when the QTextEdit empty but enabled when something in it
The myTextEdit->plainText().isEmpty() procedure does not seem to be very efficient: The plainText method needs to convert the full QTextEdit contents into a new QString buffer, which is expensive if the QTextEdit contains a large amount of text.
I suggest to use myTextEdit->document()->isEmpty() instead, which queries the QTextDocument storage, i.e. the original data structure.
In my use case, the QTextEdit contains an error log, and before appending a line I check if the text is empty; if not, I insert a newline(*). Converting the log buffer to a QString each time a line is appended would be a bad idea.
(*) I cannot insert a newline together with each log entry, because the entries themselves are comma-separated lists. Roughly speaking I have a newEntry(...) and a newLine(...) function, and newEntry does not know if newLine or newEntry will be called next.
You connect a function that enables/disables the action based on the text edits plainText(), to the textChanged() signal of the text edit.
For example:
void MyWidget::someSetupMethod()
{
// ... some code that sets up myTextEdit and myFindAction here
connect(myTextEdit, &QTextEdit::textChanged, myFindAction, [myTextEdit, myFindAction]() {
myFindAction->setEnabled(!myTextEdit->plainText().isEmpty());
});
// ...
}
or, if you cannot or do not want to use C++11, something like
void MyWidget::someSetupMethod()
{
// ... some code that sets up m_myTextEdit and m_myFindAction here
connect(m_myTextEdit, &QTextEdit::textChanged, this, &MyWidget::updateFindAction);
// ...
}
void MyWidget::updateFindAction()
{
m_myFindAction->setEnabled(!m_myTextEdit->plainText().isEmpty());
}

Iterate over dlang struct

I have a struct that looks something like this:
struct MultipartMessage {
ubyte[] mime, data;
Header header;
void setSender(string sender) {
header.sender = sender;
}
void setId(int id) {
header.id = id;
}
}
and I would like to iterate over it, in another class with something like this:
struct Socket {
...
void send(MultipartMessage msg) {
foreach (part; msg) {
sendPart(part);
}
}
...
}
Is this possible? I'd like to use something analogous to Python's __iter__ in the MultipartMessage that can return the fields in a specific order, and ideally even run some additional code, like header.serialize().
Ideally I would add a function to MultipartMessage that would look something like this (pseudocode):
ubyte[] __iter__() {
yield mime;
yield data;
yield header.serialize(); //header.serialize returns a ubyte[]
}
Use tupleof:
foreach (ref part; msg.tupleof)
sendPart(part);
This will call sendPart with mime, data and header (the struct's fields, in the order they were declared). You can filter fields by checking their type with e.g. static if (!is(typeof(part) == Header)).
To get the field's name, you can use __traits(identifier):
foreach (i, ref part; msg.tupleof)
writeln(__traits(identifier, msg.tupleof[i]));
(__traits(identifier, part) would return part.)
There's also __traits(allMembers), which also returns methods.
The closest thing to what you want is probably opApply.
See http://dlang.org/spec/statement.html , Section Foreach over Structs and Classes wit opApply
This will work:
int opApply(int delegate(ref ubyte[]) dg) {
int result = 0;
result = dg(mime);
result = dg(data);
ubyte[] header_bytes = header.serialize();
result = dg(header_bytes);
return result;
}
There are several ways to do iterate over objects in D.
One is to implement the InputRange API. Input ranges are similar to iterators, but have a different API. Implementing a range interface means that you can use all of the std.range/std.algorithm functions on your object, such as map, array, joiner and so on.
D doesn't have an __iter__ function to get an iterator from arbitrary collections, so you will need to implement a function that returns an input range.
import std.range;
auto bytes() {
return chain(mime, data, header.serialize);
}
This will return a ubyte input range, consisting of the bytes in mime, followed by the bytes in data, then in header.serialize.
You can also implement the opApply method on your struct. opApply will only work with foreach, so you can't use range methods with it, but it allows you to do things like execute the loop body in separate threads.
The gist of opApply is that D passes the loop body to opApply as a function; that is, foreach(x; myObj) { body } is transformed into myObj.opApply((x) { body }).
void opApply(void delegate(ubyte[] part) loopbody) {
loopbody(mime);
loopbody(data);
loopbody(header.serialize());
}
However, instead of either of those options, I recommend that you implement a function on your object that takes an output range and writes the data to it.
An output range is an object that accepts other objects, and does something to them. In this case, the output range should accept ubytes, making it similar to an output stream.
void serialize(Range)(ref Range outRange) if(isOutputRange!(Range, ubyte)) {
put(outRange, mime); -- `put` simply feeds data into the output range
put(outRange, data);
header.serialize(outRange); // No longer have to allocate/return a ubyte array
}
Example usage, that stores the output into an Appender, which can be converted into an array:
import std.array;
auto serializedDataAppender = appender!ubyte();
myMsg.serialize(serializedDataAppender);
auto serializedData = serializedDataAppender.data;
If you implement an output range on top of your socket, then that means that the output range solution does not have to allocate any memory from the heap.
Check out the Programming in D book (specifically, the Ranges and More Ranges sections) for info on how to implement your own ranges.

A program that will let the user input a string and then output each letter on a new line

import java.util.Scanner;
public class Program3_5
{
public static void main (String[]args)
{
Scanner scan = new Scanner(System.in);
String input = new String();
System.out.println("Please enter a string: ");
input=scan.next();
int length;
length = input.length;
input.substring();
System.out.println(charAt(0));
while (length)
{
System.out.println(charAt(0 + 1));
}
}
}
I am getting an error stating that it "cannot find symbol - variable length"
I have tried numerous things yet I am having trouble getting it to work. New to Java! Thanks in advance.
For example if the user were to input: Hello There
The Output would print the letters on separate lines.
String#length() is a method, not a field, of String. You need to call the method. In Java, methods are called (or "invoked") using parentheses. So, change
length = input.length;
// to
length = input.length();
Anticipating the next compile error you see:
while (length)
won't compile in Java because length is an int, but the condition part of a while must be a boolean. I'm guessing you want to continue as long as the string is not empty, so change the while condition to be
while (length > 0)
Other problems you'll need to solve to get your code to compile:
String#substring() requires integer arguments
Also, the code will compile with the String input = new String(); but the assignment is completely unnecessary. In Java, you almost never need to new a string. Instead, use string literals.