This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
104 lines (91 loc) · 2.77 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import {
LanguageClient,
WorkspaceConfiguration,
workspace,
ExtensionContext,
LanguageClientOptions,
StreamInfo,
ProvideCompletionItemsSignature
} from 'coc.nvim'
import {
CompletionContext,
TextDocument,
Position,
CompletionItem,
CompletionList
} from 'vscode-languageserver-protocol'
import {CancellationToken} from 'vscode-jsonrpc'
import {waitForSocket} from 'socket-retry-connect';
import {execFile} from 'child_process'
let client: LanguageClient
const logger = workspace.createOutputChannel("coc-erlang")
function startServer(serverPath: string, serverPort: number) {
return function () {
execFile(serverPath, [serverPort.toString()]);
logger.appendLine('waiting for erlang_ls to accept connection..');
return new Promise<StreamInfo>((resolve, reject) => {
waitForSocket({port: serverPort}, function (err, socket) {
if (err) {
reject(err);
}
else {
logger.appendLine('socket accepted, continuing.');
resolve({reader: socket, writer: socket});
}
});
});
};
};
export async function activate(context: ExtensionContext): Promise<void> {
const config: WorkspaceConfiguration = workspace.getConfiguration("erlang_ls")
const server_path: string = config.get<string>("erlang_ls_path")
const server_port: number = Number(config.get<number>("port"))
let clientOptions: LanguageClientOptions = {
documentSelector: [{scheme: 'file', language: 'erlang'}],
middleware: {
provideCompletionItem: async (
document: TextDocument,
position: Position,
context: CompletionContext,
token: CancellationToken,
next: ProvideCompletionItemsSignature
) => {
const res = await Promise.resolve(next(document, position, context, token));
let doc = workspace.getDocument(document.uri);
if (!doc)
return [];
if (res.hasOwnProperty('isIncomplete')) {
let itemList = res as CompletionList;
itemList.items.forEach(item => fixItem(item, position))
return itemList;
}
let items = res as CompletionItem[];
items.forEach(item => fixItem(item, position))
return items;
}
},
initializationOptions: ""
};
client = new LanguageClient(
"erlang_ls",
startServer(server_path, server_port),
clientOptions
)
client.start()
client.onReady().then(() => {
client.registerProposedFeatures()
workspace.showMessage("coc-erlang_ls is ready")
})
}
function fixItem(i: CompletionItem, pos: Position) {
i.textEdit = {
range: {start: pos, end: pos},
newText: i.label.replace(/\/[^}]*/, '')
}
}
export function deactivate(): Thenable<void> | undefined {
if (!client) {
return undefined;
}
return client.stop();
}