Skip to content

Commit

Permalink
another attempt at fixing some obsolete process errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tpeacock19 committed Mar 2, 2024
1 parent b7f03bc commit 087ad37
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions flymake-languagetool.el
Original file line number Diff line number Diff line change
Expand Up @@ -290,39 +290,45 @@ non-nil."
(defun flymake-languagetool--handle-finished (status source-buffer report-fn)
"Callback function for LanguageTool process for SOURCE-BUFFER.
STATUS provided from `url-retrieve'."
(when-let ((err (plist-get status :error))
(test (equal (current-buffer) flymake-languagetool--proc-buf)))
(funcall report-fn :panic :explanation (error-message-string err))
(error (error-message-string err)))
(if (and url-http-end-of-headers
(equal (current-buffer)
(with-current-buffer source-buffer
flymake-languagetool--proc-buf)))
(let* ((err (plist-get status :error))
(c-buf (current-buffer))
(proc-buf (buffer-local-value 'flymake-languagetool--proc-buf
source-buffer))
(proc-current (equal c-buf proc-buf)))
(cond
((and proc-current err)
;; Ignore errors about deleted processes since they are obsolete
;; calls deleted by `flymake-languagetool--check'
(unless (equal "deleted" (string-trim (nth 2 err)))
(with-current-buffer source-buffer
;; for some reason the 2nd element in error list is a
;; symbol. This needs to be changed to string to reflect in
;; `error-message-string'
(setf (nth 1 err) (symbol-name (nth 1 err)))
(funcall report-fn :panic :explanation
(format "%s: %s" c-buf (error-message-string err))))))
((and proc-current url-http-end-of-headers)
(let ((output (save-restriction
(set-buffer-multibyte t)
(goto-char url-http-end-of-headers)
(buffer-substring (point) (point-max)))))
(with-current-buffer source-buffer
(funcall report-fn
(flymake-languagetool--output-to-errors output source-buffer)
:region (cons (point-min) (point-max)))
(kill-buffer flymake-languagetool--proc-buf)
(setq flymake-languagetool--proc-buf nil)))
(with-current-buffer source-buffer
(flymake-log :warning "Canceling tha obsolete check %s"
flymake-languagetool--proc-buf))))

(defun flymake-languagetool--check (report-fn)
:region (cons (point-min) (point-max))))))
((not proc-current)
(with-current-buffer source-buffer
(flymake-log :warning "Cancelling obsolete check"))))))

(defun flymake-languagetool--check (report-fn text)
"Run LanguageTool on the current buffer's contents."
(when (buffer-live-p flymake-languagetool--proc-buf)
(flymake-log :warning "Canceling the obsolete check %s"
flymake-languagetool--proc-buf)
(when-let ((buf flymake-languagetool--proc-buf))
;; need to check if buffer has ongoing process or else we may
;; potentially delete the wrong one.
(when (get-buffer-process flymake-languagetool--proc-buf)
(delete-process (get-buffer-process flymake-languagetool--proc-buf)))
(kill-buffer flymake-languagetool--proc-buf)
(setq flymake-languagetool--proc-buf nil))
(when-let ((process (get-buffer-process buf)))
(delete-process process))
(kill-buffer buf)
(setf flymake-languagetool--proc-buf nil))
(let* ((url-request-method "POST")
(url-request-extra-headers
'(("Content-Type" . "application/x-www-form-urlencoded")))
Expand All @@ -334,9 +340,7 @@ STATUS provided from `url-retrieve'."
(unless flymake-languagetool-check-spelling
flymake-languagetool-spelling-rules))
","))
(params (list (list "text" (with-current-buffer source-buffer
(buffer-substring-no-properties
(point-min) (point-max))))
(params (list (list "text" text)
(list "language" flymake-languagetool-language)
(unless (string-empty-p disabled-rules)
(list "disabledRules" disabled-rules))
Expand All @@ -357,8 +361,9 @@ STATUS provided from `url-retrieve'."
#'flymake-languagetool--handle-finished
(list source-buffer report-fn) t))
;; can't reach LanguageTool API, try again. TODO:
(sit-for 1)
(funcall report-fn '()))))
(funcall report-fn :panic :explanation
(format "Cannot reach LanguageTool URL: %s"
flymake-languagetool-url)))))

(defun flymake-languagetool--reachable-p ()
(let ((res (or flymake-languagetool--local
Expand Down Expand Up @@ -403,14 +408,16 @@ STATUS provided from `url-retrieve'."
(defun flymake-languagetool--checker (report-fn &rest _args)
"Diagnostic checker function with REPORT-FN."
(setq flymake-languagetool--source-buffer (current-buffer))
(cond
((flymake-languagetool--reachable-p)
(flymake-languagetool--check report-fn))
((or flymake-languagetool-server-command flymake-languagetool-server-jar)
(flymake-languagetool--start-server report-fn))
(t (funcall report-fn :panic :explanation
(format "Cannot reach LanguageTool URL: %s"
flymake-languagetool-url)))))
(let ((text (buffer-substring-no-properties
(point-min) (point-max))))
(cond
((flymake-languagetool--reachable-p)
(flymake-languagetool--check report-fn text))
((or flymake-languagetool-server-command flymake-languagetool-server-jar)
(flymake-languagetool--start-server report-fn))
(t (funcall report-fn :panic :explanation
(format "Cannot reach LanguageTool URL: %s"
flymake-languagetool-url))))))

(defun flymake-languagetool--overlay-p (overlay)
"Return t if OVERLAY is a `flymake-languagetool' diagnostic overlay."
Expand Down

0 comments on commit 087ad37

Please sign in to comment.