joaotavora / eglot

A client for Language Server Protocol servers
GNU General Public License v3.0
2.21k stars 203 forks source link

Eglot Panics When Combined with Electric Pair Local mode #1314

Closed creese closed 8 months ago

creese commented 8 months ago

I've been experimenting with Eglot as my primary tool and ran into an unexpected error. I'd appreciate any insights or potential solutions.

Here's a step-by-step of what I did:

  1. Install rust-analyzer.
  2. Add a hook to ensure Eglot is enabled for Rust mode.
  3. Create a new project using cargo new hello_cargo.
  4. Below the default println!, add another println!.
  5. Upon typing the opening parenthesis for the second println!(), there is no error.
  6. Enable electric-pair-local-mode.
  7. Add another println!.
  8. Upon typing the opening parenthesis for the third println!(), the following error message appears:

*Messages*:

jsonrpc-error: "request id=7 failed:", (jsonrpc-error-code . -32603), (jsonrpc-error-message . "request handler panicked: assertion failed: !!text[usize::from(position.offset)..].starts_with(char_typed)"), (jsonrpc-error-data)

eglot-stderr-buffer:

Panic context:
> 
version: 0.0.0 (6572ec8d9 2023-10-15)
request: textDocument/onTypeFormatting DocumentOnTypeFormattingParams {
    text_document_position: TextDocumentPositionParams {
        text_document: TextDocumentIdentifier {
            uri: Url {
                scheme: "file",
                cannot_be_a_base: false,
                username: "",
                password: None,
                host: None,
                port: None,
                path: "/Source/hello_cargo/src/main.rs",
                query: None,
                fragment: None,
            },
        },
        position: Position {
            line: 3,
            character: 14,
        },
    },
    ch: "(",
    options: FormattingOptions {
        tab_size: 8,
        insert_spaces: true,
        properties: {},
        trim_trailing_whitespace: None,
        insert_final_newline: Some(
            true,
        ),
        trim_final_newlines: Some(
            true,
        ),
    },
}

thread 'LspServer' panicked at crates/rust-analyzer/src/handlers/request.rs:339:8:
assertion failed: !!text[usize::from(position.offset)..].starts_with(char_typed)
stack backtrace:
   0: rust_begin_unwind
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/std/src/panicking.rs:595:5
   1: core::panicking::panic_fmt
             at /rustc/cc66ad468955717ab92600c770da8c1601a4ff33/library/core/src/panicking.rs:67:14
   2: rust_analyzer::handlers::request::handle_on_type_formatting
   3: std::panicking::try
   4: rust_analyzer::dispatch::RequestDispatcher::on_sync
   5: rust_analyzer::main_loop::<impl rust_analyzer::global_state::GlobalState>::on_request
   6: rust_analyzer::main_loop::<impl rust_analyzer::global_state::GlobalState>::run
   7: rust_analyzer::main_loop::main_loop
   8: rust_analyzer::run_server
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Has anyone encountered a similar issue or have suggestions for a fix?

For reference, here's my configuration:

(add-hook 'rust-ts-mode-hook 'eglot-ensure)
(add-hook 'prog-mode-hook 'electric-pair-local-mode)

Thanks!

joaotavora commented 8 months ago

Converting to discussion. Not a MRE.