intellij wrap and sign - intellij-idea

What's the setting I need to change in my IntelliJ (Android Studio) IDE to make formatting change this line:
return Objects.equal(this.city, other.city) && Objects.equal(this.state, other.state) && Objects.equal(this.country, other.country) && Objects.equal(this.location, other.location) && Objects.equal(this.streetAddress1, other.streetAddress1) && Objects.equal(this.streetAddress2, other.streetAddress2) && Objects.equal(this.postalCode, other.postalCode);
to look like this:
return Objects.equal(this.city, other.city) &&
Objects.equal(this.state, other.state) &&
Objects.equal(this.country, other.country) &&
Objects.equal(this.location, other.location) &&
Objects.equal(this.streetAddress1, other.streetAddress1) &&
Objects.equal(this.streetAddress2, other.streetAddress2) &&
Objects.equal(this.postalCode, other.postalCode);

It's possible:
Go to Settings -> Code Style -> Java -> Wrapping and Braces [tab] ->
Binary Expressions [tree node]
Set Binary Expressions to Chop down if long
Check the Align when multiline
I have also had success on teams using mixed IDE's with JIndent. Although commercial its more powerful and is IDE and build agnostic.

Related

Rust Range.contains failed to be inlined/optimized

I was running my code through Clippy and it suggested changing the following:
const SPECIAL_VALUE: u8 = 0; // May change eventually.
pub fn version1(value: u8) -> bool {
(value >= 1 && value <= 9) || value == SPECIAL_VALUE
}
Into
pub fn version2(value: u8) -> bool {
(1..=9).contains(&value) || value == SPECIAL_VALUE
}
Since it is more readable. Unfortunately the resulting assembly output is twice as long, even with optimization level 3. Manually inlining it (2-nestings down), gives almost the same code as version1 and is as efficient.
pub fn manually_inlined(value: u8) -> bool {
(1 <= value && value <= 9) || value == SPECIAL_VALUE
}
If I remove the || value == SPECIAL_VALUE they all resolve with the same (though with 1 more instruction added to decrement the parameter value before a compare). Also if I change SPECIAL_VALUE to something not adjacent to the range they all resolve to same assembly code as version2, which is the reason why I kept it 0 unless I eventually have to change it.
I have a link to Godbolt with the code here: https://rust.godbolt.org/z/bMYzfcYob
Why is the compiler failing to properly inline/optimize version2? Is it an "optimization bug"? Or am I misunderstanding some semantics of Rust, maybe something with the borrowing prevents the optimization, but can't the compiler assume no mutation of value due to the aliasing and referencing rules?
Trying to do the same in C++ suggest, yields the worse option in both cases (https://godbolt.org/z/zahfz65W3)
Edit: Changing the compiler for my C++ version to GCC makes it optimized in both cases.
This was indeed a missed optimization opportunity that has now been corrected in LLVM. https://github.com/rust-lang/rust/issues/90609#issuecomment-1046037263 .

Detect if a Tcl script is run in a background process

I'm looking for a preferably cross-platform way to detect from within a Tcl script if the interpreter is running in a foreground or in a background process.
I've seen how to do it via ps (or /proc/$$/stat on Linux); is there a better way or do I have to hack something around that approach? I already have a utility library written in C so exposing the lowlevel API that ps also uses so I don't have to parse process output (or special file content) would be fine.
There's no truly cross-platform notion of foreground, but the main platforms do have ways of doing it according to the notion they have of foreground.
Linux, macOS, and other Unix:
For determining if a process is foreground or not, you need to check if its process group ID is the terminal's controlling process group ID. For Tcl, you'd be looking to surface the getpgrp() and tcgetpgrp() system calls (both POSIX). Tcl has no built-in exposure of either, so you're talking either a compiled extension (may I recommend Critcl for this?) or calling an external program like ps. Fortunately, if you use the latter (a reasonable option if this is just an occasional operation) you can typically condition the output so that you get just the information you want and need to do next to no parsing.
# Tested on macOS, but may work on other platforms
proc isForeground {{pid 0}} {
try {
lassign [exec ps -p [expr {$pid ? $pid : [pid]}] -o "pgid=,tpgid="] pgid tpgid
} on error {} {
return -code error "no such process"
}
# If tpgid is zero, the process is a daemon of some kind
expr {$pgid == $tpgid && $tpgid != 0}
}
Windows
There's code to do it, and the required calls are supported by the TWAPI extension so you don't need to make your own. (WARNING! I've not tested this!)
package require twapi_ui
proc isForeground {{pid 0}} {
set forground_pid [get_window_thread [get_foreground_window]]
return [expr {($pid ? $pid : [pid]) == $foreground_pid}]
}
Thanks to Donal I came up with the implementation below that should work on all POSIX Unix variants:
/*
processIsForeground
synopsis: processIsForeground
Returns true if the process is running in the foreground or false
if in the background.
*/
int IsProcessForegroundCmd(ClientData clientData UNUSED, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
/* Check the arg count */
if (objc != 1) {
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return TCL_ERROR;
}
int fd;
errno = 0;
if ((fd = open("/dev/tty", O_RDONLY)) != -1) {
const pid_t pgrp = getpgrp();
const pid_t tcpgrp = tcgetpgrp(fd);
if (pgrp != -1 && tcpgrp != -1) {
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(pgrp == tcpgrp));
close(fd);
return TCL_OK;
}
close(fd);
}
Tcl_SetErrno(errno);
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "processIsForeground: ", (char *)Tcl_PosixError(interp), NULL);
return TCL_ERROR;
}
int Pextlib_Init(Tcl_Interp *interp)
{
if (Tcl_InitStubs(interp, "8.4", 0) == NULL)
return TCL_ERROR;
// SNIP
Tcl_CreateObjCommand(interp, "processIsForeground", IsProcessForegroundCmd, NULL, NULL);
if (Tcl_PkgProvide(interp, "Pextlib", "1.0") != TCL_OK)
return TCL_ERROR;
return TCL_OK;
}

how to modify pocketsphinx continuous.c

i was trying to make word that was detected to execute file.py.
here the source code that i try to modify at continuous.c
if (!in_speech && utt_started) {
/* speech -> silence transition, time to start new utterance */
ps_end_utt(ps);
hyp = ps_get_hyp(ps, NULL );
if (hyp = "OPEN"){
fopen("/home/pi/project/open.py", "r");
}
if (hyp != NULL) {
printf("%s\n", hyp);
fflush(stdout);
}
the program still detect the word but it still not execute the program that i want.
and here is the command that i was using
pocketsphinx_continuous -lm /home/pi/project/3379.lm -dict /home/pi/project/3379.dic -samprate 16000/8000/48000 -inmic yes -adcdev plughw:1,0
really need help here. thanks before.
In C strings are compared with strcmp, not with =, with = you just assign pointers, not even compare them.
It should be
if (strcmp(hyp, "OPEN") == 0) {
....
}

in icecream scheduler log, what does "<host> not eligible " mean?

While diagnosing slow build times using icecream. I encounted several instances of the following message in the icecc-scheduler logs:
<hostname> not eligible
What is this trying to communicate to me?
This could stem from a few scenarios and they are documented in the source code here
bool CompileServer::is_eligible(const Job *job)
{
bool jobs_okay = int(m_jobList.size()) < m_maxJobs;
bool load_okay = m_load < 1000;
bool version_okay = job->minimalHostVersion() <= protocol;
return jobs_okay
&& (m_chrootPossible || job->submitter() == this)
&& load_okay
&& version_okay
&& m_acceptingInConnection
&& can_install(job).size()
&& this->check_remote(job);
}
I'd check to make sure that each host is:
iceccd is run as root
each host has the same version of icecc installed. Verify with /usr/bin/icecc --version
plenty of free space left
maximum number of jobs has not been exceeded

Is it possible with Antlr to detect in which rule of my grammar/part of my grammar an error happend?

I build an application where an user can create boolean rules and customizable functions based on boolean rules:
Here an Example:
((A == True) || (B == True)) && ( C >= 3
&& D == 60) && count{[E == True, F == False, G ==
True,] > 2}
the first part of this rule are common boolean comparisons. the second part starting with "count" is a function, where at least 2 statements have to true for the second part to be true.
Is it possible with Antlr in which kind of my rule an error happend?
Yes, look at the parse tree with grun (TestRig) or intellij plugin etc...