Skip to content

Commit

Permalink
Merge pull request #479 from psibi/playpen-improvements
Browse files Browse the repository at this point in the history
Rustic playground improvements
  • Loading branch information
psibi authored Dec 29, 2022
2 parents 6f53249 + 36993cd commit 4923724
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ rustic-rustfix.elc: rustic-cargo.elc
rustic-rustfmt.elc: rustic-cargo.elc
rustic-babel.elc: rustic-rustfmt.elc
rustic-lsp.elc: rustic-rustfmt.elc
rustic-playpen.elc:
rustic-playground.elc:
rustic-doc.elc:

## Without Cask
Expand Down
53 changes: 34 additions & 19 deletions rustic-playpen.el → rustic-playground.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; rustic-playpen.el --- Support for playpen -*- lexical-binding:t -*-
;;; rustic-playground.el --- Support for playground -*- lexical-binding:t -*-
;;; Commentary:
;;; Code:

Expand All @@ -7,22 +7,28 @@

;;; Options

(defcustom rustic-playpen-url-format "https://play.rust-lang.org/?code=%s"
"Format string to use when submitting code to the playpen."
(defcustom rustic-playground-url-format "https://play.rust-lang.org/?code=%s"
"Format string to use when submitting code to the playground."
:type 'string
:group 'rustic)

(defcustom rustic-shortener-url-format "https://is.gd/create.php?format=simple&url=%s"
"Format string to use for creating the shortened link of a playpen submission."
"Format string to use for creating the shortened link of a playground submission."
:type 'string
:group 'rustic)

(defcustom rustic-playground-enable-shortener t
"Enable shortend URL for playground links."
:type 'boolean
:safe #'booleanp
:group 'rustic)

;;; Commands

;;;###autoload
(defun rustic-playpen (begin end)
(defun rustic-playground (begin end)
"Create a shareable URL for the contents of the current region,
src-block or buffer on the Rust playpen."
src-block or buffer on the Rust playground."
(interactive "r")
(let (data)
(cond
Expand All @@ -33,15 +39,15 @@ src-block or buffer on the Rust playpen."
(t
(setq data (buffer-substring (point-min) (point-max)))))
(let* ((escaped-data (url-hexify-string data))
(escaped-playpen-url (url-hexify-string
(format rustic-playpen-url-format
escaped-data))))
(if (> (length escaped-playpen-url) 5000)
(error "encoded playpen data exceeds 5000 character limit (length %s)"
(length escaped-playpen-url))
(let ((shortener-url (format rustic-shortener-url-format escaped-playpen-url))
(playground-url (format rustic-playground-url-format escaped-data))
(escaped-playground-url (url-hexify-string playground-url)))
(if (> (length escaped-playground-url) 5000)
(error "Encoded playground data exceeds 5000 character limit (length %s)"
(length escaped-playground-url))
(let ((shortener-url (format rustic-shortener-url-format escaped-playground-url))
(url-request-method "POST"))
(url-retrieve shortener-url
(if rustic-playground-enable-shortener
(url-retrieve shortener-url
(lambda (state)
;; filter out the headers etc. included at the
;; start of the buffer: the relevant text
Expand All @@ -52,10 +58,19 @@ src-block or buffer on the Rust playpen."
(err (plist-get state :error)))
(kill-buffer)
(if err
(error "failed to shorten playpen url: %s" last-line)
(let ((URL (read-from-minibuffer "Playpen URL: " last-line)))
(browse-url URL)))))))))))
(error "Failed to shorten playground url: %s" last-line)
(let ((URL (read-from-minibuffer "Playground URL: " last-line)))
(browse-url URL))))))
(browse-url playground-url)))))))


(defun rustic-playground-buffer ()
"Create a shareable URL for the contents of the buffer on the Rust playground."
(interactive)
(rustic-playground (point-min) (point-max)))

(defalias 'rustic-playpen #'rustic-playground)

;;; _
(provide 'rustic-playpen)
;;; rustic-playpen.el ends here
(provide 'rustic-playground)
;;; rustic-playground.el ends here
2 changes: 1 addition & 1 deletion rustic.el
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ This variable might soon be remove again.")
(require 'rustic-racer)
(require 'rustic-rustfmt)
(require 'rustic-rustfix)
(require 'rustic-playpen)
(require 'rustic-playground)
(require 'rustic-lsp)
(require 'rustic-expand)
(require 'rustic-spellcheck)
Expand Down

0 comments on commit 4923724

Please sign in to comment.