From 4121bdb89c1d83949cc368bf81998887b6678168 Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Fri, 24 Jan 2025 12:47:09 -0800 Subject: [PATCH] oni-php: Add command to enter expected operator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The command either inserts a ‘.’ if point is next to, or inside, a string or inside a comment. Otherwise it'll insert the access operator ‘->’ instead. --- oni-php/oni-php.el | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/oni-php/oni-php.el b/oni-php/oni-php.el index 986cb35..d90251e 100644 --- a/oni-php/oni-php.el +++ b/oni-php/oni-php.el @@ -4,7 +4,7 @@ ;; Author: Tom Willemse ;; Keywords: local -;; Version: 2025.0122.121804 +;; Version: 2025.0124.124542 ;; Package-Requires: (php-mode oni-yasnippet oni-flycheck oni-company oni-hydra ggtags fic-mode company-php) ;; This program is free software; you can redistribute it and/or modify @@ -94,6 +94,23 @@ nil for some reason." (insert "use " class ";\n") (pulse-momentary-highlight-region start (point)))))) +(defun oni-php-insert-dot-dwim (N) + "Insert either a concatenation or access operator depending on context. + +Do the insert N times." + (interactive "p") + (if (or (let ((syntax (syntax-ppss))) + (or (nth 3 syntax) + (nth 4 syntax))) + (save-excursion + (skip-syntax-backward " ") + (nth 3 (syntax-ppss (1- (point))))) + (save-excursion + (skip-syntax-forward " ") + (nth 3 (syntax-ppss (1+ (point)))))) + (self-insert-command N) + (dotimes (_ N) (insert "->")))) + (defhydra php-mode-hydra (:color blue) ("a" align-current "Align current selection")) @@ -114,6 +131,7 @@ nil for some reason." (add-to-list 'company-backends 'company-ac-php-backend)) (define-key php-mode-map (kbd "C-c m") #'php-mode-hydra/body) +(define-key php-mode-map (kbd ".") #'oni-php-insert-dot-dwim) ;; In PHP code it's nice to have any ~=>~ aligned.