From 9e4c9466d064ba50c33150d3883315c66aea0711 Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Mon, 7 Aug 2017 18:40:57 +0200 Subject: [PATCH] Issue #17 - Associate with text content-type And throw an exception in install if it cannot apply properly (missing grammar). Signed-off-by: Mickael Istria --- org.eclipse.tm4e.ui/META-INF/MANIFEST.MF | 3 +- org.eclipse.tm4e.ui/plugin.xml | 7 +++ .../ui/text/TMPresentationReconciler.java | 50 ++++++++++++------- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/org.eclipse.tm4e.ui/META-INF/MANIFEST.MF b/org.eclipse.tm4e.ui/META-INF/MANIFEST.MF index 2d17bb8e5..8eeead662 100644 --- a/org.eclipse.tm4e.ui/META-INF/MANIFEST.MF +++ b/org.eclipse.tm4e.ui/META-INF/MANIFEST.MF @@ -15,7 +15,8 @@ Require-Bundle: org.eclipse.tm4e.core, org.eclipse.ui.ide;resolution:=optional, com.google.gson, org.eclipse.e4.ui.css.swt.theme, - org.eclipse.core.expressions + org.eclipse.core.expressions, + org.eclipse.ui.genericeditor;bundle-version="1.1.0";resolution:=optional Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Export-Package: org.eclipse.tm4e.ui, org.eclipse.tm4e.ui.internal.model;x-friends:="org.eclipse.tm4e.ui.tests", diff --git a/org.eclipse.tm4e.ui/plugin.xml b/org.eclipse.tm4e.ui/plugin.xml index 82d8dac16..43f3490df 100644 --- a/org.eclipse.tm4e.ui/plugin.xml +++ b/org.eclipse.tm4e.ui/plugin.xml @@ -136,5 +136,12 @@ + + + + diff --git a/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/TMPresentationReconciler.java b/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/TMPresentationReconciler.java index e853a448d..c13c19cc2 100644 --- a/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/TMPresentationReconciler.java +++ b/org.eclipse.tm4e.ui/src/main/java/org/eclipse/tm4e/ui/text/TMPresentationReconciler.java @@ -16,7 +16,9 @@ import org.eclipse.core.runtime.Assert; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.content.IContentType; import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; @@ -246,22 +248,6 @@ public void inputDocumentChanged(IDocument oldDocument, IDocument newDocument) { } } - private IGrammar findGrammar(ContentTypeInfo info) { - IGrammar grammar; - IContentType[] contentTypes = info.getContentTypes(); - // Discover the well grammar from the contentTypes - grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(contentTypes); - if (grammar == null) { - // Discover the well grammar from the filetype - String fileName = info.getFileName(); - if (fileName != null) { - String fileType = new Path(fileName).getFileExtension(); - grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarForFileType(fileType); - } - } - return grammar; - } - @Override public void textChanged(TextEvent e) { if (!e.getViewerRedrawState()) { @@ -354,6 +340,22 @@ public void run() { } } + private static IGrammar findGrammar(ContentTypeInfo info) { + IGrammar grammar; + IContentType[] contentTypes = info.getContentTypes(); + // Discover the well grammar from the contentTypes + grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarFor(contentTypes); + if (grammar == null) { + // Discover the well grammar from the filetype + String fileName = info.getFileName(); + if (fileName != null) { + String fileType = new Path(fileName).getFileExtension(); + grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarForFileType(fileType); + } + } + return grammar; + } + public void setGrammar(IGrammar grammar) { boolean changed = (viewer != null && ((this.grammar == null) || !this.grammar.equals(grammar))); this.grammar = grammar; @@ -420,7 +422,21 @@ private void themeChange(ITokenProvider oldTheme, ITokenProvider newTheme, IDocu @Override public void install(ITextViewer viewer) { Assert.isNotNull(viewer); - + + if (this.grammar == null && viewer.getDocument() != null) { + try { + ContentTypeInfo info = ContentTypeHelper.findContentTypes(viewer.getDocument()); + if (info != null) { + this.grammar = findGrammar(info); + } + } catch (CoreException e) { + TMUIPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, TMUIPlugin.PLUGIN_ID, e.getMessage(), e)); + } + if (this.grammar == null) { + throw new IllegalArgumentException("No TextMate Grammar for provided editor/viewer/document."); + } + } + this.viewer = viewer; viewer.addTextInputListener(internalListener);