简体   繁体   English

如何将prettier-js.el整合到spacemacs中

[英]How to Integrate prettier-js.el into spacemacs

I'm trying to get prettier working in spacemacs. 我想在spacemacs工作更漂亮

I have (prettier-js :location (recipe :url "https://raw.githubusercontent.com/prettier/prettier/master/editors/emacs/prettier-js.el" :fetcher url)) which is somewhat working, but then in Messages I see 我有(prettier-js :location (recipe :url "https://raw.githubusercontent.com/prettier/prettier/master/editors/emacs/prettier-js.el" :fetcher url))这有点工作,但是然后在消息我看到

Contacting host: raw.githubusercontent.com:443
Wrote /Users/travis/.emacs.d/.cache/quelpa/build/prettier-js/prettier-js.el
File: /Users/travis/.emacs.d/.cache/quelpa/build/prettier-js/prettier-js.stamp
Error getting PACKAGE-DESC: (search-failed ;;; prettier-js.el ends here)
Cannot load prettier-js

I don't know enough emacs yet to know know what a PACKAGE-DESC does, or if I need it to get prettier to load. 我不知道有足够的emacs知道PACKAGE-DESC的功能,或者我是否需要它来加载更漂亮。

I'm trying to do this in a private layer 我试图在私有层中执行此操作

The docs say: 文档说:

Add this to your init 将其添加到您的init

(require 'prettier-js)
(add-hook 'js-mode-hook
          (lambda ()
            (add-hook 'before-save-hook 'prettier-before-save)))

I think I should have something like: 我想我应该有类似的东西:

(defun myJS/post-init-prettier-js ()
  "Initialize prettier-js"
  (use-package prettier-js)
  :defer t
  :init
  (progn
    (add-hook 'before-save-hook 'prettier-before-save)
    )
  )

in my layer 在我的图层中

As of the commit 9d2a108 Spacemacs comes with a layer that adds support for Prettier. 在提交9d2a108时, Spacemacs附带了一个增加对Prettier支持的图层。 You can use it today if you're using the develop branch of Spacemacs or if you're reading this in the future and are using version 0.300 or something more recent. 如果您正在使用Spacemacs的develop分支,或者如果您将来阅读此版本并使用0.300版本或更新版本,则可以立即使用它。

To use it simply add prettier as a layer in the list of layers specified by dotspacemacs-configuration-layers . 要使用它,只需在由dotspacemacs-configuration-layers指定的图层列表中添加prettier dotspacemacs-configuration-layers Additionally you should enable Prettier as a formatter for the layers for the languages in which you want to use Prettier. 此外,您应该启用Prettier作为要使用Prettier的语言的图层的格式化程序。 How to do this is documented in the specific layers. 具体层中记录了如何执行此操作。 For JavaScript you should add the following to your dotspacemacs/user-init . 对于JavaScript,您应该将以下内容添加到dotspacemacs/user-init

(setq javascript-fmt-tool 'prettier)

With the above configuration the JavaScript layer will user Prettier to format JavaScript files. 通过上述配置,JavaScript层将使用Prettier来格式化JavaScript文件。

prettier-js包现在在melpa上安装它在你的spacemacs文件中添加更漂亮的js到dotspacemacs-additional-packages。

There are a few steps that we need to perform in order to activate prettier in Spacemacs : 我们需要执行几个步骤才能在Spacemacs中激活prettier


1. Install prettier integration in Spacemacs: 1.在Spacemacs中安装prettier集成:

  • <SPC> <SPC> (press the spacebar key twice), this will trigger HELM allowing us to search for Emacs commands. <SPC> <SPC> (按空格键两次),这将触发HELM,允许我们搜索Emacs命令。

  • After pressing <SPC> <SPC> , type package-install in the HELM buffer and press <RET> (the Return/Enter key). <SPC> <SPC> ,在HELM缓冲区中键入package-install并按<RET> (Return / Enter键)。

  • A list of packages will appear inside the HELM Package Install buffer, type prettier-js in it and press <RET> . HELM包安装缓冲区内将显示包列表,在其中键入prettier-js并按<RET>


2. Install prettier in your system: 2.在您的系统中安装prettier

  • The integration doesn't do anything without prettier itself. 如果没有prettier自身,整合就不会做任何事情。

  • Assuming you already have node and npm installed, go to your terminal and type: npm install -g prettier and press enter. 假设您已经安装了nodenpm ,请转到终端并键入: npm install -g prettier并按Enter键。


3. (Optional) Setup automatic format on save: 3.(可选)在保存时设置自动格式:

  • Open your .spacemacs config file by pressing <SPC> <f> <e> <d> . <SPC> <f> <e> <d>打开.spacemacs配置文件。

  • Find the dotspacemacs/user-config section of it and type the following snippet inside of it: 找到它的dotspacemacs/user-config部分并在其中键入以下代码段:

    (defun dotspacemacs/user-config () (add-hook 'js2-mode-hook 'prettier-js-mode) (add-hook 'web-mode-hook 'prettier-js-mode) )

  • Save the settings' change by pressing <SPC> <f> <s> <SPC> <f> <s>保存设置的更改

  • Reload the saved settings by pressing <SPC> <f> <e> <R> <SPC> <f> <e> <R>重新加载保存的设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM