首页 > 解决方案 > Atom 编辑器:如何为 php 以不同的方式突出显示单引号和双引号字符串

问题描述

我最近安装了 Clear Linux,他们的默认是 Atom 编辑器,所以我试了一下。使用 php-autocomplete,我几乎感到非常兴奋。直到我意识到我已经习惯了我的方式,我需要对单引号和双引号字符串进行不同的突出显示。

php双引号中的字符串仍将被解析为$variables和空格转义字符,如\nand \t; 而单引号字符串是文字,单引号之间没有解释。

我已经养成了始终使用单引号作为数组键的习惯,并且它扰乱了我的潜意识,因为单引号字符串与双引号字符串看起来不同。我搜索了很多,找不到解决方案。

有谁知道实现这种突出显示方案的方法?

截图来自 Geany。即使在 Geany 中,获得此设置也不是标准的。值得庆幸的是,十年前,这在他们的主题中是正常的,因此我能够更改当前可用的主题以查找并更改string_2为与string_2=string_1.

为了更好地帮助人们理解php和 之间的差异'以及它们之间的差异"可能具有怎样的重要性,以下是字符串在上下文中表现不同的一种方式:

    $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

Geany 编辑器的屏幕截图,显示了单引号和双引号类型的 php 字符串的不同突出显示颜色

标签: phpatom-editor

解决方案


使用Editor: Log cursor scope命令面板中的命令查看应用于文本部分的范围。此范围应用于 DOM 中的文本,并syntax--添加到每个段的前面。

在 的情况下language-php,范围是string.quoted.double.php双引号字符串和string.quoted.single.php单引号。以下是如何定位它们的示例。注意这部分是纯 CSS/Less;我不是很了解,所以在这里可能会更简洁。

// ~/.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;
    }
  }
}

例如,用这个试试<?php "foo $bar" ?>


推荐阅读