-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement indentation #47
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||
;;; d2-mode.el --- Major mode for working with d2 graphs -*- lexical-binding: t; -*- | ||||||
|
||||||
;; Author: Andor Kesselman <[email protected]> | ||||||
;; Copyright (C) 2022, Andor Kesselman | ||||||
|
@@ -122,14 +122,161 @@ | |||||
(org-babel-eval cmd "") | ||||||
nil)) | ||||||
|
||||||
(defun d2--locate-declaration (str) | ||||||
(defun d2--locate-declaration (str tag) | ||||||
"Locate a certain declaration and return the line difference and indentation. | ||||||
STR is the declaration." | ||||||
STR is the declaration. | ||||||
TAG is a symbol, which is prepended to the declaration data." | ||||||
(let ((l (line-number-at-pos))) | ||||||
(save-excursion | ||||||
(if (re-search-backward str (point-min) t) | ||||||
(cons (- l (line-number-at-pos)) (current-indentation)) | ||||||
(cons -1 -1))))) | ||||||
(cons tag (cons (- l (line-number-at-pos)) (current-indentation))) | ||||||
(cons tag (cons -1 -1)))))) | ||||||
|
||||||
(defun d2--combine-tokens-by-level (tokens) | ||||||
"Combine TOKENS that are at the same level. | ||||||
Collect the tags from both into a list. | ||||||
Use the newer token's line and column." | ||||||
(reverse | ||||||
(cl-reduce (lambda (acc el) | ||||||
(if (and (not (null acc)) | ||||||
(equal (d2--decl-line (car acc)) | ||||||
(d2--decl-line el))) | ||||||
;; when tokens are on the same line, combine them | ||||||
(progn | ||||||
(setf (car acc) | ||||||
;; we don't care about duplicate tags | ||||||
(cons (cons (d2--decl-tag el) (d2--decl-tag (car acc))) | ||||||
(cons (d2--decl-line el) | ||||||
(d2--decl-column el)))) | ||||||
acc) | ||||||
;; otherwise add a new token; set its tag to be a list | ||||||
(cons (cons (list (d2--decl-tag el)) | ||||||
(cons (d2--decl-line el) | ||||||
(d2--decl-column el))) | ||||||
acc))) | ||||||
tokens | ||||||
:initial-value ()))) | ||||||
|
||||||
(defun d2--sort-tokens-by-line (tokens) | ||||||
"Sort TOKENS by line number." | ||||||
(sort tokens | ||||||
Check failure on line 162 in d2-mode.el
|
||||||
:key (lambda (token) | ||||||
(d2--decl-line token)))) | ||||||
|
||||||
(defun d2--filter-not-found-tokens (tokens) | ||||||
"Filter TOKENS that were not detected. | ||||||
They have a line number of -1" | ||||||
(cl-remove-if | ||||||
(lambda (token) (< (d2--decl-line token) 0)) | ||||||
tokens)) | ||||||
|
||||||
(defun d2--parse-from-line () | ||||||
"Parse by starting at current line and searching backward." | ||||||
(let ((node (d2--locate-declaration | ||||||
(rx (group (one-or-more (any alnum "_"))) | ||||||
(? (group ":" | ||||||
(one-or-more space))) | ||||||
(? (group | ||||||
(one-or-more (not (any ?{ ?})))))) | ||||||
'node)) | ||||||
(subnode-start (d2--locate-declaration | ||||||
(rx (group (one-or-more (any alnum "_" "<->" | ||||||
"->" "--" "<-")) | ||||||
(? (group ":" (one-or-more space))) | ||||||
(? (group (one-or-more (not (any ?{ ?}))))) | ||||||
"{" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
)) | ||||||
Check failure on line 188 in d2-mode.el
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'subnode)) | ||||||
(end (d2--locate-declaration "^ *} *$" | ||||||
'end)) | ||||||
(connection (d2--locate-declaration | ||||||
(rx (group (one-or-more (any alnum "_" "-")) | ||||||
(any "->" | ||||||
"<->" | ||||||
"--" | ||||||
"<-") | ||||||
(? (seq | ||||||
":" | ||||||
(one-or-more space))) | ||||||
(? (one-or-more graph)))) | ||||||
'connection))) | ||||||
(list node subnode-start end connection))) | ||||||
|
||||||
;;; declaration part handling | ||||||
(defun d2--decl-tag (decl) (car decl)) | ||||||
Check failure on line 206 in d2-mode.el
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Please update with doc strings |
||||||
(defun d2--decl-line (decl) (cadr decl)) | ||||||
Check failure on line 207 in d2-mode.el
|
||||||
(gv-define-setter d2--decl-line (val decl) `(setf (cadr ,decl) ,val)) | ||||||
(defun d2--decl-column (decl) (cddr decl)) | ||||||
Check failure on line 209 in d2-mode.el
|
||||||
(defun d2--decl-tags-contain (decl tag) | ||||||
(seq-find (lambda (tag2) | ||||||
Check failure on line 211 in d2-mode.el
|
||||||
(equal tag2 tag)) | ||||||
(d2--decl-tag decl))) | ||||||
|
||||||
(defun d2--calculate-desired-indentation () | ||||||
(save-excursion | ||||||
Check failure on line 216 in d2-mode.el
|
||||||
(end-of-line) | ||||||
;; sort tokens by line number (distance from current line) | ||||||
;; and calculate the final indentation | ||||||
(let* ((ordered-tokens (d2--combine-tokens-by-level | ||||||
(d2--sort-tokens-by-line | ||||||
(append (d2--filter-not-found-tokens (d2--parse-from-line)) | ||||||
;; also collect tokens starting at previous line | ||||||
;; otherwise successive tokens of the same type | ||||||
;; may not be detected | ||||||
(if (equal (line-number-at-pos) 1) | ||||||
() | ||||||
(progn (forward-line -1) | ||||||
(end-of-line) | ||||||
(mapcar (lambda (token) | ||||||
;; increment line number | ||||||
;; to make it relative to starting line | ||||||
(cl-incf (d2--decl-line token)) | ||||||
token) | ||||||
(d2--filter-not-found-tokens | ||||||
(d2--parse-from-line))))))))) | ||||||
(current-token (car ordered-tokens)) | ||||||
(previous-token (cadr ordered-tokens))) | ||||||
(message "tokens %s" ordered-tokens) | ||||||
(cond ((and (d2--decl-tags-contain current-token 'node) | ||||||
(null previous-token)) | ||||||
0) | ||||||
|
||||||
((and (d2--decl-tags-contain current-token 'node) | ||||||
(d2--decl-tags-contain previous-token 'subnode)) | ||||||
(+ 4 (d2--decl-column previous-token))) | ||||||
|
||||||
((and (d2--decl-tags-contain current-token 'subnode) | ||||||
(d2--decl-tags-contain previous-token 'subnode)) | ||||||
(+ 4 (d2--decl-column previous-token))) | ||||||
|
||||||
|
||||||
((and (d2--decl-tags-contain current-token 'node) | ||||||
(d2--decl-tags-contain previous-token 'node)) | ||||||
(d2--decl-column previous-token)) | ||||||
|
||||||
((and (d2--decl-tags-contain current-token 'node) | ||||||
(d2--decl-tags-contain previous-token 'end)) | ||||||
(d2--decl-column previous-token)) | ||||||
|
||||||
((and (d2--decl-tags-contain current-token 'end) | ||||||
(d2--decl-tags-contain previous-token 'end)) | ||||||
(max (- (d2--decl-column previous-token) 4) 0)) | ||||||
|
||||||
((and (d2--decl-tags-contain current-token 'end) | ||||||
(d2--decl-tags-contain previous-token 'subnode)) | ||||||
(d2--decl-column previous-token)) | ||||||
|
||||||
((and (d2--decl-tags-contain current-token 'end) | ||||||
(d2--decl-tags-contain previous-token 'node)) | ||||||
(max (- (d2--decl-column previous-token) 4) 0)) | ||||||
|
||||||
(t (progn (message "uknown syntax %s" current-token) | ||||||
(d2--decl-column current-token))) | ||||||
)))) | ||||||
Check failure on line 275 in d2-mode.el
|
||||||
|
||||||
(defun d2-indent-line () | ||||||
(interactive) | ||||||
Check failure on line 278 in d2-mode.el
|
||||||
(indent-line-to (d2--calculate-desired-indentation))) | ||||||
|
||||||
(defun d2-compile () | ||||||
"Compile the current d2 file using d2." | ||||||
|
@@ -220,7 +367,8 @@ | |||||
(setq-local font-lock-defaults '(d2-font-lock-keywords)) | ||||||
(setq-local comment-start "#") | ||||||
(setq-local comment-end "") | ||||||
(setq-local comment-start-skip "#")) | ||||||
(setq-local comment-start-skip "#") | ||||||
(setq-local indent-line-function 'd2-indent-line)) | ||||||
|
||||||
(provide 'd2-mode) | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MoisMoshev I ran this locally, and also ran into this problem. 3 arguments, but only accepts two. Can you clean this up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I'm confused by this, sort takes just one positional, and key arguments:
(sort SEQ &key KEY LESSP REVERSE IN-PLACE)
So it looks ok...? I don't think this changed recently. I'm running emacs 30.0.50
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, just checked in Emacs 29 and indeed the signature changed. It should be
(sort SEQ PREDICATE)
brb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it. Thank you.