forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
187 lines (172 loc) · 6.84 KB
/
server.ts
File metadata and controls
187 lines (172 loc) · 6.84 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import {
IPCMessageReader, IPCMessageWriter,
createConnection, IConnection, TextDocumentSyncKind,
TextDocuments, ITextDocument, Diagnostic, DiagnosticSeverity,
InitializeParams, InitializeResult, TextDocumentIdentifier,
CompletionItem, CompletionItemKind, PublishDiagnosticsParams
} from 'vscode-languageserver';
import * as vscodeLanguageServer from 'vscode-languageserver';
import * as pylinter from './linters/pylint';
// Create a connection for the server. The connection uses
// stdin / stdout for message passing
let connection: IConnection = createConnection(new IPCMessageReader(process), new IPCMessageWriter(process));
var itHappened = "";
// Create a simple text document manager. The text document manager
// supports full document sync only
let documents: TextDocuments = new TextDocuments();
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);
// After the server has started the client sends an initilize request. The server receives
// in the passed params the rootPath of the workspace plus the client capabilites.
let workspaceRoot: string;
connection.onInitialize((params): InitializeResult => {
workspaceRoot = params.rootPath;
return {
capabilities: {
// Tell the client that the server works in FULL text document sync mode
textDocumentSync: documents.syncKind,
// Tell the client that the server support code complete
completionProvider: {
resolveProvider: true
}
}
}
});
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent((change) => {
validateTextDocument(change.document);
});
// The settings interface describe the server relevant settings part
interface Settings {
python: PythonSettings;
}
// These are the example settings we defined in the client's package.json
// file
interface PythonSettings {
maxNumberOfProblems: number;
}
var pythonSettings: any = {};
// The settings have changed. Is send on server activation
// as well.
connection.onDidChangeConfiguration((change) => {
let settings = <Settings>change.settings;
pythonSettings = settings.python;
// Revalidate any open text documents
documents.all().forEach(validateTextDocument);
});
function validateTextDocument(textDocument: ITextDocument): void {
let diagnostics: Diagnostic[] = [];
var isWin = /^win/.test(process.platform);
if (!isWin) {
return;
}
if (!pythonSettings.linting.enabled || (!pythonSettings.linting.pylintEnabled && !pythonSettings.linting.pep8Enabled)) {
return;
}
var pep8Messages = [];
var pylintMessages = [];
var pep8Done = false;
var pylintDone = false;
if (pythonSettings.linting.pylintEnabled) {
new pylinter.Linter().run(textDocument, pythonSettings, false).then((d) => {
pylintDone = true;
if (pythonSettings.linting.pep8Enabled) {
d.forEach(d=> d.message = d.message + " (pylint)");
if (pep8Done) {
d.forEach(d=> pep8Messages.push(d));
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: pep8Messages });
}
else {
pylintMessages = d;
}
}
else {
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: d });
}
});
}
if (pythonSettings.linting.pep8Enabled) {
new pylinter.Linter().run(textDocument, pythonSettings, false).then((d) => {
pylintDone = true;
if (pythonSettings.linting.pylintEnabled) {
d.forEach(d=> d.message = d.message + " (pep8)");
if (pylintDone) {
d.forEach(d=> pylintMessages.push(d));
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: pylintMessages });
}
else {
pep8Messages = d;
}
}
else {
connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: d });
}
});
}
}
//
connection.onDidChangeWatchedFiles((change) => {
// Monitored files have change in VSCode
// connection.console.log('We recevied an file change event');
});
//
// // This handler provides the initial list of the completion items.
// connection.onCompletion((textDocumentPosition: TextDocumentIdentifier): CompletionItem[] => {
// // The pass parameter contains the position of the text document in
// // which code complete got requested. For the example we ignore this
// // info and always provide the same completion items.
// return [];
// // {
// // label: 'TypeScript',
// // kind: CompletionItemKind.Text,
// // data: 1
// // },
// // {
// // label: 'JavaScript',
// // kind: CompletionItemKind.Text,
// // data: 2
// // }
// // ]
// });
//
// // This handler resolve additional information for the item selected in
// // the completion list.
// connection.onCompletionResolve((item: CompletionItem): CompletionItem => {
// // if (item.data === 1) {
// // item.detail = 'TypeScript details',
// // item.documentation = 'TypeScript documentation'
// // } else if (item.data === 2) {
// // item.detail = 'JavaScript details',
// // item.documentation = 'JavaScript documentation'
// // }
// // return item;
// return item;
// });
/*
connection.onDidOpenTextDocument((params) => {
// A text document got opened in VSCode.
// params.uri uniquely identifies the document. For documents store on disk this is a file URI.
// params.text the initial full content of the document.
connection.console.log(`${params.uri} opened.`);
});
connection.onDidChangeTextDocument((params) => {
// The content of a text document did change in VSCode.
// params.uri uniquely identifies the document.
// params.contentChanges describe the content changes to the document.
connection.console.log(`${params.uri} changed: ${JSON.stringify(params.contentChanges)}`);
});
connection.onDidCloseTextDocument((params) => {
// A text document got closed in VSCode.
// params.uri uniquely identifies the document.
connection.console.log(`${params.uri} closed.`);
});
*/
// Listen on the connection
connection.listen();