Skip to content
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

Add a simple markdown formatter #872

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ let the buffer grow forever."
"If non-nil, activate Eglot in cross-referenced non-project files."
:type 'boolean)

(defcustom eglot-markup-formatter 'gfm-view-mode
"Major mode to format markdown texts.
The third-party markdown-mode works for almost all possible input
texts. Eglot's simple, built-in formatter usually provides
acceptable output for practical cases."
:type '(choice (const :tag "markdown-mode" gfm-view-mode)
(const :tag "Eglot's rudimentary formatter"
eglot-simple-markdown-mode)
(const :tag "No formatter: view raw text"
text-mode)
(const :tag "(Prefer plain-text)" nil)
(function :tag "Other")))

(defvar eglot-withhold-process-id nil
"If non-nil, Eglot will not send the Emacs process id to the language server.
This can be useful when using docker to run a language server.")
Expand Down Expand Up @@ -669,7 +682,7 @@ treated as in `eglot-dbind'."
:contextSupport t)
:hover (list :dynamicRegistration :json-false
:contentFormat
(if (fboundp 'gfm-view-mode)
(if (fboundp eglot-markup-formatter)
["markdown" "plaintext"]
["plaintext"]))
:signatureHelp (list :dynamicRegistration :json-false
Expand Down Expand Up @@ -1420,13 +1433,24 @@ Doubles as an indicator of snippet support."
(symbol-value 'yas-minor-mode)
'yas-expand-snippet))

(define-derived-mode eglot-simple-markdown-mode org-mode "md"
(goto-char (point-min))
(while (re-search-forward "^```\\(.*\\)$" nil t)
(replace-match (concat "#+BEGIN_SRC"
(if (equal "" (match-string 1))
""
(concat " " (match-string 1)))))
(when (re-search-forward "^```.*$" nil t)
(replace-match "#+END_SRC")))
(view-mode))

(defun eglot--format-markup (markup)
"Format MARKUP according to LSP's spec."
(pcase-let ((`(,string ,mode)
(if (stringp markup) (list markup 'gfm-view-mode)
(if (stringp markup) (list markup eglot-markup-formatter)
(list (plist-get markup :value)
(pcase (plist-get markup :kind)
("markdown" 'gfm-view-mode)
("markdown" eglot-markup-formatter)
("plaintext" 'text-mode)
(_ major-mode))))))
(with-temp-buffer
Expand Down