Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Tern.java to be able to treat a custom project nature as Tern nature #38

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eclipse/tern.eclipse.ide.core/plugin.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ providerName=Angelo ZERR
# Extension point
ternServerTypes.name=Tern Server Types.
ternConsoleConnectors.name=Tern Console Connectors.
ternServerAdapters.name=Tern Nature Adapters

# Nature
ternNature.name=Tern Nature
Expand Down
2 changes: 2 additions & 0 deletions eclipse/tern.eclipse.ide.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
schema="schema/ternServerTypes.exsd" />
<extension-point id="ternConsoleConnectors" name="%ternConsoleConnectors.name"
schema="schema/ternConsoleConnectors.exsd" />
<extension-point id="ternNatureAdapters" name="%ternNatureAdapters.name"
schema="schema/ternNatureAdapters.exsd" />

<!-- =================================================================================== -->
<!-- Tern Builder -->
Expand Down
87 changes: 87 additions & 0 deletions eclipse/tern.eclipse.ide.core/schema/ternNatureAdapters.exsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version='1.0' encoding='UTF-8'?>
<!-- Schema file written by PDE -->
<schema targetNamespace="tern.eclipse.ide.core" xmlns="http://www.w3.org/2001/XMLSchema">
<annotation>
<appInfo>
<meta.schema plugin="tern.eclipse.ide.core" id="ternNatureAdapters" name="Tern Nature Adapters"/>
</appInfo>
<documentation>
Extension point to provide Natures that are to be treated as the Tern ones.
</documentation>
</annotation>

<element name="extension">
<annotation>
<appInfo>
<meta.element />
</appInfo>
</annotation>
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<element ref="ternAdaptToNature" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>
a fully-qualified name of the target extension point
</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>
an optional id
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>
an optional name
</documentation>
</annotation>
</attribute>
</complexType>
</element>

<element name="ternAdaptToNature">
<complexType>
<attribute name="id" type="string" use="required">
<annotation>
<documentation>
ID of the Nature to be treated as the Tern one
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>
an optional name
</documentation>
</annotation>
</attribute>
</complexType>
</element>

<annotation>
<appInfo>
<meta.section type="since"/>
</appInfo>
<documentation>
2.0
</documentation>
</annotation>



<annotation>
<appInfo>
<meta.section type="implementation"/>
</appInfo>
<documentation>
This plugin itself does not have any predefined builders.
</documentation>
</annotation>


</schema>
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.BadLocationException;
Expand Down Expand Up @@ -79,6 +82,8 @@ public class IDETernProject extends TernProject<IFile> {
private static final QualifiedName TERN_PROJECT = new QualifiedName(
TernCorePlugin.PLUGIN_ID + ".sessionprops", "TernProject");

private static final String EXTENSION_TERN_PROJECT_DESCRIBERS = "ternNatureAdapters";

private final IProject project;

private ITernServer ternServer;
Expand All @@ -91,6 +96,8 @@ public class IDETernProject extends TernProject<IFile> {

private final List<ITernServerListener> listeners;

private static List<String> ternNatureAdapters;

IDETernProject(IProject project) throws CoreException {
super(project.getLocation().toFile());
this.project = project;
Expand Down Expand Up @@ -184,11 +191,18 @@ public boolean isTernServerDisposed() {
*/
public static boolean hasTernNature(IProject project) {
try {
return project.hasNature(TernNature.ID);
if (project.hasNature(TernNature.ID))
return true;

loadTernProjectDescribers();
for (String adaptToNature : ternNatureAdapters) {
if (project.hasNature(adaptToNature))
return true;
}
} catch (CoreException e) {
Trace.trace(Trace.SEVERE, "Error tern nature", e);
return false;
}
return false;
}

@Override
Expand Down Expand Up @@ -240,7 +254,48 @@ private void loadIDEInfos() {

}
}

private static void loadTernProjectDescribers() {
if (ternNatureAdapters != null)
return;

Trace.trace(Trace.EXTENSION_POINT,
"->- Loading .ternProjectDescribers extension point ->-");

IExtensionRegistry registry = Platform.getExtensionRegistry();
IConfigurationElement[] cf = registry.getConfigurationElementsFor(
TernCorePlugin.PLUGIN_ID, EXTENSION_TERN_PROJECT_DESCRIBERS);
List<String> list = new ArrayList<String>(
cf.length);
addTernNatureAdapters(cf, list);
ternNatureAdapters = list;

Trace.trace(Trace.EXTENSION_POINT,
"-<- Done loading .ternProjectDescribers extension point -<-");
}

/**
* Load the tern project describers.
*/
private static synchronized void addTernNatureAdapters(
IConfigurationElement[] cf, List<String> list) {
for (IConfigurationElement ce : cf) {
try {
list.add(ce.getAttribute("id"));
Trace.trace(
Trace.EXTENSION_POINT,
" Loaded project describer: "
+ ce.getAttribute("id"));
} catch (Throwable t) {
Trace.trace(
Trace.SEVERE,
" Could not load project describers: "
+ ce.getAttribute("id"), t);
}
}
}


/**
* Returns the resource of the given path and type.
*
Expand Down
3 changes: 2 additions & 1 deletion eclipse/tern.eclipse.ide.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.ui.editors,
org.eclipse.jface.text,
org.eclipse.ui.workbench.texteditor,
tern.eclipse;bundle-version="0.2.0"
tern.eclipse;bundle-version="0.2.0",
org.eclipse.core.expressions
Bundle-ActivationPolicy: lazy
Bundle-Activator: tern.eclipse.ide.ui.TernUIPlugin
Import-Package: org.json.simple
Expand Down
34 changes: 22 additions & 12 deletions eclipse/tern.eclipse.ide.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
###############################################################################
-->
<plugin>

<extension point="org.eclipse.ui.startup">
<startup
class="tern.eclipse.ide.ui.internal.TernIDEStartup">
</startup>
</extension>

<extension point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
id="tern.eclipse.ide.ui.TernNatureTester"
type="org.eclipse.core.resources.IResource"
namespace="tern.eclipse.ide.ui"
properties="isTernProject"
class="tern.eclipse.ide.ui.internal.TernNatureTester">
</propertyTester>
</extension>

<!--Commands:-->

Expand Down Expand Up @@ -40,8 +56,7 @@
<adapt type="org.eclipse.core.resources.IProject">
<and>
<not>
<test property="org.eclipse.core.resources.projectNature"
value="tern.eclipse.ide.core.ternnature"/>
<test property="tern.eclipse.ide.ui.isTernProject" />
</not>
</and>
</adapt>
Expand Down Expand Up @@ -85,8 +100,7 @@
id="tern.eclipse.ide.internal.ui.properties.TernMainPropertyPage">
<enabledWhen>
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.projectNature"
value="tern.eclipse.ide.core.ternnature"/>
<test property="tern.eclipse.ide.ui.isTernProject" />
</adapt>
</enabledWhen>
</page>
Expand All @@ -97,8 +111,7 @@
id="tern.eclipse.ide.internal.ui.properties.TernTypeDefinitionsPropertyPage">
<enabledWhen>
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.projectNature"
value="tern.eclipse.ide.core.ternnature"/>
<test property="tern.eclipse.ide.ui.isTernProject" />
</adapt>
</enabledWhen>
</page>
Expand All @@ -109,8 +122,7 @@
id="tern.eclipse.ide.internal.ui.properties.TernPluginsPropertyPage">
<enabledWhen>
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.projectNature"
value="tern.eclipse.ide.core.ternnature"/>
<test property="tern.eclipse.ide.ui.isTernProject" />
</adapt>
</enabledWhen>
</page>
Expand All @@ -121,8 +133,7 @@
id="tern.eclipse.ide.internal.ui.properties.TernScriptPathsPropertyPage">
<enabledWhen>
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.projectNature"
value="tern.eclipse.ide.core.ternnature"/>
<test property="tern.eclipse.ide.ui.isTernProject" />
</adapt>
</enabledWhen>
</page>
Expand All @@ -133,8 +144,7 @@
id="tern.eclipse.ide.internal.ui.properties.TernConsolePropertyPage">
<enabledWhen>
<adapt type="org.eclipse.core.resources.IProject">
<test property="org.eclipse.core.resources.projectNature"
value="tern.eclipse.ide.core.ternnature"/>
<test property="tern.eclipse.ide.ui.isTernProject" />
</adapt>
</enabledWhen>
</page>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package tern.eclipse.ide.ui.internal;

import org.eclipse.ui.IStartup;

/**
* Need this to make TernNatureTester work from early start
*
* @author Victor Rubezhny
*/
public class TernIDEStartup implements IStartup {

@Override
public void earlyStartup() {
// Nothing really to do here, but need this to make TernNatureTester work from early start
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tern.eclipse.ide.ui.internal;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;

import tern.eclipse.ide.core.IDETernProject;

/**
* Property Tester for a IProject receiver object
*
* Property to be tested: "isTernProject"
*
* @author Victor Rubezhny
*/
public class TernNatureTester extends org.eclipse.core.expressions.PropertyTester {
private static final String IS_TERN_PROJECT_PROPERTY = "isTernProject";

public TernNatureTester() {
// Default constructor is required for property tester
}

/**
* Tests if the receiver object is a project is a Tern project
*
* @return true if the receiver object is a Project that has a nature that is treated as Tern nature,
* otherwise false is returned
*/
@Override
public boolean test(Object receiver, String property, Object[] args,
Object expectedValue) {

if (IS_TERN_PROJECT_PROPERTY.equals(property))
return testIsTernProject(receiver);

return false;
}

private boolean testIsTernProject(Object receiver) {
if (receiver instanceof IAdaptable) {
IProject project = (IProject)((IAdaptable)receiver).getAdapter(IProject.class);
if (project != null) {
return IDETernProject.hasTernNature(project);
}
}

return false;
}

}