-
Notifications
You must be signed in to change notification settings - Fork 9
/
InitializeLaunchConfigurations.java
173 lines (165 loc) · 8.79 KB
/
InitializeLaunchConfigurations.java
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
/*******************************************************************************
* Copyright (c) 2016 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Mickael Istria (Red Hat Inc.) - initial implementation
*******************************************************************************/
package org.eclipse.languageserver.languages;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.eclipse.core.externaltools.internal.IExternalToolConstants;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentTypeManager;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.languageserver.LSPStreamConnectionProviderRegistry;
import org.eclipse.languageserver.LaunchConfigurationStreamProvider;
import org.eclipse.ui.IStartup;
/**
* Initialize the LaunchConfiguration that will be used to start language servers.
* TODO: find a better way to contribute that, or maybe use some dedicated launch types.
* @author mistria
*
*/
public class InitializeLaunchConfigurations implements IStartup {
public static final String VSCODE_CSS_NAME = "VSCode-CSS";
public static final String VSCODE_JSON_NAME = "VSCode-JSON";
public static final String OMNISHARP_NAME = "OmniSharp";
@Override
public void earlyStartup() {
IContentTypeManager contentTypeManager = Platform.getContentTypeManager();
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType externalType = launchManager.getLaunchConfigurationType(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE);
LSPStreamConnectionProviderRegistry registry = LSPStreamConnectionProviderRegistry.getInstance();
// OmniSharp
try {
String omniSharpLaunchName = OMNISHARP_NAME;
ILaunchConfiguration omniSharpLauch = null;
for (ILaunchConfiguration launch : launchManager.getLaunchConfigurations(externalType)) {
if (launch.getName().equals(omniSharpLaunchName)) {
omniSharpLauch = launch;
}
}
if (omniSharpLauch == null) {
ILaunchConfigurationWorkingCopy workingCopy = externalType.newInstance(null, omniSharpLaunchName);
// some common config
workingCopy.setAttribute(IExternalToolConstants.ATTR_LAUNCH_IN_BACKGROUND, true);
workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILDER_ENABLED, false);
workingCopy.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, false);
workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILD_SCOPE, "${none}");
workingCopy.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, getNodeJsLocation());
workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, "/home/mistria/git/omnisharp-node-client/languageserver/server.js");
workingCopy.setAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true);
Map<String, String> environment = new HashMap<>(1);
environment.put("LD_LIBRARY_PATH", "/home/mistria/apps/OmniSharp.NET/icu54:" + System.getenv("LD_LIBRARY_PATH"));
workingCopy.setAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, environment);
omniSharpLauch = workingCopy.doSave();
registry.registerAssociation(contentTypeManager.getContentType("org.eclipse.languageserver.languages.csharp"), LaunchConfigurationStreamProvider.findLaunchConfiguration(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE, InitializeLaunchConfigurations.OMNISHARP_NAME));
}
} catch (CoreException e) {
e.printStackTrace();
}
// VSCode CSS
try {
String omniSharpLaunchName = VSCODE_CSS_NAME;
ILaunchConfiguration omniSharpLauch = null;
for (ILaunchConfiguration launch : launchManager.getLaunchConfigurations(externalType)) {
if (launch.getName().equals(omniSharpLaunchName)) {
omniSharpLauch = launch;
}
}
if (omniSharpLauch == null) {
ILaunchConfigurationWorkingCopy workingCopy = externalType.newInstance(null, omniSharpLaunchName);
// some common config
workingCopy.setAttribute(IExternalToolConstants.ATTR_LAUNCH_IN_BACKGROUND, true);
workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILDER_ENABLED, false);
workingCopy.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, false);
workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILD_SCOPE, "${none}");
workingCopy.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, getNodeJsLocation());
// Assume node is already installed on machine and uses it
// TODO: implement smarter and multi-platform discovery
workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, getVSCodeLocation() + "/resources/app/extensions/css/server/out/cssServerMain.js --stdio");
omniSharpLauch = workingCopy.doSave();
registry.registerAssociation(contentTypeManager.getContentType("org.eclipse.wst.css.core.csssource"), LaunchConfigurationStreamProvider.findLaunchConfiguration(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE, InitializeLaunchConfigurations.VSCODE_CSS_NAME));
registry.registerAssociation(contentTypeManager.getContentType("org.eclipse.languageserver.languages.less"), LaunchConfigurationStreamProvider.findLaunchConfiguration(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE, InitializeLaunchConfigurations.VSCODE_CSS_NAME));
registry.registerAssociation(contentTypeManager.getContentType("org.eclipse.languageserver.languages.scss"), LaunchConfigurationStreamProvider.findLaunchConfiguration(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE, InitializeLaunchConfigurations.VSCODE_CSS_NAME));
}
} catch (CoreException e) {
e.printStackTrace();
}
// VSCode CSS
try {
String omniSharpLaunchName = VSCODE_JSON_NAME;
ILaunchConfiguration omniSharpLauch = null;
for (ILaunchConfiguration launch : launchManager.getLaunchConfigurations(externalType)) {
if (launch.getName().equals(omniSharpLaunchName)) {
omniSharpLauch = launch;
}
}
if (omniSharpLauch == null) {
ILaunchConfigurationWorkingCopy workingCopy = externalType.newInstance(null, omniSharpLaunchName);
// some common config
workingCopy.setAttribute(IExternalToolConstants.ATTR_LAUNCH_IN_BACKGROUND, true);
workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILDER_ENABLED, false);
workingCopy.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, false);
workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILD_SCOPE, "${none}");
workingCopy.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, getNodeJsLocation());
// Assume node is already installed on machine and uses it
// TODO: implement smarter and multi-platform discovery
workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, getVSCodeLocation() + "/resources/app/extensions/json/server/out/jsonServerMain.js --stdio");
omniSharpLauch = workingCopy.doSave();
registry.registerAssociation(contentTypeManager.getContentType("org.eclipse.wst.jsdt.core.jsonSource"), LaunchConfigurationStreamProvider.findLaunchConfiguration(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE, InitializeLaunchConfigurations.VSCODE_JSON_NAME));
}
} catch (CoreException e) {
e.printStackTrace();
}
}
private String getVSCodeLocation() {
String res = null;
if (Platform.getOS().equals(Platform.OS_LINUX)) {
res = "/usr/share/code";
} else if (Platform.getOS().equals(Platform.OS_WIN32)) {
res = "C:/Program Files (x86)/Microsoft VS Code";
} else if (Platform.getOS().equals(Platform.OS_MACOSX)) {
res = "/usr/share/code";
}
if (new File(res).isDirectory()) {
return res;
}
return "/unknown/path/to/VSCode";
}
private String getNodeJsLocation() {
String res = "/path/to/node";
String[] command = new String[] {"/bin/bash", "-c", "which node"};
if (Platform.getOS().equals(Platform.OS_WIN32)) {
command = new String[] {"cmd", "/c", "where node"};
}
BufferedReader reader = null;
try {
Process p = Runtime.getRuntime().exec(command);
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
res = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(reader);
}
return res;
}
}