@yeyelailou/co 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -11
- package/bin//345/205/245/345/217/243.js +0 -8
- package/package.json +4 -5
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//346/234/272/345/231/250/347/240/201/347/224/237/346/210/220/345/231/250.co +21 -1
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/347/274/226/350/257/221.sh +73 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201//347/274/226/350/257/221/351/251/261/345/212/250.co +3 -1
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201//347/274/226/350/257/221/351/252/214/350/257/201.sh +24 -8
- package/bin/install.js +0 -276
- package//345/267/245/345/205/267/lsp/server.js +0 -419
- package//345/267/245/345/205/267/lsp//346/265/213/350/257/225.sh +0 -81
- package//345/267/245/345/205/267/vscode-cocn/extension.js +0 -203
- package//345/267/245/345/205/267/vscode-cocn/package.json +0 -58
- package//345/267/245/345/205/267/vscode-cocn/syntaxes/coCN.tmLanguage.json +0 -130
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/273/243/347/240/201/347/224/237/346/210/220/345/231/250.js +0 -2284
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/analysis.js +0 -116
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/benchmark_demo.js +0 -75
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/dominance.js +0 -158
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/gvn.js +0 -45
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/index.js +0 -121
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/ir.js +0 -237
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/constProp.js +0 -126
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/copyProp.js +0 -57
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/cse.js +0 -62
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/dce.js +0 -68
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/devirt.js +0 -38
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/escape.js +0 -67
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/inline.js +0 -142
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/passes/loopOpt.js +0 -215
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/peephole.js +0 -42
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/regalloc.js +0 -212
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/scheduler.js +0 -86
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/spill.js +0 -78
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/ssa.js +0 -235
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/traceSched.js +0 -47
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250/unroll.js +0 -46
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/harness.js +0 -33
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/run_all.js +0 -11
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_constfold.js +0 -58
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_dce.js +0 -49
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_e2e.js +0 -57
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_gvn_spill.js +0 -45
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_loop.js +0 -46
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_regalloc.js +0 -51
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//344/274/230/345/214/226/345/231/250//346/265/213/350/257/225/test_ssa.js +0 -65
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//345/205/245/345/217/243.c +0 -9
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//345/205/245/345/217/243.js +0 -38
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237/AST/345/267/245/345/205/267.js +0 -126
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237/index.js +0 -103
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//345/206/205/350/201/224.js +0 -194
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//345/207/275/346/225/260/347/211/271/345/214/226.js +0 -168
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/243/200/346/237/245/346/266/210/351/231/244.js +0 -166
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//345/206/205/350/201/224/346/265/213/350/257/225.js +0 -53
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//345/207/275/346/225/260/347/211/271/345/214/226/346/265/213/350/257/225.js +0 -67
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//347/253/257/345/210/260/347/253/257/346/265/213/350/257/225.js +0 -104
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//347/261/273/345/236/213/346/216/250/346/226/255/346/265/213/350/257/225.js +0 -87
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//350/277/220/350/241/214/346/265/213/350/257/225.js +0 -43
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//346/265/213/350/257/225//351/200/203/351/200/270/345/210/206/346/236/220/346/265/213/350/257/225.js +0 -58
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/211/271/345/214/226/347/274/223/345/255/230.js +0 -83
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/216/257/345/242/203.js +0 -126
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/261/273/345/236/213.js +0 -294
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//347/261/273/345/236/213/346/216/250/346/226/255.js +0 -461
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//350/260/203/347/224/250/345/233/276.js +0 -119
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/261/273/345/236/213/347/263/273/347/273/237//351/200/203/351/200/270/345/210/206/346/236/220.js +0 -184
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//347/274/226/350/257/221.sh +0 -45
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/alloc.c +0 -206
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/build.sh +0 -70
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/dict.c +0 -143
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/list.c +0 -148
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/math.c +0 -129
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/print.c +0 -135
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/rt.h +0 -177
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/string.c +0 -288
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/syscall.asm +0 -153
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266/test.c +0 -228
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266//344/270/264/346/227/266.c +0 -379
- package//345/267/245/345/205/267//345/216/237/347/224/237/347/274/226/350/257/221/345/231/250//350/277/220/350/241/214/346/227/266.c +0 -441
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//346/267/261/345/272/246/346/257/224/350/276/203.js +0 -51
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/Empty.java +0 -1
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.c +0 -53
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.co +0 -42
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.go +0 -43
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.java +0 -29
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.js +0 -24
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.py +0 -36
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/bench.rs +0 -35
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty.co +0 -0
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_c.c +0 -1
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_go.go +0 -2
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224/empty_rs.rs +0 -1
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/213/274/346/216/245.c +0 -32
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/213/274/346/216/245.co +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/216/222/345/272/217.c +0 -23
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/216/222/345/272/217.co +0 -31
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/226/220/346/263/242/351/202/243/345/245/221.c +0 -17
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/226/220/346/263/242/351/202/243/345/245/221.co +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/346/226/220/346/263/242/351/202/243/345/245/22120.co +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/347/264/257/345/212/240.c +0 -14
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200_/347/264/257/345/212/240.co +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/257/271C/350/257/255/350/250/200/345/257/271/346/257/224/347/273/223/346/236/234.txt +0 -164
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/fib.c +0 -9
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/fib.co +0 -2
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/sum.c +0 -10
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206/sum.co +0 -3
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206//350/256/241/346/265/213.sh +0 -32
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//345/276/256/345/237/272/345/207/206//350/256/241/346/265/213/347/273/223/346/236/234.txt +0 -7
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//350/241/245/346/265/213.sh +0 -45
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//350/256/241/346/265/213.sh +0 -163
- package//345/267/245/345/205/267//350/257/255/350/250/200/345/257/271/346/257/224//350/256/241/346/265/213/347/273/223/346/236/234.txt +0 -29
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* coCN 基础 LSP 服务器(纯 Node.js,无外部依赖)
|
|
4
|
-
*
|
|
5
|
-
* 协议:JSON-RPC 2.0 over stdio,使用 LSP 的 Content-Length 分帧。
|
|
6
|
-
*
|
|
7
|
-
* 支持能力:
|
|
8
|
-
* - textDocument/didOpen / didChange:维护文档内容
|
|
9
|
-
* - textDocument/definition:跳转函数定义(匹配「函数 名」或「名 = 函数」模式)
|
|
10
|
-
* - textDocument/hover:悬停显示标识符附近的类型标注
|
|
11
|
-
* - textDocument/diagnostics:写入临时文件,调用 bin/cocn.js 捕获 stderr
|
|
12
|
-
*
|
|
13
|
-
* 用法:node server.js
|
|
14
|
-
*/
|
|
15
|
-
'use strict';
|
|
16
|
-
|
|
17
|
-
const fs = require('fs');
|
|
18
|
-
const path = require('path');
|
|
19
|
-
const os = require('os');
|
|
20
|
-
const { spawn } = require('child_process');
|
|
21
|
-
|
|
22
|
-
// ---------- 文档存储 ----------
|
|
23
|
-
/** @type {Map<string, {text: string, version: number}>} */
|
|
24
|
-
const documents = new Map();
|
|
25
|
-
|
|
26
|
-
// ---------- 项目根目录检测 ----------
|
|
27
|
-
// server.js 位于 <项目根>/lsp/server.js
|
|
28
|
-
const PROJECT_ROOT = path.resolve(__dirname, '..');
|
|
29
|
-
const COCN_JS = path.join(PROJECT_ROOT, 'bin', 'cocn.js');
|
|
30
|
-
|
|
31
|
-
// ---------- JSON-RPC 分帧(使用 Buffer 按字节处理,兼容中文)----------
|
|
32
|
-
let rpcBuffer = Buffer.alloc(0);
|
|
33
|
-
|
|
34
|
-
process.stdin.on('data', (chunk) => {
|
|
35
|
-
rpcBuffer = Buffer.concat([rpcBuffer, chunk]);
|
|
36
|
-
// 查找 \r\n\r\n(头部与正文的分隔)
|
|
37
|
-
while (true) {
|
|
38
|
-
const sepIdx = rpcBuffer.indexOf('\r\n\r\n');
|
|
39
|
-
if (sepIdx === -1) break;
|
|
40
|
-
const headerSection = rpcBuffer.slice(0, sepIdx).toString('utf8');
|
|
41
|
-
const m = headerSection.match(/Content-Length:\s*(\d+)/i);
|
|
42
|
-
if (!m) {
|
|
43
|
-
// 无效头部,跳过这一段
|
|
44
|
-
rpcBuffer = rpcBuffer.slice(sepIdx + 4);
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
const contentLength = parseInt(m[1], 10);
|
|
48
|
-
const bodyStart = sepIdx + 4;
|
|
49
|
-
if (rpcBuffer.length < bodyStart + contentLength) break; // 等待更多数据
|
|
50
|
-
const bodyBuf = rpcBuffer.slice(bodyStart, bodyStart + contentLength);
|
|
51
|
-
rpcBuffer = rpcBuffer.slice(bodyStart + contentLength);
|
|
52
|
-
try {
|
|
53
|
-
const msg = JSON.parse(bodyBuf.toString('utf8'));
|
|
54
|
-
handleMessage(msg);
|
|
55
|
-
} catch (e) {
|
|
56
|
-
console.error('[LSP] 解析消息失败:', e.message);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
function sendResponse(id, result) {
|
|
62
|
-
const body = JSON.stringify({ jsonrpc: '2.0', id, result });
|
|
63
|
-
writeRpc(body);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function sendError(id, code, message) {
|
|
67
|
-
const body = JSON.stringify({
|
|
68
|
-
jsonrpc: '2.0',
|
|
69
|
-
id,
|
|
70
|
-
error: { code, message },
|
|
71
|
-
});
|
|
72
|
-
writeRpc(body);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function sendNotification(method, params) {
|
|
76
|
-
const body = JSON.stringify({ jsonrpc: '2.0', method, params });
|
|
77
|
-
writeRpc(body);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function writeRpc(body) {
|
|
81
|
-
const header = `Content-Length: ${Buffer.byteLength(body)}\r\n\r\n`;
|
|
82
|
-
process.stdout.write(header + body);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// ---------- 消息分发 ----------
|
|
86
|
-
function handleMessage(msg) {
|
|
87
|
-
const { id, method, params } = msg;
|
|
88
|
-
|
|
89
|
-
switch (method) {
|
|
90
|
-
case 'initialize':
|
|
91
|
-
sendResponse(id, {
|
|
92
|
-
capabilities: {
|
|
93
|
-
textDocumentSync: 1, // Full sync
|
|
94
|
-
definitionProvider: true,
|
|
95
|
-
hoverProvider: true,
|
|
96
|
-
diagnosticProvider: true,
|
|
97
|
-
},
|
|
98
|
-
serverInfo: { name: 'coCN LSP', version: '0.0.1' },
|
|
99
|
-
});
|
|
100
|
-
break;
|
|
101
|
-
|
|
102
|
-
case 'initialized':
|
|
103
|
-
// 客户端已初始化,无需响应
|
|
104
|
-
break;
|
|
105
|
-
|
|
106
|
-
case 'shutdown':
|
|
107
|
-
sendResponse(id, null);
|
|
108
|
-
break;
|
|
109
|
-
|
|
110
|
-
case 'exit':
|
|
111
|
-
process.exit(0);
|
|
112
|
-
break;
|
|
113
|
-
|
|
114
|
-
case 'textDocument/didOpen':
|
|
115
|
-
didOpen(params);
|
|
116
|
-
break;
|
|
117
|
-
|
|
118
|
-
case 'textDocument/didChange':
|
|
119
|
-
didChange(params);
|
|
120
|
-
break;
|
|
121
|
-
|
|
122
|
-
case 'textDocument/definition':
|
|
123
|
-
handleDefinition(id, params);
|
|
124
|
-
break;
|
|
125
|
-
|
|
126
|
-
case 'textDocument/hover':
|
|
127
|
-
handleHover(id, params);
|
|
128
|
-
break;
|
|
129
|
-
|
|
130
|
-
case 'textDocument/didSave':
|
|
131
|
-
// 保存时触发诊断
|
|
132
|
-
runDiagnostics(params.textDocument.uri);
|
|
133
|
-
break;
|
|
134
|
-
|
|
135
|
-
default:
|
|
136
|
-
// 未知方法,通知客户端
|
|
137
|
-
if (id !== undefined) {
|
|
138
|
-
sendError(id, -32601, `Method not found: ${method}`);
|
|
139
|
-
}
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// ---------- 文档管理 ----------
|
|
145
|
-
function didOpen(params) {
|
|
146
|
-
const uri = params.textDocument.uri;
|
|
147
|
-
documents.set(uri, {
|
|
148
|
-
text: params.textDocument.text,
|
|
149
|
-
version: params.textDocument.version || 1,
|
|
150
|
-
});
|
|
151
|
-
// 打开时自动跑一次诊断
|
|
152
|
-
runDiagnostics(uri);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function didChange(params) {
|
|
156
|
-
const uri = params.textDocument.uri;
|
|
157
|
-
const doc = documents.get(uri);
|
|
158
|
-
if (!doc) return;
|
|
159
|
-
// LSP 设置为 Full sync,contentChanges[0].text 是完整文本
|
|
160
|
-
if (params.contentChanges && params.contentChanges.length > 0) {
|
|
161
|
-
doc.text = params.contentChanges[0].text;
|
|
162
|
-
doc.version = params.textDocument.version || doc.version + 1;
|
|
163
|
-
}
|
|
164
|
-
// 防抖诊断:延迟 500ms
|
|
165
|
-
scheduleDiagnostics(uri);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// 简单防抖
|
|
169
|
-
const pendingTimers = new Map();
|
|
170
|
-
function scheduleDiagnostics(uri) {
|
|
171
|
-
if (pendingTimers.has(uri)) clearTimeout(pendingTimers.get(uri));
|
|
172
|
-
pendingTimers.set(uri, setTimeout(() => {
|
|
173
|
-
pendingTimers.delete(uri);
|
|
174
|
-
runDiagnostics(uri);
|
|
175
|
-
}, 500));
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// ---------- 跳转定义 ----------
|
|
179
|
-
/**
|
|
180
|
-
* 解析光标位置的标识符,查找其定义。
|
|
181
|
-
* 匹配模式:
|
|
182
|
-
* 1. 函数 函数名( ... ) —— 函数定义
|
|
183
|
-
* 2. 变量 名 = ... —— 变量声明
|
|
184
|
-
* 3. 常量 名 = ... —— 常量声明
|
|
185
|
-
* 4. 名 = 函数( ... ) —— 匿名函数赋值
|
|
186
|
-
*/
|
|
187
|
-
function handleDefinition(id, params) {
|
|
188
|
-
const uri = params.textDocument.uri;
|
|
189
|
-
const doc = documents.get(uri);
|
|
190
|
-
if (!doc) {
|
|
191
|
-
sendResponse(id, null);
|
|
192
|
-
return;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const lines = doc.text.split('\n');
|
|
196
|
-
const line = params.position.line;
|
|
197
|
-
const char = params.position.character;
|
|
198
|
-
|
|
199
|
-
// 从当前行提取光标下的标识符
|
|
200
|
-
const currentLine = lines[line] || '';
|
|
201
|
-
const word = extractWordAt(currentLine, char);
|
|
202
|
-
if (!word) {
|
|
203
|
-
sendResponse(id, null);
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// 在全文中搜索定义
|
|
208
|
-
const def = findDefinition(doc.text, word);
|
|
209
|
-
if (def) {
|
|
210
|
-
sendResponse(id, {
|
|
211
|
-
uri: uri,
|
|
212
|
-
range: {
|
|
213
|
-
start: { line: def.line, character: def.col },
|
|
214
|
-
end: { line: def.line, character: def.col + def.length },
|
|
215
|
-
},
|
|
216
|
-
});
|
|
217
|
-
} else {
|
|
218
|
-
sendResponse(id, null);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function extractWordAt(line, char) {
|
|
223
|
-
// 标识符字符:字母、数字、下划线、中文
|
|
224
|
-
const isWordChar = (c) => /[A-Za-z0-9_\u4e00-\u9fff]/.test(c);
|
|
225
|
-
if (char > 0 && !isWordChar(line[char])) char--;
|
|
226
|
-
let start = char;
|
|
227
|
-
let end = char;
|
|
228
|
-
while (start > 0 && isWordChar(line[start - 1])) start--;
|
|
229
|
-
while (end < line.length && isWordChar(line[end])) end++;
|
|
230
|
-
if (start === end) return null;
|
|
231
|
-
return line.substring(start, end);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function findDefinition(text, word) {
|
|
235
|
-
const lines = text.split('\n');
|
|
236
|
-
// 模式 1: 函数 函数名(
|
|
237
|
-
const fnRe = new RegExp(`^\\s*函数\\s+${escapeRegex(word)}\\s*\\(`);
|
|
238
|
-
// 模式 2: 变量 名 =
|
|
239
|
-
const varRe = new RegExp(`^\\s*变量\\s+${escapeRegex(word)}\\s*=`);
|
|
240
|
-
// 模式 3: 常量 名 =
|
|
241
|
-
const constRe = new RegExp(`^\\s*常量\\s+${escapeRegex(word)}\\s*=`);
|
|
242
|
-
// 模式 4: 名 = 函数(
|
|
243
|
-
const assignRe = new RegExp(`^\\s*${escapeRegex(word)}\\s*=\\s*函数\\s*\\(`);
|
|
244
|
-
|
|
245
|
-
for (let i = 0; i < lines.length; i++) {
|
|
246
|
-
const line = lines[i];
|
|
247
|
-
let m;
|
|
248
|
-
if ((m = line.match(fnRe))) {
|
|
249
|
-
const idx = line.indexOf(word, line.search(/函数/));
|
|
250
|
-
return { line: i, col: idx, length: word.length };
|
|
251
|
-
}
|
|
252
|
-
if ((m = line.match(varRe))) {
|
|
253
|
-
const idx = line.indexOf(word, line.search(/变量/));
|
|
254
|
-
return { line: i, col: idx, length: word.length };
|
|
255
|
-
}
|
|
256
|
-
if ((m = line.match(constRe))) {
|
|
257
|
-
const idx = line.indexOf(word, line.search(/常量/));
|
|
258
|
-
return { line: i, col: idx, length: word.length };
|
|
259
|
-
}
|
|
260
|
-
if ((m = line.match(assignRe))) {
|
|
261
|
-
const idx = line.indexOf(word);
|
|
262
|
-
return { line: i, col: idx, length: word.length };
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
return null;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
function escapeRegex(s) {
|
|
269
|
-
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
// ---------- 悬停 ----------
|
|
273
|
-
function handleHover(id, params) {
|
|
274
|
-
const uri = params.textDocument.uri;
|
|
275
|
-
const doc = documents.get(uri);
|
|
276
|
-
if (!doc) {
|
|
277
|
-
sendResponse(id, { contents: [] });
|
|
278
|
-
return;
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const lines = doc.text.split('\n');
|
|
282
|
-
const line = params.position.line;
|
|
283
|
-
const char = params.position.character;
|
|
284
|
-
const word = extractWordAt(lines[line] || '', char);
|
|
285
|
-
|
|
286
|
-
if (!word) {
|
|
287
|
-
sendResponse(id, { contents: [] });
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
// 查找定义,附带类型标注
|
|
292
|
-
const def = findDefinition(doc.text, word);
|
|
293
|
-
if (def) {
|
|
294
|
-
const defLine = lines[def.line] || '';
|
|
295
|
-
// 尝试提取返回类型标注(-> 后面的部分)
|
|
296
|
-
let typeInfo = '';
|
|
297
|
-
const arrowMatch = defLine.match(/->\s*(\S+)/);
|
|
298
|
-
if (arrowMatch) typeInfo = `\n\n返回类型: \`${arrowMatch[1]}\``;
|
|
299
|
-
|
|
300
|
-
// 尝试提取参数类型
|
|
301
|
-
const paramMatch = defLine.match(/\(([^)]*)\)/);
|
|
302
|
-
let paramInfo = '';
|
|
303
|
-
if (paramMatch && paramMatch[1].trim()) {
|
|
304
|
-
paramInfo = `\n参数: \`${paramMatch[1].trim()}\``;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
sendResponse(id, {
|
|
308
|
-
contents: {
|
|
309
|
-
kind: 'markdown',
|
|
310
|
-
value: `**${word}** (coCN 定义)${paramInfo}${typeInfo}\n\n第 ${def.line + 1} 行`,
|
|
311
|
-
},
|
|
312
|
-
});
|
|
313
|
-
} else {
|
|
314
|
-
// 检查是否是关键字
|
|
315
|
-
const keywords = ['让','变量','常量','函数','返回','如果','否则','若','当','循环','对于','从','到','步长','结束','跳出','继续','真','假','且','或','非','导入','匹配','是','默认','于','在','那么','尝试','捕获','最终','抛出','并发','等待','则','整数','浮点','布尔','字符串','列表','字典','输出','断言','断言相等'];
|
|
316
|
-
if (keywords.includes(word)) {
|
|
317
|
-
sendResponse(id, {
|
|
318
|
-
contents: {
|
|
319
|
-
kind: 'markdown',
|
|
320
|
-
value: `**关键字**: \`${word}\``,
|
|
321
|
-
},
|
|
322
|
-
});
|
|
323
|
-
} else {
|
|
324
|
-
sendResponse(id, { contents: [] });
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
// ---------- 诊断(调用 coCN 解释器)----------
|
|
330
|
-
function runDiagnostics(uri) {
|
|
331
|
-
const doc = documents.get(uri);
|
|
332
|
-
if (!doc) return;
|
|
333
|
-
|
|
334
|
-
// 如果解释器不存在,跳过
|
|
335
|
-
if (!fs.existsSync(COCN_JS)) {
|
|
336
|
-
// 静默跳过,不报错
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
// 写入临时文件
|
|
341
|
-
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cocn-lsp-'));
|
|
342
|
-
const tmpFile = path.join(tmpDir, 'diagnose.co');
|
|
343
|
-
fs.writeFileSync(tmpFile, doc.text, 'utf-8');
|
|
344
|
-
|
|
345
|
-
// 运行 coCN 解释器
|
|
346
|
-
const child = spawn('node', [COCN_JS, tmpFile], {
|
|
347
|
-
cwd: PROJECT_ROOT,
|
|
348
|
-
env: process.env,
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
let stderr = '';
|
|
352
|
-
let stdout = '';
|
|
353
|
-
|
|
354
|
-
child.stderr.on('data', (d) => (stderr += d.toString()));
|
|
355
|
-
child.stdout.on('data', (d) => (stdout += d.toString()));
|
|
356
|
-
|
|
357
|
-
child.on('exit', (code) => {
|
|
358
|
-
// 清理临时文件
|
|
359
|
-
try { fs.unlinkSync(tmpFile); fs.rmdirSync(tmpDir); } catch (e) {}
|
|
360
|
-
|
|
361
|
-
const diagnostics = [];
|
|
362
|
-
|
|
363
|
-
if (code !== 0 && stderr) {
|
|
364
|
-
// 解析 stderr 中的错误信息
|
|
365
|
-
const lines = stderr.split('\n');
|
|
366
|
-
for (const line of lines) {
|
|
367
|
-
// 尝试匹配行号信息,例如 "第 3 行" 或 "line 3"
|
|
368
|
-
const lineMatch = line.match(/第\s*(\d+)\s*行/);
|
|
369
|
-
if (lineMatch) {
|
|
370
|
-
const errLine = parseInt(lineMatch[1], 10) - 1; // 0-based
|
|
371
|
-
diagnostics.push({
|
|
372
|
-
severity: 1, // Error
|
|
373
|
-
range: {
|
|
374
|
-
start: { line: errLine, character: 0 },
|
|
375
|
-
end: { line: errLine, character: 100 },
|
|
376
|
-
},
|
|
377
|
-
message: line.trim(),
|
|
378
|
-
source: 'coCN',
|
|
379
|
-
});
|
|
380
|
-
} else if (line.trim() && !line.includes('[coCN]')) {
|
|
381
|
-
// 没有行号的错误,放在第 0 行
|
|
382
|
-
diagnostics.push({
|
|
383
|
-
severity: 1,
|
|
384
|
-
range: {
|
|
385
|
-
start: { line: 0, character: 0 },
|
|
386
|
-
end: { line: 0, character: 100 },
|
|
387
|
-
},
|
|
388
|
-
message: line.trim(),
|
|
389
|
-
source: 'coCN',
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// 发布诊断
|
|
396
|
-
sendNotification('textDocument/publishDiagnostics', {
|
|
397
|
-
uri: uri,
|
|
398
|
-
diagnostics: diagnostics,
|
|
399
|
-
});
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
child.on('error', (err) => {
|
|
403
|
-
try { fs.unlinkSync(tmpFile); fs.rmdirSync(tmpDir); } catch (e) {}
|
|
404
|
-
sendNotification('textDocument/publishDiagnostics', {
|
|
405
|
-
uri: uri,
|
|
406
|
-
diagnostics: [
|
|
407
|
-
{
|
|
408
|
-
severity: 1,
|
|
409
|
-
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 10 } },
|
|
410
|
-
message: `无法运行 coCN 解释器: ${err.message}`,
|
|
411
|
-
source: 'coCN LSP',
|
|
412
|
-
},
|
|
413
|
-
],
|
|
414
|
-
});
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// ---------- 启动 ----------
|
|
419
|
-
console.error('[coCN LSP] 服务器已启动,监听 stdio');
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
#!/bin/bash
|
|
2
|
-
# coCN LSP 服务器测试脚本
|
|
3
|
-
# 通过管道向 stdio LSP 服务器发送示例 JSON-RPC 消息,验证响应。
|
|
4
|
-
set -e
|
|
5
|
-
|
|
6
|
-
LSP_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
7
|
-
SERVER="$LSP_DIR/server.js"
|
|
8
|
-
|
|
9
|
-
echo "=== coCN LSP 服务器测试 ==="
|
|
10
|
-
echo "服务器路径: $SERVER"
|
|
11
|
-
echo ""
|
|
12
|
-
|
|
13
|
-
# 构造一个测试 coCN 文件内容
|
|
14
|
-
TEST_DOC='函数 加(整数 a, 整数 b) -> 整数
|
|
15
|
-
返回 a + b
|
|
16
|
-
结束
|
|
17
|
-
|
|
18
|
-
输出 加(3, 5)
|
|
19
|
-
'
|
|
20
|
-
|
|
21
|
-
# 构造 JSON-RPC 消息
|
|
22
|
-
# 1. initialize
|
|
23
|
-
INIT=$(printf '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"capabilities":{},"processId":%d}}' $$)
|
|
24
|
-
|
|
25
|
-
# 2. initialized notification
|
|
26
|
-
INITIALIZED='{"jsonrpc":"2.0","method":"initialized","params":{}}'
|
|
27
|
-
|
|
28
|
-
# 3. didOpen
|
|
29
|
-
DIDOPEN=$(cat <<EOF
|
|
30
|
-
{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{"textDocument":{"uri":"file:///tmp/test.co","languageId":"cocn","version":1,"text":$(node -e "console.log(JSON.stringify(process.argv[1]))" "$TEST_DOC")}}}
|
|
31
|
-
EOF
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
# 4. definition 请求(查找 "加" 的定义;第4行 `输出 加(3, 5)`,"加" 在第3列)
|
|
35
|
-
DEFINITION=$(cat <<EOF
|
|
36
|
-
{"jsonrpc":"2.0","id":2,"method":"textDocument/definition","params":{"textDocument":{"uri":"file:///tmp/test.co"},"position":{"line":4,"character":3}}}
|
|
37
|
-
EOF
|
|
38
|
-
)
|
|
39
|
-
|
|
40
|
-
# 5. hover 请求(同样悬停 "加")
|
|
41
|
-
HOVER=$(cat <<EOF
|
|
42
|
-
{"jsonrpc":"2.0","id":3,"method":"textDocument/hover","params":{"textDocument":{"uri":"file:///tmp/test.co"},"position":{"line":4,"character":3}}}
|
|
43
|
-
EOF
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
# 6. shutdown
|
|
47
|
-
SHUTDOWN='{"jsonrpc":"2.0","id":4,"method":"shutdown","params":null}'
|
|
48
|
-
EXIT='{"jsonrpc":"2.0","method":"exit"}'
|
|
49
|
-
|
|
50
|
-
# 打包成带 Content-Length 头的消息序列
|
|
51
|
-
pack() {
|
|
52
|
-
local body="$1"
|
|
53
|
-
local len=$(printf '%s' "$body" | wc -c)
|
|
54
|
-
printf 'Content-Length: %d\r\n\r\n%s' "$len" "$body"
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
INPUT=""
|
|
58
|
-
INPUT="$INPUT$(pack "$INIT")"
|
|
59
|
-
INPUT="$INPUT$(pack "$INITIALIZED")"
|
|
60
|
-
INPUT="$INPUT$(pack "$DIDOPEN")"
|
|
61
|
-
INPUT="$INPUT$(pack "$DEFINITION")"
|
|
62
|
-
INPUT="$INPUT$(pack "$HOVER")"
|
|
63
|
-
INPUT="$INPUT$(pack "$SHUTDOWN")"
|
|
64
|
-
INPUT="$INPUT$(pack "$EXIT")"
|
|
65
|
-
|
|
66
|
-
echo "--- 发送消息序列 ---"
|
|
67
|
-
echo "1. initialize"
|
|
68
|
-
echo "2. initialized"
|
|
69
|
-
echo "3. textDocument/didOpen"
|
|
70
|
-
echo "4. textDocument/definition (查找 加)"
|
|
71
|
-
echo "5. textDocument/hover (悬停 加)"
|
|
72
|
-
echo "6. shutdown + exit"
|
|
73
|
-
echo ""
|
|
74
|
-
echo "--- 服务器输出 ---"
|
|
75
|
-
|
|
76
|
-
echo "$INPUT" | node "$SERVER" 2>/dev/null | while IFS= read -r line; do
|
|
77
|
-
echo "$line"
|
|
78
|
-
done
|
|
79
|
-
|
|
80
|
-
echo ""
|
|
81
|
-
echo "=== 测试完成 ==="
|
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const vscode = require('vscode');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const { spawn } = require('child_process');
|
|
7
|
-
|
|
8
|
-
let lspProcess = null;
|
|
9
|
-
let lspConnection = null;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 激活扩展:启动 LSP 服务器并建立 stdio 通信
|
|
13
|
-
* @param {vscode.ExtensionContext} context
|
|
14
|
-
*/
|
|
15
|
-
function activate(context) {
|
|
16
|
-
console.log('coCN 扩展已激活');
|
|
17
|
-
|
|
18
|
-
// 启动 LSP 服务器
|
|
19
|
-
startLspServer(context);
|
|
20
|
-
|
|
21
|
-
// 注册命令:重新启动 LSP
|
|
22
|
-
const restartCmd = vscode.commands.registerCommand('cocn.restartServer', () => {
|
|
23
|
-
stopLspServer();
|
|
24
|
-
startLspServer(context);
|
|
25
|
-
vscode.window.showInformationMessage('coCN LSP 服务器已重启');
|
|
26
|
-
});
|
|
27
|
-
context.subscriptions.push(restartCmd);
|
|
28
|
-
|
|
29
|
-
// 注册命令:手动诊断
|
|
30
|
-
const checkCmd = vscode.commands.registerCommand('cocn.checkFile', () => {
|
|
31
|
-
const editor = vscode.window.activeTextEditor;
|
|
32
|
-
if (!editor) return;
|
|
33
|
-
const doc = editor.document;
|
|
34
|
-
if (doc.languageId !== 'cocn') return;
|
|
35
|
-
// 触发 didChange 来重新诊断
|
|
36
|
-
vscode.commands.executeCommand('cocn.restartServer');
|
|
37
|
-
});
|
|
38
|
-
context.subscriptions.push(checkCmd);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function startLspServer(context) {
|
|
42
|
-
const config = vscode.workspace.getConfiguration('cocn');
|
|
43
|
-
let serverPath = config.get('lspPath', '');
|
|
44
|
-
|
|
45
|
-
// 自动查找 server.js
|
|
46
|
-
if (!serverPath) {
|
|
47
|
-
const candidates = [
|
|
48
|
-
path.join(context.extensionPath, '..', 'lsp', 'server.js'),
|
|
49
|
-
path.join(context.extensionPath, 'lsp', 'server.js'),
|
|
50
|
-
];
|
|
51
|
-
for (const c of candidates) {
|
|
52
|
-
if (fs.existsSync(c)) {
|
|
53
|
-
serverPath = c;
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!serverPath || !fs.existsSync(serverPath)) {
|
|
60
|
-
vscode.window.showWarningMessage(
|
|
61
|
-
'未找到 coCN LSP 服务器 (server.js)。请在设置中指定 cocn.lspPath。语法高亮仍可正常使用。'
|
|
62
|
-
);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
console.log('启动 LSP 服务器:', serverPath);
|
|
67
|
-
|
|
68
|
-
lspProcess = spawn('node', [serverPath], {
|
|
69
|
-
cwd: path.dirname(path.dirname(serverPath)), // 项目根目录
|
|
70
|
-
stdio: ['pipe', 'pipe', 'pipe'],
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
lspProcess.stderr.on('data', (data) => {
|
|
74
|
-
console.error('[LSP stderr]', data.toString());
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
lspProcess.on('exit', (code) => {
|
|
78
|
-
console.log('LSP 服务器退出,代码:', code);
|
|
79
|
-
lspProcess = null;
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
// 初始化
|
|
83
|
-
sendLspRequest({
|
|
84
|
-
jsonrpc: '2.0',
|
|
85
|
-
id: 1,
|
|
86
|
-
method: 'initialize',
|
|
87
|
-
params: {
|
|
88
|
-
processId: process.pid,
|
|
89
|
-
rootUri: vscode.workspace.rootPath ? vscode.Uri.file(vscode.workspace.rootPath).toString() : null,
|
|
90
|
-
capabilities: {
|
|
91
|
-
textDocument: {
|
|
92
|
-
synchronization: { didOpen: true, didChange: true },
|
|
93
|
-
definition: true,
|
|
94
|
-
hover: true,
|
|
95
|
-
publishDiagnostics: true,
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
// 绑定文档事件
|
|
102
|
-
context.subscriptions.push(
|
|
103
|
-
vscode.workspace.onDidOpenTextDocument((doc) => {
|
|
104
|
-
if (doc.languageId !== 'cocn') return;
|
|
105
|
-
sendLspNotification('textDocument/didOpen', {
|
|
106
|
-
textDocument: {
|
|
107
|
-
uri: doc.uri.toString(),
|
|
108
|
-
languageId: doc.languageId,
|
|
109
|
-
version: doc.version,
|
|
110
|
-
text: doc.getText(),
|
|
111
|
-
},
|
|
112
|
-
});
|
|
113
|
-
})
|
|
114
|
-
);
|
|
115
|
-
|
|
116
|
-
context.subscriptions.push(
|
|
117
|
-
vscode.workspace.onDidChangeTextDocument((event) => {
|
|
118
|
-
if (event.document.languageId !== 'cocn') return;
|
|
119
|
-
sendLspNotification('textDocument/didChange', {
|
|
120
|
-
textDocument: {
|
|
121
|
-
uri: event.document.uri.toString(),
|
|
122
|
-
version: event.document.version,
|
|
123
|
-
},
|
|
124
|
-
contentChanges: [
|
|
125
|
-
{
|
|
126
|
-
range: null,
|
|
127
|
-
text: event.document.getText(),
|
|
128
|
-
},
|
|
129
|
-
],
|
|
130
|
-
});
|
|
131
|
-
})
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
// 监听来自服务器的诊断(用 Buffer 按字节分帧,兼容中文)
|
|
135
|
-
let buffer = Buffer.alloc(0);
|
|
136
|
-
lspProcess.stdout.on('data', (data) => {
|
|
137
|
-
buffer = Buffer.concat([buffer, data]);
|
|
138
|
-
let idx;
|
|
139
|
-
while ((idx = buffer.indexOf('\r\n\r\n')) !== -1) {
|
|
140
|
-
const headerPart = buffer.slice(0, idx).toString('utf8');
|
|
141
|
-
const m = headerPart.match(/Content-Length:\s*(\d+)/i);
|
|
142
|
-
if (!m) {
|
|
143
|
-
buffer = buffer.slice(idx + 4);
|
|
144
|
-
continue;
|
|
145
|
-
}
|
|
146
|
-
const len = parseInt(m[1], 10);
|
|
147
|
-
const bodyStart = idx + 4;
|
|
148
|
-
if (buffer.length < bodyStart + len) break;
|
|
149
|
-
const body = buffer.slice(bodyStart, bodyStart + len).toString('utf8');
|
|
150
|
-
buffer = buffer.slice(bodyStart + len);
|
|
151
|
-
try {
|
|
152
|
-
const msg = JSON.parse(body);
|
|
153
|
-
handleLspMessage(msg);
|
|
154
|
-
} catch (e) {
|
|
155
|
-
console.error('解析 LSP 消息失败:', e);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function handleLspMessage(msg) {
|
|
162
|
-
if (msg.method === 'textDocument/publishDiagnostics' && msg.params) {
|
|
163
|
-
const { uri, diagnostics } = msg.params;
|
|
164
|
-
// 转换并显示诊断
|
|
165
|
-
const converted = (diagnostics || []).map((d) => ({
|
|
166
|
-
severity: d.severity || 1,
|
|
167
|
-
range: {
|
|
168
|
-
start: { line: d.range.start.line, character: d.range.start.character },
|
|
169
|
-
end: { line: d.range.end.line, character: d.range.end.character },
|
|
170
|
-
},
|
|
171
|
-
message: d.message,
|
|
172
|
-
source: 'coCN',
|
|
173
|
-
}));
|
|
174
|
-
const collection = vscode.languages.createDiagnosticCollection('cocn');
|
|
175
|
-
collection.set(vscode.Uri.parse(uri), converted);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function sendLspRequest(req) {
|
|
180
|
-
if (!lspProcess || !lspProcess.stdin.writable) return;
|
|
181
|
-
const body = JSON.stringify(req);
|
|
182
|
-
const header = `Content-Length: ${Buffer.byteLength(body)}\r\n\r\n`;
|
|
183
|
-
lspProcess.stdin.write(header + body);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function sendLspNotification(method, params) {
|
|
187
|
-
sendLspRequest({ jsonrpc: '2.0', method, params });
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function stopLspServer() {
|
|
191
|
-
if (lspProcess) {
|
|
192
|
-
try {
|
|
193
|
-
lspProcess.kill();
|
|
194
|
-
} catch (e) {}
|
|
195
|
-
lspProcess = null;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function deactivate() {
|
|
200
|
-
stopLspServer();
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
module.exports = { activate, deactivate };
|