Atom Editor: multiple snippets - code-snippets

This is such a simple question but I can't find any documentation besides the readme.
How can I have multiple custom snippets in Atom Editior:
For example I have this in my snippets.cson right now
'.source.js':
'Normal Comment Block':
'prefix': 'cmm'
'body': """
//**********************************************************************************
//
//**********************************************************************************
"""
'.source.js':
'Dashed Comment Block':
'prefix': 'c--'
'body': """
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
"""
But cmm doesn't work, I can only use the last item in the snippets.cson. Any ideas on how to fix this? I have about a dozen different snippets I'd like to use but I cannot figure out how to include them properly.

The configuration file format is called CSON, CoffeeScript Object Notation. Like JSON (JavaScript Object Notation) it is a text format for describing simple objects. Because of which, when you specify a key twice, like .source.js in your example, the second instance overwrites the first. If you simply have one .source.js everything will work fine:
'.source.js':
'Normal Comment Block':
'prefix': 'cmm'
'body': """
//**********************************************************************************
// $1
//**********************************************************************************
$0
"""
'Dashed Comment Block':
'prefix': 'c--'
'body': """
//----------------------------------------------------------------------------------
// $1
//----------------------------------------------------------------------------------
$0
"""
Additionally, I took the liberty of adding tab stops to your snippets so that when you expand the snippet, your cursor should land first inside the comment. You can enter your comment and then press TAB to exit out and continue on.

In addition to #Lee's explanation, here's an example if you wan't to setup multiple snippets organized by programming language:
# HTML Snippets
'.text.html':
'HTML Comment':
'prefix': '<!'
'body': '<!-- $1 -->'
# Sass Snippets
'.source.scss':
'Section Comment':
'prefix': 'sc'
'body': """
/*=================================================
$1
=================================================== */
"""
'Sub Section Comment':
'prefix': 'ssc'
'body': """
/* $1
=================================================== */
"""
# JavaScript Snippets
'.source.js':
'jQuery - Bind Event':
'prefix': 'bind'
'body': """
$( $1 ).on( '$2', '$3', function( $4 ) {
$5
});
"""
On this example I included HTML, Sass and Javascript but you could include others like CSS, ...
Hope this was usefull.

Found a weird bug with multiple snippets in Atom. I'm hoping this answer can help someone with the same problem (I am using the mac version of Atom). So I went to add a new snippet to the snippets.cson file and I copied the old snippet and pasted it below as a template like this and saved them, even though they were the same still
'.source.php':
'Debug':
'prefix': 'prepr'
'body': """
echo "<pre>",print_r($_POST, 1),"</pre>";
die();
"""
'Debug':
'prefix': 'prepr'
'body': """
echo "<pre>",print_r($_POST, 1),"</pre>";
die();
"""
After saving this I edited the second one to have a different title and prefix and body code
'.source.php':
'Debug':
'prefix': 'prepr'
'body': """
echo "<pre>",print_r($_POST, 1),"</pre>";
die();
"""
'different':
'prefix': 'different'
'body': """
echo "different";
"""
I saved again after having edited the second snippet. This time the tab expand for the second snippet wouldn't work, however the first one still did work. After much fooling around with it making sure I had the right syntax I tried a hunch that maybe because I saved with two duplicate snippets that it messed with the cson output somehow. I then deleted the second snippet, then saved it with only the first one in there, then duplicated the first one, then changed it, THEN saved it. After all that both snippets were working normally.
I have been using multiple snippets for a while and never really ran into this problem until now. So strange but there it is.

Starting the next snippet with a comma followed with new line by giving same structure as of the first one worked for me.
'.source.php':
'var dump':
'prefix': 'vd'
'body': """
echo "<pre>";
var_dump($);
echo "</pre>";
""",
'this->db':
'prefix': 'trans'
'body': """
$this->db->trans_start();
""",
'comment block':
'prefix': 'cm'
'body': """
/****************************************
*
*
****************************************/
"""

I had the same problem, here is the fix:
'.source.js':
'First function':
'prefix': 'first'
'body': """
function $1() {
var overall = true;
if (overall)
{
var result = {};
result.test1 = "";
return test2(result);
}
return catched("");
} """,
'Next function':
'prefix': 'next'
'body': """
function $1(result) {
var overall = true;
if (overall)
{
result.test1 = "";
return test2(result);
}
return catched("");
} """,
'Next next function':
'prefix': 'pz'
'body': """
function $1(result) {
var overall = true;
if (overall)
{
result.test1 = "";
return test2(result);
}
return catched("");
} """
Please note that you have to do a couple of things:
Add comma (,) after each """.
Start the next define in the same start line of the prev define!
I really did not understand why it works like that.. but.. that is the case.
Use '.source.PROGRAM LANGUAGE': only once per language.
Home it helps :)

With proper indentation your snippets will work just fine. No need for extra comma
Since the file is in cson format similar to json file. You can write your snippets like below. Where
Each scope (e.g. '.source.js' below) can only be declared once.
Second line is for title.
third line is for keyboard shortcuts.
Fourth line is the actual code to be written inside quotes.
'.source.js':
'Console log':
'prefix': 'cl'
'body': 'console.log($1)'
'log error':
'prefix': 'cle'
'body': 'console.log(err)$1'
'log data':
'prefix': 'cld'
'body': 'console.log(data)$1'

Related

syntax highlighting autocmd is not working in neovim

I am new to vim and esp. in lua scripting. I want to create an autocmd such that all the jinja files will get yaml syntax highlighting.
local a = vim.api
a.nvim_create_autocmd( { "BufNewFile", "BufRead" }, {
pattern = { "*.j2" },
command = [[ lua(syntax = "html")]],
})
but this is not working. Could someone point the obvious.
DD.
I give you an Example on how i do Lua Syntaxhighlightning for my own *.luado files.
Before i have copied ( as super Q User: root.root ) /usr/share/nvim/runtime/syntax/lua.vim to /usr/share/nvim/runtime/syntax/luado.vim.
So i can change it independently from original lua.vim.
It is not necessary to change luado.vim for the Example below.
~/.config/nvim/lua/init.lua required by ~/.config/nvim/init.vim
( At First and off course before: syntax on )
--[[ Automatic Execution of Lua Oneliner if file extension *.luado
With Lua Syntaxhighlighting ]]
vim.api.nvim_create_autocmd({"BufEnter"},{
pattern = {"*.luado"},
command = "luado vim.api.nvim_command('setfiletype luado') load(line, 'koys_nvim_auto_luado')()"
})
Triggers at "BufEnter" and shows that "BufNewFile", "BufRead" not really needed.
( Every time before it is shown from Buffer ;-) )
Impression
Now lets change to next Buffer with :bn to test3.luado
And back with :bp to test2.luado (Output of set)
(test2.luado will be shown after ENTER/RETURN)

Passing URL parameter to link on page

I am trying to grab a parameter from a webpage and insert it into a URL link on that same page but am having problems with the syntax.
So, for example, the webpage is www.website.com?src=mm
Currently the code on the page that does not pull in the parameter is
<?php echo "<A HREF='http://www.website2.com?offer=AAt&sub1=422'><B>Click Here</B></A><BR>" ?>
I would like to include that "mm" parameter at the end of the URL so the final URL is:
http://www.website2.com?offer=AA&sub1=422&sub2=mm
I tried the following but does not work:
<?php echo "<B>Click Here</B><BR>" ?>
Any ideas on how to get this to work? Thanks
Your code doesn't even compile:
Parse error: syntax error, unexpected 'http' (T_STRING), expecting ',' or ';' in /var/www/html/ImagePT/test.php on line 1
it has to be
<?php echo '<B>Click Here</B><BR>'; ?>
but since I'm just in the mood to give you some further advice:
You don't have to write HTML in uppercase, it's rather unusual (not impossible, but you don't see it very often) - then this script is horrible, when the $_GET['src'] variable is undefinied, therefore I'd check if it is set and then modifiy the URL accordingly. So my advice would be to use the following:
<?php
if(isset($_GET['src']))
{
echo '<b>Click Here</b></br>';
}
else
{
echo '<b>Click Here</b></br>';
}
?>

How can I force Jenkins Blue Ocean to display print output instead of "Print Message"?

In the below screenshot some debug entries display the output text (with - Print Message at the end) while others simply display Print Message. To view these you have to expand the step to see the output.
All lines are using the format print "TEXT HERE". I've tried using print, println, and echo. All have the same output.
Why do these sometimes display the message, while others force it into a collapsed section? Is it possible to configure this to always show? The normal non-Blue Ocean Jenkins interface displays fine but there is a lot of verbosity.
This seems to be a known issue:
https://issues.jenkins-ci.org/browse/JENKINS-53649
It looks like that BlueOcean does not handle the Groovy GStrings correctly. This is what I've observed:
A simple:
echo "hello world"
will work as expected and will display correctly.
Whereas a templated string with variables, like:
echo "hello ${some_variable}"
will hide the message under a "Print Message" dropdown.
See also this answer.
It appears that if echo uses a variable with value from params or environment (i.e. "params.*"), then step label gets "Print message" name instead of actual value being echoed. It does not matter if the variable itself is a String or not. Even explicitly converting the params value to String does not help.
String param_str
String text_var_2
parameters {
string(name: 'str_param', defaultValue: 'no value')
}
param_str = params.str_param.toString()
echo "string text in double quotes is ${param_str}"
echo "simple quoted string is here"
echo 'simple quoted string is here'
echo 'Single quoted with str ' + param_str + ' is here'
echo param_str
text_var_2 = 'Single quoted str ' + param_str + ' combined'
echo "GString global text2 is ${text_var_2}"
echo 'String global text2 is' + text_var_2
BlueOcean shows simple quoted strings in step label, but everything else as "Print message".
BlueOcean output
Note that 'normal' variables (strings, integers) are not included into this example, but they are also shown in the label normally. So if you have a code like this
def text_str = 'Some string'
def int_var = 1+2
echo text_str + ' is here'
echo int_var
These will be shown on the label.
And indeed it appears to be a known Jenkins issue as stated in a previous answer.
This is a known BlueOcean bug. The console output in the "classic" view interpolates variables correctly.
One workaround is to use the label parameter of the sh step:
def message = 'Hello World'
sh(script: "echo $message", label: message)
I tried lots of things and seems the moment an environment variable is going to be displayed, it uses Print Message instead the text.
Another workaround would be to split the multiline string into an array and iterate over it :-
String[] splitData = MULTI_LINE_STRING.split("\n");
for (String eachSplit : splitData) {
print(eachSplit);
}

Displaying line breaks in the response text

I use Swagger UI v2.2.0. I have a RESTful method which returns plain text. I want to display this text with line breaks.
At the moment, the returned text contains new line characters, but they are displayed as \n. The Content-Type response header is text/plain.
I can return the text with something else inserted of new line characters (e.g., <br> tags). I also can change the Content-Type. I just need actual line breaks in the displayed text.
Is there a way to achieve this?
Not sure if it helps, but I also faced the same issue.
I wanted to execute the following code in nodejs server:
app.post('/xyz', function (req, res) {
res.status(400).send("MyError\nMyErrorStack:\nStackLine1\nStackline2")
}
and wanted output like:
"MyError
MyErrorStack:
StackLine1
Stackline2"
but got :
"MyError\nMyErrorStack:\nStackLine1\nStackline2"
so instead i split the string into array as shown used following:
err.error = errObj.stack.split("\n")
app.post('/xyz', function (req, res) {
res.status(400).send(err)
}
and this printed
"Error": {
"error": [
"Error: ENOENT: no such file or directory, open 'c:\\....'",
" at Error (native)",
" at Object.fs.openSync (fs.js:584:18)",
" at Object.fs.writeFileSync (fs.js:1224:33)",
" at Object.fs.appendFileSync (fs.js:1283:6)",
" at ...",
" at ...",
" at ...",
" at ...",
" at ...",
" at ..."
]
}
This worked for me.
This is currently not possible due to a documented bug in Swagger UI. Reference:
Inconsistent Markdown Newlines #2981
Bug in Model (Definition) Description with newline characters #3078
The second Issue listed, #3078 contains some discussion on overriding the styles used to render that part of the UI, but results appear inconsistent.
Note: I have subscribed to those issues and will update the answer and/or flag to close as no longer relevant when it is resolved.

preg_replace change brackets and text

I have a text string in PHP:
$str = 'This is a simple \^{text}. It does not look well \_{styled}.';
What I want to do is to change the "\^{text}" and "_{styled}" using preg_replace to get html code like this
$str = 'This is a simple <sup>text<sup>. It does not look well <sub>styled</sub>.';
What I have done now is this:
function Translate($str)
{
return preg_replace('/\\\\\^\{.*\}/i', '<sup>$1</sup>', $str);
}
echo Translate("this offer starts at \^{abc}");
This does not work properly with $1! In $0 the entire match is correct...
Is there someone who can help me with that? Regars!
Solution: Quick and dirty: $str = 'This is a simple \^{text}. It does not look well _{styled}.'; echo preg_replace('~(\\\^{)(.?)(})~', '$2', $str); echo preg_replace('~(\\_{)(.?)(})~', '$2', $str); Maybe someone has a better solution?

Resources