From: Eshel Yaron Date: Tue, 25 Oct 2022 06:44:46 +0000 (+0300) Subject: ADDED: completion-at-point for variable names X-Git-Tag: V8.5.20-sweep-0.8.1~4 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=2f6cbc5e8b36a556e117edcc86efd44400c81a6b;p=sweep.git ADDED: completion-at-point for variable names * sweeprolog.el (sweeprolog-local-variables-collection, sweeprolog-variable-completion-at-point): new functions. (sweeprolog-mode): add to completion-at-point-functions. (sweeprolog-mode-syntax-table): update according to char_type(Char, prolog_symbol). --- diff --git a/README.org b/README.org index 567a34a..904fbf7 100644 --- a/README.org +++ b/README.org @@ -1424,9 +1424,9 @@ there some further improvements that we want to pursue: predicates]]). - Add commands for narrowing and moving by predicate definitions :: ~sweeprolog-mode~ - should include commands moving point to the next/previous predicate - definition. We already have commands for clause-based motion - (~C-M-a~, ~C-M-e~) but it would be useful to have predicate-based + should include commands for moving point to the next/previous + predicate definition. We already have commands for clause-based + motion (~C-M-a~, ~C-M-e~) but it would be useful to have predicate-based variants as well. These commands could then be bound to ~C-c C-n~ for moving to the next predicate definition and ~C-c C-p~ for moving to the previous. diff --git a/sweeprolog.el b/sweeprolog.el index a8778d9..b0c4c07 100644 --- a/sweeprolog.el +++ b/sweeprolog.el @@ -635,6 +635,49 @@ default." (when (sweeprolog-true-p sol) (cdr sol)))) +(defun sweeprolog-local-variables-collection (&rest exclude) + "Return a list of variable names that occur in the current clause. + +EXCLUDE is a list of variables name to be excluded from the +resulting list even when found in the current clause." + (let* ((beg (save-mark-and-excursion + (unless (sweeprolog-at-beginning-of-top-term-p) + (sweeprolog-beginning-of-top-term)) + (max (1- (point)) (point-min)))) + (end (save-mark-and-excursion + (sweeprolog-end-of-top-term) + (point))) + (vars nil)) + (save-excursion + (goto-char beg) + (save-match-data + (while (search-forward-regexp (rx bow (or "_" upper) + (* alnum)) + end t) + (unless (nth 8 (syntax-ppss)) + (let ((match (match-string-no-properties 0))) + (unless (or (member match exclude) + (member match vars)) + (push (match-string-no-properties 0) vars))))))) + vars)) + + +(defun sweeprolog-variable-completion-at-point () + "Prolog variable name completion backend for `completion-at-point'." + (when-let ((bounds (bounds-of-thing-at-point 'symbol)) + (beg (car bounds)) + (end (cdr bounds))) + (when (and (<= beg (point) end) + (let ((first (char-after beg))) + (or (char-uppercase-p first) + (= first ?_)))) + (when-let ((col (sweeprolog-local-variables-collection + (buffer-substring-no-properties beg end)))) + (list beg end col + :exclusive 'no + :annotation-function + (lambda (_) " Var")))))) + (defun sweeprolog-completion-at-point-function () (when-let ((bounds (sweeprolog-predicate-prefix-boundaries))) (let ((start (car bounds)) @@ -2110,12 +2153,21 @@ of them signal success by returning non-nil." (defvar sweeprolog-mode-syntax-table (let ((table (make-syntax-table))) - (modify-syntax-entry ?_ "_" table) + (modify-syntax-entry ?# "." table) + (modify-syntax-entry ?$ "." table) + (modify-syntax-entry ?& "." table) (modify-syntax-entry ?+ "." table) (modify-syntax-entry ?- "." table) - (modify-syntax-entry ?= "." table) + (modify-syntax-entry ?. "." table) + (modify-syntax-entry ?: "." table) (modify-syntax-entry ?< "." table) + (modify-syntax-entry ?= "." table) (modify-syntax-entry ?> "." table) + (modify-syntax-entry ?? "." table) + (modify-syntax-entry ?@ "." table) + (modify-syntax-entry ?^ "." table) + (modify-syntax-entry ?~ "." table) + (modify-syntax-entry ?_ "_" table) (modify-syntax-entry ?| "." table) (modify-syntax-entry ?\' "\"" table) (modify-syntax-entry ?` "\"" table) @@ -3034,6 +3086,7 @@ if-then-else constructs in SWI-Prolog." (add-hook 'xref-backend-functions #'sweeprolog--xref-backend nil t) (add-hook 'file-name-at-point-functions #'sweeprolog-file-at-point nil t) (add-hook 'completion-at-point-functions #'sweeprolog-completion-at-point-function nil t) + (add-hook 'completion-at-point-functions #'sweeprolog-variable-completion-at-point nil t) (when sweeprolog-colourise-buffer-on-idle (setq sweeprolog--timer (run-with-idle-timer