"
))))
+(ert-deftest custom-indentation ()
+ "Test forcefully setting custom indentation levels."
+ (with-temp-buffer
+ (sweeprolog-mode)
+ (insert "
+foo :-
+ repeat,
+ bar,
+ baz")
+ (call-interactively #'indent-for-tab-command)
+ (should (string= (buffer-substring-no-properties (point-min) (point-max))
+ "
+foo :-
+ repeat,
+ bar,
+ baz"))))
(defun sweeprolog-test-indentation (given expected)
(with-temp-buffer
"Tests indentation rules."
(sweeprolog-test-indentation
"
+colourise_declaration(Module:Goal, table, TB,
+ term_position(_,_,QF,QT,
+"
+ "
+colourise_declaration(Module:Goal, table, TB,
+ term_position(_,_,QF,QT,
+")
+ (sweeprolog-test-indentation
+ "
some_functor(
arg1,
arg2,
"
functor(
foo")
- )
+ (sweeprolog-test-indentation
+ "
+replace_key_value(Replacement, Key - AtVar, Out, Used0, Used1),
+atom_concat(@, Var, AtVar) =>
+foo.
+"
+ "
+replace_key_value(Replacement, Key - AtVar, Out, Used0, Used1),
+ atom_concat(@, Var, AtVar) =>
+ foo.
+"
+ )
+ (sweeprolog-test-indentation
+ "
+head,
+right_hand_context -->
+body.
+"
+ "
+head,
+ right_hand_context -->
+ body.
+"))
;;; sweeprolog-tests.el ends here
(scan-error nil)))
(defun sweeprolog--backward-term (pre)
- (while t
- (pcase (sweeprolog-last-token-boundaries)
- ('nil
- (signal 'scan-error
- (list "Cannot scan backwards beyond beginning of buffer."
- (point-min)
- (point-min))))
- (`(open ,obeg ,oend)
- (signal 'scan-error
- (list "Cannot scan backwards beyond opening parenthesis or bracket."
- obeg
- oend)))
- (`(functor ,obeg ,oend)
- (signal 'scan-error
- (list "Cannot scan backwards beyond functor."
- obeg
- oend)))
- (`(operator ,obeg ,oend)
- (if (and (string= "." (buffer-substring-no-properties obeg oend))
- (or (not (char-after (1+ obeg)))
- (member (char-syntax (char-after (1+ obeg)))
- '(?> ? ))))
- (signal 'scan-error
- (list "Cannot scan backwards beyond fullstop."
- obeg
- (1+ obeg)))
- (if-let ((opre (sweeprolog-op-infix-precedence
- (buffer-substring-no-properties obeg oend))))
+ (let ((infix-flag t))
+ (while t
+ (pcase (sweeprolog-last-token-boundaries)
+ ('nil
+ (signal 'scan-error
+ (list "Cannot scan backwards beyond beginning of buffer."
+ (point-min)
+ (point-min))))
+ (`(open ,obeg ,oend)
+ (signal 'scan-error
+ (list "Cannot scan backwards beyond opening parenthesis or bracket."
+ obeg
+ oend)))
+ (`(functor ,obeg ,oend)
+ (signal 'scan-error
+ (list "Cannot scan backwards beyond functor."
+ obeg
+ oend)))
+ (`(operator ,obeg ,oend)
+ (if (and (string= "." (buffer-substring-no-properties obeg oend))
+ (or (not (char-after (1+ obeg)))
+ (member (char-syntax (char-after (1+ obeg)))
+ '(?> ? ))))
+ (signal 'scan-error
+ (list "Cannot scan backwards beyond fullstop."
+ obeg
+ (1+ obeg)))
+ (if-let ((opre (sweeprolog-op-infix-precedence
+ (buffer-substring-no-properties obeg oend))))
+ (if (> opre pre)
+ (signal 'scan-error
+ (list (format "Cannot scan backwards beyond infix operator of higher precedence %s." opre)
+ obeg
+ oend))
+ (goto-char obeg)
+ (setq infix-flag t))
+ (if-let ((ppre (sweeprolog-op-prefix-precedence
+ (buffer-substring-no-properties obeg oend))))
+ (if (> ppre pre)
+ (signal 'scan-error
+ (list (format "Cannot scan backwards beyond prefix operator of higher precedence %s." opre)
+ obeg
+ oend))
+ (goto-char obeg)
+ (setq infix-flag nil))
+ (goto-char obeg)
+ (setq infix-flag nil)))))
+ (`(symbol ,obeg ,oend)
+ (if-let ((opre (sweeprolog-op-infix-precedence (buffer-substring-no-properties obeg oend))))
(if (> opre pre)
(signal 'scan-error
(list (format "Cannot scan backwards beyond infix operator of higher precedence %s." opre)
obeg
oend))
- (goto-char obeg))
- (if-let ((ppre (sweeprolog-op-prefix-precedence
- (buffer-substring-no-properties obeg oend))))
+ (goto-char obeg)
+ (setq infix-flag t))
+ (if-let ((ppre (and (not infix-flag)
+ (sweeprolog-op-prefix-precedence (buffer-substring-no-properties obeg oend)))))
(if (> ppre pre)
(signal 'scan-error
(list (format "Cannot scan backwards beyond prefix operator of higher precedence %s." opre)
obeg
oend))
- (goto-char obeg))
- (goto-char obeg)))))
- (`(symbol ,obeg ,oend)
- (if-let ((opre (sweeprolog-op-infix-precedence
- (buffer-substring-no-properties obeg oend))))
- (if (> opre pre)
- (signal 'scan-error
- (list (format "Cannot scan backwards beyond infix operator of higher precedence %s." opre)
- obeg
- oend))
- (goto-char obeg))
- (if-let ((ppre (sweeprolog-op-prefix-precedence
- (buffer-substring-no-properties obeg oend))))
- (if (> ppre pre)
- (signal 'scan-error
- (list (format "Cannot scan backwards beyond prefix operator of higher precedence %s." opre)
- obeg
- oend))
- (goto-char obeg))
- (goto-char obeg))))
- (`(close ,lbeg ,_lend)
- (goto-char (nth 1 (syntax-ppss lbeg)))
- (when (or (= (char-syntax (char-before)) ?w)
- (= (char-syntax (char-before)) ?_))
- (skip-syntax-backward "w_")))
- (`(,_ ,lbeg ,_)
- (goto-char lbeg)))))
+ (goto-char obeg)
+ (setq infix-flag nil))
+ (goto-char obeg)
+ (setq infix-flag nil))))
+ (`(close ,lbeg ,_lend)
+ (goto-char (nth 1 (syntax-ppss lbeg)))
+ (when (or (= (char-syntax (char-before)) ?w)
+ (= (char-syntax (char-before)) ?_))
+ (skip-syntax-backward "w_"))
+ (setq infix-flag nil))
+ (`(,_ ,lbeg ,_)
+ (goto-char lbeg)
+ (setq infix-flag nil))))))
(defun sweeprolog-backward-term (pre)
(condition-case _
(defun sweeprolog-indent-line-after-infix (fbeg _fend pre)
(save-excursion
(goto-char fbeg)
- (let ((lim (or (nth 1 (syntax-ppss)) (point-min)))
- (cur (point))
- (go t))
+ (sweeprolog-backward-term (1- pre))
+ (let ((go t)
+ (line-beg (line-beginning-position)))
(while go
- (setq cur (point))
- (sweeprolog-backward-term pre)
- (when (< (point) lim)
- (goto-char cur))
- (when (= (point) cur)
- (setq go nil))))
- (current-column)))
+ (pcase (sweeprolog-last-token-boundaries)
+ ((or `(operator ,lbeg ,lend)
+ `(symbol ,lbeg ,lend))
+ (if-let ((sym (buffer-substring-no-properties lbeg lend))
+ (ppre (sweeprolog-op-infix-precedence sym))
+ (res (= ppre pre)))
+ (if (< lbeg line-beg)
+ (setq go nil)
+ (goto-char lbeg)
+ (sweeprolog-backward-term (1- pre)))
+ (setq go nil)))
+ (_ (setq go nil))))
+ (let ((col (current-column)))
+ (if (= col 0)
+ (/ sweeprolog-indent-offset 2)
+ col)))))
(defun sweeprolog-indent-line ()
"Indent the current line in a `sweeprolog-mode' buffer."