Krista K
Krista K

Reputation: 21871

Atom Editor: how to highlight singe and double quote strings differently for php

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

Upvotes: 0

Views: 755

Answers (1)

Benjamin Gray
Benjamin Gray

Reputation: 245

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" ?>.

Upvotes: 1

Related Questions