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

Snippet completions from JDTLS not inserting text properly #21603

Closed
1 task done
valentinegb opened this issue Dec 5, 2024 · 4 comments
Closed
1 task done

Snippet completions from JDTLS not inserting text properly #21603

valentinegb opened this issue Dec 5, 2024 · 4 comments
Labels
bug [core label] language server failure Language server doesn't work as expected

Comments

@valentinegb
Copy link
Contributor

valentinegb commented Dec 5, 2024

Check for existing issues

  • Completed

Describe the bug / provide steps to reproduce it

When attempting to autocomplete a snippet completion with the Zed Java extension (which uses Eclipse JDT Language Server, aka. JDTLS), Zed inserts the name of the snippet instead of the snippet's insertText.

For example, if I autocomplete class in a file called DriveSubsystem.java, Zed just inserts the text class when I would've expected:

/**
 * DriveSubsystem
 */
public class DriveSubsystem {

  ${0}
}

Here are the LSP RPC logs around the time of autocompleting:

// Receive:
{"jsonrpc":"2.0","method":"$/progress","params":{"token":"5c0bc3c5-48af-4bb5-aa79-3d587a7dd3cc","value":{"kind":"report","message":"Publish Diagnostics - 0% ","percentage":0}}}
// Send:
{"jsonrpc":"2.0","id":"22","result":null}
// Receive:
{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":"file:///Users/valentinebriese/Developer/SwerveDrive2025/src/main/java/frc/robot/subsystems/DriveSubsystem.java","diagnostics":[{"range":{"start":{"line":1,"character":0},"end":{"line":1,"character":3}},"severity":1,"code":"1610612968","source":"Java","message":"Syntax error on token \"cla\", delete this token"}]}}
// Receive:
{"jsonrpc":"2.0","method":"$/progress","params":{"token":"5c0bc3c5-48af-4bb5-aa79-3d587a7dd3cc","value":{"kind":"end","message":"Publish Diagnostics"}}}
// Send:
{"jsonrpc":"2.0","method":"textDocument/didChange","params":{"textDocument":{"uri":"file:///Users/valentinebriese/Developer/SwerveDrive2025/src/main/java/frc/robot/subsystems/DriveSubsystem.java","version":4},"contentChanges":[{"range":{"start":{"line":1,"character":3},"end":{"line":1,"character":3}},"text":"ss"}]}}
// Send:
{"jsonrpc":"2.0","id":16,"method":"completionItem/resolve","params":{"label":"class","kind":15,"documentation":"/**\n * DriveSubsystem\n */\npublic class DriveSubsystem {\n\n\t\n}","sortText":"999999212","filterText":"class","insertText":"/**\n * DriveSubsystem\n */\npublic class DriveSubsystem {\n\n\t${0}\n}","insertTextFormat":2,"insertTextMode":2,"textEdit":{"newText":"class","insert":{"start":{"line":1,"character":0},"end":{"line":1,"character":3}},"replace":{"start":{"line":1,"character":0},"end":{"line":1,"character":3}}}}}
// Receive:
{"jsonrpc":"2.0","id":16,"result":{"label":"class","kind":15,"documentation":"/**\n * DriveSubsystem\n */\npublic class DriveSubsystem {\n\n\t\n}","sortText":"999999212","filterText":"class","insertText":"/**\n * DriveSubsystem\n */\npublic class DriveSubsystem {\n\n\t${0}\n}","insertTextFormat":2,"insertTextMode":2,"textEdit":{"newText":"class","insert":{"start":{"line":1,"character":0},"end":{"line":1,"character":3}},"replace":{"start":{"line":1,"character":0},"end":{"line":1,"character":3}}}}}
// Receive:
{"jsonrpc":"2.0","id":"23","method":"window/workDoneProgress/create","params":{"token":"a597e761-6ad4-4d01-acb7-6d83b82b2691"}}
// Receive:
{"jsonrpc":"2.0","method":"$/progress","params":{"token":"a597e761-6ad4-4d01-acb7-6d83b82b2691","value":{"kind":"begin","title":"Validate documents","message":"Validate documents"}}}
// Receive:
{"jsonrpc":"2.0","method":"$/progress","params":{"token":"a597e761-6ad4-4d01-acb7-6d83b82b2691","value":{"kind":"report","message":"Validate documents - 0% ","percentage":0}}}
// Send:
{"jsonrpc":"2.0","id":"23","result":null}
// Receive:
{"jsonrpc":"2.0","method":"$/progress","params":{"token":"a597e761-6ad4-4d01-acb7-6d83b82b2691","value":{"kind":"end","message":"Validate documents"}}}

Environment

Zed: v0.165.2 (Zed Preview)
OS: macOS 15.2.0
Memory: 8 GiB
Architecture: aarch64

If applicable, add mockups / screenshots to help explain present your vision of the feature

No response

If applicable, attach your Zed.log file to this issue.

Zed.log

@valentinegb valentinegb added admin read Pending admin review bug [core label] triage Maintainer needs to classify the issue labels Dec 5, 2024
@notpeter notpeter added language server failure Language server doesn't work as expected and removed triage Maintainer needs to classify the issue admin read Pending admin review labels Dec 5, 2024
@osiewicz
Copy link
Contributor

osiewicz commented Dec 6, 2024

I think we're actually doing the correct thing here; the completion you've listed has both insertText and textEdit fields, and in the LSP spec it is clearly stated that when textEdit is provided, insertText should be ignored.

/**
	 * An edit which is applied to a document when selecting this completion.
	 * When an edit is provided the value of `insertText` is ignored.
	 *
	 * *Note:* The range of the edit must be a single line range and it must
	 * contain the position at which completion has been requested.
	 *
	 * Most editors support two different operations when accepting a completion
	 * item. One is to insert a completion text and the other is to replace an
	 * existing text with a completion text. Since this can usually not be
	 * predetermined by a server it can report both ranges. Clients need to
	 * signal support for `InsertReplaceEdit`s via the
	 * `textDocument.completion.completionItem.insertReplaceSupport` client
	 * capability property.
	 *
	 * *Note 1:* The text edit's range as well as both ranges from an insert
	 * replace edit must be a [single line] and they must contain the position
	 * at which completion has been requested.
	 * *Note 2:* If an `InsertReplaceEdit` is returned the edit's insert range
	 * must be a prefix of the edit's replace range, that means it must be
	 * contained and starting at the same position.
	 *
	 * @since 3.16.0 additional type `InsertReplaceEdit`
	 */
	textEdit?: [TextEdit](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEdit) | [InsertReplaceEdit](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#insertReplaceEdit);

I'd suggest to file an issue with the language server. Feel free to reopen this issue if anything.

@osiewicz osiewicz closed this as completed Dec 6, 2024
@valentinegb
Copy link
Contributor Author

Hello again @osiewicz, I've spoken with a JDTLS dev here and we think this is in fact the fault of Zed. We noticed a discrepancy between the completion that Zed receives and the completion that Zed asks to autocomplete:

// Send:
{"jsonrpc":"2.0","id":38,"method":"textDocument/completion","params":{"textDocument":{"uri":"file:///Users/valentinebriese/Developer/SwerveDrive2025/src/main/java/frc/robot/subsystems/Test.java"},"position":{"line":0,"character":3},"context":{"triggerKind":1}}}
// Receive:
{"jsonrpc":"2.0","id":38,"result":{"isIncomplete":false,"items":[{"label":"class","kind":15,"documentation":"package frc.robot.subsystems;\n\npublic class Test {\n\n\t\n}","sortText":"999999212","filterText":"class","insertText":"package frc.robot.subsystems;\n\npublic class Test {\n\n\t${0}\n}","insertTextMode":2,"textEditText":"package frc.robot.subsystems;\n\npublic class Test {\n\n\t${0}\n}"},{"label":"class","kind":14,"sortText":"999999213","filterText":"class","textEditText":"class","command":{"title":"","command":"java.completion.onDidSelect","arguments":["10","0"]},"data":{"pid":"0","rid":"10"}}],"itemDefaults":{"editRange":{"insert":{"start":{"line":0,"character":0},"end":{"line":0,"character":3}},"replace":{"start":{"line":0,"character":0},"end":{"line":0,"character":3}}},"insertTextFormat":2,"data":{"completionKinds":[3]}}}}
// Send:
{"jsonrpc":"2.0","id":39,"method":"completionItem/resolve","params":{"label":"class","kind":15,"documentation":"package frc.robot.subsystems;\n\npublic class Test {\n\n\t\n}","sortText":"999999212","filterText":"class","insertText":"package frc.robot.subsystems;\n\npublic class Test {\n\n\t${0}\n}","insertTextFormat":2,"insertTextMode":2,"textEdit":{"newText":"class","insert":{"start":{"line":0,"character":0},"end":{"line":0,"character":3}},"replace":{"start":{"line":0,"character":0},"end":{"line":0,"character":3}}},"data":{"completionKinds":[3]}}}
// Receive:
{"jsonrpc":"2.0","id":39,"result":{"label":"class","kind":15,"documentation":"package frc.robot.subsystems;\n\npublic class Test {\n\n\t\n}","sortText":"999999212","filterText":"class","insertText":"package frc.robot.subsystems;\n\npublic class Test {\n\n\t${0}\n}","insertTextFormat":2,"insertTextMode":2,"textEdit":{"newText":"class","insert":{"start":{"line":0,"character":0},"end":{"line":0,"character":3}},"replace":{"start":{"line":0,"character":0},"end":{"line":0,"character":3}}}}}

This comment may be particularly informative.

I believe this means this issue should be reopened, which I am unable to do myself ^^'

@SomeoneToIgnore
Copy link
Contributor

Oh, then it's #21277

@valentinegb
Copy link
Contributor Author

Looks like it, sorry for making a duplicate issue then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug [core label] language server failure Language server doesn't work as expected
Projects
None yet
Development

No branches or pull requests

4 participants