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());
}
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));
}
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));
}
}
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());
}
}
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());
}
Aggregations