snippets icon indicating copy to clipboard operation
snippets copied to clipboard

Problem defining snippets

Open recifx opened this issue 6 years ago • 4 comments

Snippets defined in .doom.d/snippets seem to work fine. However snippets defined using the yas-define-snippets function don't seem to be working properly. A minimum example file loaded from .doom.d/config.el is listed below to indicate how I am using it.

In python mode some snippets like np expands fine, but many of the other snippets defined do not expand, or trigger company completion.

In asy-mode only the snippets defined in the doom.d/snippets/asy-mode subdirectory seem to be recognized, even though a file with an asy extension opens correctly in asy-mode.

I prefer to use the yas-define-snippets function to collect together multiple snippets in one file and I have used this approach before. Would this be possible with doom-emacs? Thanks.

;;; ~/.doom.d/+yas.el -*- lexical-binding: t; -*-

(load! "lisp/asy-mode.el" )
(autoload 'asy-mode "asy-mode.el" "Asymptote major mode." t)
(add-to-list 'auto-mode-alist '("\\.asy$" . asy-mode))

(after! snippets
  (yas-define-snippets
   'python-mode
   '(
     ("pd" "import pandas as pd\n$0" "pandas")
     ("np" "import numpy as np\n$0" "numpy")
     ("sp" "import scipy as sp\n$0" "scipy")))
  (yas-define-snippets
   'asy-mode
   '(
     ("gm" "import geometry;\n$0" "i_geometry")
     ("PI" "path ${1:P} = ${2:pA}--${3:pB};\n$0" "pathI"))))

recifx avatar Dec 11 '19 16:12 recifx

after! takes the name of a package. There is no package named snippets. If you replace it with yasnippet, then it should work.

hlissner avatar Dec 12 '19 03:12 hlissner

I changed the line (after! snippets to (after! yasnippet but I still have the same issue. M-x menu-bar-open YASnippet>pyhton-mode for example seems to show only the snippets in the python-mode subdirectories of .doom.d/snippets and the doom-snippets repo.

I also had a couple of other questions:

  1. Is it possible to selectively disable snippets defined in the doom-snippets repo? I have a snippet with keyword fr for \frac{$1}{$2} in LaTeX mode, but fr<TAB> shows a menu with the selection from the doom-snippets repo also displayed.

  2. Can the precedence for <TAB> be yas-expand then company-complete if no snippet expansion is found? Right now the company completion popup appears on pressing <TAB>, even if the text before corresponds to a snippet keyword, and snippet expansion is often only possible if I can press <TAB> before the company delay. If that is not possible, how should I disable company mode? I tried (remove-hook 'LaTeX-mode-hook #'company!) but that seems to work only partially.

Thanks for your help.

recifx avatar Dec 12 '19 10:12 recifx

Can the precedence for <TAB> be yas-expand then company-complete if no snippet expansion is found?

This is already the case, unless you've disabled :config (default +bindings) or remapped TAB yourself.

Is it possible to selectively disable snippets defined in the doom-snippets repo?

This should also already be the case, unless you've disabled :editor snippets OR you've cloned doom-snippets to ~/.doom.d/snippets. Otherwise, if you have a private snippet in ~/.doom.d/snippets with the same name as a built-in snippet, yours will shadow the built-in one (it won't prompt about it, it'll just use yours, unless you have duplicates in your private snippet library).

hlissner avatar Dec 12 '19 20:12 hlissner

I fixed most of the issues by disabling company-mode for tex files, as I use yasnippets and cdlatex in any case for most of the expansions, and the shadowing issue by duplicating the file from the doom repo and giving it a different trigger-key. However, snippets defined by

(after! yasnippet
  (yas-define-snippets
   'asy-mode
   '(
     ("gm" "import geometry;\n$0" "i_geometry")
     ("PI" "path ${1:P} = ${2:pA}--${3:pB};\n$0" "pathI"))))

are not recognized in asy mode, although they are when converted to individual snippets in the doom.d/snippets/asy-mode directory. As yas-define-snippets is not widely used and probably giving rise to an obscure error, I will look into converting the snippet definitions to individual files.

recifx avatar Dec 15 '19 22:12 recifx