Skip to content

Commit

Permalink
Issue eclipse-tm4e#17 - Associate with text content-type
Browse files Browse the repository at this point in the history
And throw an exception in install if it cannot apply properly (missing
grammar).

Signed-off-by: Mickael Istria <[email protected]>
  • Loading branch information
mickaelistria committed Aug 7, 2017
1 parent ec19c04 commit 9e4c946
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
3 changes: 2 additions & 1 deletion org.eclipse.tm4e.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions org.eclipse.tm4e.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,12 @@
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.genericeditor.presentationReconcilers">
<presentationReconciler
class="org.eclipse.tm4e.ui.text.TMPresentationReconciler"
contentType="org.eclipse.core.runtime.text">
</presentationReconciler>
</extension>

</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 9e4c946

Please sign in to comment.