1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-21 21:52:10 +02:00

Bug 558664 - Cquery integration should be extracted from LSP Core

Part 2:
Introduce SupportedLanguageServers interface and remove knowledge about
specific language server from CPPStreamConnectionProvider. Replace
ICPPLanguageServer with LanguageServerConfiguration.
Move Clangd and Cquery server configurations to its packages.

Change-Id: I825c39bc6f8004616e639fda28660d3c98d08057
Signed-off-by: Alexander Fedorov <alexander.fedorov@arsysop.ru>
This commit is contained in:
Alexander Fedorov 2020-08-18 20:03:35 +03:00
parent afaa593d34
commit 05ae2a8185
10 changed files with 166 additions and 45 deletions

View file

@ -23,8 +23,8 @@ Require-Bundle: com.google.gson;bundle-version="2.8.2",
Import-Package: org.eclipse.ui.editors.text, Import-Package: org.eclipse.ui.editors.text,
org.eclipse.ui.texteditor org.eclipse.ui.texteditor
Export-Package: org.eclipse.cdt.cquery;x-friends:="org.eclipse.cdt.lsp.ui", Export-Package: org.eclipse.cdt.cquery;x-friends:="org.eclipse.cdt.lsp.ui",
org.eclipse.cdt.internal.cquery;x-internal:=true, org.eclipse.cdt.internal.clangd;x-friends:="org.eclipse.cdt.lsp.ui",
org.eclipse.cdt.internal.cquery.core;x-internal:=true, org.eclipse.cdt.internal.cquery;x-friends:="org.eclipse.cdt.lsp.ui",
org.eclipse.cdt.internal.cquery.ui;x-internal:=true, org.eclipse.cdt.internal.cquery.ui;x-internal:=true,
org.eclipse.cdt.lsp.core;x-friends:="org.eclipse.cdt.lsp.ui", org.eclipse.cdt.lsp.core;x-friends:="org.eclipse.cdt.lsp.ui",
org.eclipse.cdt.lsp.internal.core;x-internal:=true, org.eclipse.cdt.lsp.internal.core;x-internal:=true,

View file

@ -9,15 +9,29 @@
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.lsp.core; package org.eclipse.cdt.internal.clangd;
import java.net.URI; import java.net.URI;
public class ClangdLanguageServer implements ICPPLanguageServer { import org.eclipse.cdt.lsp.LanguageServerConfiguration;
public class ClangdLanguageServer implements LanguageServerConfiguration {
public static final String CLANGD_ID = "clangd"; //$NON-NLS-1$
@Override @Override
public Object getLSSpecificInitializationOptions(Object defaultInitOptions, URI rootPath) { public String identifier() {
return defaultInitOptions; return ClangdLanguageServer.CLANGD_ID;
}
@Override
public String label() {
return "ClangD";
}
@Override
public Object options(Object defaults, URI uri) {
return defaults;
} }
} }

View file

@ -9,28 +9,37 @@
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.cquery.core; package org.eclipse.cdt.internal.cquery;
import java.net.URI; import java.net.URI;
import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.lsp.core.ICPPLanguageServer; import org.eclipse.cdt.lsp.LanguageServerConfiguration;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
public class CqueryLanguageServer implements ICPPLanguageServer { public class CqueryLanguageServer implements LanguageServerConfiguration {
public static final String CQUERY_ID = "cquery"; //$NON-NLS-1$
@Override @Override
public Object getLSSpecificInitializationOptions(Object defaultInitOptions, URI rootPath) { public String identifier() {
return CqueryLanguageServer.CQUERY_ID;
}
@Override
public String label() {
return "CQuery";
}
@Override
public Object options(Object defaults, URI uri) {
// TODO: Allow user to specify cache directory path // TODO: Allow user to specify cache directory path
IPath cacheDirectory = Path.fromOSString(uri.getPath()).append(".cdt-lsp/cquery_index"); //$NON-NLS-1$
IPath cacheDirectory = Path.fromOSString(rootPath.getPath()).append(".cdt-lsp/cquery_index"); //$NON-NLS-1$ JsonObject result = (defaults instanceof JsonObject) ? (JsonObject) defaults : new JsonObject();
JsonObject result = (defaultInitOptions instanceof JsonObject) ? (JsonObject) defaultInitOptions
: new JsonObject();
result.addProperty("cacheDirectory", cacheDirectory.toString()); //$NON-NLS-1$ result.addProperty("cacheDirectory", cacheDirectory.toString()); //$NON-NLS-1$
result.addProperty("emitInactiveRegions", //$NON-NLS-1$ result.addProperty("emitInactiveRegions", //$NON-NLS-1$
CUIPlugin.getDefault().getPreferenceStore().getBoolean(CEditor.INACTIVE_CODE_ENABLE)); CUIPlugin.getDefault().getPreferenceStore().getBoolean(CEditor.INACTIVE_CODE_ENABLE));

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2018 Manish Khurana , Nathan Ridge and others. * Copyright (c) 2020 ArSysOp and others.
* *
* This program and the accompanying materials * This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0 * are made available under the terms of the Eclipse Public License 2.0
@ -7,17 +7,25 @@
* https://www.eclipse.org/legal/epl-2.0/ * https://www.eclipse.org/legal/epl-2.0/
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.lsp;
package org.eclipse.cdt.lsp.core;
import java.net.URI; import java.net.URI;
/* /**
* Encapsulates functionality specific to a particular C++ language server (e.g., CQuery) *
* Holds the configuration for a contributed language server.
*
*/ */
public interface ICPPLanguageServer { public interface LanguageServerConfiguration {
public Object getLSSpecificInitializationOptions(Object defaultInitOptions, URI rootPath); String identifier();
String label();
Object options(Object defaults, URI uri);
} }

View file

@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp;
import java.util.Collection;
/**
*
* Provides access to configurations of supported language servers.
*
*/
public interface SupportedLanguageServers {
Collection<LanguageServerConfiguration> all();
LanguageServerConfiguration preferred() throws IllegalStateException;
}

View file

@ -24,8 +24,8 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.eclipse.cdt.internal.cquery.core.CqueryLanguageServer; import org.eclipse.cdt.lsp.LanguageServerConfiguration;
import org.eclipse.cdt.lsp.internal.core.LspCoreMessages; import org.eclipse.cdt.lsp.internal.core.ContributedLanguageServers;
import org.eclipse.cdt.utils.CommandLineUtil; import org.eclipse.cdt.utils.CommandLineUtil;
import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
@ -43,29 +43,19 @@ public class CPPStreamConnectionProvider extends ProcessStreamConnectionProvider
private static final IPreferenceStore store = Activator.getDefault().getPreferenceStore(); private static final IPreferenceStore store = Activator.getDefault().getPreferenceStore();
private ICPPLanguageServer languageServer; private final LanguageServerConfiguration configuration;
public static final String CLANGD_ID = "clangd"; //$NON-NLS-1$
public static final String CQUERY_ID = "cquery"; //$NON-NLS-1$
//FIXME: AF: the list of available servers should be extracted from some service
public CPPStreamConnectionProvider() throws UnsupportedOperationException { public CPPStreamConnectionProvider() throws UnsupportedOperationException {
List<String> commands = new ArrayList<>(); //FIXME: should obtain an instance of interface
if (store.getString(PreferenceConstants.P_SERVER_CHOICE).equals(CQUERY_ID)) { configuration = new ContributedLanguageServers().preferred();
languageServer = new CqueryLanguageServer(); File defaultLSLocation = getDefaultLSLocation(configuration.identifier());
} else if (store.getString(PreferenceConstants.P_SERVER_CHOICE).equals(CLANGD_ID)) {
languageServer = new ClangdLanguageServer();
} else {
throw new UnsupportedOperationException(LspCoreMessages.CPPStreamConnectionProvider_e_unsupported);
}
File defaultLSLocation = getDefaultLSLocation(store.getString(PreferenceConstants.P_SERVER_CHOICE));
if (defaultLSLocation != null) { if (defaultLSLocation != null) {
store.setDefault(PreferenceConstants.P_SERVER_PATH, defaultLSLocation.getAbsolutePath()); store.setDefault(PreferenceConstants.P_SERVER_PATH, defaultLSLocation.getAbsolutePath());
} }
File languageServerLocation = getLanguageServerLocation(); File languageServerLocation = getLanguageServerLocation();
String parent = ""; //$NON-NLS-1$ String parent = ""; //$NON-NLS-1$
String flags = store.getString(PreferenceConstants.P_SERVER_OPTIONS); String flags = store.getString(PreferenceConstants.P_SERVER_OPTIONS);
List<String> commands = new ArrayList<>();
if (languageServerLocation != null) { if (languageServerLocation != null) {
commands.add(languageServerLocation.getAbsolutePath()); commands.add(languageServerLocation.getAbsolutePath());
if (!flags.isEmpty()) { if (!flags.isEmpty()) {
@ -89,8 +79,7 @@ public class CPPStreamConnectionProvider extends ProcessStreamConnectionProvider
@Override @Override
public Object getInitializationOptions(URI rootPath) { public Object getInitializationOptions(URI rootPath) {
installResourceChangeListener(rootPath); installResourceChangeListener(rootPath);
Object defaultInitOptions = super.getInitializationOptions(rootPath); return configuration.options(super.getInitializationOptions(rootPath), rootPath);
return languageServer.getLSSpecificInitializationOptions(defaultInitOptions, rootPath);
} }
private void installResourceChangeListener(URI rootPath) { private void installResourceChangeListener(URI rootPath) {

View file

@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (c) 2020 ArSysOp and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexander Fedorov (ArSysOp) - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.lsp.internal.core;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import org.eclipse.cdt.internal.clangd.ClangdLanguageServer;
import org.eclipse.cdt.internal.cquery.CqueryLanguageServer;
import org.eclipse.cdt.lsp.LanguageServerConfiguration;
import org.eclipse.cdt.lsp.SupportedLanguageServers;
import org.eclipse.cdt.lsp.core.PreferenceConstants;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.osgi.framework.FrameworkUtil;
//FIXME ok, not really contributed at the moment, but will be
public final class ContributedLanguageServers implements SupportedLanguageServers {
private final Map<String, LanguageServerConfiguration> configs;
public ContributedLanguageServers() {
configs = new LinkedHashMap<>();
register(new ClangdLanguageServer());
register(new CqueryLanguageServer());
}
@Override
public Collection<LanguageServerConfiguration> all() {
return configs.values();
}
public void register(LanguageServerConfiguration configuration) {
configs.put(configuration.identifier(), configuration);
}
public void unregister(LanguageServerConfiguration configuration) {
configs.remove(configuration.identifier());
}
@Override
public LanguageServerConfiguration preferred() throws IllegalStateException {
return Optional.ofNullable(InstanceScope.INSTANCE.getNode(nodeQualifier()))//
.filter(IEclipsePreferences.class::isInstance)//
.map(IEclipsePreferences.class::cast)//
.flatMap(x -> Optional.ofNullable(x.get(nodePath(), null)))//
.map(configs::get)//
.orElseThrow(() -> new IllegalStateException("No preferred server was found"));
}
private String nodeQualifier() {
return FrameworkUtil.getBundle(getClass()).getSymbolicName();
}
private String nodePath() {
//FIXME: rework
return PreferenceConstants.P_SERVER_CHOICE;
}
}

View file

@ -23,7 +23,6 @@ public class LspCoreMessages extends NLS {
NLS.initializeMessages(BUNDLE_NAME, LspCoreMessages.class); NLS.initializeMessages(BUNDLE_NAME, LspCoreMessages.class);
} }
public static String CPPStreamConnectionProvider_e_unsupported;
public static String ShowStatus_busy; public static String ShowStatus_busy;
public static String ShowStatus_idle; public static String ShowStatus_idle;

View file

@ -12,6 +12,5 @@
# Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 558484 # Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 558484
############################################################################### ###############################################################################
CPPStreamConnectionProvider_e_unsupported=Unsupported Language Server
ShowStatus_busy={0} : Busy | {1} Jobs ShowStatus_busy={0} : Busy | {1} Jobs
ShowStatus_idle={0} : Idle ShowStatus_idle={0} : Idle

View file

@ -18,6 +18,8 @@ package org.eclipse.cdt.lsp.internal.ui.preferences;
import java.io.File; import java.io.File;
import org.eclipse.cdt.internal.clangd.ClangdLanguageServer;
import org.eclipse.cdt.internal.cquery.CqueryLanguageServer;
import org.eclipse.cdt.lsp.core.CPPStreamConnectionProvider; import org.eclipse.cdt.lsp.core.CPPStreamConnectionProvider;
import org.eclipse.cdt.lsp.core.PreferenceConstants; import org.eclipse.cdt.lsp.core.PreferenceConstants;
import org.eclipse.cdt.lsp.internal.ui.LspUiActivator; import org.eclipse.cdt.lsp.internal.ui.LspUiActivator;
@ -52,9 +54,8 @@ public class CPPLanguageServerPreferencePage extends FieldEditorPreferencePage i
serverChoice = new RadioGroupFieldEditor(PreferenceConstants.P_SERVER_CHOICE, serverChoice = new RadioGroupFieldEditor(PreferenceConstants.P_SERVER_CHOICE,
LspUiMessages.CPPLanguageServerPreferencePage_server_selector, 1, LspUiMessages.CPPLanguageServerPreferencePage_server_selector, 1,
new String[][] { new String[][] {
{ LspUiMessages.CPPLanguageServerPreferencePage_clangd, CPPStreamConnectionProvider.CLANGD_ID }, { LspUiMessages.CPPLanguageServerPreferencePage_clangd, ClangdLanguageServer.CLANGD_ID },
{ LspUiMessages.CPPLanguageServerPreferencePage_cquery, { LspUiMessages.CPPLanguageServerPreferencePage_cquery, CqueryLanguageServer.CQUERY_ID } },
CPPStreamConnectionProvider.CQUERY_ID } },
getFieldEditorParent()); getFieldEditorParent());
addField(serverChoice); addField(serverChoice);