Skip to content

Commit

Permalink
Implement themeAssociation + preview with theme/grammar. See
Browse files Browse the repository at this point in the history
#32

Signed-off-by: angelozerr <[email protected]>
  • Loading branch information
angelozerr committed Apr 28, 2017
1 parent c6c4805 commit eaf09cc
Show file tree
Hide file tree
Showing 24 changed files with 875 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,42 @@
* Contributors:
* - Microsoft Corporation: Initial code, written in TypeScript, licensed under MIT license
* - Angelo Zerr <[email protected]> - translation and adaptation to Java
*/
package org.eclipse.tm4e.core.grammar;

/**
* TextMate grammar API.
*
* @see https://github.com/Microsoft/vscode-textmate/blob/master/src/main.ts
*
*/
public interface IGrammar {

/**
* Tokenize `lineText`.
*
* @param lineText
* the line text to tokenize.
* @return the result of the tokenization.
*/
ITokenizeLineResult tokenizeLine(String lineText);

/**
* Tokenize `lineText` using previous line state `prevState`.
*
* @param lineText
* the line text to tokenize.
* @param prevState
* previous line state.
* @return the result of the tokenization.
*/
ITokenizeLineResult tokenizeLine(String lineText, StackElement prevState);

}
*/
package org.eclipse.tm4e.core.grammar;

/**
* TextMate grammar API.
*
* @see https://github.com/Microsoft/vscode-textmate/blob/master/src/main.ts
*
*/
public interface IGrammar {

/**
* Returns the scope name of the grammar.
*
* @return the scope name of the grammar.
*/
String getScopeName();

/**
* Tokenize `lineText`.
*
* @param lineText
* the line text to tokenize.
* @return the result of the tokenization.
*/
ITokenizeLineResult tokenizeLine(String lineText);

/**
* Tokenize `lineText` using previous line state `prevState`.
*
* @param lineText
* the line text to tokenize.
* @param prevState
* previous line state.
* @return the result of the tokenization.
*/
ITokenizeLineResult tokenizeLine(String lineText, StackElement prevState);

}
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,8 @@ public ITokenizeLineResult tokenizeLine(String lineText, StackElement prevState)
return new TokenizeLineResult(_produced, nextState);
}

@Override
public String getScopeName() {
return _grammar.getScopeName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
*/
public interface IGrammarDefinition {

/**
* Returns the name of the TextMate grammar.
*
* @return the name of the TextMate grammar.
*/
String getName();

/**
* Returns the scope name of the TextMate grammar.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface IGrammarRegistryManager {
* the content type.
* @return the {@link IGrammar} for the given scope name and null otherwise.
*/
IGrammar getGrammarFor(String scopeName);
IGrammar getGrammarForScope(String scopeName);

/**
* Returns the list of registered TextMate grammars.
Expand All @@ -47,4 +47,6 @@ public interface IGrammarRegistryManager {
*/
IGrammarDefinition[] getDefinitions();

String[] getContentTypesForScope(String scopeName);

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,30 @@ public class GrammarDefinition implements IGrammarDefinition {

private static final String PLATFORM_PLUGIN = "platform:/plugin/"; //$NON-NLS-1$

private String path;
private String scopeName;
private String pluginId;
private final String name;
private final String path;
private final String scopeName;
private final String pluginId;

public GrammarDefinition(IConfigurationElement element) {
this.path = element.getAttribute(XMLConstants.PATH_ATTR);
this.scopeName = element.getAttribute(XMLConstants.SCOPE_NAME_ATTR);
this.name = getName(element.getAttribute(XMLConstants.NAME_ATTR), scopeName);
this.pluginId = element.getNamespaceIdentifier();
}

private String getName(String name, String scopeName) {
if (name != null && name.length() > 0) {
return name;
}
return scopeName;
}

@Override
public String getName() {
return name;
}

@Override
public String getScopeName() {
return scopeName;
Expand All @@ -55,7 +69,7 @@ public String getScopeName() {
public String getPath() {
return path;
}

@Override
public String getPluginId() {
return pluginId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionDelta;
Expand Down Expand Up @@ -70,7 +71,7 @@ public IGrammar getGrammarFor(IContentType[] contentTypes) {
for (IContentType contentType : contentTypes) {
String scopeName = getScopeName(contentType.getId());
if (scopeName != null) {
IGrammar grammar = getGrammarFor(scopeName);
IGrammar grammar = getGrammarForScope(scopeName);
if (grammar != null) {
return grammar;
}
Expand All @@ -80,7 +81,7 @@ public IGrammar getGrammarFor(IContentType[] contentTypes) {
}

@Override
public IGrammar getGrammarFor(String scopeName) {
public IGrammar getGrammarForScope(String scopeName) {
loadGrammarsIfNeeded();
return registry.getGrammar(scopeName);
}
Expand All @@ -95,6 +96,13 @@ private String getScopeName(String contentTypeId) {
return scopeNameBindings.get(contentTypeId);
}

@Override
public String[] getContentTypesForScope(String scopeName) {
loadGrammarsIfNeeded();
return scopeNameBindings.entrySet().stream().filter(map -> scopeName.equals(map.getValue()))
.map(map -> map.getKey()).collect(Collectors.toList()).toArray(new String[0]);
}

@Override
public void registryChanged(final IRegistryChangeEvent event) {
IExtensionDelta[] deltas = event.getExtensionDeltas(TMEclipseRegistryPlugin.PLUGIN_ID, EXTENSION_GRAMMARS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class XMLConstants {

// grammar definition
public static final String GRAMMAR_ELT = "grammar";
public static final String NAME_ATTR = "name";
public static final String SCOPE_NAME_ATTR = "scopeName";
public static final String PATH_ATTR = "path";

Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Require-Bundle: org.eclipse.tm4e.core,
org.eclipse.ui,
org.eclipse.core.resources,
org.eclipse.core.filesystem,
org.eclipse.tm4e.registry;bundle-version="0.0.1",
org.eclipse.tm4e.registry,
org.eclipse.ui.ide;resolution:=optional
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Export-Package: org.eclipse.tm4e.ui,
Expand Down
33 changes: 21 additions & 12 deletions org.eclipse.tm4e.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@
<!-- Register default TextMate Themes -->
<extension
point="org.eclipse.tm4e.ui.themes">
<!-- Default theme used for any Eclipse E4 theme -->
<theme
id="org.eclipse.tm4e.ui.themes.SolarizedLight"
name="%Theme.SolarizedLight.name"
path="./themes/Solarized-light.css" >
</theme>
<themeAssociation
themeId="org.eclipse.tm4e.ui.themes.SolarizedLight" >
</themeAssociation>
<!-- Dark theme bound with Eclipse E4 Dark theme -->
<theme
id="org.eclipse.tm4e.ui.themes.Dark"
name="%Theme.Dark.name"
path="./themes/Dark.css" >
</theme>
<themeAssociation
themeId="org.eclipse.tm4e.ui.themes.Dark"
eclipseThemeId="org.eclipse.e4.ui.css.theme.e4_dark" >
</themeAssociation>

<!-- Other themes -->
<theme
id="org.eclipse.tm4e.ui.themes.Light"
name="%Theme.Light.name"
Expand All @@ -31,18 +52,6 @@
name="%Theme.Monokai.name"
path="./themes/Monokai.css" >
</theme>
<theme
id="org.eclipse.tm4e.ui.themes.SolarizedLight"
name="%Theme.SolarizedLight.name"
path="./themes/Solarized-light.css"
eclipseThemeId="*" >
</theme>
<theme
id="org.eclipse.tm4e.ui.themes.Dark"
name="%Theme.Dark.name"
path="./themes/Dark.css"
eclipseThemeId="org.eclipse.e4.ui.css.theme.e4_dark" >
</theme>
</extension>

<!-- Preference Pages -->
Expand Down
35 changes: 23 additions & 12 deletions org.eclipse.tm4e.ui/schema/themes.exsd
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<element ref="theme" minOccurs="0" maxOccurs="unbounded"/>
<element ref="themeContentTypeBinding" minOccurs="0" maxOccurs="unbounded"/>
<element ref="themeAssociation" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
Expand Down Expand Up @@ -83,33 +83,44 @@
</appinfo>
</annotation>
</attribute>
<attribute name="eclipseThemeId" type="string">
<annotation>
<documentation>

</documentation>
</annotation>
</attribute>
</complexType>
</element>

<element name="themeContentTypeBinding">
<element name="themeAssociation">
<annotation>
<documentation>
This extension point allows developers to register TextMate theme.
</documentation>
</annotation>
<complexType>
<attribute name="themeId" type="string" use="required">
<annotation>
<documentation>

The referenced TextMate theme id.
</documentation>
<appinfo>
<meta.attribute kind="identifier" basedOn="org.eclipse.tm4e.ui.themes/theme/@id"/>
</appinfo>
</annotation>
</attribute>
<attribute name="contentTypeId" type="string">
<attribute name="scopeName" type="string">
<annotation>
<documentation>

Use the TextMate theme &lt;code&gt;themeId&lt;/code&gt; for the given grammar identified with &lt;code&gt;scopeName&lt;/code&gt;
</documentation>
<appinfo>
<meta.attribute kind="identifier" basedOn="org.eclipse.tm4e.registry.grammars/grammar/@scopeName"/>
</appinfo>
</annotation>
</attribute>
<attribute name="eclipseThemeId" type="string">
<annotation>
<documentation>
Use the TextMate theme &lt;code&gt;themeId&lt;/code&gt; when the Eclipse CSS theme id is activated.
</documentation>
<appinfo>
<meta.attribute kind="identifier" basedOn="org.eclipse.e4.ui.css.swt.theme/theme/@id"/>
</appinfo>
</annotation>
</attribute>
</complexType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class TMUIMessages extends NLS {
public static String GrammarPreferencePage_column_scopeName;
public static String GrammarPreferencePage_column_path;
public static String GrammarPreferencePage_column_pluginId;
public static String GrammarPreferencePage_ScopeNameContentTypeBinding;
public static String GrammarPreferencePage_ThemeAssociations;
public static String GrammarPreferencePage_preview;

// Theme preferences page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ GrammarPreferencePage_description=&Create, edit or remove TextMate grammars:
GrammarPreferencePage_column_scopeName=Scope
GrammarPreferencePage_column_path=Path
GrammarPreferencePage_column_pluginId=Plugin ID
GrammarPreferencePage_ScopeNameContentTypeBinding=Content type bindings:
GrammarPreferencePage_ThemeAssociations=Theme associations:
GrammarPreferencePage_preview=Previe&w:

ThemePreferencePage_title=TextMate themes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright (c) 2015-2017 Angelo ZERR.
* 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
*
* Contributors:
* Angelo Zerr <[email protected]> - initial API and implementation
*/
package org.eclipse.tm4e.ui.internal.preferences;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

/**
* Label provider for {@link IContentType}.
*/
class ContentTypeLabelProvider extends LabelProvider implements ITableLabelProvider {

@Override
public Image getColumnImage(Object element, int columnIndex) {
return null;
}

@Override
public String getText(Object element) {
return getColumnText(element, 0);
}

@Override
public String getColumnText(Object element, int columnIndex) {
String contentTypeId = (String) element;
switch (columnIndex) {
case 0:
IContentType contentType = Platform.getContentTypeManager().getContentType(contentTypeId);
if (contentType == null) {
return contentTypeId;
}
return contentType.getName() + " (" + contentType.getId() + ")";
default:
return ""; //$NON-NLS-1$
}
}
}
Loading

0 comments on commit eaf09cc

Please sign in to comment.