@@ -103,7 +103,7 @@ When non-nil, contains the error message to display.")
103103 (goto-char (point-min ))
104104 (forward-line (1- line)))
105105
106- (defun prettier-js--apply-rcs-patch (patch-buffer )
106+ (defun prettier-js--apply-rcs-patch (patch-buffer &optional start )
107107 " Apply an RCS-formatted diff from PATCH-BUFFER to the current buffer."
108108 (let ((target-buffer (current-buffer ))
109109 ; ; Relative offset between buffer line numbers and line numbers
@@ -116,7 +116,7 @@ When non-nil, contains the error message to display.")
116116 ; ; Appending lines decrements the offset (possibly making it
117117 ; ; negative), deleting lines increments it. This order
118118 ; ; simplifies the forward-line invocations.
119- (line-offset 0 ))
119+ (line-offset ( if start ( * ( - ( line-number-at-pos start) 1 ) -1 ) 0 ) ))
120120 (save-excursion
121121 (with-current-buffer patch-buffer
122122 (goto-char (point-min ))
@@ -249,17 +249,17 @@ Returns the exit code from prettier."
249249 prettier-cmd bufferfile (list (list :file outputfile) errorfile)
250250 nil (append prettier-js-args width-args (list " --stdin-filepath" file-path)))))
251251
252- (defun prettier-js--call-diff (outputfile patchbuf )
252+ (defun prettier-js--call-diff (outputfile patchbuf &optional start end )
253253 " Call diff command to generate patch between current buffer and OUTPUTFILE.
254254PATCHBUF is the buffer where the diff output will be written."
255- (let ((diff-cmd (prettier-js--get-diff-command)))
256- (call-process-region (point-min ) (point-max ) diff-cmd nil patchbuf nil
255+ (let ((start-point (or start (point-min )))
256+ (end-point (or end (point-max )))
257+ (diff-cmd (prettier-js--get-diff-command)))
258+ (call-process-region start-point end-point diff-cmd nil patchbuf nil
257259 " -n" " --strip-trailing-cr" " -" outputfile)))
258260
259- ;;;### autoload
260- (defun prettier-js ()
261- " Format the current buffer according to the prettier tool."
262- (interactive )
261+ (defun prettier-js--prettify (&optional start end )
262+ " Format content in the current buffer from START to END."
263263 (let* ((file-path
264264 (or (prettier-js--file-path)
265265 (user-error " Buffer `%s' is not visiting a file" (buffer-name ))))
@@ -280,7 +280,7 @@ PATCHBUF is the buffer where the diff output will be written."
280280 (unwind-protect
281281 (save-restriction
282282 (widen )
283- (write-region nil nil bufferfile)
283+ (write-region start end bufferfile)
284284 (if errbuf
285285 (with-current-buffer errbuf
286286 (setq buffer-read-only nil )
@@ -294,9 +294,9 @@ PATCHBUF is the buffer where the diff output will be written."
294294 ; ; 1 - The files are different (differences found).
295295 ; ; 2 - Trouble occurred (e.g. invalid options).
296296 ; ; 0/1 = success, 2 = problem
297- (if (<= (prettier-js--call-diff outputfile patchbuf) 1 )
297+ (if (<= (prettier-js--call-diff outputfile patchbuf start end ) 1 )
298298 (progn
299- (prettier-js--apply-rcs-patch patchbuf)
299+ (prettier-js--apply-rcs-patch patchbuf start )
300300 (message " Applied prettier with args `%s' " prettier-js-args)
301301 (if errbuf (prettier-js--kill-error-buffer errbuf)))
302302 (setq prettier-js-error-state " Diff command had an issue" )
@@ -309,6 +309,22 @@ PATCHBUF is the buffer where the diff output will be written."
309309 (delete-file bufferfile)
310310 (delete-file outputfile))))
311311
312+ ;;;### autoload
313+ (defalias 'prettier-js 'prettier-js-prettify )
314+
315+ ;;;### autoload
316+ (defun prettier-js-prettify ()
317+ " Format the current buffer according to the prettier tool."
318+ (interactive )
319+ (prettier-js--prettify))
320+
321+ ;;;### autoload
322+ (defun prettier-js-prettify-region ()
323+ " Format the current region according to the prettier tool."
324+ (interactive )
325+ (when (region-active-p )
326+ (prettier-js--prettify (region-beginning ) (region-end ))))
327+
312328(defvar prettier-js-mode-menu-map
313329 (let ((map (make-sparse-keymap " Prettier" )))
314330 map)
@@ -344,7 +360,8 @@ Adds an error item at the top of the menu if there is an error state."
344360(easy-menu-define prettier-js-mode-menu prettier-js-mode-menu-map
345361 " Menu for Prettier mode"
346362 '(" Prettier" :filter prettier-js--menu-filter
347- [" Format buffer" prettier-js t]
363+ [" Format buffer" prettier-js-prettify t]
364+ [" Format region" prettier-js-prettify-region (region-active-p )]
348365 " ---"
349366 [" Turn off minor mode" prettier-js-mode :visible prettier-js-mode]
350367 [" Help for minor mode" (describe-function 'prettier-js-mode ) t]))
@@ -356,8 +373,8 @@ Adds an error item at the top of the menu if there is an error state."
356373 :global nil
357374 :keymap prettier-js-mode-menu-map
358375 (if prettier-js-mode
359- (add-hook 'before-save-hook 'prettier-js nil 'local )
360- (remove-hook 'before-save-hook 'prettier-js 'local )))
376+ (add-hook 'before-save-hook 'prettier-js-prettify nil 'local )
377+ (remove-hook 'before-save-hook 'prettier-js-prettify 'local )))
361378
362379(provide 'prettier-js )
363380; ;; prettier-js.el ends here
0 commit comments