Skip to content

Commit

Permalink
[lsp-ui-peek] Generalize lsp-ui-peek-force-fontify to lsp-ui-peek-fon…
Browse files Browse the repository at this point in the history
…tify and add another choice 'on-demand (#148)

'on-demand fontifies the chunk when the selection changes.
  • Loading branch information
MaskRay authored Jun 19, 2018
1 parent ee259fd commit bd5970e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lsp-ui-peek.el
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@
:type 'integer
:group 'lsp-ui-peek)

(defcustom lsp-ui-peek-force-fontify t
"Force to fontify chunks of code (use semantics colors).
WARNING: This can heavily slow the processing when `lsp-ui-peek-expand-function'
(defcustom lsp-ui-peek-fontify 'on-demand
"Whether to fontify chunks of code (use semantics colors).
WARNING: 'always can heavily slow the processing when `lsp-ui-peek-expand-function'
expands more than 1 file. It is recommended to keeps the default value of
`lsp-ui-peek-expand-function' when this variable is non-nil."
:type 'boolean
`lsp-ui-peek-expand-function' when this variable is 'always."
:type '(choice (const :tag "Never" never)
(const :tag "On demand" on-demand)
(const :tag "Always" always))
:group 'lsp-ui-peek)

(defcustom lsp-ui-peek-always-show nil
Expand Down Expand Up @@ -138,8 +140,8 @@ The function takes one parameter: a list of cons where the car is the
filename and the cdr is the number of references in that file.
It should returns a list of filenames to expand.
WARNING: If you change this variable and expand more than 1 file, it is
recommended to set `lsp-ui-peek-force-fontify' to nil, otherwise it will cause
performances issues.")
recommended to set `lsp-ui-peek-fontify' to 'never or 'on-demand, otherwise it
will cause performances issues.")

(defvar-local lsp-ui-peek--overlay nil)
(defvar-local lsp-ui-peek--list nil)
Expand Down Expand Up @@ -320,13 +322,26 @@ XREFS is a list of references/definitions."
(append list (-repeat (- min-len len) ""))
list)))

(defun lsp-ui-peek--render (major string)
(with-temp-buffer
(insert string)
(delay-mode-hooks
(let ((inhibit-message t))
(funcall major))
(ignore-errors
(font-lock-ensure)))
(buffer-string)))

(defun lsp-ui-peek--peek ()
"Show reference's chunk of code."
(-let* ((xref (lsp-ui-peek--get-selection))
((&plist :file file :chunk chunk) (or xref lsp-ui-peek--last-xref))
(header (concat " " (lsp-ui--workspace-path file) "\n"))
(header2 (format " %s %s" lsp-ui-peek--size-list (symbol-name lsp-ui-peek--kind)))
(ref-view (--> chunk
(if (eq lsp-ui-peek-fontify 'on-demand)
(lsp-ui-peek--render major-mode it)
chunk)
(subst-char-in-string ?\t ?\s it)
(concat header it)
(split-string it "\n")))
Expand Down Expand Up @@ -636,7 +651,7 @@ LOCATION can be either a LSP Location or SymbolInformation."
:len (- end start))))

(defun lsp-ui-peek--fontify-buffer (filename)
(when lsp-ui-peek-force-fontify
(when (eq lsp-ui-peek-fontify 'always)
(unless buffer-file-name
(make-local-variable 'delay-mode-hooks)
(let ((buffer-file-name filename)
Expand Down
12 changes: 12 additions & 0 deletions lsp-ui.el
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@
(require 'lsp-ui-imenu)
(require 'lsp-ui-doc)

(defun lsp-ui-peek--render (major string)
(with-temp-buffer
(insert string)
(delay-mode-hooks
(let ((inhibit-message t))
(funcall major))
(ignore-errors
(font-lock-ensure)))
(buffer-string))
)


(defun lsp-ui--workspace-path (path)
"Return the PATH relative to the workspace.
If the PATH is not in the workspace, it returns the original PATH."
Expand Down

0 comments on commit bd5970e

Please sign in to comment.