~mgmarlow/deno-ts-mode

263481eda543186b871f740f711a856afaae9d26 — mgmarlow 1 year, 21 days ago 7429ad5 compile-commands
Add task automation
2 files changed, 53 insertions(+), 4 deletions(-)

M README.md
M deno-ts-mode.el
M README.md => README.md +20 -1
@@ 2,6 2,13 @@

A major mode for Deno, based on `typescript-ts-mode`.

## Features

- Syntax highlighting (based on `typescript-ts-mode`)
- Task automation
- Eglot configuration
- TypeScript file extension helpers for `auto-mode`

## Installation

Requires Emacs 29+ with tree-sitter installed.


@@ 38,7 45,19 @@ Project](https://www.gnu.org/software/emacs/manual/html_node/emacs/Projects.html
speaking, projects are only detectable if they are under version
control.

## Eglot usage example
## Task automation

If your project contains a [deno configuration
file](https://deno.land/manual@v1.36.2/getting_started/configuration_file)
you can run tasks directly from Emacs.

Run a task:

```
M-x deno-ts-run-task
```

## Eglot setup example

```elisp
(use-package deno-ts-mode

M deno-ts-mode.el => deno-ts-mode.el +33 -3
@@ 35,17 35,30 @@
(require 'eglot)
(require 'project)
(require 'typescript-ts-mode) ; Make sure to load auto-mode-alist here first
(require 'json)

(defgroup deno-ts nil
  "Major mode for Deno."
  :link '(url-link "https://git.sr.ht/~mgmarlow/deno-ts-mode")
  :group 'languages)

(defun deno-ts-project-p ()
  "Return t if `project-current' is a Deno project."
(defcustom deno-ts-bin "deno"
  "Path to deno executable."
  :type 'string
  :group 'deno)

(defun deno-ts--project-config ()
  "Return the filepath of the current project's deno config.

Return nil if no project or config is found."
  (when-let* ((project (project-current))
              (p-root (project-root project)))
    (file-exists-p (concat p-root "deno.json"))))
    (concat p-root "deno.json")))

(defun deno-ts-project-p ()
  "Return t if `project-current' is a Deno project."
  (when-let ((project-config (deno-ts--project-config)))
    (file-exists-p project-config)))

;; https://deno.land/manual@v1.36.1/getting_started/setup_your_environment#eglot
(defun deno-ts-setup-eglot ()


@@ 53,6 66,20 @@
  (add-to-list 'eglot-server-programs
               '(deno-ts-mode . ("deno" "lsp" :initializationOptions (:enable t :lint t)))))

(defun deno-ts--project-tasks ()
  "List tasks from the current project's deno config."
  (let ((p-config (deno-ts--project-config)))
    (unless p-config
      (error "No project deno.json file found"))
    (alist-get 'tasks (json-read-file p-config))))

(defun deno-ts-run-task ()
  "Execute a deno task from the current project's deno config."
  (interactive)
  (let ((tasks (mapcar 'car (deno-ts--project-tasks))))
    (compile (format "deno task %s"
                     (completing-read "Run task: " tasks nil t)))))

(define-derived-mode deno-ts-mode
  typescript-ts-mode "Deno"
  "Major mode for Deno."


@@ 77,5 104,8 @@ determined by `deno-project-p') this function will fallback to
  (add-to-list 'auto-mode-alist '("\\.ts\\'" . deno-ts--ts-auto-mode))
  (add-to-list 'auto-mode-alist '("\\.tsx\\'" . deno-ts--tsx-auto-mode)))

;; Required for Deno's color output.
(add-hook 'compilation-filter-hook 'ansi-color-compilation-filter)

(provide 'deno-ts-mode)
;;; deno-ts-mode.el ends here