Skip to content

Commit

Permalink
Pull in formatter implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dahtah authored and gcv committed Jan 11, 2022
1 parent a4bfe49 commit 69cd68a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
10 changes: 10 additions & 0 deletions formatter.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#Format regions and buffers via JuliaFormatter
#Inspired by https://codeberg.org/FelipeLema/julia-formatter.el/

function format_data(str::String)
try
JuliaFormatter.format_text(str)
catch #sthg went wrong, syntax probably invalid
return []
end
end
41 changes: 41 additions & 0 deletions julia-snail.el
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,47 @@ autocompletion aware of the available modules."
(define-key map (kbd "C-k") #'julia-snail-repl-vterm-kill-line)
map))

;; Format regions and buffers via JuliaFormatter
;; Inspired by https://codeberg.org/FelipeLema/julia-formatter.el/

(defun julia-snail--format-text (txt)
(julia-snail--send-to-server
:JuliaSnail
(format "format_data(\"%s\")" txt)
:async nil))


(defun julia-snail-format-region (begin end)
"Format region delimited by BEGIN and END using JuliaFormatter.jl.
The code in the region must be syntactically valid Julia, otherwise no formatting will take place. "
(interactive "r")
(let* ((text-to-be-formatted
(buffer-substring-no-properties
begin end))
(ftext (julia-snail--format-text
text-to-be-formatted))
)

(if (eq :nothing ftext)
(message "Parsing error, formatting failed")
(progn
(delete-region begin end)
(insert ftext)
))
))


(defun julia-snail-format-buffer ()
"Format buffer using JuliaFormatter.jl.
The buffer must be syntactically valid Julia, otherwise no formatting will take place.
Point placement after reformatting is heuristic, since the code might have changed quite a bit. "

(interactive )
(let* ((old-point (point) ))
(julia-snail-format-region (point-min) (point-max))
(goto-char old-point)
)
)

;;; --- mode definitions

Expand Down

0 comments on commit 69cd68a

Please sign in to comment.