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

unexpected edit returned from textDocument/onTypeFormatting with newline at beginning of file #1076

Open
siegel opened this issue Mar 23, 2022 · 2 comments

Comments

@siegel
Copy link

siegel commented Mar 23, 2022

Please describe the problem.

With client developer context from #1074 , I'm seeing an unexpected zero-length edit returned in response to textDocument/onTypeFormatting sent for insertion of a blank line at the beginning of the file.

Open source file:

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char **argv)
{
	printf("hello world");
	
	if (1) { printf("abcdef\n"); exit (0); }
 	//	this is a test this is another test
}

Place insertion point at beginning of file (before the first #include), press Return.

Expected: no edits are required, so I would have expected no edits in response. (It's unclear what the supplied edit is supposed to mean.)

Actual: received what looks like an empty edit.

Logs

2022-03-23 12:34:52.464: stderr output from server: V[12:34:27.495] <<< {"jsonrpc":"2.0","method":"textDocument/didChange","params":{"contentChanges":[{"range":{"end":{"character":0,"line":0},"start":{"character":0,"line":0}},"text":"\n"}],"textDocument":{"uri":"file:///Users/siegel/Desktop/LSPTest/main.c","version":2}}}

I[12:34:27.495] <-- textDocument/didChange

2022-03-23 12:34:52.464: stderr output from server: I[12:34:27.549] ASTWorker building file /Users/siegel/Desktop/LSPTest/main.c version 2 with command inferred from /Users/siegel/Desktop/LSPTest/wumpus.c
[/Users/siegel/Desktop/LSPTest/]
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -resource-dir=/opt/homebrew/Cellar/llvm/13.0.1_1/lib/clang/13.0.1 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -- /Users/siegel/Desktop/LSPTest/main.c

2022-03-23 12:34:52.464: stderr output from server: V[12:34:27.550] Driver produced command: cc1 -cc1 -triple arm64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -fsyntax-only -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -fno-rounding-math -munwind-tables -target-sdk-version=12.3 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +fp-armv8 -target-feature +neon -target-feature +crc -target-feature +crypto -target-feature +dotprod -target-feature +fp16fml -target-feature +ras -target-feature +lse -target-feature +rdm -target-feature +rcpc -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -fallow-half-arguments-and-returns -debugger-tuning=lldb -target-linker-version 711 -fcoverage-compilation-dir=/Users/siegel/Desktop/LSPTest/ -resource-dir /opt/homebrew/Cellar/llvm/13.0.1_1/lib/clang/13.0.1 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /opt/homebrew/Cellar/llvm/13.0.1_1/lib/clang/13.0.1/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -fdebug-compilation-dir=/Users/siegel/Desktop/LSPTest/ -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fmax-type-align=16 -D__GCC_HAVE_DWARF2_CFI_ASM=1 -x c /Users/siegel/Desktop/LSPTest/main.c

2022-03-23 12:34:52.464: stderr output from server: V[12:34:27.550] Rebuilding invalidated preamble for /Users/siegel/Desktop/LSPTest/main.c version 2 (previous was version 1)

2022-03-23 12:34:52.464: stderr output from server: V[12:34:27.588] indexed preamble AST for /Users/siegel/Desktop/LSPTest/main.c version 2:
  symbol slab: 2316 symbols, 623448 bytes
  ref slab: 0 symbols, 0 refs, 128 bytes
  relations slab: 0 relations, 24 bytes

2022-03-23 12:34:52.465: stderr output from server: V[12:34:27.602] Build dynamic index for header symbols with estimated memory usage of 2160008 bytes

2022-03-23 12:34:52.465: stderr output from server: V[12:34:27.604] Built preamble of size 907172 for file /Users/siegel/Desktop/LSPTest/main.c version 2

2022-03-23 12:34:52.465: stderr output from server: V[12:34:27.608] indexed file AST for /Users/siegel/Desktop/LSPTest/main.c version 2:
  symbol slab: 1 symbols, 4448 bytes
  ref slab: 3 symbols, 4 refs, 4320 bytes
  relations slab: 0 relations, 24 bytes
V[12:34:27.608] Build dynamic index for main-file symbols with estimated memory usage of 11688 bytes

2022-03-23 12:34:52.465: stderr output from server: I[12:34:27.608] --> textDocument/publishDiagnostics
V[12:34:27.608] >>> {"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"diagnostics":[],"uri":"file:///Users/siegel/Desktop/LSPTest/main.c","version":2}}


2022-03-23 12:34:52.465: stderr output from server: V[12:34:48.330] <<< {"id":2,"jsonrpc":"2.0","method":"textDocument/onTypeFormatting","params":{"ch":"\n","options":{"insertFinalNewline":false,"insertSpaces":false,"tabSize":4,"trimTrailingWhitespace":false},"position":{"character":0,"line":0},"textDocument":{"uri":"file:///Users/siegel/Desktop/LSPTest/main.c"}}}

I[12:34:48.330] <-- textDocument/onTypeFormatting(2)

2022-03-23 12:34:52.466: stderr output from server: V[12:34:48.331] Typed a newline, but we're still on the first line!

2022-03-23 12:35:19.652: stderr output from server: I[12:34:48.332] --> reply:textDocument/onTypeFormatting(2) 1 ms
V[12:34:48.332] >>> {"id":2,"jsonrpc":"2.0","result":[{"newText":"","range":{"end":{"character":0,"line":0},"start":{"character":0,"line":0}}}]}

Additionally, the Typed a newline, but we're still on the first line! seems to indicate some sort of internal error, but it's not clear to me how clangd expects to interpret the position parameter. (The location I provide is the position at which the character is typed, not the location of the insertion point after it's inserted. Do I need to change that?)

I'm also confused as to whether the typed character should even be inserted, and the specification is no help.

System information

Output of clangd --version:

Homebrew clangd version 13.0.1
Features: mac+xpc
Platform: arm64-apple-darwin21.4.0

Editor/LSP plugin: BBEdit 14.x development version

Operating system: macOS 12.3 (21E230)

@HighCommander4
Copy link
Contributor

Additionally, the Typed a newline, but we're still on the first line! seems to indicate some sort of internal error, but it's not clear to me how clangd expects to interpret the position parameter. (The location I provide is the position at which the character is typed, not the location of the insertion point after it's inserted. Do I need to change that?)

I'm also confused as to whether the typed character should even be inserted, and the specification is no help.

I agree the spec is unclear here.

It looks like clangd does expect position to be the current cursor position at the time of the request, which is assumed to be after the typed character has been inserted.

I think the idea is that onTypeFormatting is a "fix-up" request: "I typed something, now fix it up and make it nice (indented etc.)".

@HighCommander4
Copy link
Contributor

HighCommander4 commented Mar 28, 2022

As for the issue of the server sending an empty edit, I can confirm this happens with vscode as well.

I agree it's technically a bug (the empty edit could surely be elided), but it also seems fairly harmless (applying an empty edit should be a no-op on the client side).

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