Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManish Khurana2018-08-11 20:39:31 +0000
committerNathan Ridge2018-08-11 21:14:41 +0000
commit1983d41afbe9129fb974f44de97eaf94be9d9741 (patch)
tree76b3875c6ef05bba361fa7d534d39a89ca78ebef /lsp4e-cpp
parentac81db720cdd2668747f5ac36a5ef07e84780937 (diff)
downloadorg.eclipse.cdt-1983d41afbe9129fb974f44de97eaf94be9d9741.tar.gz
org.eclipse.cdt-1983d41afbe9129fb974f44de97eaf94be9d9741.tar.xz
org.eclipse.cdt-1983d41afbe9129fb974f44de97eaf94be9d9741.zip
Bug 535428. Add indentation and bracket completion support in LSP4E-CPP
Change-Id: Ib9187a3ad28796305c47fe8e9543e1aed6a7bf58 Signed-off-by: Manish Khurana <mkmanishkhurana98@gmail.com>
Diffstat (limited to 'lsp4e-cpp')
-rw-r--r--lsp4e-cpp/org.eclipse.lsp4e.cpp.language/META-INF/MANIFEST.MF3
-rw-r--r--lsp4e-cpp/org.eclipse.lsp4e.cpp.language/plugin.xml7
-rw-r--r--lsp4e-cpp/org.eclipse.lsp4e.cpp.language/src/org/eclipse/lsp4e/cpp/language/AutoIndentStrategyCPP.java25
-rw-r--r--lsp4e-cpp/org.eclipse.lsp4e.cpp.language/src/org/eclipse/lsp4e/cpp/language/PresentationReconcilerCPP.java20
4 files changed, 54 insertions, 1 deletions
diff --git a/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/META-INF/MANIFEST.MF b/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/META-INF/MANIFEST.MF
index b8162722ac0..68b58e77ba0 100644
--- a/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/META-INF/MANIFEST.MF
+++ b/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/META-INF/MANIFEST.MF
@@ -24,4 +24,5 @@ Export-Package: org.eclipse.lsp4e.cpp.language,
org.eclipse.lsp4e.cpp.language.cquery
Bundle-Activator: org.eclipse.lsp4e.cpp.language.Activator
Bundle-ActivationPolicy: lazy
-Import-Package: org.eclipse.ui.texteditor
+Import-Package: org.eclipse.ui.editors.text,
+ org.eclipse.ui.texteditor
diff --git a/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/plugin.xml b/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/plugin.xml
index 58929f970e5..f81c0b3e9f4 100644
--- a/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/plugin.xml
+++ b/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/plugin.xml
@@ -62,4 +62,11 @@
class="org.eclipse.lsp4e.cpp.language.PreferenceInitializer">
</initializer>
</extension>
+ <extension
+ point="org.eclipse.ui.genericeditor.autoEditStrategies">
+ <autoEditStrategy
+ class="org.eclipse.lsp4e.cpp.language.AutoIndentStrategyCPP"
+ contentType="org.eclipse.lsp4e.languages.cpp">
+ </autoEditStrategy>
+ </extension>
</plugin>
diff --git a/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/src/org/eclipse/lsp4e/cpp/language/AutoIndentStrategyCPP.java b/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/src/org/eclipse/lsp4e/cpp/language/AutoIndentStrategyCPP.java
new file mode 100644
index 00000000000..8289ef4133d
--- /dev/null
+++ b/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/src/org/eclipse/lsp4e/cpp/language/AutoIndentStrategyCPP.java
@@ -0,0 +1,25 @@
+/*******************************************************************************
+ * Copyright (c) 2018 Manish Khurana , Nathan Ridge 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
+ *******************************************************************************/
+
+package org.eclipse.lsp4e.cpp.language;
+
+import org.eclipse.cdt.internal.ui.text.CAutoIndentStrategy;
+import org.eclipse.cdt.ui.CUIPlugin;
+
+/*
+ * Class to re-use existing auto-indentation support of CEditor in Generic Editor of LSP4E-CPP.
+ */
+
+@SuppressWarnings("restriction")
+public class AutoIndentStrategyCPP extends CAutoIndentStrategy {
+
+ public AutoIndentStrategyCPP() {
+ // TODO: Pass in the project so the auto edit strategy respects the project's preferences.
+ super(CUIPlugin.getDefault().getTextTools().getDocumentPartitioning(), null, true);
+ }
+}
diff --git a/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/src/org/eclipse/lsp4e/cpp/language/PresentationReconcilerCPP.java b/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/src/org/eclipse/lsp4e/cpp/language/PresentationReconcilerCPP.java
index 1b16aeb9350..03a756ce3db 100644
--- a/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/src/org/eclipse/lsp4e/cpp/language/PresentationReconcilerCPP.java
+++ b/lsp4e-cpp/org.eclipse.lsp4e.cpp.language/src/org/eclipse/lsp4e/cpp/language/PresentationReconcilerCPP.java
@@ -26,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.GPPLanguage;
import org.eclipse.cdt.core.model.ICLanguageKeywords;
import org.eclipse.cdt.core.model.ILanguage;
+import org.eclipse.cdt.internal.ui.editor.CEditor.BracketInserter;
import org.eclipse.cdt.internal.ui.text.CCodeScanner;
import org.eclipse.cdt.internal.ui.text.CCommentScanner;
import org.eclipse.cdt.internal.ui.text.CPreprocessorScanner;
@@ -52,8 +53,10 @@ import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextInputListener;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextPresentation;
+import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.RuleBasedScanner;
+import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.lsp4e.cpp.language.cquery.CquerySemanticHighlights;
import org.eclipse.lsp4e.cpp.language.cquery.HighlightSymbol;
import org.eclipse.lsp4j.Range;
@@ -62,6 +65,8 @@ import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.editors.text.TextEditor;
/**
* Hack-ish reconciler to get some colors in the generic editor using the C/C++
@@ -77,6 +82,7 @@ public class PresentationReconcilerCPP extends CPresentationReconciler {
private CqueryLineBackgroundListener fLineBackgroundListener = new CqueryLineBackgroundListener();
private ITextViewer textViewer;
private TextInputListenerCPP textInputListener;
+ private BracketInserter fBracketInserter;
public static Set<PresentationReconcilerCPP> presentationReconcilers = ConcurrentHashMap.newKeySet();
@@ -345,6 +351,19 @@ public class PresentationReconcilerCPP extends CPresentationReconciler {
StyledText textWidget = textViewer.getTextWidget();
textWidget.addLineBackgroundListener(fLineBackgroundListener);
presentationReconcilers.add(this);
+
+// Using asyncExec() to make sure that by the time Runnable runs,
+// the Editor is active and we don't get a NPE.
+ Display.getDefault().asyncExec(new Runnable() {
+ @Override
+ public void run() {
+// To provide bracket auto-completion support of CEditor in Generic Editor of LSP4E-CPP
+
+ TextEditor editor = (TextEditor) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
+ fBracketInserter = new BracketInserter(editor, true);
+ fBracketInserter.setSourceViewer((SourceViewer) textViewer);
+ ((TextViewer) textViewer).prependVerifyKeyListener(fBracketInserter);
+ }});
}
@Override
@@ -352,6 +371,7 @@ public class PresentationReconcilerCPP extends CPresentationReconciler {
super.uninstall();
textViewer.getTextWidget().removeLineBackgroundListener(fLineBackgroundListener);
textViewer.removeTextInputListener(textInputListener);
+ ((TextViewer) textViewer).removeVerifyKeyListener(fBracketInserter);
presentationReconcilers.remove(this);
}
}

Back to the top