How can I clean up blocks like the following:
if(a)
{
foo();
}
into
if(a)
{
foo();
}
Check these options:
# Whether to remove blank lines after '{'
eat_blanks_after_open_brace = true # false/true
# Whether to remove blank lines before '}'
eat_blanks_before_close_brace = true # false/true
But some other options such as mod_full_brace_if and nl_if_leave_one_liners can also have an impact. Could convert it to if(a) foo(); for example.
Related
I am currently working on a Scriptrunner script that has the objectives of rejecting push if the user pushes a not freeze dependancies. The script itself is not really hard but I don't know how to use Scriptrunner well enough to make it work, any advices ?
I tried several ways to make my script work, here is the script as it is right now :
def regex = ".*requires\\(\"[^\\d|_|\\-](\\S)+/\\[(\\d)(\\.\\d)*\\]#(eca|exail)/stable\""
def fileName = "conanfile.py"
def file = new File(fileName)
if (file.exists()) {
file.readLines().each { line ->
if (line.startsWith("self.requires")) {
if (!(line =~ regex)) {
log.error("Error: Freeze your dependancies")
return false
}
}
}
// log.info("All 'self.requires' lines match the regex.")
return true
} else {
//log.error("Error: $fileName does not exist.")
return true
}
as you can see it is pretty simple, we check if there is a file named "conanfile.py", and read it to find a line starting with "self.requires" and compare the line with our Regex ( the double \ is because of groovy). I am quite lost on how to make it works on Scriptrunner.
So let's say I have a rule like this:
rule : '(' rule ')' | '!' rule '!';
Now in my runtime I have this method:
antlrcpp::Any runtimeVisitor::visitRule(tinycParser::RuleContext *ctx) {
...
}
How can I check whether I am in the fist or in the second case? Might something like this works?
if(ctx->rule(0)) visitRule(ctx->rule(0))
You can label your alternatives like this:
rule
: '(' rule ')' #ParenthesizedRule
| '!' rule '!' #ExclamationMarkRule
| ...
;
Then you can define specific visitor methods for each alternative (i.e. visitParenthesizedRule, visitExclamationMarkRule etc.) instead of visitRule.
If you don't want to add anything to your grammar, you can also just check whether the first child of the rule is an opening parenthesis or an exclamation mark:
if (ctx.children[0].getText() == "(") {
...
} else if (ctx.children[0].getText() == "!") {
...
} else {
...
}
I am trying to get clang-format to leave if-statements with one statement on the same line.
Example input:
if(is_finished) break;
if(foo) {
// do something
}
clang-format output:
if(is_finished)
break;
if(foo) {
// do something
}
Wanted output:
if(is_finished) break;
if(foo) {
// do something
}
None of the space related options seem to match this style.
current config:
---
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: ForIndentation
SpaceBeforeParens: Never
BraceWrapping:
AfterControlStatement: false
The relevant config option is AllowShortIfStatementsOnASingleLine.
The choices are:
Never
WithoutElse
Always
https://clang.llvm.org/docs/ClangFormatStyleOptions.html
AllowShortIfStatementsOnASingleLine
And
AllowShortBlocksOnASingleLine
The first one does what you want, AllowShortBlocksOnASingleLine will also allow code such as
if (expression) { Something(); }
The code itself: (you can see it on DartPad)
void main() {
print(new RegExp("[0-9]|'|\"|\.").hasMatch('g')); // should return false
print(new RegExp("[0-9]|'|\"|\.").hasMatch('0')); // return correctly true
}
Output:
true
true
With the same version on regex101 but with JS, the return value is correct.
Is there something I'm missing with my RegExp or should I report a bug?
Either you use a raw string
print(new RegExp(r'''[0-9]|'|"|\.''').hasMatch('g'));
(''' is to avoid a conflict with " inside the string)
or escape \
print(new RegExp("[0-9]|'|\"|\\.").hasMatch('g'));
DartPad example
I'm confused with capturing text using Qt QRegExp. I want a grab data from PHP code as simple:
public function asr($x)
{
echo "index";
}
Explanations:
public function <can be more spaces or new lines...> asr($x) <can be more spaces or/and new lines or/and tabs...>
{
echo "index";
}
What I want to grab from PHP file? This is one...
public function asr($x)
{
And how I'm now grabbed?
QRegExp reg("(public|private|protected|static|final)(.*)function(.*)[\(](.*)[\)](.*)[\n](.*)[\{]", Qt::CaseInsensitive);
Explanations:
When I try add [\{] or (\{) to grab a braces from PHP file it will not grab a data:
Results only:
public function asr($x)
Instead:
public function asr($x) {
Note: between ($x) and { can be newline \n or some spaces as \s.
This will be as (\))(\n|\s) but no luck :(
Resolved results with example code:
if (file.open(QIODevice::ReadOnly))
{
QTextStream in(&file);
// regex for function with new line
QRegExp reg("((.*)public|private|protected|static|final)( *)function(.*)[(](.*)", Qt::CaseInsensitive);
// capture to list
QStringList list = reg.capturedTexts();
qDebug() << list;
while ( !in.atEnd() )
{
QString line_read = in.readLine();
//qDebug() << line_read; // read lines
qDebug() << reg.exactMatch(line_read); // find matches
}
}
}
FINAL RESULTS:
false
false
false
false
false
false
false
false
true // means found on this line for QRegExp policies
false
false
false
false
true
false
false
false
false
true
false
false
false
false
true
false
false
false
false
false
false
false
You have to use \\ for a backslash inside a "".
Edit:
You also don't have to escape (){ inside a []
QRegExp function("(public|private|protected|static|final)( *)function(.*)[(](.*)[)](.*)[{]", Qt::CaseInsensitive);
auto b = function.exactMatch( "public function ($d) \n {" );
b = function.exactMatch( "public function ($d){" );
works for me. The whitespace is consumed by (.*)
I know what is problem, reading line by line instead of all lines on file and then should replace text in file. I should not reading a file using in.readLine(), so I'm finding { on this line, but not exists because braces { are on next line!