Skip to content

Commit

Permalink
[#105] Move and rename DefaultCLanguageServerProvider (#107)
Browse files Browse the repository at this point in the history
fixes #105
  • Loading branch information
ghentschke committed Jun 3, 2023
1 parent 20310fa commit 7034dba
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 33 deletions.
3 changes: 2 additions & 1 deletion bundles/org.eclipse.cdt.lsp.clangd/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Bundle-ActivationPolicy: lazy
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.eclipse.cdt.lsp.clangd;singleton:=true
Bundle-Version: 1.0.0.qualifier
Export-Package: org.eclipse.cdt.lsp.internal.clangd;x-internal:=true,
Export-Package: org.eclipse.cdt.lsp.clangd;x-internal:=true,
org.eclipse.cdt.lsp.internal.clangd;x-internal:=true,
org.eclipse.cdt.lsp.internal.clangd.editor;x-internal:=true,
org.eclipse.cdt.lsp.internal.clangd.editor.expressions;x-internal:=true,
org.eclipse.cdt.lsp.internal.clangd.editor.handlers;x-internal:=true,
Expand Down
2 changes: 1 addition & 1 deletion bundles/org.eclipse.cdt.lsp.clangd/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<extension
point="org.eclipse.cdt.lsp.serverProvider">
<server
class="org.eclipse.cdt.lsp.internal.clangd.CdtCLanguageServerProvider"
class="org.eclipse.cdt.lsp.internal.clangd.ClangdLanguageServerProvider"
priority="low">
<enabledWhen>
<test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,33 @@
* Gesa Hentschke (Bachmann electronic GmbH) - initial implementation
*******************************************************************************/

package org.eclipse.cdt.lsp.server;
package org.eclipse.cdt.lsp.clangd;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.cdt.lsp.server.EnableExpression;
import org.eclipse.cdt.lsp.server.ICLanguageServerProvider;
import org.eclipse.cdt.utils.PathUtil;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;

public class DefaultCLanguageServerProvider implements ICLanguageServerProvider {
public static final String CLANG_TIDY = "--clang-tidy";
public static final String BACKGROUND_INDEX = "--background-index";
public static final String COMPLETION_STYLE = "--completion-style=detailed";
public static final String PRETTY = "--pretty";
public static final String QUERY_DRIVER = "--query-driver=";
/**
* Base class for a clangd language server provider. Can be extended by vendors.
*
*/
public abstract class BaseClangdLanguageServerProvider implements ICLanguageServerProvider {
public static final String CLANG_TIDY = "--clang-tidy"; //$NON-NLS-1$
public static final String BACKGROUND_INDEX = "--background-index"; //$NON-NLS-1$
public static final String COMPLETION_STYLE = "--completion-style=detailed"; //$NON-NLS-1$
public static final String PRETTY = "--pretty"; //$NON-NLS-1$
public static final String QUERY_DRIVER = "--query-driver="; //$NON-NLS-1$

protected List<String> commands;

protected EnableExpression enableExpression;

public DefaultCLanguageServerProvider() {
public BaseClangdLanguageServerProvider() {
commands = createCommands();
}

Expand All @@ -46,17 +52,19 @@ public void setCommands(List<String> commands) {

protected List<String> createCommands() {
List<String> commands = new ArrayList<>();
IPath clangdLocation = PathUtil.findProgramLocation("clangd", null); //in case pathStr is null environment variable ${PATH} is inspected
IPath clangdLocation = PathUtil.findProgramLocation("clangd", null); // in case pathStr is null environment //$NON-NLS-1$
// variable ${PATH} is inspected
if (clangdLocation != null) {
commands.add(clangdLocation.toOSString());
commands.add(CLANG_TIDY);
commands.add(BACKGROUND_INDEX);
commands.add(COMPLETION_STYLE);
commands.add(PRETTY);
// clangd will execute drivers and fetch necessary include paths to compile your code:
IPath compilerLocation = PathUtil.findProgramLocation("gcc", null);
// clangd will execute drivers and fetch necessary include paths to compile your
// code:
IPath compilerLocation = PathUtil.findProgramLocation("gcc", null); //$NON-NLS-1$
if (compilerLocation != null) {
commands.add(QUERY_DRIVER + compilerLocation.removeLastSegments(1).append(IPath.SEPARATOR + "*"));
commands.add(QUERY_DRIVER + compilerLocation.removeLastSegments(1).append(IPath.SEPARATOR + "*")); //$NON-NLS-1$
}
}
return commands;
Expand All @@ -77,7 +85,8 @@ public boolean isEnabledFor(IProject project) {
if (enableExpression != null) {
return enableExpression.evaluate(project);
}
//language server is always enabled when no enable expression has been defined in the extension point:
// language server is always enabled when no enable expression has been defined
// in the extension point:
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Gesa Hentschke (Bachmann electronic GmbH) - initial implementation
*******************************************************************************/

package org.eclipse.cdt.lsp.internal.clangd;
package org.eclipse.cdt.lsp.clangd;

import java.util.Optional;

Expand All @@ -23,7 +23,7 @@
import org.eclipse.osgi.service.environment.Constants;

/**
* Used to set the clangd fallbackFlags.
* Used to create and set the clangd fallbackFlags.
* This is needed for Windows OS to allow clangd to determine the system includes in case no compile_commands.json can be found.
* see also: https://clangd.llvm.org/extensions#compilation-commands
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.lsp.clangd.BaseClangdLanguageServerProvider;
import org.eclipse.cdt.lsp.clangd.ClangdFallbackManager;
import org.eclipse.cdt.lsp.internal.clangd.editor.LspEditorUiPlugin;
import org.eclipse.cdt.lsp.internal.clangd.editor.preferences.LspEditorPreferences;
import org.eclipse.cdt.lsp.server.DefaultCLanguageServerProvider;
import org.eclipse.cdt.utils.CommandLineUtil;
import org.eclipse.jface.preference.IPreferenceStore;

public class CdtCLanguageServerProvider extends DefaultCLanguageServerProvider {
public class ClangdLanguageServerProvider extends BaseClangdLanguageServerProvider {
//FIXME: AF: rework to core preferences
private static final IPreferenceStore preferenceStore = LspEditorUiPlugin.getDefault().getLsPreferences();
private final ClangdFallbackManager clangdFallbackManager = new ClangdFallbackManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@

package org.eclipse.cdt.lsp.internal.clangd.editor.preferences;

import org.eclipse.cdt.ui.newui.MultiLineTextFieldEditor;
import org.eclipse.cdt.utils.CommandLineUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.eclipse.cdt.lsp.LspPlugin;
import org.eclipse.cdt.lsp.internal.clangd.editor.LspEditorUiMessages;
import org.eclipse.cdt.lsp.internal.clangd.editor.LspEditorUiPlugin;
import org.eclipse.cdt.ui.newui.MultiLineTextFieldEditor;
import org.eclipse.cdt.utils.CommandLineUtil;
import org.eclipse.core.runtime.preferences.PreferenceMetadata;
import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
Expand Down Expand Up @@ -70,28 +70,35 @@ private void writeServerPathToProvider(String path) {
if (path == null || path.isBlank()) {
return;
}
List<String> commands = LspPlugin.getDefault().getCLanguageServerProvider().getCommands();
List<String> commands = Optional.ofNullable(LspPlugin.getDefault())
.map(plugin -> plugin.getCLanguageServerProvider()).map(provider -> provider.getCommands())
.orElse(null);
if (commands != null && !commands.isEmpty()) {
commands.set(0, path);
} else if (commands == null) {
commands = new ArrayList<>();
commands.add(path);
}
LspPlugin.getDefault().getCLanguageServerProvider().setCommands(commands);
final List<String> finalCommands = commands;
Optional.ofNullable(LspPlugin.getDefault()).map(plugin -> plugin.getCLanguageServerProvider())
.ifPresent(provider -> provider.setCommands(finalCommands));
}

private void writeServerOptionsToProvider(String options) {
if (options == null) {
return;
}
List<String> commands = LspPlugin.getDefault().getCLanguageServerProvider().getCommands();
List<String> commands = Optional.ofNullable(LspPlugin.getDefault())
.map(plugin -> plugin.getCLanguageServerProvider()).map(provider -> provider.getCommands())
.orElse(null);
if (commands != null && !commands.isEmpty()) {
String serverPath = commands.get(0); // save server path
commands.clear(); // clear all old options
commands.add(serverPath);
commands.addAll(Arrays.asList(CommandLineUtil.argumentsToArray(options)));
Optional.ofNullable(LspPlugin.getDefault()).map(plugin -> plugin.getCLanguageServerProvider())
.ifPresent(provider -> provider.setCommands(commands));
}
LspPlugin.getDefault().getCLanguageServerProvider().setCommands(commands);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,36 @@

package org.eclipse.cdt.lsp.test.server;

import org.eclipse.cdt.lsp.server.DefaultCLanguageServerProvider;
import java.util.List;

public class MockCLanguageServerProvider extends DefaultCLanguageServerProvider {
import org.eclipse.cdt.lsp.server.EnableExpression;
import org.eclipse.cdt.lsp.server.ICLanguageServerProvider;
import org.eclipse.core.resources.IProject;

public class MockCLanguageServerProvider implements ICLanguageServerProvider {

@Override
public List<String> getCommands() {
// TODO Auto-generated method stub
return null;
}

@Override
public void setCommands(List<String> commands) {
// TODO Auto-generated method stub

}

@Override
public void setEnableExpression(EnableExpression enableExpression) {
// TODO Auto-generated method stub

}

@Override
public boolean isEnabledFor(IProject project) {
// TODO Auto-generated method stub
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public IEditorDescriptor overrideDefaultEditor(String fileName, IContentType con
}

private boolean isEnabledFor(IEditorInput editorInput) {
if (cLanguageServerProvider == null)
return false;
IResource resource = editorInput.getAdapter(IResource.class);
if (resource != null) {
return cLanguageServerProvider.isEnabledFor(resource.getProject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Optional;

import org.eclipse.cdt.lsp.LspPlugin;
import org.eclipse.cdt.lsp.server.DefaultCLanguageServerProvider;
import org.eclipse.cdt.lsp.server.EnableExpression;
import org.eclipse.cdt.lsp.server.ICLanguageServerProvider;
import org.eclipse.core.expressions.ExpressionConverter;
Expand Down Expand Up @@ -51,7 +50,7 @@ public CLanguageServerRegistry() {
public ICLanguageServerProvider createCLanguageServerProvider() {
prioritizedProvider = null;
highestPrio = Priority.low;
HashMap<Priority, ICLanguageServerProvider> providers = new HashMap<Priority, ICLanguageServerProvider>();
HashMap<Priority, ICLanguageServerProvider> providers = new HashMap<>();
for (IConfigurationElement configurationElement : cExtensionPoint.getConfigurationElements()) {
if (SERVER_ELEMENT.equals(configurationElement.getName())) {
ICLanguageServerProvider provider = (ICLanguageServerProvider) getInstanceFromExtension(
Expand Down Expand Up @@ -84,7 +83,6 @@ public ICLanguageServerProvider createCLanguageServerProvider() {
}
if (providers.isEmpty()) {
Platform.getLog(getClass()).warn("No C/C++ language server defined");
prioritizedProvider = new DefaultCLanguageServerProvider();
} else {
// get provider with highest priority:
providers.forEach((key, value) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.cdt.lsp.internal.server;

import java.net.URI;
import java.util.Optional;

import org.eclipse.cdt.lsp.LspPlugin;
import org.eclipse.cdt.lsp.server.ICLanguageServerProvider;
Expand All @@ -23,15 +24,15 @@ public class CLanguageServerStreamConnectionProvider extends ProcessStreamConnec

public CLanguageServerStreamConnectionProvider() {
this.cLanguageServerProvider = LspPlugin.getDefault().getCLanguageServerProvider();
setCommands(cLanguageServerProvider.getCommands());
Optional.ofNullable(cLanguageServerProvider).ifPresent(p -> setCommands(p.getCommands()));

// set the working directory for the Java process which runs the C/C++ language server:
setWorkingDirectory(System.getProperty("user.dir")); //$NON-NLS-1$
}

@Override
public Object getInitializationOptions(URI rootUri) {
return this.cLanguageServerProvider.getInitializationOptions(rootUri);
return Optional.ofNullable(cLanguageServerProvider).map(p -> p.getInitializationOptions(rootUri)).orElse(null);
}

}

0 comments on commit 7034dba

Please sign in to comment.