在Emacs中使用SQL查询编写PHP

Osk*_*son 4 php mysql emacs

我应该在Emacs中使用什么来开发带有SQL查询的PHP文件?

缩进缓冲区时,代码应如下所示:

<?php
$query = "
    SELECT
        id,
        name
    FROM 
        products
    WHERE 
        id > 12"
?>
Run Code Online (Sandbox Code Playgroud)

在web模式和php模式下,它看起来像这样:

<?php 
$query = "
SELECT
id,
name
FROM
products
WHERE
id > 12"
?>
Run Code Online (Sandbox Code Playgroud)

如果这是不可能的,一种替代方法是让它在PHP代码中的多行字符串时启用手动缩进(使用TABShiftTAB,就像在Sublime和其他编辑器中一样).我该怎么办?

Jor*_*ndo 10

缓冲区缩进时自动执行此操作将非常困难,您可以尝试多种主要模式,但这并不理想.

一种解决方案是编写一个函数来格式化光标下的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))))
Run Code Online (Sandbox Code Playgroud)

该函数通过复制光标下的字符串并将其移动到临时缓冲区来工作,其中sql-indent将格式化它,然后返回到原始缓冲区并用新字符串替换旧字符串.它功能齐全但不漂亮.

在此输入图像描述