Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@
*.app

.idea/
.vs/
.cache/
out/
compile_commands.json
CMakeSettings.json
cmake-build-debug/
21 changes: 13 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
cmake_minimum_required(VERSION 3.14)
project(Lsp)
cmake_minimum_required(VERSION 3.5)
project(LspCPP VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

option(LspBuildTest "build test" ON)

add_subdirectory(lsp)
if(LspBuildTest)
add_subdirectory(test)
endif()


set(CMAKE_CXX_STANDARD 14)

include_directories(include)
include_directories(src)
aux_source_directory(src/ sources)
set(lsp_include_dirs ${CMAKE_CURRENT_SOURCE_DIR}/include CACHE INTERNAL "" )
add_executable(LspClientTest ${sources})
129 changes: 118 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,123 @@
# lsp-cpp
An easy [language-server-protocol](https://github.com/microsoft/language-server-protocol) client
# lsp-cpp

* 一个简单的lsp客户端
* c++上目前似乎并没有一个简单好用lsp的客户端
* 所以就想给我的代码编辑框写一个lsp client
该项目从 "lsp-cpp项目(An easy language-server-protocol client)"Fork而来。源项目:[https://github.com/alextsao1999/lsp-cpp]
由于源项目已经不维护了,所以本fork项目独立了出来,新的项目地址:[https://github.com/firemeatman/LspClient]

只有头文件(header-only) 直接引用就好了
源项目是一个十分简单的Lsp语言客户端,通俗易懂。官方的LspCpp有点庞大不太用的来,而此项目足够简单,适合简单使用。

cmake:
本项目的目的是基于lsp-client进行完善和扩展,包括跨平台支持、模块化改造、协议扩充等。

`
include_directories(${lsp_include_dirs})
`

> 很多代码参考clangd 有的直接Ctrl+C了 (●'◡'●)
# 1.平台支持

> 可以运行的平台
- windows
- linux(未完成)
> 与其他框架的集成使用
- Qt6.x,编译通过,正常使用。示例项目: 代码编辑器[https://github.com/firemeatman/CodeEditor]

# 2. 如何引入第三方项目
cmake

# 3. 代码示例

```c++
#include <iostream>
#include <thread>
#include <fstream>
#include <lsp/client.h>
using namespace nlohmann;
using namespace LspCore;

bool readFile(std::string& path, std::string* data)
{
// 这里就不实现了
return true;
}

void copyCompileCommandsFiles()
{
// 这里就不实现了
}
auto debugFunc = [](value& j) {
//value method = j.at("method");
std::cout << "========================================\n";
std::cout << j << std::endl;
};

int main() {
std::string file1_uri = "file:///yourTestCodeFilePath";
std::string file1_path = "yourTestCodeFilePath";
std::string root_uri = "file:///yourTestCodeFileRootPath";
// 1. 创建Lsp客户端
PipJsonIO jsonIO(R"(clangd.exe)"); // Lsp服务器路径, 这里我用的是Clangd.
MapMessageHandler msgHandler;
LanguageClient client(msgHandler, jsonIO);
// 2. 复制compile_commands.json到工作区, clangd需要这个文件;通过命令应该也能指定该文件路径和clangd工作文件夹
copyCompileCommandsFiles();

// 3. 绑定一些通知消息的回调函数
{
MapMessageHandler::Accessor accessor = msgHandler.access(); // 访问者模式访问MapMessageHandler的接口,线程安全
accessor.bindNotify(METHOD_PublishDiagnostics.c_str(), debugFunc);
accessor.bindNotify(METHOD_DidOpen.c_str(), debugFunc);
accessor.bindNotify(METHOD_DidClose.c_str(), debugFunc);
}
// 4. 线程中运行Lsp客户端
std::thread thread([&] {
client.safeLoop();
});

// 5. 测试与本地Lsp服务器的通信
int res;
while (scanf("%d", &res)) {

switch (res)
{
case 0: // 退出
{
client.Exit();
client.requestStopLoop();
thread.join();
return 0;
break;
}
case 1: // 初始化
{
MapMessageHandler::Accessor accessor = msgHandler.access();// 访问者模式访问MapMessageHandler的接口,线程安全
accessor.bindResponse(client.Initialize(string_ref(root_uri)), debugFunc);// 发出初始化请求消息,并绑定回调函数
break;
}
case 2: // 打开文件
{
std::string text;
readFile(file1_path, &text);
client.DidOpen(file1_uri, text);
break;
}
case 3: // 关闭文件
{
client.DidClose(string_ref(file1_uri));
break;
}
case 4: // 获取词语列表
{
MapMessageHandler::Accessor accessor = msgHandler.access();
accessor.bindResponse(client.SemanticTokensALL(string_ref(file1_uri)), debugFunc);
break;
}
case 5: // 悬停信息
{
MapMessageHandler::Accessor accessor = msgHandler.access();
Position pos{8, 13};
accessor.bindResponse(client.Hover(string_ref(file1_uri), pos), debugFunc);
break;
}
default:
break;
}
}
return 0;
}

```
131 changes: 0 additions & 131 deletions include/transport.h

This file was deleted.

23 changes: 23 additions & 0 deletions lsp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.5)

project(LspCore VERSION 0.1 LANGUAGES CXX)

file(GLOB_RECURSE CODE_FILES
*.cpp
*.h
*.hpp
)

message( ${CODE_FILES})

add_library( ${PROJECT_NAME}
${CODE_FILES}
)

target_include_directories(${PROJECT_NAME} PUBLIC include)

set_target_properties(${PROJECT_NAME} PROPERTIES
ENABLE_EXPORTS TRUE
)


Loading