首页 > 解决方案 > 如何在 VS Code 中显示 LSP 的相关信息

问题描述

我正在写一个语言服务器。它提供额外的DiagnosticRelatedInformation作为其诊断响应的一部分。就像在https://code.visualstudio.com/updates/v1_22#_related-information-in-errors-and-warnings中一样。目前,虽然我确实在“问题”窗口和主文本区域中都看到了主要错误,但看不到额外的“相关信息”。

我怀疑这可能有两个原因。任一客户端:在 VS Code 中未启用显示此信息的功能。至少在协议启动的时候,这个能力是看不到的,下面是来自客户端的初始 JSON 消息:

[Trace - 2:22:13 PM] Sending request 'initialize - (0)'.
Params: {
    "processId": 6640,
    "rootPath": null,
    "rootUri": null,
    "capabilities": {
        "workspace": {
            "applyEdit": true,
            "didChangeConfiguration": {
                "dynamicRegistration": true
            },
            "didChangeWatchedFiles": {
                "dynamicRegistration": true
            },
            "symbol": {
                "dynamicRegistration": true
            },
            "executeCommand": {
                "dynamicRegistration": true
            }
        },
        "textDocument": {
            "synchronization": {
                "dynamicRegistration": true,
                "willSave": true,
                "willSaveWaitUntil": true,
                "didSave": true
            },
            "completion": {
                "dynamicRegistration": true,
                "completionItem": {
                    "snippetSupport": true,
                    "commitCharactersSupport": true
                }
            },
            "hover": {
                "dynamicRegistration": true
            },
            "signatureHelp": {
                "dynamicRegistration": true
            },
            "definition": {
                "dynamicRegistration": true
            },
            "references": {
                "dynamicRegistration": true
            },
            "documentHighlight": {
                "dynamicRegistration": true
            },
            "documentSymbol": {
                "dynamicRegistration": true
            },
            "codeAction": {
                "dynamicRegistration": true
            },
            "codeLens": {
                "dynamicRegistration": true
            },
            "formatting": {
                "dynamicRegistration": true
            },
            "rangeFormatting": {
                "dynamicRegistration": true
            },
            "onTypeFormatting": {
                "dynamicRegistration": true
            },
            "rename": {
                "dynamicRegistration": true
            },
            "documentLink": {
                "dynamicRegistration": true
            }
        }
    },
    "trace": "verbose"
}

顺便说一句,我在 Win10 上使用 VS Code 1.28.2。

另一个原因可能是服务器端:包含诊断的响应可能格式错误。它看起来像这样:

Params: {
    "diagnostics": [
        {
            "code": null,
            "message": "the declaration of 'x' shadows an existing declaration",
            "range": {
                "end": {
                    "character": 17,
                    "line": 8
                },
                "start": {
                    "character": 17,
                    "line": 8
                }
            },
            "relatedInformation": [
                {
                    "location": {
                        "uri": "file:///c%3A/Users/blabla/XlocalShadow3.blc",
                        "range": {
                            "end": {
                                "character": 13,
                                "line": 6
                            },
                            "start": {
                                "character": 13,
                                "line": 6
                            }
                        }
                    },
                    "message": "existing declaration"
                },
                {
                    "location": {
                        "uri": "file:///c%3A/Users/blabla/XlocalShadow3.blc",
                        "range": {
                            "end": {
                                "character": 17,
                                "line": 8
                            },
                            "start": {
                                "character": 17,
                                "line": 8
                            }
                        }
                    },
                    "message": "shadowing declaration"
                }
            ],
            "severity": {
                "_tag": 0
            },
            "source": "Parsing Error"
        }
    ],
    "uri": "file:///c%3A/Users/blabla/XlocalShadow3.blc"
}

至少 URI 是正确的(尽管此处为便于阅读而进行了修改),因为我可以单击它们并且编辑器会跳转到正确的文件。

我的问题是我不知道如何检验关于出了什么问题的一个或另一个假设。或者,也许我错过了一些完全不同的东西?

标签: javascriptjsonvisual-studio-codevscode-extensionslanguage-server-protocol

解决方案


问题只是vscode-languageclientpackage.json 的依赖项部分中的过时版本。该功能是在 4.1.0 中实现的,但由于我的项目有点过时,它只需要 3.5 以上的东西。我现在已经更新到最新版本,一切正常。


推荐阅读