Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eglot should flush recent changes before didSave notification #60

Closed
mkcms opened this issue Aug 4, 2018 · 3 comments
Closed

eglot should flush recent changes before didSave notification #60

mkcms opened this issue Aug 4, 2018 · 3 comments

Comments

@mkcms
Copy link
Collaborator

mkcms commented Aug 4, 2018

When I make a change in the buffer, and then immediately save it, didChange events are ran after didSave notifications. This leads to eglot and some servers having different document versions. This happens to me with eclipse.jdt.ls LSP server. Other servers that I tested simply ignore TextDocument/didSave notifications so it will never happen. Here is an example with eclipse.jdt.ls:

package com.frog.test;

public class App
{
    public static void main(String[] args)
    {
	int a;
    }
}
  1. Go to 7th line.
  2. M-x kill-whole-line
  3. Quickly do C-x C-s (before recent changes are sent)
client-notification Sat Aug  4 11:36:16 2018:
(:jsonrpc "2.0" :method "textDocument/didSave" :params
	  (:text "package com.frog.test;\n\npublic class App\n{\n    public static void main(String[] args)\n    {\n    }\n}\n" :textDocument
		 (:uri "file:///home/michal/src/eclipse-workspace/test/src/main/java/com/frog/test/App.java")))

server-notification Sat Aug  4 11:36:16 2018:
(:jsonrpc "2.0" :method "window/logMessage" :params
	  (:type 3 :message "Aug 4, 2018 11:36:16 AM >> document/didSave"))

client-notification Sat Aug  4 11:36:17 2018:
(:jsonrpc "2.0" :method "textDocument/didChange" :params
	  (:textDocument
	   (:uri "file:///home/michal/src/eclipse-workspace/test/src/main/java/com/frog/test/App.java" :version 2)
	   :contentChanges
	   [(:range
	     (:start
	      (:line 6 :character 0)
	      :end
	      (:line 6 :character 7))
	     :rangeLength 7 :text "")
	    (:range
	     (:start
	      (:line 6 :character 0)
	      :end
	      (:line 7 :character 0))
	     :rangeLength 1 :text "")]))

One solution to it is to ensure recent changes are flushed before every notification.

@mkcms
Copy link
Collaborator Author

mkcms commented Aug 4, 2018

Despite the fact that the program is correct, this leads to diagnostics such as:

server-notification Sat Aug  4 11:36:18 2018:
(:jsonrpc "2.0" :method "textDocument/publishDiagnostics" :params
	  (:uri "file:///home/michal/src/eclipse-workspace/test/src/main/java/com/frog/test/App.java" :diagnostics
		[(:range
		  (:start
		   (:line 3 :character 0)
		   :end
		   (:line 3 :character 1))
		  :severity 1 :code "1610612976" :source "Java" :message "Syntax error, insert \"}\" to complete ClassBody")
		 (:range
		  (:start
		   (:line 5 :character 4)
		   :end
		   (:line 5 :character 5))
		  :severity 1 :code "1610612976" :source "Java" :message "Syntax error, insert \"}\" to complete MethodBody")]))

@joaotavora
Copy link
Owner

This happens to me with eclipse.jdt.ls LSP server.

By the way, when you can, make a PR to add this to the README.md and eglot-server-programs.

One solution to it is to ensure recent changes are flushed before every notification.

Don't you mean before each didSave notification?

diff --git a/eglot.el b/eglot.el
index c3a0d51..afe6911 100644
--- a/eglot.el
+++ b/eglot.el
@@ -1116,6 +1116,7 @@ When called interactively, use the currently active server"
 
 (defun eglot--signal-textDocument/didSave ()
   "Send textDocument/didSave to server."
+  (eglot--signal-textDocument/didChange)
   (jsonrpc-notify
    (eglot--current-server-or-lose)
    :textDocument/didSave

@mkcms
Copy link
Collaborator Author

mkcms commented Aug 6, 2018

By the way, when you can, make a PR to add this to the README.md and eglot-server-programs.

I'll try, although this server has a lot of configurable parameters. I think at least two defcustoms are needed for it.

Don't you mean before each didSave notification?

I meant before every notification just to be safe but perhaps that's too much. I'm not sure if other notifications depend on didChange notifications.

bhankas pushed a commit to bhankas/emacs that referenced this issue Sep 18, 2022
…e notification

* eglot.el (eglot--signal-textDocument/didSave): Call
  eglot--signal-textDocument/didChange.
bhankas pushed a commit to bhankas/emacs that referenced this issue Sep 19, 2022
…e notification

* eglot.el (eglot--signal-textDocument/didSave): Call
  eglot--signal-textDocument/didChange.
bhankas pushed a commit to bhankas/emacs that referenced this issue Sep 19, 2022
* eglot.el (eglot--signal-textDocument/didSave): Call
  eglot--signal-textDocument/didChange.
#60: joaotavora/eglot#60
jollaitbot pushed a commit to sailfishos-mirror/emacs that referenced this issue Oct 12, 2022
* eglot.el (eglot--signal-textDocument/didSave): Call
  eglot--signal-textDocument/didChange.

GitHub-reference: close joaotavora/eglot#60
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants