Can't get the loop working in init.coffee


#1

I’m trying to get a custom command into init.coffee to run multiple regex replaces consequently. Here is what I’m trying:

atom.commands.add 'atom-text-editor', 'custom:process-report', ->
  editor = atom.workspace.getActiveTextEditor()
  regs=[]
  res=[]
  regs[0] = /""(?!;)/g
  res[0]="'" # double quotes to single
  regs[1] = /;"([A-Z])/g
  res[1]=";$1" # quoted descriptions
  for (i=0,len=regs.length;i<len;i++){
     editor.scan(regs[i], (match) -> match.replace(res[i]))
  }

Upon loading I get

Failed to load C:\Users\Vitaly.Sokolov\.atom\init.coffee
unexpected (

Syntax is more or less taken from the CoffeeScript documentation. What am I doing wrong? Are loops not supported in the init.coffee? How do I do it properly then? I have about 7 regex to implement and I don’t want to hard-code variables for them.


#2

Hi.

You are mixing Coffeescript and Javascript with each other. Web search how a for-loop looks like in Coffeescript. example

Else… if you could import Javascript code into your init.coffee would it help you? LINK

Regards.


#3

This is the part that’s causing an error. CoffeeScript doesn’t use parentheses in its for loops. You should write it like this:

for i in [0..regs.length]
  editor.scan(regs[i], (match) -> match.replace(res[i]))

#4

CoffeeScript on the left, compiled JavaScript output on the right. - missed that part, went with more familiar example on the right


#5

(smile) No worries friend. It happens - especially when working alone.
Thank you for your reply. Good luck.