我应该在 Emacs 中使用什么来开发带有 SQL 查询的 PHP 文件?
缩进缓冲区时,代码应如下所示:
<?php
$query = "
SELECT
id,
name
FROM
products
WHERE
id > 12"
?>
在 web 模式和 php 模式下,它看起来像这样:
<?php
$query = "
SELECT
id,
name
FROM
products
WHERE
id > 12"
?>
如果这不可能,一种替代方法是让它启用手动缩进(使用 TAB 和 ShiftTAB,例如Sublime 和其他编辑器)在 PHP 代码中使用多行字符串时。我该怎么做?
最佳答案
当缓冲区缩进时自动执行此操作将非常困难,您可以尝试多种主要模式,但这并不理想。
一个解决方案是编写一个函数来格式化光标下的 sql,然后您可以在完成查询字符串的编写后手动运行此命令。
此示例需要两个包:expand-region
和 sql-indent
,它们都可以在 MELPA 上下载。
如果您安装了软件包,此函数将在指定位置格式化 SQL,与注释中的解决方案不同,这还可以根据代码周围的深度缩进整个 SQL 字符串。
(defun sql-indent-string ()
"Indents the string under the cursor as SQL."
(interactive)
(save-excursion
(er/mark-inside-quotes)
(let* ((text (buffer-substring-no-properties (region-beginning) (region-end)))
(pos (region-beginning))
(column (progn (goto-char pos) (current-column)))
(formatted-text (with-temp-buffer
(insert text)
(delete-trailing-whitespace)
(sql-indent-buffer)
(replace-string "\n" (concat "\n" (make-string column (string-to-char " "))) nil (point-min) (point-max))
(buffer-string))))
(delete-region (region-beginning) (region-end))
(goto-char pos)
(insert formatted-text))))
该函数的工作原理是复制光标下的字符串并将其移动到一个临时缓冲区,sql-indent 将在其中格式化它,然后返回到原始缓冲区并将旧字符串替换为新字符串。它很实用但不漂亮。
关于php - 在 Emacs 中使用 SQL 查询编写 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24397274/