This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathmain.js
195 lines (166 loc) · 6.35 KB
/
main.js
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
188
189
190
191
192
193
194
195
const fs = require('fs')
const path = require('path')
const { AutoLanguageClient } = require('atom-languageclient')
const jsScopes = ['source.js', 'source.js.jsx', 'javascript']
const tsScopes = ['source.ts', 'source.tsx', 'typescript']
const allScopes = tsScopes.concat(jsScopes)
const tsExtensions = ['*.json', '.ts', '.tsx']
const jsExtensions = ['.js', '.jsx']
const allExtensions = tsExtensions.concat(jsExtensions)
class TypeScriptLanguageClient extends AutoLanguageClient {
getGrammarScopes() {
return atom.config.get('ide-typescript.javascriptSupport') ? allScopes : tsScopes
}
getLanguageName() { return 'TypeScript' }
getServerName() { return 'Theia' }
startServerProcess(projectPath) {
this.supportedExtensions = atom.config.get('ide-typescript.javascriptSupport') ? allExtensions : tsExtensions
const serverPath = this.getServerPath(projectPath)
console.info(`starting typescript server: ${serverPath}`)
return super.spawnChildNode([
'node_modules/typescript-language-server/lib/cli',
'--stdio',
'--tsserver-path', serverPath
], { cwd: path.join(__dirname, '..') })
}
consumeLinterV2() {
if (atom.config.get('ide-typescript.diagnosticsEnabled') === true) {
super.consumeLinterV2.apply(this, arguments)
}
}
deactivate() {
let deactivate = super.deactivate();
let cancel = new Promise((resolve, _reject) => {
deactivate.then((_result) => {
resolve();
})
});
return Promise.race([deactivate, this.createTimeoutPromise(2000, cancel)])
}
shouldStartForEditor(editor) {
const projectPath = this.getProjectPath(editor.getURI() || '');
if (!projectPath) return false
if (atom.config.get('ide-typescript.ignoreFlow') === true) {
const flowConfigPath = path.join(projectPath, '.flowconfig')
if (fs.existsSync(flowConfigPath)) return false
}
if (!this.validateTypeScriptServerPath(projectPath)) return false
return super.shouldStartForEditor(editor);
}
validateTypeScriptServerPath(projectPath) {
const tsPath = this.getServerPath(projectPath);
if (fs.existsSync(tsPath)) return true
atom.notifications.addError('ide-typescript could not locate the TypeScript server', {
dismissable: true,
buttons: [
{ text: 'Set TypeScript server path', onDidClick: () => this.openPackageSettings() },
],
description:
`No TypeScript server could be found at <b>${tsPath}</b>`
})
}
openPackageSettings() {
atom.workspace.open('atom://config/packages/ide-typescript')
}
getProjectPath(filePath) {
const projectPath = atom.project.getDirectories().find(d => filePath.startsWith(d.path))
return projectPath != null ? projectPath.path : ''
}
getServerPath(projectPath) {
const relativePathSpecifiedByUser = atom.config.get('ide-typescript.typeScriptServer.path')
const relativePathDefault = 'node_modules/typescript/lib/tsserver.js'
const absPathLocal = path.resolve(projectPath, relativePathSpecifiedByUser)
const absPathGlobal = path.resolve(__dirname, '..', relativePathSpecifiedByUser)
const absPathGlobalDefault = path.resolve(__dirname, '..', relativePathDefault)
if (fs.existsSync(absPathLocal)) {
return absPathLocal
}
if (fs.existsSync(absPathGlobal)) {
return absPathGlobal
}
return absPathGlobalDefault
}
createTimeoutPromise(milliseconds, cancelPromise) {
let cancel = false;
cancelPromise.then((_result) => {
cancel = true;
})
return new Promise((resolve, reject) => {
let timeout = setTimeout(() => {
clearTimeout(timeout)
if (cancel !== true) {
this.logger.error(`Server failed to shutdown in ${milliseconds}ms, forcing termination`);
resolve();
} else {
reject();
}
}, milliseconds)
})
}
provideAutocomplete() {
const autocompleteResultsFirst = atom.config.get('ide-typescript.autocompleteResultsFirst')
const provided = super.provideAutocomplete()
provided.suggestionPriority = autocompleteResultsFirst ? 2 : 1
return provided
}
onDidConvertAutocomplete(_completionItem, suggestion, _request) {
TypeScriptLanguageClient.setLeftAndRightLabels(suggestion)
// Theia language server sets snippets to '' leading to ambiguity between using that and text
if (suggestion.snippet === '' && suggestion.text != null && suggestion.text !== '') {
suggestion.snippet = undefined
}
}
static setLeftAndRightLabels(suggestion) {
if (suggestion.rightLabel == null || suggestion.displayText == null) return
const nameIndex = suggestion.rightLabel.indexOf(suggestion.displayText)
if (nameIndex >= 0) {
const signature = suggestion.rightLabel.substr(nameIndex + suggestion.displayText.length).trim()
let paramsStart = -1
let paramsEnd = -1
let returnStart = -1
let bracesDepth = 0
for (let i = 0; i < signature.length; i++) {
switch (signature[i]) {
case '(': {
if (bracesDepth++ === 0 && paramsStart === -1) {
paramsStart = i;
}
break;
}
case ')': {
if (--bracesDepth === 0 && paramsEnd === -1) {
paramsEnd = i;
}
break;
}
case ':': {
if (returnStart === -1 && bracesDepth === 0) {
returnStart = i;
}
break;
}
}
}
if (atom.config.get('ide-typescript.returnTypeInAutocomplete') === 'left') {
if (paramsStart > -1) {
suggestion.rightLabel = signature.substring(paramsStart, paramsEnd + 1).trim()
}
if (returnStart > -1) {
suggestion.leftLabel = signature.substring(returnStart + 1).trim()
}
// We have a 'property' icon, we don't need to pollute the signature with '(property) '
const propertyPrefix = '(property) '
if (suggestion.rightLabel.startsWith(propertyPrefix)) {
suggestion.rightLabel = suggestion.rightLabel.substring(propertyPrefix.length)
}
} else {
suggestion.rightLabel = signature.substring(paramsStart).trim()
suggestion.leftLabel = ''
}
}
}
filterChangeWatchedFiles(filePath) {
return this.supportedExtensions.indexOf(path.extname(filePath).toLowerCase()) > -1;
}
}
module.exports = new TypeScriptLanguageClient()