Skip to content

Commit 9f4731a

Browse files
committed
[#83] Rework access to preferences #83
Support unified way to work with both project and worksapce preferences Provide UI to configure preferences in details Signed-off-by: Alexander Fedorov <[email protected]>
1 parent 4328fc7 commit 9f4731a

30 files changed

+1014
-313
lines changed

bundles/org.eclipse.cdt.lsp.clangd/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ Require-Bundle: org.eclipse.cdt.lsp;bundle-version="0.0.0",
3232
org.eclipse.ui.workbench;bundle-version="0.0.0",
3333
org.eclipse.ui.workbench.texteditor;bundle-version="0.0.0"
3434
Bundle-Activator: org.eclipse.cdt.lsp.internal.clangd.editor.LspEditorUiPlugin
35-
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.internal.clangd.ClangdFallbackManager.xml
35+
Service-Component: OSGI-INF/org.eclipse.cdt.lsp.internal.clangd.ClangdConfigurationAccess.xml,
36+
OSGI-INF/org.eclipse.cdt.lsp.internal.clangd.ClangdFallbackManager.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" name="org.eclipse.cdt.lsp.internal.clangd.ClangdConfigurationAccess">
3+
<service>
4+
<provide interface="org.eclipse.cdt.lsp.clangd.ClangdConfiguration"/>
5+
</service>
6+
<reference cardinality="1..1" field="preferences" interface="org.eclipse.core.runtime.preferences.IPreferencesService" name="preferences"/>
7+
<reference cardinality="1..1" field="workspace" interface="org.eclipse.core.resources.IWorkspace" name="workspace"/>
8+
<implementation class="org.eclipse.cdt.lsp.internal.clangd.ClangdConfigurationAccess"/>
9+
</scr:component>

bundles/org.eclipse.cdt.lsp.clangd/OSGI-INF/org.eclipse.cdt.lsp.internal.clangd.ClangdFallbackManager.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
</service>
66
<reference cardinality="1..1" field="build" interface="org.eclipse.cdt.core.build.ICBuildConfigurationManager" name="build"/>
77
<reference cardinality="1..1" field="uri" interface="org.eclipse.cdt.lsp.InitialUri" name="uri"/>
8+
<reference cardinality="1..1" field="workspace" interface="org.eclipse.core.resources.IWorkspace" name="workspace"/>
89
<implementation class="org.eclipse.cdt.lsp.internal.clangd.ClangdFallbackManager"/>
910
</scr:component>

bundles/org.eclipse.cdt.lsp.clangd/plugin.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
SPDX-License-Identifier: EPL-2.0
1212
-->
1313
<plugin>
14+
<extension
15+
point="org.eclipse.core.runtime.preferences">
16+
<initializer
17+
class="org.eclipse.cdt.lsp.internal.clangd.ClangdPreferenceInitializer">
18+
</initializer>
19+
</extension>
1420
<extension
1521
point="org.eclipse.ui.preferencePages">
1622
<!-- FIXME: AF: Exract clangd-specific preferences -->

bundles/org.eclipse.cdt.lsp.clangd/src/org/eclipse/cdt/lsp/clangd/BaseClangdLanguageServerProvider.java

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,63 +12,12 @@
1212

1313
package org.eclipse.cdt.lsp.clangd;
1414

15-
import java.util.ArrayList;
16-
import java.util.List;
17-
1815
import org.eclipse.cdt.lsp.server.ICLanguageServerProvider;
19-
import org.eclipse.cdt.utils.PathUtil;
20-
import org.eclipse.core.resources.IProject;
21-
import org.eclipse.core.runtime.IPath;
2216

2317
/**
2418
* Base class for a clangd language server provider. Can be extended by vendors.
2519
*
2620
*/
2721
public abstract class BaseClangdLanguageServerProvider implements ICLanguageServerProvider {
28-
public static final String CLANG_TIDY = "--clang-tidy"; //$NON-NLS-1$
29-
public static final String BACKGROUND_INDEX = "--background-index"; //$NON-NLS-1$
30-
public static final String COMPLETION_STYLE = "--completion-style=detailed"; //$NON-NLS-1$
31-
public static final String PRETTY = "--pretty"; //$NON-NLS-1$
32-
public static final String QUERY_DRIVER = "--query-driver="; //$NON-NLS-1$
33-
34-
protected List<String> commands;
35-
36-
public BaseClangdLanguageServerProvider() {
37-
commands = createCommands();
38-
}
39-
40-
@Override
41-
public List<String> getCommands() {
42-
return commands;
43-
}
44-
45-
@Override
46-
public void setCommands(List<String> commands) {
47-
this.commands = commands;
48-
}
49-
50-
protected List<String> createCommands() {
51-
List<String> commands = new ArrayList<>();
52-
IPath clangdLocation = PathUtil.findProgramLocation("clangd", null); //$NON-NLS-1$
53-
//in case pathStr is null environment variable ${PATH} is inspected
54-
if (clangdLocation != null) {
55-
commands.add(clangdLocation.toOSString());
56-
commands.add(CLANG_TIDY);
57-
commands.add(BACKGROUND_INDEX);
58-
commands.add(COMPLETION_STYLE);
59-
commands.add(PRETTY);
60-
// clangd will execute drivers and fetch necessary include paths to compile your code:
61-
IPath compilerLocation = PathUtil.findProgramLocation("gcc", null); //$NON-NLS-1$
62-
if (compilerLocation != null) {
63-
commands.add(QUERY_DRIVER + compilerLocation.removeLastSegments(1).append(IPath.SEPARATOR + "*")); //$NON-NLS-1$
64-
}
65-
}
66-
return commands;
67-
}
6822

69-
@Override
70-
public boolean isEnabledFor(IProject project) {
71-
// check if server has been defined:
72-
return !getCommands().isEmpty();
73-
}
7423
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 ArSysOp.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Alexander Fedorov (ArSysOp) - Initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.cdt.lsp.clangd;
15+
16+
import java.net.URI;
17+
18+
import org.eclipse.core.resources.IResource;
19+
import org.eclipse.core.runtime.preferences.IPreferenceMetadataStore;
20+
import org.eclipse.core.runtime.preferences.IScopeContext;
21+
22+
/**
23+
* Provides access to the clangd options according to the required scope
24+
*
25+
* @see ClangdOptions
26+
* @see IScopeContext
27+
*
28+
*/
29+
public interface ClangdConfiguration {
30+
31+
/**
32+
* Returns the clangd options for the given context like {@link IResource} or {@link URI}, must not return <code>null</code>
33+
* @param context to be adapter to the proper scope
34+
*
35+
* @return clangd options
36+
*/
37+
ClangdOptions options(Object context);
38+
39+
/**
40+
* Returns the clangd preference store for the given context like {@link IResource} or {@link URI}, must not return <code>null</code>
41+
* @param context to be adapter to the proper scope
42+
*
43+
* @return preference store
44+
*/
45+
IPreferenceMetadataStore storage(Object context);
46+
47+
/**
48+
* Return the metadata for clangd options, must not return <code>null</code>
49+
*
50+
* @return the clangd option metadata
51+
*/
52+
ClangdMetadata metadata();
53+
54+
/**
55+
* Default qualifier to use for preference storage
56+
* @return preference qualifier
57+
*/
58+
String qualifier();
59+
60+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 ArSysOp.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Alexander Fedorov (ArSysOp) - Initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.cdt.lsp.clangd;
15+
16+
import org.eclipse.core.runtime.preferences.PreferenceMetadata;
17+
18+
/**
19+
* The metadata for options to configure clangd
20+
*
21+
* @see ClangdOptions
22+
*
23+
*/
24+
public interface ClangdMetadata {
25+
26+
/**
27+
* Returns the metadata for the "Prefer clangd" option, must not return <code>null</code>.
28+
*
29+
* @return the metadata for the "Prefer clangd" option
30+
*
31+
* @see ClangdOptions#preferClangd()
32+
*/
33+
PreferenceMetadata<Boolean> preferClangd();
34+
35+
/**
36+
* Returns the metadata for the "Clangd path" option, must not return <code>null</code>.
37+
*
38+
* @return the metadata for the "Clangd path" option
39+
*
40+
* @see ClangdOptions#clangdPath()
41+
*/
42+
PreferenceMetadata<String> clangdPath();
43+
44+
/**
45+
* Returns the metadata for the "Enable clang-tidy" option, must not return <code>null</code>.
46+
*
47+
* @return the metadata for the "Enable clang-tidy" option
48+
*
49+
* @see ClangdOptions#useTidy()
50+
*/
51+
PreferenceMetadata<Boolean> useTidy();
52+
53+
/**
54+
* Returns the metadata for the "Background index" option, must not return <code>null</code>.
55+
*
56+
* @return the metadata for the "Background index" option
57+
*
58+
* @see ClangdOptions#useBackgroundIndex()
59+
*/
60+
PreferenceMetadata<Boolean> useBackgroundIndex();
61+
62+
/**
63+
* Returns the metadata for the "Completion style" option, must not return <code>null</code>.
64+
*
65+
* @return the metadata for the "Completion style" option
66+
*
67+
* @see ClangdOptions#completionStyle()
68+
*/
69+
PreferenceMetadata<String> completionStyle();
70+
71+
/**
72+
* Returns the metadata for the "Pretty print" option, must not return <code>null</code>.
73+
*
74+
* @return the metadata for the "Pretty print" option
75+
*
76+
* @see ClangdOptions#prettyPrint()
77+
*/
78+
PreferenceMetadata<Boolean> prettyPrint();
79+
80+
/**
81+
* Returns the metadata for the "Query driver" option, must not return <code>null</code>.
82+
*
83+
* @return the metadata for the "Query driver" option
84+
*
85+
* @see ClangdOptions#queryDriver()
86+
*/
87+
PreferenceMetadata<String> queryDriver();
88+
89+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 ArSysOp.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Alexander Fedorov (ArSysOp) - Initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.cdt.lsp.clangd;
15+
16+
import java.util.List;
17+
18+
import org.eclipse.lsp4e.server.ProcessStreamConnectionProvider;
19+
20+
/**
21+
* Options to configure clangd
22+
*
23+
*/
24+
public interface ClangdOptions {
25+
26+
/**
27+
* Prefer to use clangd language server and related editor implementation
28+
*
29+
* @return if clangd language server should be preferred
30+
*/
31+
boolean preferClangd();
32+
33+
/**
34+
* Path to clangd executable to be launched, must not return <code>null</code>
35+
*
36+
* @return path to clangd
37+
*/
38+
String clangdPath();
39+
40+
/**
41+
* Use clang-tidy diagnostics
42+
*
43+
* @return if clang-tidy diagnostics is enabled
44+
*/
45+
boolean useTidy();
46+
47+
/**
48+
* Index project code in the background and persist index on disk
49+
*
50+
* @return if background index is enabled
51+
*/
52+
boolean useBackgroundIndex();
53+
54+
/**
55+
* Granularity of code completion suggestions either <code>detailed</code> or <code>bundled</code>, must not return <code>null</code>
56+
*
57+
* @return granularity of code completion suggestions
58+
*/
59+
String completionStyle();
60+
61+
/**
62+
* Pretty-print JSON output
63+
*
64+
* @return if pretty-print for JSON output is enabled
65+
*/
66+
boolean prettyPrint();
67+
68+
/**
69+
* Comma separated list of globs for white-listing gcc-compatible drivers that are safe to execute, must not return <code>null</code>
70+
*
71+
* @return comma separated list of globs
72+
*/
73+
String queryDriver();
74+
75+
/**
76+
* Provides list of commands suitable for {@link ProcessStreamConnectionProvider}
77+
*
78+
* @return list of commands
79+
*/
80+
List<String> toList();
81+
82+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 ArSysOp.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Alexander Fedorov (ArSysOp) - Initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.cdt.lsp.clangd;
15+
16+
import java.util.function.Supplier;
17+
18+
public final class ClangdQualifier implements Supplier<String> {
19+
20+
@Override
21+
public String get() {
22+
return "org.eclipse.cdt.lsp.clangd"; //$NON-NLS-1$
23+
}
24+
25+
}

0 commit comments

Comments
 (0)