Skip to content

Commit

Permalink
migrate files and remove xtend nature from n4js.json
Browse files Browse the repository at this point in the history
  • Loading branch information
mmews committed Dec 14, 2023
1 parent aa47980 commit c096d65
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 190 deletions.
5 changes: 0 additions & 5 deletions plugins/org.eclipse.n4js.json/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
<attribute name="ignore_optional_problems" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="xtend-gen">
<attributes>
<attribute name="ignore_optional_problems" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="module" value="true"/>
Expand Down
3 changes: 1 addition & 2 deletions plugins/org.eclipse.n4js.json/build.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
source.. = src/,\
src-gen/,\
xtend-gen/
src-gen/
bin.includes = .,\
META-INF/,\
plugin.xml
Expand Down
5 changes: 0 additions & 5 deletions plugins/org.eclipse.n4js.json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ Contributors:
</dependency>
</dependencies>
</plugin>

<plugin>
<groupId>org.eclipse.xtend</groupId>
<artifactId>xtend-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Copyright (c) 2017 NumberFour AG.
* 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:
* NumberFour AG - Initial API and implementation
*/
package org.eclipse.n4js.json.formatting2;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.n4js.json.JSON.JSONArray;
import org.eclipse.n4js.json.JSON.JSONDocument;
import org.eclipse.n4js.json.JSON.JSONObject;
import org.eclipse.n4js.json.JSON.JSONValue;
import org.eclipse.n4js.json.JSON.NameValuePair;
import org.eclipse.xtext.formatting2.AbstractFormatter2;
import org.eclipse.xtext.formatting2.IFormattableDocument;
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion;
import org.eclipse.xtext.xbase.lib.Pair;

/**
* A simple formatter for JSON files.
*
* Generally, puts name-value-pairs of objects and array elements on a separate line.
*/
public class JSONFormatter extends AbstractFormatter2 {

@Override
public void format(Object obj, IFormattableDocument document) {
// TODO Auto-generated method stub

}

/** */
public void format(JSONDocument jSONDocument, IFormattableDocument document) {
// make sure all document elements are formatted
document.format(jSONDocument.getContent());
for (ISemanticRegion sr : textRegionExtensions.allSemanticRegions(jSONDocument)) {
document.append(sr, f -> f.indent());
}
}

/** Put both brackets and every element of a JSONArray on a separate line. */
public void format(JSONArray al, IFormattableDocument document) {
// avoid comma-only lines
configureCommas(al, document);

Pair<ISemanticRegion, ISemanticRegion> bracketPair = textRegionExtensions.regionFor(al).keywordPairs("[", "]")
.get(0);

// if bracePair can be determined
if (bracketPair != null) {
// indent array elements
document.interior(bracketPair, f -> f.indent());

// format empty arrays to be a bracket pair without space (nor newline) in between
if (al.getElements().isEmpty()) {
document.append(bracketPair.getKey(), f -> f.noSpace());
document.prepend(bracketPair.getValue(), f -> f.noSpace());
return;
}

// put closing bracket on a separate line
document.prepend(bracketPair.getValue(), f -> f.newLine());
}

// put every array element on a separate line
for (JSONValue val : al.getElements()) {
document.prepend(val, f -> f.newLine());
}
// recursively format each element
for (JSONValue val : al.getElements()) {
document.format(val);
}
}

/** On the direct level of an semantic Object enforce commas to ", " with autoWrap option. */
private void configureCommas(EObject semEObject, IFormattableDocument document) {
for (ISemanticRegion sr : textRegionExtensions.regionFor(semEObject).keywords(",")) {
document.prepend(sr, f -> f.noSpace());
document.append(sr, f -> {
f.oneSpace();
f.autowrap();
});
}
}

/** Put both curly braces and every name-value-pair of a JSONObject on a separate line. */
public void format(JSONObject ol, IFormattableDocument document) {
configureCommas(ol, document);

Pair<ISemanticRegion, ISemanticRegion> bracePair = textRegionExtensions.regionFor(ol).keywordPairs("{", "}")
.get(0);

// if bracePair can be determined
if (bracePair != null) {
document.interior(bracePair, f -> f.indent());

// format empty objects to be a brace pair without space (nor newline) in between
if (ol.getNameValuePairs().isEmpty()) {
document.append(bracePair.getKey(), f -> f.noSpace());
document.prepend(bracePair.getValue(), f -> f.noSpace());
return;
}

document.prepend(bracePair.getValue(), f -> f.newLine()); // format WS in front of closing brace
for (NameValuePair nvp : ol.getNameValuePairs()) {
document.prepend(nvp, f -> f.newLine());
}

if (bracePair.getKey() != null
&& bracePair.getKey().getNextSemanticRegion() == bracePair.getValue()) {
// empty multiline, trigger formatting:
document.append(bracePair.getKey(), f -> f.newLine());
}
}

// recursively format each name-value pair
for (NameValuePair nvp : ol.getNameValuePairs()) {
document.format(nvp);
}
}

/***/
public void format(NameValuePair nameValuePair, IFormattableDocument document) {
ISemanticRegion colon = textRegionExtensions.regionFor(nameValuePair).keyword(":");
JSONValue value = nameValuePair.getValue();

document.prepend(colon, f -> f.noSpace());
document.append(colon, f -> f.oneSpace());

document.format(value);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.eclipse.n4js.json.resource;

import java.util.Collection;

import org.eclipse.n4js.json.extension.IJSONResourceDescriptionExtension;
import org.eclipse.n4js.json.extension.JSONExtensionRegistry;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.IResourceDescription.Delta;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionManager;

import com.google.inject.Inject;

/**
* Resource description manager for JSON files. Delegates to registered JSON extensions.
*/
public class JSONResourceDescriptionManager extends DefaultResourceDescriptionManager {

@Inject
private JSONExtensionRegistry extensionRegistry;

/**
* Delegates to registered resource description extensions.
*/
@Override
public boolean isAffected(Collection<Delta> deltas, IResourceDescription candidate, IResourceDescriptions context) {
for (IJSONResourceDescriptionExtension ext : extensionRegistry.getResourceDescriptionExtensions()) {
if (ext.isAffected(deltas, candidate, context)) {
return true;
}
}
return false;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.eclipse.n4js.json.resource;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.n4js.json.JSON.JSONDocument;
import org.eclipse.n4js.json.extension.IJSONResourceDescriptionExtension;
import org.eclipse.n4js.json.extension.JSONExtensionRegistry;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionStrategy;
import org.eclipse.xtext.util.IAcceptor;

import com.google.inject.Inject;

/**
* JSON resource description strategy based on {@link IJSONResourceDescriptionExtension}.
*
* Does nothing per default, except for the case in which an extension provides a custom resource description.
*/
public class JSONResourceDescriptionStrategy extends DefaultResourceDescriptionStrategy {

@Inject
private JSONExtensionRegistry extensionRegistry;

/** Delegates to registered resource description extensions. */
@Override
public boolean createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
if (eObject instanceof JSONDocument) {
for (IJSONResourceDescriptionExtension ext : extensionRegistry.getResourceDescriptionExtensions()) {
ext.createJSONDocumentDescriptions((JSONDocument) eObject, acceptor);
}
}
return false;
}
}
Loading

0 comments on commit c096d65

Please sign in to comment.