如何直接从终端运行Rust Analyser?

发布于 2025-02-01 17:07:23 字数 563 浏览 4 评论 0原文

我正在尝试将Rust Analyzer与浏览器的编辑器集成在一起。

我的第一步是直接从终端运行Rust Analyser,并通过STDIO发送请求。

$ rust-analyzer
> Content-Length:207\r\n\r\n{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":null,"rootPath":"/mnt/78b33d24-344b-43da-a40c-8b81a6fd0b34/projects/rustexplorer/quick_test","initializationOptions":{},"capabilities":{}}}

但是我有一个错误:

[ERROR rust_analyzer] Unexpected error: expected initialize request, got Err(RecvError)
expected initialize request, got Err(RecvError)

我在这里缺少什么?

I'm trying to integrate Rust Analyzer with a browser based editor.

My first step is to run Rust Analyzer directly from a terminal and send requests via stdio.

$ rust-analyzer
> Content-Length:207\r\n\r\n{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":null,"rootPath":"/mnt/78b33d24-344b-43da-a40c-8b81a6fd0b34/projects/rustexplorer/quick_test","initializationOptions":{},"capabilities":{}}}

But I got this error:

[ERROR rust_analyzer] Unexpected error: expected initialize request, got Err(RecvError)
expected initialize request, got Err(RecvError)

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时浪漫 2025-02-08 17:07:23

仅仅是因为四个字面字符\ r \ n未转换为两个特殊字符\ r\ n直接在终端中输入时。

为了通过手动实验在终端中,您应该改变线路结束。

$ sed -u -re 's/^(.*)$/\1\r/' | rust-analyzer 
Content-Length: 207

{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":null,"rootPath":"/mnt/78b33d24-344b-43da-a40c-8b81a6fd0b34/projects/rustexplorer/quick_test","initializationOptions":{},"capabilities":{}}}

... then the response is displayed here ...

请注意,我们在此处按Enter键;我们不会尝试输入\ n特殊字符。

第二个想法,我认为在这种情况下(交互式终端)您应该提供内容长度:209,因为JSON内容将以\ r \ n结束。 (另外两个字节,被忽略为分离器)。
这样,下一个请求可以在下一行中输入。
如果您保留207,则您的下一个请求应与JSON Content相同的行开始(在最后一个}之后)。

另一个解决方案是更改终端的设置。
stty -icrnl使Enter键制作\ r(control -m)字符;然后,您必须输入控制-J以产生\ n字符。

$ stty -icrnl
$ rust-analyser
Content-Length: 208^M <-- Enter + control-J
^M <-- Enter + control-J
{"jsonrpc":"2.0", ... ,"capabilities":{}}} <-- control-J
... the response is displayed here ...

It's just because the four literal characters \ r \ n are not transformed into the two special characters \r and \n when you input them directly in the terminal.

In order to experiment by hand in the terminal, you should transform the line ending.

$ sed -u -re 's/^(.*)$/\1\r/' | rust-analyzer 
Content-Length: 207

{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"processId":null,"rootPath":"/mnt/78b33d24-344b-43da-a40c-8b81a6fd0b34/projects/rustexplorer/quick_test","initializationOptions":{},"capabilities":{}}}

... then the response is displayed here ...

Note that we press the Enter key here; we do not try to input the \n special character.

And on a second thought, I think that in this case (interactive terminal) you should provide Content-Length: 209 because the json content will be ended with \r\n (two more bytes, ignored as separators).
This way, the next request can be input on the next line.
If you keep 207, then your next request should start on the same line as the json content (right after the last }).

Another solution would be to change the settings of the terminal.
stty -icrnl makes the Enter key produce the \r (control-M) character; you then have to input control-J to produce the \n character.

$ stty -icrnl
$ rust-analyser
Content-Length: 208^M <-- Enter + control-J
^M <-- Enter + control-J
{"jsonrpc":"2.0", ... ,"capabilities":{}}} <-- control-J
... the response is displayed here ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文