prevent clang-format from indenting lambdas - clang-format

what clang-format makes:
QObject::connect(&w, &sap::Window::keyPress, [&w](auto* event)
{
if(event->key() == Qt::Key_Escape)
w.close();
});
what i want:
QObject::connect(&w, &sap::Window::keyPress, [&w](auto* event)
{
if(event->key() == Qt::Key_Escape)
w.close();
});
is there a way i can make clang-format NOT indent lambda bodies? can't find anything about it in the documentation.
this is what i have so far:
BasedOnStyle: LLVM,
BreakBeforeBraces: Allman,
NamespaceIndentation: All,
SpaceBeforeParens: Never,
AccessModifierOffset: -4,
AllowShortIfStatementsOnASingleLine: false,
AllowShortBlocksOnASingleLine: false,
AllowShortFunctionsOnASingleLine: None,
AllowShortCaseLabelsOnASingleLine: false,
AllowShortLoopsOnASingleLine: false,
ColumnLimit: 100,
AlwaysBreakTemplateDeclarations: true,
PenaltyReturnTypeOnItsOwnLine: 9999,
IndentWidth: 4,
PointerAlignment: Left

which version on clang-format are you using?
the default configuration on recent version (v3.9.0 or v3.8.0) does almost what you want:
QObject::connect(&w, &sap::Window::keyPress, [&w](auto *event) {
if (event->key() == Qt::Key_Escape)
w.close();
});
you can try it online: http://zed0.co.uk/clang-format-configurator/
but for longer argument packs, the default config returns:
QObject::connect(sender, &sap::ClassName::signalName, receiver,
&sap::OtherClass::slotFunc,
[this](auto dummy, const auto* event) {
if (event->key() == Qt::Key_Escape)
doStuff();
});
by .clang-format like this:
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 80
Language: Cpp
AlignAfterOpenBracket: AlwaysBreak
BinPackArguments: false
BinPackParameters: false
PointerAlignment: Left
you'll get:
QObject::connect(&w, &sap::Window::keyPress, [&w](auto* event) {
if (event->key() == Qt::Key_Escape)
w.close();
});
QObject::connect(
sender,
&sap::ClassName::signalName,
receiver,
&sap::OtherClass::slotFunc,
[this](auto dummy, const auto* event) {
if (event->key() == Qt::Key_Escape)
doStuff();
});
at the moment, BraceWrapping has no special member for lambdas.

For anyone wondering in 2021, there is a LambdaBodyIndentation option since Clang 13. The behavior OP wants can be achieved with
LambdaBodyIndentation: OuterScope

Related

Clang-format multiple conditions new line

If I have a code something like this
void function()
{
return XXXXXXXXX && FFFFFFFFFFFFFF && MMMMMMMMMMMMMM;
}
How can I convert it in something like this
void function()
{
return XXXXXXXXX &&
FFFFFFFFFFFFFF &&
MMMMMMMMMMMMMM;
}
clang-format won't break a line like that unless you have reached the line limit, you can align the operands with:
AlignOperands: Align
you might want to check out
BreakBeforeBinaryOperators : All
You can find descriptions of these here:
https://clang.llvm.org/docs/ClangFormatStyleOptions.html

Customizing default y-axis label in Highcharts

I want to prefix a $ to the default y-axis label. My bar chart is using values in the millions so the chart is returning value-MM (80MM, 30MM). What I would like to do is format the y-axis like $-value-MM ($80MMm $30MM). I have tried the code below and can't get it to work?
yAxis: [{ // Primary yAxis
labels: {
formatter: function () {
return '$' + this.value;
}
},
title: {
text: 'Revenue',
If I understand the question correctly, your data already has 'MM' suffix and you want to add the prefix '$'.
Try,
yAxis: {
labels: {
format: '${value}'
}
}
One rather elaborate way to achieve this is to re-use the code Highcharts uses in their internal defaultLabelFormatter for axis that are numeric, and use it in the axis formatter.
An example of this, with your added prefix (JSFiddle):
yAxis: {
labels: {
formatter: function() {
var numericSymbols = Highcharts.getOptions().lang.numericSymbols;
var i = numericSymbols && numericSymbols.length;
var numericSymbolDetector = this.axis.isLog ? this.value : this.axis.tickInterval;
var UNDEFINED, ret, multi;
while (i-- && ret === UNDEFINED) {
multi = Math.pow(1000, i + 1);
if (numericSymbolDetector >= multi && (this.value * 10) % multi === 0 && numericSymbols[i] !== null) {
ret = Highcharts.numberFormat(this.value / multi, -1) + numericSymbols[i];
}
}
if (ret === UNDEFINED) {
if (Math.abs(this.value) >= 10000) {
ret = Highcharts.numberFormat(this.value, -1);
} else {
ret = Highcharts.numberFormat(this.value, -1, UNDEFINED, '');
}
}
return "$"+ret; // Adding the prefix
}
},
}
A experimental short form of this would be to call the defaultLabelFormatter with the essential parts of the context it requires. An example of this (JSFiddle):
yAxis: {
labels: {
formatter: function() {
return "$" + this.axis.defaultLabelFormatter.call({
axis: this.axis,
value: this.value
});
}
},
}
As the context is incomplete it wouldn't work as expected if your axis was datetime or categories or perhaps logarithmical, but should work for numeric axis. For the full picture I suggest looking at the full defaultLabelFormatter implementation.

Javascript doesn't working in production

I'm using datatables, a plugin in JavaScript to sort, filter, search... of records in a table.
Searching in some foruns, I saw if I set this line below in config/enviroments/production.rb
config.serve_static_assets = false
to true
config.serve_static_assets = true
worked and really worked.
But I do not do that, because another JavaScripts works and that way is not recommended.
So, I tested another way, I copied all content in datatables.js and cut directly in my view.
The firts line are the firsts line of datatables.js.
<script type="text/javascripts">
(function(i,zap,p){i.fn.dataTableSettings=[]; var D=i.fn.dataTablesSettings;i.fn...
...etc...
$("#ranking").dataTable({
sPaginationType: "full_numbers",
iDisplayLength: 20,
bFilter: true,
aLengthMenu: false,
bLengthChange: false,
bAutoWidth: false,
/* fixa os valores da primeira coluna (index) */
fnDrawCallback: function ( oSettings ) {
/* Need to redo the counters if filtered or sorted */
if ( oSettings.bSorted || oSettings.bFiltered ){
for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ ){
$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
}
}
},
aoColumnDefs: [
{ bSortable: false, aTargets: [ 0 ] },
{ aTargets: [ 14 ], bVisible: false }
],
aaSorting: [[ 13, 'desc' ]],
oLanguage: {
sUrl: "/javascripts/i18n/data_tables_lang.json"
}
});
</script>
And again, did not works in production.
I wanna know what does not work in production, well, I copied the code, I'm not using anyother gem like Jammit.
Why does not working?
If that is your exact code, your script tag appears to be incorrect:
<script type="text/javascripts">
should be
<script type="text/javascript">
Remove the "s" from the end of "javascripts," and see if that works.

Override Minimum length string of Select2

Select2 Jquery Plugin
I was having hard time how to override the default message for minimum length input in jquery Select2.
by default the plugin gives the following message.
Default Text
Please enter 1 more characters
My requirement was to show, the following text
Required Text
Enter 1 Character
please share the solution.
Thanks.
The accepted answer does not work for Select2 v4. Expanding on the comment by #IsaacKleinman, the way to override the default messages for an individual Select2 instance is through the language property:
var opts = {
language: {
inputTooShort: function(args) {
// args.minimum is the minimum required length
// args.input is the user-typed text
return "Type more stuff";
},
inputTooLong: function(args) {
// args.maximum is the maximum allowed length
// args.input is the user-typed text
return "You typed too much";
},
errorLoading: function() {
return "Error loading results";
},
loadingMore: function() {
return "Loading more results";
},
noResults: function() {
return "No results found";
},
searching: function() {
return "Searching...";
},
maximumSelected: function(args) {
// args.maximum is the maximum number of items the user may select
return "Error loading results";
}
}
};
$('#mySelect').select2(opts);
To override the functions globally, call the set function on the defaults (according to the docs):
$.fn.select2.defaults.set("key", "value")
However, in our code we do it like this:
$.fn.select2.defaults.defaults['language'].searching = function(){
return 'Custom searching message'
};
I don't know why we don't follow the docs, but it works.
Solution
Here is the solution that i have found out.
Prior to v4
Initialize
$("input[name='cont_responsible'],input[name='corr_responsible'],input[name='prev_responsible'],input[name='pfmea_responsible']").select2({
minimumInputLength: 1,
formatInputTooShort: function () {
return "Enter 1 Character";
},
});
Note
Do not forget to add this code in your document. ready function.
$(document).ready(function () {
});
I shared my solution, any better solutions are welcome.
Thanks.
Using v4 and onwards
The following worked for V4. #Isaac Kleinman
language: { inputTooShort: function () { return ''; } },
You can try this on version 4.0 or higher
you can see reference for answer frome this link :
issues reference
$("#select2").select2({
minimumInputLength: 1,
language: {
inputTooShort: function() {
return 'Please Add More Text';
}
}
});
If you are using django-select2, just add attributes to your form in forms.py:
widget=BookSelect2Widget(
attrs={'data-minimum-input-length': 1}
)
Override the function behaviour like below
$.fn.select2.defaults = $.extend($.fn.select2.defaults, {
formatMatches: function(matches) {
return matches + $filter('translate')('label.matches.found');
},
formatNoMatches: function() {
return $filter('translate')('noMatches.found');
},
formatInputTooShort: function(input, min) {
var n = min - input.length;
return $filter('translate')('label.please.enter ') + n + $filter('translate')(' more.characters') + (n == 1 ? "" : "s");
},
formatInputTooLong: function(input, max) {
var n = input.length - max;
return $filter('translate')('please.delete ') + n + $filter('translate')('')('delete.characters') + (n == 1 ? "" : "s");
},
formatSelectionTooBig: function(limit) {
return $filter('translate')('select.only') + limit + $filter('translate')('select.item ') + (limit == 1 ? "" : "s");
},
formatLoadMore: function(pageNumber) {
return $filter('translate')('load.results');
},
formatSearching: function() {
return $filter('translate')('label.search');
}
});
}

jQuery UI Spinner with letters A-Z or other custom range

Is there a way to customize jQuery UI spinner, so that A-Z letters (or any custom range) is possible?
Yes, this is possible. Here's a simple example using A-Z, adapted from the provided time example:
$.widget("ui.alphaspinner", $.ui.spinner, {
options: {
min: 65,
max: 90
},
_parse: function(value) {
if (typeof value === "string") {
return value.charCodeAt(0);
}
return value;
},
_format: function(value) {
return String.fromCharCode(value);
}
});
Usage:
$("#my-input").alphaspinner();
Example: http://jsfiddle.net/4nwTc/1/
The above example creates a new widget called alphaspinner that inherits from spinner. You can do this for just one spinner with the following:
$(function() {
var spinner = $("#alpha-spinner").spinner({
min: 65,
max: 90
}).data("spinner");
spinner._parse = function (value) {
if (typeof value === "string") {
return value.charCodeAt(0);
}
return value;
};
spinner._format = function (value) {
return String.fromCharCode(value);
}
});​
Example: http://jsfiddle.net/4nwTc/2/
I built up on Andrews code and built a spinner widget that takes a string array for input.
You can see the solution here.

Resources