forked from atom/ide-typescript
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
174 lines (150 loc) · 5.78 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
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() {
this.supportedExtensions = atom.config.get('ide-typescript.javascriptSupport') ? allExtensions : tsExtensions
return super.spawnChildNode([
'node_modules/typescript-language-server/lib/cli',
'--stdio',
'--tsserver-path', atom.config.get('ide-typescript.typeScriptServer.path')
], { 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) {
if (atom.config.get('ide-typescript.ignoreFlow') === true) {
const flowConfigPath = path.join(this.getProjectPath(editor.getURI() || ''), '.flowconfig')
if (fs.existsSync(flowConfigPath)) return false
}
if (!this.validateTypeScriptServerPath()) return false
return super.shouldStartForEditor(editor);
}
validateTypeScriptServerPath() {
const tsSpecifiedPath = atom.config.get('ide-typescript.typeScriptServer.path')
const isAbsolutelySpecified = path.isAbsolute(tsSpecifiedPath)
const tsAbsolutePath = isAbsolutelySpecified ? tsSpecifiedPath : path.join(__dirname, '..', tsSpecifiedPath)
if (fs.existsSync(tsAbsolutePath)) 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>${tsAbsolutePath}</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 : ''
}
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()