1

I'm trying to setup the F1 key for documentation within typescript mode only. Here is what I got so far.

(use-package tide
  :init
  ;; (add-hook 'before-save-hook 'tide-format-before-save)  ;; formats the buffer before saving
  (add-hook 'js2-mode-hook #'setup-tide-mode)  ;; JS Setup
  (add-hook 'typescript-mode-hook #'setup-tide-mode)
  :defer 5
  :config
  ;; TIDE setup
  (defun setup-tide-mode ()
    (interactive)
    (tide-setup)
    (flycheck-mode +1)
    (setq flycheck-check-syntax-automatically '(save mode-enabled))
    (eldoc-mode +1)
    ;; company is an optional dependency. You have to
    ;; install it separately via package-install
    (company-mode +1)
    (local-set-key [f1] 'tide-documentation-at-point))

  ;; aligns annotation to the right hand side
  (setq company-tooltip-align-annotations t)

  ;; Set tide-tsserver
  (setq tide-tsserver-executable "node_modules/typescript/bin/tsserver")

  ;; Add node to the path
  (setenv "PATH" (concat (getenv "PATH") ":~/n/bin"))
  (setq exec-path (append exec-path '("~/n/bin")))

  ;; format options
  (setq tide-format-options '(:insertSpaceAfterFunctionKeywordForAnonymousFunctions t :placeOpenBraceOnNewLineForFunctions nil))
  ;; see https://github.com/Microsoft/TypeScript/blob/cc58e2d7eb144f0b2ff89e6a6685fb4deaa24fde/src/server/protocol.d.ts#L421-473 for the full list available options
  )

(use-package web-mode
  :init
  ;; TSX setup
  (add-to-list 'auto-mode-alist '("\\.tsx\\'" . web-mode))
  (add-hook 'web-mode-hook
            (lambda ()
              (when (string-equal "tsx" (file-name-extension buffer-file-name))
                (setup-tide-mode))))

  ;; JSX Setup
  (add-to-list 'auto-mode-alist '("\\.jsx\\'" . web-mode))
  (add-hook 'web-mode-hook
            (lambda ()
              (when (string-equal "jsx" (file-name-extension buffer-file-name))
                (setup-tide-mode))))
  :defer 5)

If try the following in sketch it also doesn't work

(defun setup-tide-shortcuts ()
  (local-set-key [f1] 'tide-documentation-at-point))

(add-hook 'typescript-mode-hook #'setup-tide-shortcuts)

If I set the hook up using global it works

(global-set-key [f1] 'tide-documentation-at-point)

Problem here is that it is set to global so when I work on non-typescript files it asks me to start a tide server and I would like to setup F1 as the documentation key for all different languages.

1 Answer 1

1

You need to define the key in the mode-map for tide, which is called tide-mode-map. You can't do this until after you've loaded tide. The following should accomplish this:

(eval-after-load "tide"
  '(define-key tide-mode-map [F1] 'tide-documentation-at-point))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.