Search in sources :

Example 1 with ReplaceRegion

use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.

the class AbstractSmokeTest method testSkipNodesInBetween.

@Test
public void testSkipNodesInBetween() throws Exception {
    for (String string : getSmokeTestModels()) {
        LazyLinkingResource resource = createResource(string);
        if (resource != null) {
            ICompositeNode rootNode = resource.getParseResult().getRootNode();
            ReplaceRegion replaceRegion = null;
            for (INode node : rootNode.getAsTreeIterable()) {
                int offset = node.getTotalOffset();
                int length = node.getTotalLength();
                if (replaceRegion == null || replaceRegion.getOffset() != offset || replaceRegion.getLength() != length) {
                    replaceRegion = new ReplaceRegion(offset, length, "");
                    StringBuilder builder = new StringBuilder(string);
                    replaceRegion.applyTo(builder);
                    processModel(builder.toString());
                }
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) LazyLinkingResource(org.eclipse.xtext.linking.lazy.LazyLinkingResource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) Test(org.junit.Test)

Example 2 with ReplaceRegion

use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.

the class ReplacingAppendable method insertNewImports.

public int insertNewImports() throws BadLocationException {
    List<ReplaceRegion> importChanges = getImportSection().rewrite();
    int lengthDelta = 0;
    if (!importChanges.isEmpty()) {
        for (ReplaceRegion change : importChanges) {
            lengthDelta = lengthDelta - change.getLength() + change.getText().length();
        }
        replaceConverter.convertToTextEdit(importChanges).apply(getDocument());
    }
    return lengthDelta;
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion)

Example 3 with ReplaceRegion

use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.

the class DocumentRewriter method getChanges.

public List<ReplaceRegion> getChanges() {
    List<ReplaceRegion> changes = newArrayList();
    for (Section section : sections) {
        ReplaceRegion change = section.getChange();
        changes.add(change);
    }
    for (ReplaceRegion change : importSection.rewrite()) {
        changes.add(change);
    }
    return changes;
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) RewritableImportSection(org.eclipse.xtext.xbase.imports.RewritableImportSection)

Example 4 with ReplaceRegion

use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.

the class MultiImportOrganizer method organizeImports.

/**
 * @param files
 *            ordered by corresponding {@link IProject}
 * @param mon
 * @return Creates {@link Change}s for each {@link IFile} using {@link ImportOrganizer}
 * @throws InterruptedException
 */
public List<Change> organizeImports(Multimap<IProject, IFile> files, IProgressMonitor mon) throws InterruptedException {
    List<Change> result = Lists.newArrayList();
    for (IProject project : files.keySet()) {
        ResourceSet resSet = resSetProvider.get(project);
        for (IFile file : files.get(project)) {
            mon.subTask("Calculating imports in - " + file.getName());
            URI uri = storageMapper.getUri(file);
            if (uri != null) {
                XtextResource resource = (XtextResource) resSet.getResource(uri, true);
                List<ReplaceRegion> replace = importOrganizer.getOrganizedImportChanges(resource);
                // TODO - find out why \n\n changes are produced, even if there are any unused imports
                if (replace == null) {
                    continue;
                }
                TextEdit textEdit = replaceConverter.convertToTextEdit(replace);
                IRefactoringDocument iRefactoringDocument = provider.get(uri, status);
                Change change = iRefactoringDocument.createChange(Messages.OrganizeImports, textEdit);
                if (change instanceof EditorDocumentChange) {
                    if (((EditorDocument) iRefactoringDocument).getEditor().isDirty()) {
                        ((EditorDocumentChange) change).setDoSave(false);
                    }
                }
                result.add(change);
            }
            mon.worked(1);
            if (mon.isCanceled()) {
                throw new InterruptedException();
            }
        }
    }
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) IRefactoringDocument(org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument) XtextResource(org.eclipse.xtext.resource.XtextResource) Change(org.eclipse.ltk.core.refactoring.Change) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) TextEdit(org.eclipse.text.edits.TextEdit)

Example 5 with ReplaceRegion

use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.

the class OrganizeImportsHandler method doOrganizeImports.

public void doOrganizeImports(final IXtextDocument document) {
    List<ReplaceRegion> result = document.priorityReadOnly(new IUnitOfWork<List<ReplaceRegion>, XtextResource>() {

        @Override
        public List<ReplaceRegion> exec(XtextResource state) throws Exception {
            return importOrganizer.getOrganizedImportChanges(state);
        }
    });
    if (result == null || result.isEmpty())
        return;
    try {
        DocumentRewriteSession session = null;
        if (document instanceof IDocumentExtension4) {
            session = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
        }
        replaceConverter.convertToTextEdit(result).apply(document);
        if (session != null) {
            ((IDocumentExtension4) document).stopRewriteSession(session);
        }
    } catch (BadLocationException e) {
        LOG.error(Messages.OrganizeImportsHandler_organizeImportsErrorMessage, e);
    }
}
Also used : DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) List(java.util.List) XtextResource(org.eclipse.xtext.resource.XtextResource) ExecutionException(org.eclipse.core.commands.ExecutionException) BadLocationException(org.eclipse.jface.text.BadLocationException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)52 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)20 Test (org.junit.Test)19 XtextResource (org.eclipse.xtext.resource.XtextResource)18 Resource (org.eclipse.emf.ecore.resource.Resource)13 INode (org.eclipse.xtext.nodemodel.INode)11 IParseResult (org.eclipse.xtext.parser.IParseResult)10 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)9 EObject (org.eclipse.emf.ecore.EObject)7 LazyLinkingResource (org.eclipse.xtext.linking.lazy.LazyLinkingResource)6 StringReader (java.io.StringReader)5 SyntheticCompositeNode (org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode)4 TextRegion (org.eclipse.xtext.util.TextRegion)4 List (java.util.List)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 TextEdit (org.eclipse.text.edits.TextEdit)3 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 PartialParsingPointers (org.eclipse.xtext.parser.impl.PartialParsingPointers)3 ITextRegion (org.eclipse.xtext.util.ITextRegion)3 ArrayList (java.util.ArrayList)2