LSP
的全称是Language Server Protocol
,是微软推出的一项标准化协议,旨在用来统一开发工具与Language Server
以前的通讯。它支持语言的自动补全、定义跳转、查看定义、查看引用、lint、语法高亮
等等,但具体实现要看各类语言的LS支持是否完善。在这以前,各类IDE都须要本身实现一套相似的东西,显得比较重复。借助于LSP
,开发工具只要按规则接入该协议,即可以享受到各类语言提供的服务。html
目前支持的语言
汇总在这里,下图只截出了部分,维护者有民间组织,微软,还有正牌。好比对swift的支持就是Apple维护的sourcekit-lsp。node
官网上有段介绍LSP是如何工做的。git
client和server以前使用JSONRPC
进行通讯,采用request-response
的方式,以下图。github
主要步骤:shell
textDocument/didOpen
通知,告诉服务器。这时文件内容保存在内存中。textDocument/didChange
通知,而后server会发回textDocument/publishDiagnostics
通知,会分析出error和warning。client根据这些error和warning进行对应的UI显示。textDocument/definition
请求,server返回相关的位置信息。textDocument/didClose
通知,文件内容更新到磁盘。下面来看下具体的request和response,以textDocument/definition
来举例。npm
request: 其主要参数是method
,params
。params会带上当前的文件信息,要查询定义的符号信息(第几行,第几个字符)json
{
"jsonrpc": "2.0",
"id" : 1,
"method": "textDocument/definition",
"params": {
"textDocument": {
"uri": "file:///p%3A/mseng/VSCode/Playgrounds/cpp/use.cpp"
},
"position": {
"line": 3,
"character": 12
}
}
}
复制代码
response: 响应包括符号定义的文件位置,符号的起始和终止位置。swift
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"uri": "file:///p%3A/mseng/VSCode/Playgrounds/cpp/provide.cpp",
"range": {
"start": {
"line": 0,
"character": 4
},
"end": {
"line": 0,
"character": 11
}
}
}
}
复制代码
LSP中定义了不少method
,用来区分不一样事件。包括请求和通知。sublime-text
如initialize
,是client发给server的第一个请求。 Shutdown
,关闭请求。 textDocument/definition
,查看符号定义的请求。 ...xcode
其详细定义文档在这里。
众所周知,VSCode是一款功能强大的编辑器,其提供了很是丰富的插件来支持各类语言的开发,而且它是众多编辑器中率先支持LSP
的。
Swift For LSP
还在早期开发的阶段,所以并无提供安装包或者插件。因此目前咱们只能手动下载安装。步骤以下:
这步应该能够略过。
这步应该也能够略过。
到Swift.org下载最新的主干包,安装好以后,到XCode->Preferences->Components选择刚安装的toolchain。或者这里不选择,在vscode中设置toolchain的路径。
因为VSCode的插件都是用js/ts来写的,因此须要js的运行环境。推荐直接下载安装包来安装。
验证是否装好了,能够执行如下命令
nmp --version
复制代码
clone仓库:
git clone https://github.com/apple/sourcekit-lsp.git
复制代码
跳转到sourcekit-lsp目录:
cd sourcekit-lsp
复制代码
编译:
swift build
复制代码
编译成功后,会在.build/debug
找到二进制文件。咱们将其移到/usr/local/bin
目录下,以即可以直接使用。
mv .build/debug/sourcekit-lsp /usr/local/bin
复制代码
这个命令会启动lsp的进程。
sourcekit-lsp
复制代码
该插件的做用是让VSCode
与SourceKit-LSP
之间能够进行通讯。
Editors/vscode/
目录cd Editors/vscode/
复制代码
npm run createDevPackage
npm run createDevPackage
复制代码
若是在这步遇到npm ERR! 404 Not Found: flatmap-stream@0.1.1
的问题,能够尝试删除lock文件,清除缓存试试。
rm package-lock.json
npm cache clean --force
复制代码
sourcekit-lsp-vscode-dev.vsix
code --install-extension out/sourcekit-lsp-vscode-dev.vsix
复制代码
首先要在VSCode中安装code
命令,cmd+shift+p
,输入shell command
,而后安装code命令。如图所示。
重启VSCode。
使用快捷键cmd,
(或者preference-->settings),进入settings
页面,搜索sourcekit-lsp
,ToolChain Path
中填入以前下载安装的toolchain路径。好比个人是/Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2018-11-25-a.xctoolchain
。
以上就配置完成了。
最后,打开一个swift的xcode工程,鼠标停留在关键字上,会展现出具体的定义。以下图。
将设置中的Trace Server打开,设置成message
,能够看到请求/响应/通知信息;设置成verbose
,会更加详细,具体字段以及内容。其中请求是有id编号的,通知没有。
initialize
,initialized
,textDocument/didOpen
。window/logMessage
是server发过来的通知。[Trace - 3:00:00 PM] Sending request 'initialize - (0)'.
[Trace - 3:00:00 PM] Received notification 'window/logMessage'.
could not open compilation database for /Users/liusilan/Documents/workspace/my/LeetCode/countSegments noEntry
[Trace - 3:00:00 PM] Received notification 'window/logMessage'.
failed to open IndexStoreDB: indexstoredb_index_create error: index store path does not exist: /Users/liusilan/Documents/workspace/my/LeetCode/countSegments/.build/debug/index/store
[Trace - 3:00:00 PM] Received response 'initialize - (0)' in 234ms.
[Trace - 3:00:00 PM] Sending notification 'initialized'.
[Trace - 3:00:00 PM] Sending notification 'textDocument/didOpen'.
[Trace - 3:00:00 PM] Received notification 'textDocument/publishDiagnostics'.
[Trace - 3:00:01 PM] Received notification 'textDocument/publishDiagnostics'.
复制代码
textDocument/didChange
,server会返回textDocument/publishDiagnostics
。[Trace - 2:58:57 PM] Sending notification 'textDocument/didChange'.
[Trace - 2:58:57 PM] Received notification 'textDocument/publishDiagnostics'.
[Trace - 2:58:57 PM] Received notification 'textDocument/publishDiagnostics'.
复制代码
textDocument/hover
。[Trace - 2:53:43 PM] Sending request 'textDocument/hover - (3)'.
[Trace - 2:53:43 PM] Received response 'textDocument/hover - (3)' in 3ms.
复制代码
textDocument/documentHighlight
。[Trace - 2:55:07 PM] Sending request 'textDocument/documentHighlight - (22)'.
[Trace - 2:55:07 PM] Received response 'textDocument/documentHighlight - (22)' in 2ms.
复制代码
textDocument/definition
。[Trace - 2:55:40 PM] Sending request 'textDocument/definition - (43)'.
[Trace - 2:55:40 PM] Received response 'textDocument/definition - (43)' in 8ms.
复制代码
关于versbose打印的信息,你们能够尝试设置看看。
另外,SouceKit-LSP
也是支持Sublime Text
的,具体配置能够参照sublime-text配置。
目前,由于它还在早期开发中,支持的功能还不是不少,相信之后会愈来愈完善。
Feature | Status | Notes |
---|---|---|
Swift | ✅ | |
C/C++/ObjC | ❌ | clangd is not available in the recommended toolchain. You can try out C/C++/ObjC support by building clangd from source and putting it in PATH . |
Code completion | ✅ | |
Quick Help (Hover) | ✅ | |
Diagnostics | ✅ | |
Fix-its | ❌ | |
Jump to Definition | ✅ | |
Find References | ✅ | |
Background Indexing | ❌ | Build project to update the index using Indexing While Building |
Workspace Symbols | ❌ | |
Refactoring | ❌ | |
Formatting | ❌ | |
Folding | ❌ | |
Syntax Highlighting | ❌ | Not currently part of LSP. |
Document Symbols | ❌ |
调试在开发过程当中是必不可少的,以前老是生成xcodeproj
,而后在xcode
中进行调试,总归有些不太方便。
在VSCode中
调试swift
也挺简单的,步骤以下:
安装插件CoreLLDB
配置launch.json
在插件安装完成以后,打开swift工程,按F5
,会出现配置弹框,以下。
点击Open launch.json
进行配置,具体配置以下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
// Running executables
{
"type": "lldb",
"request": "launch",
"name": "Run your Executable",
"program": "${workspaceFolder}/.build/debug/xxx",
"args": [],
"cwd": "${workspaceFolder}",
"preLaunchTask": "swift-build"
}
]
}
复制代码
注意将"program": "${workspaceFolder}/.build/debug/xxx",
中的xxx
改为本身工程可执行文件的名字。
launch.json
配置好后,继续按F5
,此时会出现配置task的弹窗,以下图。
点击Configure Task
进行配置,以下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
// compile your SPM project
{
"label": "swift-build",
"type": "shell",
"command": "swift build"
},
// compile your SPM tests
{
"label": "swift-build-tests",
"type": "process",
"command": "swift",
"group": "build",
"args": [
"build",
"--build-tests"
]
}]
}
复制代码
至此,所有配置完成。按F5
开始调试,若是仍然出现弹窗,则说明某个地方没配置好,需对比下上面的配置。
最后就能够愉快的调试了。