Skip to content

Commit d5941b9

Browse files
authored
Add spelling support (#244)
[222] add spell checking support Basic spelling support for the new LSP based C/C++ Editor. fixes #222
1 parent 4502b0b commit d5941b9

File tree

8 files changed

+159
-2
lines changed

8 files changed

+159
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Require-Bundle: org.eclipse.ui,
2929
org.eclipse.lsp4j,
3030
org.eclipse.lsp4j.jsonrpc,
3131
org.eclipse.cdt.codan.core,
32-
org.eclipse.cdt.debug.ui
32+
org.eclipse.cdt.debug.ui,
33+
org.eclipse.ui.workbench.texteditor
3334
Bundle-RequiredExecutionEnvironment: JavaSE-17
3435
Automatic-Module-Name: org.eclipse.cdt.lsp
3536
Bundle-ActivationPolicy: lazy

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@
215215
properties="hasLanguageServer"
216216
type="java.lang.Object">
217217
</propertyTester>
218+
<propertyTester
219+
class="org.eclipse.cdt.lsp.internal.editor.SpellingEnabled"
220+
id="org.eclipse.cdt.lsp.editor.spelling.EnabledTester"
221+
namespace="org.eclipse.cdt.lsp.editor.spelling"
222+
properties="enabled"
223+
type="java.lang.Object">
224+
</propertyTester>
218225
</extension>
219226
<extension
220227
point="org.eclipse.core.expressions.definitions">
@@ -302,6 +309,18 @@
302309
<keywordReference id="org.eclipse.cdt.ui.saveactions"/>
303310
<keywordReference id="org.eclipse.cdt.ui.common"/>
304311
</page>
305-
</extension>
312+
</extension>
313+
<extension
314+
point="org.eclipse.ui.genericeditor.reconcilers">
315+
<reconciler
316+
class="org.eclipse.cdt.lsp.internal.editor.CSpellingReconciler"
317+
contentType="org.eclipse.core.runtime.text">
318+
<enabledWhen>
319+
<test
320+
property="org.eclipse.cdt.lsp.editor.spelling.enabled">
321+
</test>
322+
</enabledWhen>
323+
</reconciler>
324+
</extension>
306325
</plugin>
307326

bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/editor/EditorConfigurationPage.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,25 @@ private Control createPreferenceContent(Composite parent, boolean isProjectScope
162162
Composite composite = new Composite(parent, SWT.NONE);
163163
composite.setLayout(GridLayoutFactory.fillDefaults().create());
164164
composite.setFont(parent.getFont());
165+
if (!isProjectScope) {
166+
createSpellingPreferencesLink(composite);
167+
Label line = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
168+
line.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false, 2, 1));
169+
}
165170
area = getConfigurationArea(composite, isProjectScope);
166171
return composite;
167172
}
168173

174+
private Control createSpellingPreferencesLink(Composite parent) {
175+
Link link = new Link(parent, SWT.NONE);
176+
link.setText(LspUiMessages.LspEditorConfigurationPage_spelling_link);
177+
link.addListener(SWT.Selection,
178+
event -> PreferencesUtil.createPreferenceDialogOn(getShell(), event.text, null, null));
179+
link.setToolTipText(LspUiMessages.LspEditorConfigurationPage_spelling_link_tooltip);
180+
link.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
181+
return link;
182+
}
183+
169184
protected void refreshWidgets(Object options) {
170185
setErrorMessage(null);
171186
area.load(options, useProjectSettings() || !projectScope().isPresent());
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Bachmann electronic GmbH and others.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Gesa Hentschke (Bachmann electronic GmbH) - initial implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.cdt.lsp.internal.editor;
15+
16+
import org.eclipse.cdt.internal.ui.text.spelling.CSpellingService;
17+
import org.eclipse.jface.text.source.ISourceViewer;
18+
import org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy;
19+
20+
public final class CSpellingReconcileStrategy extends SpellingReconcileStrategy {
21+
22+
public CSpellingReconcileStrategy(ISourceViewer viewer) {
23+
super(viewer, CSpellingService.getInstance());
24+
}
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Bachmann electronic GmbH and others.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Gesa Hentschke (Bachmann electronic GmbH) - initial implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.cdt.lsp.internal.editor;
15+
16+
import org.eclipse.jface.text.IDocument;
17+
import org.eclipse.jface.text.ITextViewer;
18+
import org.eclipse.jface.text.reconciler.Reconciler;
19+
import org.eclipse.jface.text.source.ISourceViewer;
20+
21+
public final class CSpellingReconciler extends Reconciler {
22+
23+
@Override
24+
public void install(ITextViewer textViewer) {
25+
if (textViewer instanceof ISourceViewer sourceViewer) {
26+
this.setReconcilingStrategy(new CSpellingReconcileStrategy(sourceViewer), IDocument.DEFAULT_CONTENT_TYPE);
27+
}
28+
// call super.install AFTER the CSpellingReconcileStrategy has been added to the super class via setReconcilingStrategy call,
29+
// otherwise reconcilerDocumentChanged (which is called during super.install) would not be performed on our CSpellingReconcileStrategy
30+
super.install(textViewer);
31+
}
32+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Bachmann electronic GmbH and others.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
*
10+
* Contributors:
11+
* Gesa Hentschke (Bachmann electronic GmbH) - initial implementation
12+
*******************************************************************************/
13+
14+
package org.eclipse.cdt.lsp.internal.editor;
15+
16+
import java.util.Optional;
17+
18+
import org.eclipse.cdt.lsp.LspUtils;
19+
import org.eclipse.core.expressions.PropertyTester;
20+
import org.eclipse.core.runtime.CoreException;
21+
import org.eclipse.ui.IEditorInput;
22+
import org.eclipse.ui.IFileEditorInput;
23+
import org.eclipse.ui.editors.text.EditorsUI;
24+
import org.eclipse.ui.editors.text.TextEditor;
25+
import org.eclipse.ui.texteditor.spelling.SpellingService;
26+
27+
public final class SpellingEnabled extends PropertyTester {
28+
private static final String EMPTY = ""; //$NON-NLS-1$
29+
30+
@Override
31+
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
32+
return isSpellingEnabled() && isCContentType(receiver);
33+
}
34+
35+
private static boolean isSpellingEnabled() {
36+
return EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED);
37+
}
38+
39+
private static boolean isCContentType(Object receiver) {
40+
if (receiver instanceof TextEditor editor) {
41+
return LspUtils.isCContentType(getContentType(editor.getEditorInput()));
42+
}
43+
return false;
44+
}
45+
46+
private static String getContentType(IEditorInput editorInput) {
47+
if (editorInput instanceof IFileEditorInput fileEditorInput) {
48+
try {
49+
return Optional.ofNullable(fileEditorInput.getFile().getContentDescription())
50+
.map(cd -> cd.getContentType()).map(ct -> ct.getId()).orElse(EMPTY);
51+
} catch (CoreException e) {
52+
// do nothing
53+
}
54+
}
55+
return EMPTY;
56+
}
57+
58+
}

bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/messages/LspUiMessages.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public class LspUiMessages extends NLS {
2424
}
2525

2626
public static String NavigatorView_ErrorOnLoad;
27+
28+
public static String LspEditorConfigurationPage_spelling_link;
29+
public static String LspEditorConfigurationPage_spelling_link_tooltip;
30+
2731
public static String LspEditorConfigurationPage_enable_project_specific;
2832
public static String LspEditorConfigurationPage_configure_ws_specific;
2933
public static String LspEditorConfigurationPage_preferLspEditor;

bundles/org.eclipse.cdt.lsp/src/org/eclipse/cdt/lsp/internal/messages/LspUiMessages.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
NavigatorView_ErrorOnLoad = Loading the symbols encountered an error; see the Error Log for more information
1515

16+
LspEditorConfigurationPage_spelling_link=Spelling preferences are set via <a href="org.eclipse.ui.editors.preferencePages.Spelling">Text Editors Spelling</a>.
17+
LspEditorConfigurationPage_spelling_link_tooltip=Show the shared text editor spelling preferences
18+
1619
LspEditorConfigurationPage_enable_project_specific=Enable project-specific settings
1720
LspEditorConfigurationPage_configure_ws_specific=Configure Workspace Settings...
1821
LspEditorConfigurationPage_preferLspEditor=Set C/C++ Editor (LSP) as default

0 commit comments

Comments
 (0)