Skip to content

Commit d9b6493

Browse files
committed
feat: create @volar/coc.nvim and export middleware
volarjs#12
1 parent 7b930a6 commit d9b6493

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

packages/coc.nvim/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023-present Johnson Chu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/coc.nvim/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "@volar/coc.nvim",
3+
"version": "1.4.0-alpha.2",
4+
"main": "out/index.js",
5+
"license": "MIT",
6+
"files": [
7+
"out/**/*.js",
8+
"out/**/*.d.ts"
9+
],
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/volarjs/volar.js.git",
13+
"directory": "packages/coc.nvim"
14+
},
15+
"dependencies": {
16+
"@volar/language-server": "1.4.0-alpha.2"
17+
},
18+
"devDependencies": {
19+
"coc.nvim": "latest"
20+
},
21+
"peerDependencies": {
22+
"coc.nvim": "*"
23+
}
24+
}

packages/coc.nvim/src/index.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as coc from 'coc.nvim';
2+
import { Location } from '@volar/language-server';
3+
4+
// TODO: @volar/vscode/src/features/*
5+
6+
export const middleware: coc.Middleware = {
7+
async provideCodeActions(document, range, context, token, next) {
8+
let actions = await next(document, range, context, token);
9+
actions = actions?.map(action => {
10+
if (typeof action.command === 'object') {
11+
action.command = parseServerCommand(action.command);
12+
}
13+
return action;
14+
});
15+
return actions;
16+
},
17+
async resolveCodeAction(item, token, next) {
18+
const action = await next(item, token);
19+
if (action?.command) {
20+
action.command = parseServerCommand(action.command);
21+
}
22+
return action;
23+
},
24+
async provideCodeLenses(document, token, next) {
25+
let codeLenses = await next(document, token);
26+
codeLenses = codeLenses?.map(action => {
27+
if (action.command) {
28+
action.command = parseServerCommand(action.command);
29+
}
30+
return action;
31+
});
32+
return codeLenses;
33+
},
34+
async resolveCodeLens(item, token, next) {
35+
const codeLens = await next(item, token);
36+
if (codeLens?.command) {
37+
codeLens.command = parseServerCommand(codeLens.command);
38+
}
39+
return codeLens;
40+
},
41+
};
42+
43+
export function parseServerCommand(command: coc.Command) {
44+
if (command.command === 'editor.action.rename' && command.arguments) {
45+
return {
46+
...command,
47+
// TODO: test editor.action.rename command behavior in coc.nvim
48+
arguments: [[
49+
coc.Uri.parse(command.arguments[0]),
50+
coc.Position.create(command.arguments[1].line, command.arguments[1].character),
51+
]],
52+
};
53+
}
54+
else if (command.command === 'editor.action.showReferences' && command.arguments) {
55+
return {
56+
...command,
57+
arguments: [
58+
coc.Uri.parse(command.arguments[0]),
59+
coc.Position.create(command.arguments[1].line, command.arguments[1].character),
60+
command.arguments[2].map((ref: Location) => coc.Location.create(
61+
ref.uri,
62+
coc.Range.create(ref.range.start.line, ref.range.start.character, ref.range.end.line, ref.range.end.character),
63+
)),
64+
],
65+
};
66+
}
67+
return command;
68+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": false,
5+
"outDir": "out",
6+
"rootDir": "src",
7+
},
8+
"include": [
9+
"src"
10+
],
11+
"references": [
12+
{
13+
"path": "../language-server/tsconfig.build.json"
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)