Does the completions file "contents" have to be 1 line (no newline)? - sublimetext

New to sublime text and trying to setup some completions as I have a bunch of snippets and I'd rather just use 1 file, instead of separate files for each snippet.
When I save the below code, I get the error that it can't parse the file, unexpected newline. I'd assume it's because I'm using linebreaks. Are completions only for short 1 line code? Is there anything that can be done so I don't have to use individual files for snippets when I have longer code like below?
{
"completions":
[
{ "trigger": "clearforms", "contents":
"//clear form fields
\$(function() {
\$.fn.clearInput = function() {
return this.focus(function() {
if( this.value == this.defaultValue ) {
this.value = "";
}
}).blur(function() {
if( !this.value.length ) {
this.value = this.defaultValue;
}
});
};
\$('#subscribe').clearInput();
\$('#search').clearInput();
});"
}
]
}

JSON does not allow multi-line strings - the line breaks need to be encoded as \n. The $ symbol needs to be double-escaped like so \\$, and your comment forward slashes single-escaped like so - \/\/. So, your completions file needs to look like this:
{
"completions":
[
{ "trigger": "clearforms", "contents": "\/\/ clear forms \n\\$(function() { \n\\$.fn.clearInput = function() { \nreturn this.focus(function() { \nif( this.value == this.defaultValue ) { \nthis.value = \"\"; \n} \n}).blur(function() { \nif( !this.value.length ) { \nthis.value = this.defaultValue; \n} \n}); \n}; \n\n\\$('#subscribe').clearInput(); \n\\$('#search').clearInput(); \n});" }
]
}
I'll leave it up to you to put in the appropriate whitespace after each line break. I should note that while it may be slightly more "clutter-y" to have individual snippet files, they do allow multi-line entries with no newline escaping needed, allowing you to format your output more easily. However, the $ and possibly the JS comments might need escapes.

Related

Laravel Pint json config not wokring

I've been playing around with Laravel Pint and I can't seem to make it work the way I want.
My goal is to have the curly brackets inline with class or function
so instead
function test()
{
}
I want to have a format like this
function test() {
}
I have this on pint.json but doesn't change anything.
{
"preset": "laravel",
"braces": {
"position_after_functions_and_oop_constructs": "same",
"position_after_anonymous_constructs": "same"
}
}
I event tried using psr12 preset and still does not change anything
{
"preset": "psr12"
}
Additionally, I'd like to know how I can allow this format
if ( !$num )
return;
it changes to this after running pint, (it removes the space between if condition and added a space after ! and wrap the state with brackets)
if (! $num) {
return;
}
The rule in a pint.json will be
{
"preset": "laravel",
"rules": {
"not_operator_with_successor_space": false,
"curly_braces_position": {
"functions_opening_brace": "same_line",
"classes_opening_brace": "same_line"
}
}
}
As per PHP-CS-Fixer Rule, use curly_braces_position
Ref: How with pint can I remove space after negative “!” symbol?

How to modify just a property from a dexie store without deleting the rest?

I'm having the dexie stores showed in the print screen below:
Dexie stores print screen
My goal is to update a dexie field row from a store without losing the rest of the data.
For example: when I edit and save the field "com_name" from the second row (key={2}) I want to update "com_name" only and not lose the rest of the properties, see first and the third row.
I already tried with collection.modify and table.update but both deleted the rest of the properties when used the code below:
dexieDB.table('company').where('dexieKey').equals('{1}')
//USING table.update
//.update(dexieRecord.dexiekey, {
// company: {
// com_name: "TOP SERVE 2"
// }
//})
.modify(
{
company:
{
com_name: TOP SERVE 2
}
}
)
.then(function (updated) {
if (updated)
console.log("Success.");
else
console.log("Nothing was updated.");
})
.catch(function (err) { console.log(err); });
Any idea how can I accomplish that?
Thanks
Alex
You where right to use Table.update or Collection.modify. They should never delete other properties than the ones specified. Can you paste a jsitor.com or jsfiddle repro of that and someone may help you pinpoint why the code doesn't work as expected.
Now that you are saying I realised that company and contact stores are created dynamically and editedRecords store has the indexes explicitly declared therefore when update company or contact store, since dexie doesn't see the indexes will overwrite. I haven't tested it yet but I suspect this is the behaviour.
See the print screen below:
Dexie stores overview
Basically I have json raw data from db and in the browser I create the stores and stores data based on it, see code below:
function createDexieTables(jsonData) { //jsonData - array, is the json from db
const stores = {};
const editedRecordsTable = 'editedRecords';
jsonData.forEach((jsonPackage) => {
for (table in jsonPackage) {
if (_.find(dexieDB.tables, { 'name': table }) == undefined) {
stores[table] = 'dexieKey';
}
}
});
stores[editedRecordsTable] = 'dexieKey, table';
addDataToDexie(stores, jsonData);
}
function addDataToDexie(stores, jsonData) {
dbv1 = dexieDB.version(1);
if (jsonData.length > 0) {
dbv1.stores(stores);
jsonData.forEach((jsonPackage) => {
for (table in jsonPackage) {
jsonPackage[table].forEach((tableRow) => {
dexieDB.table(table).add(tableRow)
.then(function () {
console.log(tableRow, ' added to dexie db.');
})
.catch(function () {
console.log(tableRow, ' already exists.');
});
});
}
});
}
}
This is the json, which I convert to object and save to dexie in the value column and the key si "dexieKey":
[
{
"company": [
{
"dexieKey": "{1}",
"company": {
"com_pk": 1,
"com_name": "CloudFire",
"com_city": "Round Rock",
"serverLastEdit": [
{
"com_pk": "2021-06-02T11:30:24.774Z"
},
{
"com_name": "2021-06-02T11:30:24.774Z"
},
{
"com_city": "2021-06-02T11:30:24.774Z"
}
],
"userLastEdit": []
}
}
]
}
]
Any idea why indexes were not populated when generating them dynamically?
Given the JSON data, i understand what's going wrong.
Instead of passing the following to update():
{
company:
{
com_name: "TOP SERVE 2"
}
}
You probably meant to pass this:
{
"company.com_name": "TOP SERVE 2"
}
Another hint is to do the add within an rw transaction, or even better if you can use bulkAdd() instead to optimize the performance.

Jenkins pipeline to verify multpile file existence in when condition

I need to verify multiple file existence in single when condition using && operation.
check for any file that ends with .doc along with final.txt and proceed further.
Second fileexistence(final.txt) seems to be working fine seperately. Please suggest on this
when {
expression
{
return (fileExists("""ls ${Path}/${version}/test/*.doc""")) && !(fileExists("""${Path}/${Version}/test2/final.txt"""))
}
}
You could use allOf and not
when {
allOf {
expression {
return fileExists("ls ${Path}/${version}/test/*.doc")
}
not {
expression {
return fileExists("${Path}/${Version}/test2/final.txt")
}
}
}
}

Using SyntaxHighlightRules for Syntax validation

I've built some new Ace Editor Modes for my custom language (JMS Message representations) with a sophisticated state machine. Now it would be great to reuse that syntax highlighting also to create the errors. Is that possible ?
In other words, let's say my syntax highlighting creates 'invalid' tokens and I want to use the line number of that token to flag an error and then do something like this: https://github.com/ajaxorg/ace/wiki/Syntax-validation
The simplest format is the HEX format:
this.$rules = {
"start": [
{ regex: /[!#].*$/, token: "comment" },
{ regex: /^0x[0-9a-f]+:/, token: "constant" }, // hex offset
{ regex: /(?:[0-9a-fA-F]{4} |[0-9a-fA-F]{2} )/, token: "constant.numeric" }, // hex value
{ regex: /[\S ]{1,16}$/, token: "string" }, // printable value
{ regex: "\\s+", token: "text" },
{ defaultToken: "invalid" }
]
};
And let's say the editor created this state with an invalid token in line 4:
Is there a (preferably easy) way to get to the line numbers of my invalid tokens ? Or to reuse my $rules state machine for syntax checking ?
Found it - I must admin, Ace Editor is really good stuff. Always works as expected.
What works for me, after computing the tokens of the document with the rules state machine, I iterate through all tokens and find the once that are 'invalid' and then set annotations on those lines. Initially simply 'Syntax error' but different types of 'invalid' could mean different things in the future. This way I only have to write the validation syntax validation once.
aceEditor.session.on('change', function(delta) {
var sess = aceEditor.session;
sess.clearAnnotations();
var invalids = [];
for( var row=0;row<sess.getLength();row++ ) {
var tokens = sess.getTokens(row);
if( !tokens ) continue;
for( var t=0;t<tokens.length;t++ ) {
if( tokens[t].type==="invalid" ) {
invalids.push({ row: row, column: 0, text: "Syntax error", type: "error" });
}
}
}
sess.setAnnotations( invalids );
});
There might be a smarter way to do this (maybe an onToken(type,row,column) function somewhere ?), but above works for me.

Kendo variables in Command section in a grid

I have noticed that it is possible to have some kendo logic and variables inside a template in the columns sections.
This is an example from my column section template
template: "#= myVariable# #if(myBoolean) {# <img src='/images/myImage.png' /> #}#"
Please do note that myVariable and myBoolean are variables (fields) of each row of the grid.
Unfortunately i tried the same under the command section in the template. I get the following error "ReferenceError: myVariable is not defined"
Is there any way to add variables in the command sections as it happens with the columns?
As far as I know using templates in columns.command is not even documented: although it works. And you can do things like:
columns : [
{
command: {
template : "# console.log('this', this); console.log('data', data); # toto"
}
},
...
]
or even like:
command: {
template : function (arg) {
console.log("this" ,this);
console.log("arg", arg);
console.log("data", data);
return "toto";
}
}
But what the template returns needs to be a string and in the console of your browser you will see that this is window, arg is the object command and data is an array containing grid data.
Although you can include extra arguments as:
command: {
template : function (arg) {
console.log("this" ,this);
console.log("arg", arg);
console.log("arg.a", arg.a);
console.log("data", data);
return "toto";
},
a: "extra argument"
}
Where I add an extra a argument that can be accessed via arg.a you still cannot get access to current row data since the element is still not inserted.
Instead of that what I do propose is doing something like:
columns : [
{
title: " ",
template: "#= myVariable# #if(myBoolean) {# <img src='/images/myImage.png' /> #}#"
},
...
]
You don't need to have it as command, right? Why do you need it as command?

Resources