0

I recently installed Clear Linux and their default is Atom editor, so I gave it a try. With the php-autocomplete, I was almost very excited. Until I realized I have become set in my ways and I need to have different highlighting for single vs double quote strings.

In php double quoted strings will still be parsed for $variables and whitespace escape characters like \n and \t; whereas single quoted strings are literal and there is no interpretation between single quotes.

I have developed the habit of always using single quotes for array keys and it disturbed my subconscious to not have the single quoted strings appear different than double quoted strings. I searched quite a bit and couldn't find a solution.

Does anyone know of a way to achieve this highlighting scheme?

The screenshot is from Geany. And even in Geany, getting this setting is not standard. Thankfully, a decade ago, this was normal in their themes so I am able to alter the currently available themes to find and change string_2 to a different color than string_2=string_1.

To better help people understand php and how the differences between ' and " may have importance, here is one way the strings behave differently in context:

    $customer = "Bill Hawthorne";
    $_address = "123 Main St\nGlendale, CA 91202";

    $output = "Dear $customer, please confirm the below address is correct:\n\n$_address\n";

    // $output renders as:
    // Dear Bill Waltz, please confirm the below address is correct:
    //
    // 123 Main St
    // Glendale, CA 91202
    // 

    $output = 'Dear $customer, please confirm the below address is correct:\n\n$_address\n';
    // $output renders as:
    // Dear $customer, please confirm the below address is correct:\n\n$_address\n

screenshot of Geany editor showing different highlight colors for php strings of type single quote and double quote

7
  • 1
    but.. a string = a string, regardless of quotes., (also.. you're open to SQL injection from the look of things..) - probably have to code something for this.. besides, it's never good to mix quotes anyway, just have single quotes IMO. Unless you prefer less readability and/or need to use \n (even then. PHP_EOL exists)
    – treyBake
    Jul 10, 2019 at 15:39
  • Please don't distract from the issue. This code is not public facing. And strings are not strings: Having $variables interpreted inside of "Injector $inj_label is good" will result in Injector DX is good, whereas 'Injector $inj_label is good' shows as Injector $inj_label is good. As far as I can tell, those two rendered outputs are not the same.
    – Krista K
    Jul 10, 2019 at 15:44
  • 1
    I'm using PHPStorm and agree strings are strings. What I see is the highlighting on the variable in "Injector $inj_label is good" (your dark purple), and not 'Injector $inj_label is good' which is what matters most to me. I can clearly see if it is parsed or a literal. I'd actually appreciate having what you describe though, it would just help me be more consistent, array keys, etc.
    – ficuscr
    Jul 10, 2019 at 15:51
  • @treyBake you are factually incorrect. php will interpret inside the double quotes for a comparison. If there is a $string variable set, '$string' != "$string"
    – Krista K
    Jul 10, 2019 at 15:58
  • 1
    just as a general query - what happens if you do echo 'hello' in that editor with the different-quote highlighting? I notice that the single quotes seem to be only used for array keys.. I wonder if they have some regex to do $var[] and highlight the key.. would it still highlight the same if it was double quotes? (as in, is it still yellow with double quotes for array keys)
    – treyBake
    Jul 10, 2019 at 16:06

1 Answer 1

1

Use the command Editor: Log cursor scope in the command palette to see the scope applied to a section of text. This scope is applied to the text in the DOM, with syntax-- prepended to each segment.

In the case of language-php, the scope is string.quoted.double.php for double quoted strings, and string.quoted.single.php for single quoted. The following is an example of how you might target them. Note this part is pure CSS / Less; I don't really know it well, so it may be possible to be more concise here.

// ~/.atom/styles.less
atom-text-editor[data-grammar="text html php"] { // target PHP
  .syntax--string.syntax--quoted {
    &.syntax--double,
    &.syntax--double .syntax--punctuation.syntax--definition.syntax--string { // get the quote chars too
      color: red;
    }

    &.syntax--single,
    &.syntax--single .syntax--punctuation.syntax--definition.syntax--string {
      color: yellow;
    }
  }
}

E.g., try it with this <?php "foo $bar" ?>.

1
  • I did View->Toggle Command Palette and it said command not found about the editor input.
    – Krista K
    Jul 14, 2019 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.