|
| 1 | +;;; ocaml-eglot-type-enclosing.el --- Type Enclosing feature -*- coding: utf-8; lexical-binding: t -*- |
| 2 | + |
| 3 | +;; Copyright (C) 2024-2025 Xavier Van de Woestyne |
| 4 | +;; Licensed under the MIT license. |
| 5 | + |
| 6 | +;; Author: Xavier Van de Woestyne <[email protected]> |
| 7 | +;; Created: 10 January 2025 |
| 8 | +;; SPDX-License-Identifier: MIT |
| 9 | + |
| 10 | +;;; Commentary: |
| 11 | + |
| 12 | +;; Plumbing needed to implement the primitives related to type |
| 13 | +;; enclosing commands. |
| 14 | + |
| 15 | +;;; Code: |
| 16 | + |
| 17 | +(require 'cl-lib) |
| 18 | +(require 'ocaml-eglot-util) |
| 19 | +(require 'ocaml-eglot-req) |
| 20 | + |
| 21 | +;;; Customizable variables |
| 22 | + |
| 23 | +(defcustom ocaml-eglot-type-buffer-name "*ocaml-eglot-types*" |
| 24 | + "The name of the buffer storing types." |
| 25 | + :group 'ocaml-eglot |
| 26 | + :type 'string) |
| 27 | + |
| 28 | +;;; Internal variables |
| 29 | + |
| 30 | +(defvar-local ocaml-eglot-type-enclosing-types nil |
| 31 | + "Current list of enclosings related to types.") |
| 32 | + |
| 33 | +(defvar-local ocaml-eglot-type-enclosing-current-type nil |
| 34 | + "Current type for the current enclosing.") |
| 35 | + |
| 36 | +(defvar-local ocaml-eglot-type-enclosing-offset 0 |
| 37 | + "The offset of the requested enclosings.") |
| 38 | + |
| 39 | +(defvar-local ocaml-eglot-type-enclosing-verbosity 0 |
| 40 | + "The verbosity of the current enclosing request.") |
| 41 | + |
| 42 | +;;; Key mapping for type enclosing |
| 43 | + |
| 44 | +(defvar ocaml-eglot-type-enclosing-map |
| 45 | + (let ((keymap (make-sparse-keymap))) |
| 46 | + (define-key keymap (kbd "C-<up>") #'ocaml-eglot-type-enclosing-grow) |
| 47 | + (define-key keymap (kbd "C-<down>") #'ocaml-eglot-type-enclosing-shrink) |
| 48 | + (define-key keymap (kbd "C-w") #'ocaml-eglot-type-enclosing-copy) |
| 49 | + (define-key keymap (kbd "C-c C-t") #'ocaml-eglot-type-enclosing-increase-verbosity) |
| 50 | + (define-key keymap (kbd "C-<right>") #'ocaml-eglot-type-enclosing-increase-verbosity) |
| 51 | + (define-key keymap (kbd "C-<left>") #'ocaml-eglot-type-enclosing-decrease-verbosity) |
| 52 | + keymap) |
| 53 | + "Keymap for OCaml-eglot's type enclosing transient mode.") |
| 54 | + |
| 55 | +;;; Internal functions |
| 56 | + |
| 57 | +(defun ocaml-eglot-type-enclosing-copy () |
| 58 | + "Copy the type of the current enclosing to the Kill-ring." |
| 59 | + (interactive) |
| 60 | + (when ocaml-eglot-type-enclosing-current-type |
| 61 | + (eglot--message "Copied `%s' to kill-ring" |
| 62 | + ocaml-eglot-type-enclosing-current-type) |
| 63 | + (kill-new ocaml-eglot-type-enclosing-current-type))) |
| 64 | + |
| 65 | +(defun ocaml-eglot-type-enclosing--with-fixed-offset () |
| 66 | + "Compute the type enclosing for a dedicated offset." |
| 67 | + (let* ((verbosity ocaml-eglot-type-enclosing-verbosity) |
| 68 | + (index ocaml-eglot-type-enclosing-offset) |
| 69 | + (at (ocaml-eglot-util--current-position-or-range)) |
| 70 | + (result (ocaml-eglot-req--type-enclosings at index verbosity)) |
| 71 | + (type (cl-getf result :type))) |
| 72 | + (setq ocaml-eglot-type-enclosing-current-type type) |
| 73 | + (ocaml-eglot-type-enclosing--display type t))) |
| 74 | + |
| 75 | +(defun ocaml-eglot-type-enclosing-increase-verbosity () |
| 76 | + "Increase the verbosity of the current request." |
| 77 | + (interactive) |
| 78 | + (setq ocaml-eglot-type-enclosing-verbosity |
| 79 | + (1+ ocaml-eglot-type-enclosing-verbosity)) |
| 80 | + (ocaml-eglot-type-enclosing--with-fixed-offset)) |
| 81 | + |
| 82 | +(defun ocaml-eglot-type-enclosing-decrease-verbosity () |
| 83 | + "Decrease the verbosity of the current request." |
| 84 | + (interactive) |
| 85 | + (when (> ocaml-eglot-type-enclosing-verbosity 0) |
| 86 | + (setq ocaml-eglot-type-enclosing-verbosity |
| 87 | + (1- ocaml-eglot-type-enclosing-verbosity))) |
| 88 | + (ocaml-eglot-type-enclosing--with-fixed-offset)) |
| 89 | + |
| 90 | +(defun ocaml-eglot-type-enclosing-grow () |
| 91 | + "Growing of the type enclosing." |
| 92 | + (interactive) |
| 93 | + (when ocaml-eglot-type-enclosing-types |
| 94 | + (setq ocaml-eglot-type-enclosing-offset |
| 95 | + (mod (1+ ocaml-eglot-type-enclosing-offset) |
| 96 | + (length ocaml-eglot-type-enclosing-types))) |
| 97 | + (ocaml-eglot-type-enclosing--with-fixed-offset))) |
| 98 | + |
| 99 | +(defun ocaml-eglot-type-enclosing-shrink () |
| 100 | + "Display the type enclosing of a smaller enclosing if possible." |
| 101 | + (interactive) |
| 102 | + (when ocaml-eglot-type-enclosing-types |
| 103 | + (setq ocaml-eglot-type-enclosing-offset |
| 104 | + (mod (1- ocaml-eglot-type-enclosing-offset) |
| 105 | + (length ocaml-eglot-type-enclosing-types))) |
| 106 | + (ocaml-eglot-type-enclosing--with-fixed-offset))) |
| 107 | + |
| 108 | +(defun ocaml-eglot-type-enclosing--type-buffer (type-expr) |
| 109 | + "Create buffer with content TYPE-EXPR of the enclosing type buffer." |
| 110 | + ; We store the current major mode to be used in the type buffer for |
| 111 | + ; syntax highlighting. |
| 112 | + (let ((curr-dir default-directory) |
| 113 | + (current-major-mode major-mode)) |
| 114 | + (with-current-buffer (get-buffer-create ocaml-eglot-type-buffer-name) |
| 115 | + (funcall current-major-mode) |
| 116 | + (read-only-mode 0) |
| 117 | + (erase-buffer) |
| 118 | + (insert type-expr) |
| 119 | + (goto-char (point-min)) |
| 120 | + (read-only-mode 1) |
| 121 | + (setq default-directory curr-dir)))) |
| 122 | + |
| 123 | +(defun ocaml-eglot-type-enclosing--display (type-expr &optional current) |
| 124 | + "Display the type-enclosing for TYPE-EXPR in a dedicated buffer. |
| 125 | +If CURRENT is set, the range of the enclosing will be highlighted." |
| 126 | + (ocaml-eglot-type-enclosing--type-buffer type-expr) |
| 127 | + (if (ocaml-eglot-util--text-less-than type-expr 8) |
| 128 | + (message "%s" (with-current-buffer ocaml-eglot-type-buffer-name |
| 129 | + (font-lock-fontify-region (point-min) (point-max)) |
| 130 | + (buffer-string))) |
| 131 | + (display-buffer ocaml-eglot-type-buffer-name)) |
| 132 | + (when (and current (> (length ocaml-eglot-type-enclosing-types) 0)) |
| 133 | + (let ((current (aref ocaml-eglot-type-enclosing-types |
| 134 | + ocaml-eglot-type-enclosing-offset))) |
| 135 | + (ocaml-eglot-util--highlight-range current |
| 136 | + 'ocaml-eglot-highlight-region-face)))) |
| 137 | + |
| 138 | +(defun ocaml-eglot-type-enclosing--reset () |
| 139 | + "Reset local variables defined by the enclosing query." |
| 140 | + (setq ocaml-eglot-type-enclosing-current-type nil) |
| 141 | + (setq ocaml-eglot-type-enclosing-verbosity 0) |
| 142 | + (setq ocaml-eglot-type-enclosing-types nil) |
| 143 | + (setq ocaml-eglot-type-enclosing-offset 0)) |
| 144 | + |
| 145 | +(defun ocaml-eglot-type-enclosing--call () |
| 146 | + "Print the type of the expression under point." |
| 147 | + (ocaml-eglot-type-enclosing--reset) |
| 148 | + (let* ((verbosity ocaml-eglot-type-enclosing-verbosity) |
| 149 | + (index ocaml-eglot-type-enclosing-offset) |
| 150 | + (at (ocaml-eglot-util--current-position-or-range)) |
| 151 | + (result (ocaml-eglot-req--type-enclosings at index verbosity)) |
| 152 | + (type (cl-getf result :type)) |
| 153 | + (enclosings (cl-getf result :enclosings))) |
| 154 | + (setq ocaml-eglot-type-enclosing-types enclosings) |
| 155 | + (setq ocaml-eglot-type-enclosing-current-type type) |
| 156 | + (ocaml-eglot-type-enclosing--display type t) |
| 157 | + (set-transient-map ocaml-eglot-type-enclosing-map t |
| 158 | + 'ocaml-eglot-type-enclosing--reset))) |
| 159 | + |
| 160 | +(provide 'ocaml-eglot-type-enclosing) |
| 161 | +;;; ocaml-eglot-type-enclosing.el ends here |
0 commit comments