(goto-char (point-max))
(backward-word)
(should (equal (sweeprolog-definition-at-point)
- '(1 "foo" 1 21)))))
+ '(1 "foo" 1 21 ":-")))))
(ert-deftest syntax-errors ()
"Test clearing syntax error face after errors are fixed."
(should fsap)
(should (string= "lists" (file-name-base fsap))))))
+(ert-deftest dwim-next-clause-fact ()
+ "Tests inserting a new clause after a fact."
+ (with-temp-buffer
+ (sweeprolog-mode)
+ (insert "
+foo.")
+ (sweeprolog-insert-term-dwim)
+ (should (string= (buffer-string)
+ "
+foo.
+foo :- Body.
+"))))
+
+(ert-deftest dwim-next-clause-dcg ()
+ "Tests inserting a non-terminal with `sweeprolog-insert-term-dwim'."
+ (with-temp-buffer
+ (sweeprolog-mode)
+ (insert "
+foo --> bar.")
+ (sweeprolog-insert-term-dwim)
+ (should (string= (buffer-string)
+ "
+foo --> bar.
+foo --> Body.
+"))))
+
+(ert-deftest dwim-next-clause-ssu ()
+ "Tests inserting an SSU rule with `sweeprolog-insert-term-dwim'."
+ (with-temp-buffer
+ (sweeprolog-mode)
+ (insert "
+foo => bar.")
+ (sweeprolog-insert-term-dwim)
+ (should (string= (buffer-string)
+ "
+foo => bar.
+foo => Body.
+"))))
+
(ert-deftest dwim-next-clause ()
"Tests inserting a new clause with `sweeprolog-insert-term-dwim'."
(with-temp-buffer
(should (string= (buffer-string)
"
foo :- bar.
-foo :- _.
+foo :- Body.
"))))
(ert-deftest dwim-define-predicate ()
`("head" ,_ ,f ,a)
`("goal" ,_ ,f ,a))
(setq id-at-point (list f a)))))))
- (when id-at-point
+ (when (and id-at-point
+ (not (eq (car id-at-point) 'variable)))
(sweeprolog--query-once "sweep" "sweep_functor_arity_pi"
id-at-point))))))
'sweeprolog-hole t
'rear-sticky '(sweeprolog-hole)))
-(defun sweeprolog-insert-clause (functor arity)
- (let ((point nil))
+(defun sweeprolog-insert-clause (functor arity &optional neck)
+ (let ((point nil)
+ (neck (or neck ":-")))
(combine-after-change-calls
(insert "\n" functor)
(setq point (point))
(dotimes (_ (1- arity))
(insert (sweeprolog--hole) ", "))
(insert (sweeprolog--hole) ")"))
- (insert " :- " (sweeprolog--hole) ".\n"))
+ (insert " " neck " " (sweeprolog--hole "Body") ".\n"))
(goto-char point)
(sweeprolog-forward-hole)))
(defun sweeprolog-maybe-insert-next-clause (point kind beg end)
(when-let ((current-predicate (and (eq kind 'operator)
(string= "." (buffer-substring-no-properties beg end))
- (cdr (sweeprolog-definition-at-point point))))
- (functor (car current-predicate))
- (arity (cadr current-predicate)))
+ (sweeprolog-definition-at-point point)))
+ (functor (nth 1 current-predicate))
+ (arity (nth 2 current-predicate))
+ (neck (nth 4 current-predicate)))
(goto-char end)
(end-of-line)
- (sweeprolog-insert-clause functor arity)
+ (sweeprolog-insert-clause functor
+ (- arity (if (string= neck "-->") 2 0))
+ neck)
t))
(defun sweeprolog-default-new-predicate-location (_pred)
(defun sweeprolog-definition-at-point (&optional point)
(save-excursion
(when point (goto-char point))
- (let ((def-at-point nil))
- (sweeprolog-analyze-term-at-point (lambda (beg _end arg)
+ (let ((def-at-point nil)
+ (neck ":-"))
+ (sweeprolog-analyze-term-at-point (lambda (beg end arg)
(pcase arg
(`("head_term" ,_ ,f ,a)
(setq def-at-point
(list beg f a)))
+ ("neck"
+ (setq neck
+ (buffer-substring-no-properties beg end)))
("fullstop"
(when def-at-point
(setq def-at-point
(append def-at-point
(list beg))))))))
- def-at-point)))
+ (when def-at-point
+ (append def-at-point (list neck))))))
(defun sweeprolog-insert-pldoc-for-predicate (functor arguments det summary)
(insert "\n\n")