Writing PHP with SQL Queries in Emacs

What should I use in Emacs to develop PHP files with SQL queries?

When indenting the buffer, the code should look like this:

<?php
$query = "
    SELECT
        id,
        name
    FROM 
        products
    WHERE 
        id > 12"
?>

In web mode and in php mode it looks like this:

<?php 
$query = "
SELECT
id,
name
FROM
products
WHERE
id > 12"
?>

If this is not possible, one alternative would be to enable manual indentation (using TABand Shift TAB, as in Sublime and other editors), when in multi-line lines in PHP code. How should I do it?

+4
source share
3 answers

The latest version (9.0.84) of web mode (available on github ) provides its own sql indentation in block lines.

+2
source

, , , .

, 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 , . , .

enter image description here

+7

Emacs doesn't handle SQL very well with PHP. There you can get the extension here , which supposedly helps with SQL, so I'm not sure if it will behave the way you want with PHP code. However, there are alternatives to using Emacs, and I would highly recommend it.

+1
source

Source: https://habr.com/ru/post/1545909/


All Articles