Search in sources :

Example 1 with ScriptElement

use of org.eclipse.n4js.n4JS.ScriptElement in project n4js by eclipse.

the class ImportsRemovalChangesComputer2 method getImportDeletionChanges.

/**
 * Compute changes that will remove all imports.
 *
 * @param resource
 *            the resource to modify
 * @param document
 *            the document connected to the xtextResource, for textual changes.
 * @return list of changes to the document.
 */
public static List<IChange> getImportDeletionChanges(XtextResource resource, IXtextDocument document) throws BadLocationException {
    List<IChange> changes = new ArrayList<>();
    List<ScriptElement> elements = XtextResourceUtils.getScript(resource).getScriptElements();
    // elements.filter(ImportDeclaration).map[findActualNodeFor(it)].forEach[changes.add(document.removeNodeButKeepComments(it))]
    for (ScriptElement el : elements) {
        if (el instanceof ImportDeclaration) {
            INode nodeToRemove = NodeModelUtils.findActualNodeFor(el);
            changes.add(removeNodeButKeepComments(document, nodeToRemove));
        }
    }
    return changes;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ArrayList(java.util.ArrayList) ScriptElement(org.eclipse.n4js.n4JS.ScriptElement) ImportDeclaration(org.eclipse.n4js.n4JS.ImportDeclaration) IChange(org.eclipse.n4js.ui.changes.IChange)

Example 2 with ScriptElement

use of org.eclipse.n4js.n4JS.ScriptElement in project n4js by eclipse.

the class ScriptFactory method buildComplexNode.

static ComplexNode buildComplexNode(ReentrantASTIterator astpp, Script script) {
    ComplexNode cNode = new ComplexNode(astpp.container(), script);
    Node entryNode = new HelperNode(NodeNames.ENTRY, astpp.pos(), script);
    List<Node> scriptNodes = new LinkedList<>();
    EList<ScriptElement> scriptElems = script.getScriptElements();
    for (int n = 0; n < scriptElems.size(); n++) {
        ScriptElement scriptElem = getScriptElementAt(script, n);
        if (isControlFlowStatement(scriptElem)) {
            Node blockNode = DelegatingNodeFactory.create(astpp, "stmt_" + n, script, (Statement) scriptElem);
            scriptNodes.add(blockNode);
        }
    }
    Node exitNode = new HelperNode(NodeNames.EXIT, astpp.pos(), script);
    cNode.addNode(entryNode);
    for (Node scriptNode : scriptNodes) cNode.addNode(scriptNode);
    cNode.addNode(exitNode);
    List<Node> nodes = new LinkedList<>();
    nodes.add(entryNode);
    nodes.addAll(scriptNodes);
    nodes.add(exitNode);
    cNode.connectInternalSucc(nodes);
    cNode.setEntryNode(entryNode);
    cNode.setExitNode(exitNode);
    exitNode.addCatchToken(new CatchToken(ControlFlowType.CatchesAll));
    return cNode;
}
Also used : HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) HelperNode(org.eclipse.n4js.flowgraphs.model.HelperNode) ComplexNode(org.eclipse.n4js.flowgraphs.model.ComplexNode) Node(org.eclipse.n4js.flowgraphs.model.Node) ScriptElement(org.eclipse.n4js.n4JS.ScriptElement) CatchToken(org.eclipse.n4js.flowgraphs.model.CatchToken) LinkedList(java.util.LinkedList)

Example 3 with ScriptElement

use of org.eclipse.n4js.n4JS.ScriptElement in project n4js by eclipse.

the class ScriptFactory method getScriptElementAt.

private static ScriptElement getScriptElementAt(Script script, int i) {
    EList<ScriptElement> scriptElems = script.getScriptElements();
    ScriptElement scriptElement = scriptElems.get(i);
    if (scriptElement instanceof ExportDeclaration) {
        ExportableElement expElem = ((ExportDeclaration) scriptElement).getExportedElement();
        if (expElem instanceof ExportedVariableStatement) {
            scriptElement = (ExportedVariableStatement) expElem;
        }
    }
    return scriptElement;
}
Also used : ExportableElement(org.eclipse.n4js.n4JS.ExportableElement) ScriptElement(org.eclipse.n4js.n4JS.ScriptElement) ExportDeclaration(org.eclipse.n4js.n4JS.ExportDeclaration) ExportedVariableStatement(org.eclipse.n4js.n4JS.ExportedVariableStatement)

Example 4 with ScriptElement

use of org.eclipse.n4js.n4JS.ScriptElement in project n4js by eclipse.

the class ImportRewriter method findInsertionOffset.

private int findInsertionOffset() {
    int result = 0;
    List<ScriptElement> scriptElements = script.getScriptElements();
    for (int i = 0, size = scriptElements.size(); i < size; i++) {
        ScriptElement element = scriptElements.get(i);
        if (element instanceof ImportDeclaration) {
            // Instead of getting the total offset for the first non-import-declaration, we try to get the
            // total end offset for the most recent import declaration which is followed by any other script element
            // this is required for the linebreak handling for automatic semicolon insertion.
            final ICompositeNode importNode = NodeModelUtils.findActualNodeFor(element);
            if (null != importNode) {
                result = importNode.getTotalOffset() + getLengthWithoutAutomaticSemicolon(importNode);
            }
        } else {
            // Otherwise, we assume there is no import declarations yet, we can put it to the top of the document.
            return result;
        }
    }
    return result;
}
Also used : ScriptElement(org.eclipse.n4js.n4JS.ScriptElement) ImportDeclaration(org.eclipse.n4js.n4JS.ImportDeclaration) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode)

Example 5 with ScriptElement

use of org.eclipse.n4js.n4JS.ScriptElement in project n4js by eclipse.

the class BuiltInTypeScopeTest method testParsing.

@SuppressWarnings("javadoc")
@Test
public void testParsing() throws Exception {
    String content = "var name: any = 'global'";
    Script result = parseHelper.parse(content);
    Assert.assertNotNull(result);
    Assert.assertEquals(1, result.getScriptElements().size());
    ScriptElement elem = result.getScriptElements().get(0);
    Assert.assertTrue(elem instanceof VariableStatement);
    VariableStatement stmt = (VariableStatement) elem;
    VariableDeclaration varDecl = stmt.getVarDecl().get(0);
    ParameterizedTypeRef typeRef = (ParameterizedTypeRef) varDecl.getDeclaredTypeRef();
    Assert.assertFalse("Proxy URI: " + ((InternalEObject) typeRef.getDeclaredType()).eProxyURI(), typeRef.getDeclaredType().eIsProxy());
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) Script(org.eclipse.n4js.n4JS.Script) VariableStatement(org.eclipse.n4js.n4JS.VariableStatement) ScriptElement(org.eclipse.n4js.n4JS.ScriptElement) VariableDeclaration(org.eclipse.n4js.n4JS.VariableDeclaration) InternalEObject(org.eclipse.emf.ecore.InternalEObject) Test(org.junit.Test)

Aggregations

ScriptElement (org.eclipse.n4js.n4JS.ScriptElement)5 ImportDeclaration (org.eclipse.n4js.n4JS.ImportDeclaration)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 InternalEObject (org.eclipse.emf.ecore.InternalEObject)1 CatchToken (org.eclipse.n4js.flowgraphs.model.CatchToken)1 ComplexNode (org.eclipse.n4js.flowgraphs.model.ComplexNode)1 HelperNode (org.eclipse.n4js.flowgraphs.model.HelperNode)1 Node (org.eclipse.n4js.flowgraphs.model.Node)1 ExportDeclaration (org.eclipse.n4js.n4JS.ExportDeclaration)1 ExportableElement (org.eclipse.n4js.n4JS.ExportableElement)1 ExportedVariableStatement (org.eclipse.n4js.n4JS.ExportedVariableStatement)1 Script (org.eclipse.n4js.n4JS.Script)1 VariableDeclaration (org.eclipse.n4js.n4JS.VariableDeclaration)1 VariableStatement (org.eclipse.n4js.n4JS.VariableStatement)1 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)1 IChange (org.eclipse.n4js.ui.changes.IChange)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 INode (org.eclipse.xtext.nodemodel.INode)1 Test (org.junit.Test)1