Problems with init.coffee to autocomplete the tags in PHP


#1

Hello,

I have problems with init.coffee. I am writing PHP files. In PHP, when you write echo "string" and you type only h4 inside the quotes, the tag h4 doesn’t autocomplete or it doesn’t pop up the snippet, it is why I added new init scripts, but I got a warning that it has a newline.

Init Script:

atom.commands.add 'atom-workspace',
  'custom:wrap-with-strong': ->
    editor = atom.workspace.getActiveTextEditor()
    for selection in editor.getSelections()
    textToWrap = selection.getText()
    selection.insertText("<b>#{textToWrap}</b>")

  'custom:wrap-with-h4': ->
    editor = atom.workspace.getActiveTextEditor()
    for selection in editor.getSelections()
    extToWrap = selection.getText()
    selection.insertText("<h4>#{textToWrap}</h4>")

Keymap:

'atom-workspace':
  'cmd-b': 'custom:wrap-with-strong'
  'cmd-4': 'custom:wrap-with-h4'

If I didn’t add the last setting, the tag b's setting still worked. But after adding a new setting, it doesn’t work.


#2

Please wrap your code in backticks so that the indentation is preserved. You can do so by placing three backticks before and after each code block, or just by selecting it all and pressing the </> button in the editor.


#3

I edited. Done.


#4

Okay, to answer your question, you need another atom.commands.add 'atom-workspace', line above the second command. You also need to indent the code inside the for loops. CoffeeScript is indentation-based, so right now it doesn’t think that there’s anything inside the loops.

I think you’re over-engineering this, however. It would be a lot easier to just add the following to your snippets.cson.

'.text.html.php':
  'Bold':
    prefix: 'b'
    body: '<b>$1</b>'
  'Heading 4':
    prefix: 'h4'
    body: '<h4>$1</h4>'

More details about snippets can be found here.


#5

Both Init and Snippets don’t work.

I modified the codes in Init Coffee like:

atom.commands.add 'atom-workspace',
  'custom:wrap-with-strong': ->
    editor = atom.workspace.getActiveTextEditor()
      for selection in editor.getSelections()
    textToWrap = selection.getText()
    selection.insertText("<b>#{textToWrap}</b>")

atom.commands.add 'atom-workspace',
  'custom:wrap-with-h4': ->
    editor = atom.workspace.getActiveTextEditor()
      for selection in editor.getSelections()
    extToWrap = selection.getText()
    selection.insertText("<h4>#{textToWrap}</h4>")

#6

You need to indent the code inside the for loops. Like this:

atom.commands.add 'atom-workspace',
  'custom:wrap-with-strong': ->
    editor = atom.workspace.getActiveTextEditor()
    for selection in editor.getSelections()
      textToWrap = selection.getText()
      selection.insertText("<b>#{textToWrap}</b>")

atom.commands.add 'atom-workspace',
  'custom:wrap-with-h4': ->
    editor = atom.workspace.getActiveTextEditor()
    for selection in editor.getSelections()
      textToWrap = selection.getText()
      selection.insertText("<h4>#{textToWrap}</h4>")

What do you have in your snippets.cson?


TextEditor - Indices to Point object
#7

In Snippets, it worked now, because you forgot of putting quotes. I put quotes and it worked. I read that I needed to press Tab in place of pressing Return.

I tested in Init Script… It worked fine.


#8

I did not. You don’t need quotes around keys in CSON unless there’s a hyphen or space in the key name. None of the snippets in my snippets.cson have quotes around prefix and body because it’s more legible that way.


#9

I see, but in mine, it didn’t work without quotes, Atom accused of having an unexpected indentation or newline.

I posted like:

'.text.html':
  'Bootstrap PHP':
    'prefix': 'bph'
    'body': """
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta http-equiv="x-ua-compatible" content="ie=edge">
        <title></title>
        <link rel="stylesheet" href="bootstrap.min.css">
        <script src="jquery.min.js"></script>
        <script src="bootstrap.min.js" ></script>
      </head>

      <body>

        <h1><small></small></h1>

        <?php



        ?>

      </body>
    </html>
    """

'.text.html.php':
  'Inserir a tag br no PHP':
    'prefix': '.logp'
    'body': '."<br>"'
  'Bold':
    'prefix': 'bb'
    'body': '<b>$1</b>'
  'Heading 4':
    'prefix': 'hh4'
    'body': '<h4>$1</h4>'

I replaced b and h4 for bb and hh4 because when I pressed Tab, it didn’t autocomplete in a PHP file.

Observe that in the first command, I modified it to .text.html.php and it didn’t work when I typed bph in a PHP file. I didn’t know why. I had again to change to .text.html and it worked.


#10

Maybe you needed to indent the HTML more. This is an example from mine:

'.text.html':
  'Form':
    prefix: 'form'
    body: """
      <form class="$1" action="index.html" method="post">
        $2
      </form>
    """

#11

Although it worked with quotes, I tested without quotes and it worked.