Search in sources :

Example 1 with Element

use of org.eclipse.xtext.ui.tests.refactoring.refactoring.Element in project xtext-eclipse by eclipse.

the class DefaultRenameElementStrategyTest method testRenameElementStrategy.

@Test
public void testRenameElementStrategy() throws Exception {
    final XtextResource resource = getResourceFromString("A { B { C } }");
    EObject targetElement = resource.getContents().get(0).eContents().get(0);
    assertNotNull(targetElement);
    assertTrue(targetElement instanceof Element);
    assertEquals("A", ((Element) targetElement).getName());
    IRenameStrategy renameElementStrategy = strategyProvider.get(targetElement, null);
    assertNotNull(renameElementStrategy);
    assertEquals("A", renameElementStrategy.getOriginalName());
    RefactoringStatus validateNewNameStatus = renameElementStrategy.validateNewName("A");
    assertTrue(validateNewNameStatus.isOK());
    validateNewNameStatus = renameElementStrategy.validateNewName("}");
    assertTrue(validateNewNameStatus.hasFatalError());
    validateNewNameStatus = renameElementStrategy.validateNewName("ref");
    assertTrue(validateNewNameStatus.hasError());
    renameElementStrategy.applyDeclarationChange("D", resource.getResourceSet());
    assertEquals("D", ((Element) targetElement).getName());
    renameElementStrategy.createDeclarationUpdates("D", resource.getResourceSet(), this);
    assertEquals(0, changes.size());
    assertEquals(1, textEdits.size());
    assertTrue(textEdits.get(0) instanceof ReplaceEdit);
    ReplaceEdit renameEdit = (ReplaceEdit) textEdits.get(0);
    assertEquals(0, renameEdit.getOffset());
    assertEquals(1, renameEdit.getLength());
    assertEquals("D", renameEdit.getText());
}
Also used : IRenameStrategy(org.eclipse.xtext.ui.refactoring.IRenameStrategy) EObject(org.eclipse.emf.ecore.EObject) Element(org.eclipse.xtext.ui.tests.refactoring.refactoring.Element) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 2 with Element

use of org.eclipse.xtext.ui.tests.refactoring.refactoring.Element in project xtext-eclipse by eclipse.

the class RenamedElementTrackerTest method testResolveElements.

@Test
public void testResolveElements() throws Exception {
    URI resourceURI = URI.createFileURI("testresource.refactoringtestlanguage");
    String textualModel = "A { B { C { ref A.B } } ref B }";
    XtextResource resource = getResource(textualModel, resourceURI.toString());
    Element elementA = (Element) resource.getContents().get(0).eContents().get(0);
    Element elementB = elementA.getContained().get(0);
    Element elementC = elementB.getContained().get(0);
    URI uriB = EcoreUtil.getURI(elementB);
    URI uriC = EcoreUtil.getURI(elementC);
    String newName = "newB";
    List<URI> renamedElementURIs = newArrayList(uriB, uriC);
    IRenameStrategy renameStrategy = getInjector().getInstance(IRenameStrategy.Provider.class).get(elementB, null);
    IRenamedElementTracker renamedElementTracker = new RenamedElementTracker();
    Map<URI, URI> original2newElementURIs = renamedElementTracker.renameAndTrack(renamedElementURIs, newName, resource.getResourceSet(), renameStrategy, new NullProgressMonitor());
    assertEquals("B", elementB.getName());
    assertEquals(2, original2newElementURIs.size());
    assertEquals(resourceURI.appendFragment(newName), original2newElementURIs.get(uriB));
    assertEquals(uriC, original2newElementURIs.get(uriC));
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IRenameStrategy(org.eclipse.xtext.ui.refactoring.IRenameStrategy) RenamedElementTracker(org.eclipse.xtext.ui.refactoring.impl.RenamedElementTracker) IRenamedElementTracker(org.eclipse.xtext.ui.refactoring.IRenamedElementTracker) Element(org.eclipse.xtext.ui.tests.refactoring.refactoring.Element) XtextResource(org.eclipse.xtext.resource.XtextResource) URI(org.eclipse.emf.common.util.URI) IRenamedElementTracker(org.eclipse.xtext.ui.refactoring.IRenamedElementTracker) RefactoringTestLanguageFragmentProvider(org.eclipse.xtext.ui.tests.refactoring.resource.RefactoringTestLanguageFragmentProvider) Test(org.junit.Test)

Example 3 with Element

use of org.eclipse.xtext.ui.tests.refactoring.refactoring.Element in project xtext-eclipse by eclipse.

the class RefactoringCrossReferenceSerializerTest method testSerializer.

@Test
public void testSerializer() throws Exception {
    RefactoringCrossReferenceSerializer.RefTextEvaluator evaluator = new RefactoringCrossReferenceSerializer.RefTextEvaluator() {

        @Override
        public boolean isValid(IEObjectDescription target) {
            return true;
        }

        @Override
        public boolean isBetterThan(String newText, String currentText) {
            return newText.length() < currentText.length();
        }
    };
    String model = "bar { ref foo } foo";
    String wsRelativePath = "test/test." + getCurrentFileExtension();
    IFile file = createFile(wsRelativePath, model);
    try (InputStream contents = file.getContents()) {
        Main main = (Main) getModel(contents);
        XtextResource resource = (XtextResource) main.eResource();
        resource.setURI(URI.createPlatformResourceURI(wsRelativePath, true));
        Element bar = (Element) main.getElements().get(0);
        Element foo = bar.getReferenced().get(0);
        assertEquals("foo", foo.getName());
        CrossReference crossref = GrammarUtil.containedCrossReferences(grammarAccess.getElementRule()).get(0);
        TextRegion linkTextRegion = new TextRegion(model.lastIndexOf("foo"), 3);
        String linkText = facade.getCrossRefText(bar, crossref, foo, evaluator, linkTextRegion, status);
        assertEquals(linkText, "foo");
        assertTrue(status.getRefactoringStatus().isOK());
        foo.setName("fooBar");
        resource.getCache().clear(resource);
        String linkText1 = facade.getCrossRefText(bar, crossref, foo, evaluator, linkTextRegion, status);
        assertEquals(linkText1, "fooBar");
        assertTrue(status.getRefactoringStatus().isOK());
        assertEquals(foo, ((Main) resource.getContents().get(0)).getElements().get(1));
        foo.setName("bar");
        resource.getCache().clear(resource);
        assertNull(facade.getCrossRefText(bar, crossref, foo, evaluator, linkTextRegion, status));
    }
}
Also used : RefactoringCrossReferenceSerializer(org.eclipse.xtext.ui.refactoring.impl.RefactoringCrossReferenceSerializer) IFile(org.eclipse.core.resources.IFile) TextRegion(org.eclipse.xtext.util.TextRegion) InputStream(java.io.InputStream) Element(org.eclipse.xtext.ui.tests.refactoring.refactoring.Element) CrossReference(org.eclipse.xtext.CrossReference) XtextResource(org.eclipse.xtext.resource.XtextResource) Main(org.eclipse.xtext.ui.tests.refactoring.refactoring.Main) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Test(org.junit.Test)

Example 4 with Element

use of org.eclipse.xtext.ui.tests.refactoring.refactoring.Element in project xtext-eclipse by eclipse.

the class LinkedModelCalculatorIntegrationTest method testCorrectTextEditsDeclarationNotInFile.

@Test
public void testCorrectTextEditsDeclarationNotInFile() throws Exception {
    String initialModel1 = "A { B ref A } C { D ref A }";
    createFile(pathToFile1, initialModel1);
    String initialModel2 = "E { ref A } F { ref E ref A}";
    IFile file2 = createFile(pathToFile2, initialModel2);
    waitForBuild();
    XtextEditor editor = openEditor(file2);
    EObject a = editor.getDocument().readOnly(new IUnitOfWork<EObject, XtextResource>() {

        @Override
        public EObject exec(XtextResource state) throws Exception {
            return ((Element) state.getContents().get(0).eContents().get(0)).getReferenced().get(0);
        }
    });
    URI uri = EcoreUtil.getURI(a);
    selectElementInEditor(a, uri, editor, (XtextResource) a.eResource());
    IRenameElementContext renameElementContext = new IRenameElementContext.Impl(uri, a.eClass(), editor, editor.getSelectionProvider().getSelection(), uriToFile2);
    LinkedPositionGroup linkedPositionGroup = linkedModelCalculator.getLinkedPositionGroup(renameElementContext, new NullProgressMonitor()).get();
    LinkedPosition[] positions = linkedPositionGroup.getPositions();
    assertEquals(2, positions.length);
    int[] offsets = { 8, 26 };
    for (int i = 0; i < positions.length; i++) {
        assertEquals(offsets[i], positions[i].getOffset());
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LinkedPosition(org.eclipse.jface.text.link.LinkedPosition) IFile(org.eclipse.core.resources.IFile) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) Element(org.eclipse.xtext.ui.tests.refactoring.refactoring.Element) XtextResource(org.eclipse.xtext.resource.XtextResource) URI(org.eclipse.emf.common.util.URI) LinkedPositionGroup(org.eclipse.jface.text.link.LinkedPositionGroup) EObject(org.eclipse.emf.ecore.EObject) IRenameElementContext(org.eclipse.xtext.ui.refactoring.ui.IRenameElementContext) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest) Test(org.junit.Test)

Example 5 with Element

use of org.eclipse.xtext.ui.tests.refactoring.refactoring.Element in project xtext-eclipse by eclipse.

the class DefaultRenameElementStrategyTest method testValueConversion.

@Test
public void testValueConversion() throws Exception {
    DefaultRenameStrategy strategy = new DefaultRenameStrategy() {

        @Override
        protected String getNameRuleName(EObject targetElement, EAttribute nameAttribute) {
            return "STRING";
        }
    };
    getInjector().injectMembers(strategy);
    final XtextResource resource = getResourceFromString("foo { }");
    Element targetElement = (Element) resource.getContents().get(0).eContents().get(0);
    assertEquals("foo", targetElement.getName());
    strategy.initialize(targetElement, null);
    assertEquals("\"foo\"", strategy.getOriginalName());
    strategy.createDeclarationUpdates("\"bar\"", resource.getResourceSet(), this);
    assertEquals(1, textEdits.size());
    assertEquals(0, textEdits.get(0).getOffset());
    assertEquals(3, textEdits.get(0).getLength());
    assertEquals("\"bar\"", ((ReplaceEdit) textEdits.get(0)).getText());
    strategy.applyDeclarationChange("\"bar\"", resource.getResourceSet());
    assertEquals("bar", targetElement.getName());
    strategy.revertDeclarationChange(resource.getResourceSet());
    assertEquals("foo", targetElement.getName());
}
Also used : EAttribute(org.eclipse.emf.ecore.EAttribute) DefaultRenameStrategy(org.eclipse.xtext.ui.refactoring.impl.DefaultRenameStrategy) EObject(org.eclipse.emf.ecore.EObject) Element(org.eclipse.xtext.ui.tests.refactoring.refactoring.Element) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Aggregations

Element (org.eclipse.xtext.ui.tests.refactoring.refactoring.Element)6 XtextResource (org.eclipse.xtext.resource.XtextResource)5 Test (org.junit.Test)5 EObject (org.eclipse.emf.ecore.EObject)3 IFile (org.eclipse.core.resources.IFile)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 URI (org.eclipse.emf.common.util.URI)2 IRenameStrategy (org.eclipse.xtext.ui.refactoring.IRenameStrategy)2 Main (org.eclipse.xtext.ui.tests.refactoring.refactoring.Main)2 InputStream (java.io.InputStream)1 EAttribute (org.eclipse.emf.ecore.EAttribute)1 EPackage (org.eclipse.emf.ecore.EPackage)1 LinkedPosition (org.eclipse.jface.text.link.LinkedPosition)1 LinkedPositionGroup (org.eclipse.jface.text.link.LinkedPositionGroup)1 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)1 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)1 Action (org.eclipse.xtext.Action)1 CrossReference (org.eclipse.xtext.CrossReference)1 Parameter (org.eclipse.xtext.Parameter)1 ParserRule (org.eclipse.xtext.ParserRule)1