Commands synchronously


#1

Guys, I have a problem with Beautfy, when I try to apply it in some code that has {{}} it transforms the excerpt and that gives an error. I thought I’d transpose that part of the code into something like “{{}}” before running Beautify and then changing again, I already made the code in init.coffee inclusive. But I’m having trouble because I do not know how to run the commands synchronously.


#2

It might help if you posted your code here.


#3

atom.commands.add ‘atom-text-editor’, ‘custom:beautfy’, ->
editor = atom.workspace.getActiveTextEditor()
editorElement = atom.views.getView(atom.workspace.getActiveTextEditor())

regMatch = /{{/
editor.scan(regMatch, (match) -> match.replace(’"{{’))
regMatch = /}}/
editor.scan(regMatch, (match) -> match.replace(’}}"’))

atom.commands.dispatch(editor, ‘atom-beautify:beautify-editor’)

regMatch = /"{{/
editor.scan(regMatch, (match) -> match.replace(’{{’))
regMatch = /}}"/
editor.scan(regMatch, (match) -> match.replace(’}}’))

The problem is that the commands do not run in sequence, I need them to be synchronous. If anyone has another solution it would be good too.


Specifying different grammars for an extension, one for the code editor (syntax highlighting) and another for Atom Beautify
#4

Please could you repeat the code.
Where the code is indented is important.

Use the following format when putting the Coffeescript in the forum:

```coffeescript
>>>>>>> place you code here
```

#5

Hello.

I am a novice with this AND it is not tested…
try something like:

waitTask = false  # extra security

taskDone = (event) ->
  return false unless waitTask
  return false unless event.type is 'atom-beautify:beautify-editor'

  waitTask = false
  editor = atom.workspace.getActiveTextEditor()
  regMatch = /"{{/
  editor.scan(regMatch, (match) -> match.replace('{{'))
  regMatch = /}}"/
  editor.scan(regMatch, (match) -> match.replace('}}'))
    
  
atom.commands.add 'atom-text-editor', 'custom:beautfy', ->
  return false unless editor = atom.workspace.getActiveTextEditor()
  editorElement = atom.views.getView(atom.workspace.getActiveTextEditor())

  regMatch = /{{/
  editor.scan(regMatch, (match) -> match.replace('"{{'))
  regMatch = /}}/
  editor.scan(regMatch, (match) -> match.replace('}}"'))

  waitTask = true
  atom.commands.dispatch(editor, 'atom-beautify:beautify-editor')
  atom.commands.onDidDispatch(taskDone)  # work-around the synch issue

#6

A recent topic introduced doubt to what I suggested before.
Looking at this topic a little closer the following codes might be worth while testing.


Ignoring the synch issue…

atom.commands.add 'atom-text-editor', 'custom:beautify', ->
  return false unless editor = atom.workspace.getActiveTextEditor()
  editorElement = atom.views.getView(editor)

  regMatch = /{{/
  editor.scan(regMatch, (match) -> match.replace('"{{'))
  regMatch = /}}/
  editor.scan(regMatch, (match) -> match.replace('}}"'))

  atom.commands.dispatch(editorElement, 'atom-beautify:beautify-editor')

  regMatch = /"{{/
  editor.scan(regMatch, (match) -> match.replace('{{'))
  regMatch = /}}"/
  editor.scan(regMatch, (match) -> match.replace('}}'))


Providing for the synch issue…

waitTask = false  # extra security

taskDone = (event) ->
  return false unless waitTask
  return false unless event.type is 'atom-beautify:beautify-editor'

  waitTask = false
  editor = atom.workspace.getActiveTextEditor()

  regMatch = /"{{/
  editor.scan(regMatch, (match) -> match.replace('{{'))
  regMatch = /}}"/
  editor.scan(regMatch, (match) -> match.replace('}}'))


atom.commands.add 'atom-text-editor', 'custom:beautify', ->
  return false unless editor = atom.workspace.getActiveTextEditor()
  editorElement = atom.views.getView(editor)

  regMatch = /{{/
  editor.scan(regMatch, (match) -> match.replace('"{{'))
  regMatch = /}}/
  editor.scan(regMatch, (match) -> match.replace('}}"'))

  # Prepare call back when BEAUTIFY task is complete
  waitTask = true
  atom.commands.onDidDispatch(taskDone)  # work-around the synch issue
  
  # Call BEAUTIFY task
  atom.commands.dispatch(editorElement, 'atom-beautify:beautify-editor')