Giter VIP home page Giter VIP logo

Comments (9)

watofundefined avatar watofundefined commented on May 16, 2024 3

If anyone still cares >= 2020, I found the shell-command-on-region to be handy (https://www.masteringemacs.org/article/executing-shell-commands-emacs).

Here's a short and naive implementation of formatting an Org-mode code block (my Emacs Fu isn't great, but it shouldn't be a lot of work to change it to format current selection).

(defun format-js-org-code-block ()
  "Use Prettier to format a JavaScript Org-mode code block."
  (interactive)
  (shell-command-on-region
   (code-block-start)
   (code-block-end)
   "prettier --parser babel"
   (current-buffer)
   t))

(defun code-block-start ()
  "Find the beginning of code block."
  (goto-char (search-backward "#+BEGIN_SRC"))
  (next-line)
  (beginning-of-line)
  (point))

(defun code-block-end ()
  "Find the end of code block."
  (goto-char (search-forward "#+END_SRC"))
  (previous-line)
  (end-of-line)
  (point))

from prettier-emacs.

rcoedo avatar rcoedo commented on May 16, 2024 2

We don't support it because in most cases selections are not valid AST. If you really want this you could use or copy the code from this branch, which has the prettier-js-prettify-region function.

from prettier-emacs.

nickmccurdy avatar nickmccurdy commented on May 16, 2024 1

Since prettier can take string inputs instead of files, we could try to limit formatting to the region if it's defined and cancel formatting if the region doesn't have a valid AST.

from prettier-emacs.

nickmccurdy avatar nickmccurdy commented on May 16, 2024

Some Emacs packages allow automatic filtering by region when the mark is active. However, because Prettier is a full AST parser, I'm not sure it would be possible for Prettier itself for Prettier to format only part of a file because a region may not always have a completely valid JS AST, it might only be a child of a valid AST.

from prettier-emacs.

it6 avatar it6 commented on May 16, 2024

I agree, but most of the time I change small portion of a file and don't want to reformat entire file. If there is a selection which would form a valid AST, please consider letting the user format only the selection. May be a flag to enable this behavior.

from prettier-emacs.

it6 avatar it6 commented on May 16, 2024

Please consider a fix for this, I consistently run into this limitation.

from prettier-emacs.

rtm avatar rtm commented on May 16, 2024

There are scenarios related to literate programming where this would be useful. If the fragment cannot be parsed, then could you not just return an error, which is what it does anyway now when the file is not parseable.

from prettier-emacs.

rodrigues avatar rodrigues commented on May 16, 2024

I seems that prettier range options could be used? https://prettier.io/docs/en/options.html#range

from prettier-emacs.

jackschu avatar jackschu commented on May 16, 2024

Thanks @watofundefined here's what I ended up putting together for future onlookers :)

(defun prettier-region (posBegin posEnd)
  "Print number of words and chars in region."
  (interactive "r")
  (message "Formatting …")
  (let* (
         (old-prettier-args prettier-js-args)
         (prettier-js-args (append old-prettier-args 
                                                 (list "--range-start" (number-to-string posBegin) 
                                                        "--range-end" (number-to-string posEnd))))
         )
    (prettier-js)
    )
  )

from prettier-emacs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.