@@ -1332,29 +1332,49 @@ This is written mainly to be used as `end-of-defun-function' for Rust."
1332
1332
; ; Formatting using rustfmt
1333
1333
(defun rust--format-call (buf )
1334
1334
" Format BUF using rustfmt."
1335
- (with-current-buffer (get-buffer-create " *rustfmt*" )
1336
- (erase-buffer )
1337
- (insert-buffer-substring buf)
1338
- (let* ((tmpf (make-temp-file " rustfmt" ))
1339
- (ret (call-process-region (point-min ) (point-max ) rust-rustfmt-bin
1340
- t `(t , tmpf ) nil )))
1341
- (unwind-protect
1335
+ (let ((fmt-buffer (get-buffer-create " *rustfmt*" )))
1336
+ (with-current-buffer fmt-buffer
1337
+ (let ((buffer-read-only nil ))
1338
+ (erase-buffer )
1339
+ (insert-buffer-substring buf)
1340
+ (let ((ret (shell-command-on-region (point-min ) (point-max )
1341
+ rust-rustfmt-bin fmt-buffer
1342
+ nil nil t )))
1342
1343
(cond
1343
1344
((zerop ret)
1344
1345
(if (not (string= (buffer-string )
1345
1346
(with-current-buffer buf (buffer-string ))))
1346
1347
(copy-to-buffer buf (point-min ) (point-max )))
1347
1348
(kill-buffer ))
1348
1349
((= ret 3 )
1349
- (if (not (string= (buffer-string )
1350
- (with-current-buffer buf (buffer-string ))))
1351
- (copy-to-buffer buf (point-min ) (point-max )))
1352
- (erase-buffer )
1353
- (insert-file-contents tmpf)
1354
- (error " Rustfmt could not format some lines, see *rustfmt* buffer for details " ))
1350
+ (let ((path (buffer-file-name buf))
1351
+ (line-error " error: line exceeded maximum width[[:ascii:]]+" )
1352
+ buf-string)
1353
+ (copy-to-buffer buf (point-min ) (point-max ))
1354
+ (with-current-buffer buf
1355
+ (while (re-search-forward line-error nil t )
1356
+ (replace-match " " ))
1357
+ (setq buf-string (buffer-string )))
1358
+ (goto-char (point-min ))
1359
+ (save-excursion
1360
+ (while (re-search-forward buf-string nil t )
1361
+ (replace-match " " ))
1362
+ (while (re-search-forward " stdin" nil t )
1363
+ (replace-match path)
1364
+ (goto-char (line-end-position ))
1365
+ (insert " :1" )))
1366
+ (compilation-mode )
1367
+ (next-error )
1368
+ (pop-to-buffer fmt-buffer)))
1355
1369
(t
1356
- (error " Rustfmt failed, see *rustfmt* buffer for details " ))))
1357
- (delete-file tmpf))))
1370
+ (let ((path (buffer-file-name buf)))
1371
+ (goto-char (point-min ))
1372
+ (save-excursion
1373
+ (while (re-search-forward " stdin" nil t )
1374
+ (replace-match path)))
1375
+ (compilation-mode )
1376
+ (next-error )
1377
+ (pop-to-buffer fmt-buffer)))))))))
1358
1378
1359
1379
(defconst rust--format-word " \\ b\\ (else\\ |enum\\ |fn\\ |for\\ |if\\ |let\\ |loop\\ |match\\ |struct\\ |union\\ |unsafe\\ |while\\ )\\ b" )
1360
1380
(defconst rust--format-line " \\ ([\n ]\\ )" )
0 commit comments