How can I make ATOM understand the \n means carrage return?

Hi Everyone,
Bare with me till the end i have to explain a bit first.

I am very new to coding and i am trying to write/modify a “Virtual Driver” or VD for the Fibaro Home Automation system.

Now the driver is supplied to the Fibaro controller in JSON and then is interpolated and changed by the Fibaro system into something else. Not sure what that is, but its not really needed to this discussion.

Here is a sample of the code

-=-=-=-=-=- SNIP -=-=-=-=-=-=-
{“name”:“Leviton HiFi2 8x8 ver 2 Amp”,“type”:“virtual_device”,“properties”:{“deviceIcon”:8,“currentIcon”:“8”,“log”:"",“logTemp”:"",“mainLoop”:“fibaro:log(“Connecting…”);\nlocal selfId = fibaro:getSelfId(); \n\nlocal ip = fibaro:get(selfId, ‘IPAddress’);\nlocal port = fibaro:get(selfId, ‘TCPPort’);\ntcpSocket = Net.FTcpSocket(ip, port);\ntcpSocket:setReadTimeout(10000);\n\nfunction trim(s)\n — trim leading and trailing whitespaces\n return (s:gsub(”^%s*(.-)%s*\", \"%1\"))\nend\n\n--- input Volume: *Z1VOLUME?<CR> ---\ntcpSocket:write(string.char(0x2a, 0x5a, 0x56, 0x4f, 0x4c, 0x55, 0x4d, 0x45, 0x30));\nfibaro:sleep(1000);\nresult, err = tcpSocket:read();\ntcpSocket:disconnect();\nif (err == 0) then\n fibaro:log(\"transfer OK: \"..string.sub(result,3,4));\n fibaro:debug(string.sub(result,3,4));\n fibaro:call(34, \"setProperty\", \"ui.Volume.value\", string.sub(result,3,4));\nelse\n fibaro:log(\"transfer failed\");\n fibaro:debug(\"transfer failed\");\nend\nfibaro:sleep(60*1000)","ui.Label1.value":"","ui.Label2.value":"","ui.Label3.value":"","ui.Slider11.value":0,"visible":"true","rows":[{"type":"button","elements":[{"id":1,"lua":true,"waitForResponse":false,"caption":"On","name":"Button11","empty":false,"msg":"fibaro:log(\"Connecting...\");\nlocal selfId = fibaro:getSelfId(); \nlocal icon = fibaro:get(selfId, \"deviceIcon\");\nfibaro:call(selfId, \"setProperty\", \"currentIcon\", icon)\nlocal ip = fibaro:get(selfId, 'IPAddress');\nlocal port = fibaro:get(selfId, 'TCPPort');\ntcpSocket = Net.FTcpSocket(ip, port);\ntcpSocket:setReadTimeout(2000);\n\nfunction trim(s)\n -- trim leading and trailing whitespaces --\n return (s:gsub(\"^%s*(.-)%s*", “%1”))\nend\n\n-- power on --\n–tcpSocket:write(string.char(0x5a, 0x4d, 0x4f, 0x4e, 0x0d));\ntcpSocket:write(“Z0POWER1"…"\r")\nfibaro:sleep(1000);\nresult, err = tcpSocket:read();\ntcpSocket:disconnect();\nif (err == 0) then\n fibaro:log(“transfer OK”);\n fibaro:debug(“transfer OK”);\nelse\n fibaro:log(“transfer failed”);\n fibaro:debug(“transfer failed”);\nend",“buttonIcon”:9,“favourite”:false,“main”:true},{“id”:2,“lua”:true,“waitForResponse”:false,“caption”:“Off”,“name”:“Button12”,“empty”:false,“msg”:“fibaro:log(“Connecting…”);\nlocal selfId = fibaro:getSelfId(); \nlocal icon = fibaro:get(selfId, “deviceIcon”);\nfibaro:call(selfId, “setProperty”, “currentIcon”, icon)\nlocal ip = fibaro:get(selfId, ‘IPAddress’);\nlocal port = fibaro:get(selfId, ‘TCPPort’);\ntcpSocket = Net.FTcpSocket(ip, port);\ntcpSocket:setReadTimeout(2000);\n\nfunction trim(s)\n – trim leading and trailing whitespaces --\n return (s:gsub(”^%s(.-)%s*$”, “%1”))\nend\n\n-- power off --\n–tcpSocket:write(string.char(0x5a, 0x4d, 0x4f, 0x46, 0x46, 0x0d));\ntcpSocket:write("*Z0POWER0"…"\r")\nfibaro:sleep(1000);\nresult, err = tcpSocket:read();\ntcpSocket:disconnect();\nif (err == 0) then\n fibaro:log(“transfer OK”);\n

-=-=-=-=-=-=- End Snip --=-=-=-=-=-=-=-=-

Now, when you import the file this code, now matter now big, is one single line of text.
When i first start working on the code, the first time it is imported into ATOM, it uses “Plain Text”. I then change it to JSON, which make the code a lot more readable with colors etc
(which i can’t show due to plain text in the forum.)
Normally i would run this through “codebeautify” to make it even more readable, codebeautify seems to attempt to make the code more readable than ATOM.
Although i did recently do a bit of reading on the ATOM webste/forum and found several packages that mimic codebeautify, Pretty JSON. and that does a decent job of indenting etc.

But the thing I really need is to try and make ATOM understand the “\n” is a carriage return.
This might be a little difficult as some of the ‘\n’ are part of a word without a space between them.
Example:
(“transfer OK”);\nelse\n fibaro:log(“transfer failed”);\n fibaro:debug(“transfer failed”);
“\nelse\n”

But when the JSON formatting is applied to the file, ATOM does color this ‘\n’ a different color to the word it’s ‘combined’ with
IE \nelse\n

image

Is there a way that i can make this happen?
Can I change the “Pretty JSON” package to understand this?
( I am going to assume this is going to be quite hard…)

Thanks for hanging in this far!
Chris

Rather than trying to fiddle with the beautifier, you could use a find-and-replace utility to reformat the file before you open it in Atom. Atom’s find-and-replace package won’t accomplish this as far as I know, but you can use a plain text editor like Notepad++ for it, or this online tool (just enter \n as your “needle”).

Hi there,

thanks for that, I did a bit more searching and found this


the issue seems to be closed with no real solution.

I tried to use search and replace in ATOM but i cant figure out the special CHAR For a Carriage return.

I also tried the same thing in NOTEPAD++ but ran into the same issue.
NOTEPAD++ seems to thing the \n is a “word” not an actual carriage return.

I would be happy just to do a search and replace for \n and replace it with \n but i don’t know the right symbol or expression to use to achieve this.

Chris

ok,
after some more googling, i found this
https://www.giannistsakiris.com/2011/07/05/replace-n-with-new-line-characters-using-notepad/

seems i have to ‘escape’ the slash first
find: \n
replace \n \n

works in notepad++ to keep the ‘\n’ and give me the carriage return.
testing in ATOM

Chris

Nope, that will not work in ATOM.

So i found a workaround, but can i do this in ATOM any other way?

So the process to get this readable, after a bit of stuffing around is.

use code beautify to make the code nice.
then cut and paste into notepad++
then use the \n replaced with \n \n (to keep the \n in the code
then copy that code, into ATOM.
then apply JSON Grammer

Bit of a roundabout way to get this file to be ‘readable’

Chris

Damm, the forum does not like two slashes n “\n”.
that makes my above posts look stupid.
putting it in quotes to see if it will accept that.

image