Java Code Examples for org.eclipse.xtext.ui.refactoring.ui.IRenameSupport#startDirectRefactoring()

The following examples show how to use org.eclipse.xtext.ui.refactoring.ui.IRenameSupport#startDirectRefactoring() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RefactoringTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRenameElement_1() throws Exception {
	setLanguageConfig(true, false);
	IRenameSupport renameSupport = createRenameSupportForElement();
	assertNotNull(renameSupport);
	renameSupport.startDirectRefactoring();
	waitForBuild();
	assertXtextElementRefactored();
	assertJavaUnchanged();
}
 
Example 2
Source File: RefactoringTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRenameElement_2() throws Exception {
	setLanguageConfig(true, true);
	IRenameSupport renameSupport = createRenameSupportForElement();
	assertNotNull(renameSupport);
	renameSupport.startDirectRefactoring();
	waitForBuild();
	assertXtextElementRefactored();
	assertJavaUnchanged();
}
 
Example 3
Source File: RefactoringTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRenameJvmReference_2() throws Exception {
	setLanguageConfig(true, true);
	IRenameSupport renameSupport = createRenameSupportForJvmReference();
	assertNotNull(renameSupport);
	renameSupport.startDirectRefactoring();
	waitForBuild();
	assertXtextJvmRefRefactored();
	assertJavaRefactored();
}
 
Example 4
Source File: RenameRefactoringXpectMethod.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Rename refactoring Xpect method
 */
// Note: arg1=OFFSET makes the 'offset' parameter contain the right offset value
@ParameterParser(syntax = "('at' arg2=OFFSET 'to' arg3=STRING) ('resource' arg4=STRING)?")
@Xpect
@ConsumedIssues({ Severity.INFO, Severity.ERROR, Severity.WARNING })
public void renameRefactoring(
		@StringDiffExpectation(whitespaceSensitive = false) IStringDiffExpectation expectation, // arg0
		@ThisResource XtextResource resource, // arg1
		IEObjectCoveringRegion offset, // arg2
		String newName, // arg3
		String specifiedResourcePath, // arg4
		@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectedResult)
		throws Exception {
	try {
		EObject context = offset.getEObject();
		EObject selectedElement = offsetHelper.resolveElementAt((XtextResource) context.eResource(),
				offset.getOffset());

		// LiteralOrComputedPropertyName does not have a type model but its container does
		if (selectedElement instanceof LiteralOrComputedPropertyName) {
			selectedElement = selectedElement.eContainer();
		}

		// An IdentifierRef refers to an AST FormalParameter and not TFormalParameter
		if (!(selectedElement instanceof FormalParameter)
				&& (N4JSLanguageUtils.getDefinedTypeModelElement(selectedElement) != null)) {
			selectedElement = N4JSLanguageUtils.getDefinedTypeModelElement(selectedElement);
		}

		// while (selectedElement != null) {
		// while (Display.getCurrent().readAndDispatch())
		// ;
		// Display.getCurrent().sleep();
		// }

		URI targetResourceUri = context.eResource().getURI();
		Optional<XtextEditor> editorOp = EditorsUtil.openXtextEditor(targetResourceUri,
				N4JSActivator.ORG_ECLIPSE_N4JS_N4JS);
		XtextEditor editor = editorOp.get();
		final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();

		IRenameElementContext renameElementContext = renameContextFactory
				.createRenameElementContext(
						selectedElement, editor, selection, resource);

		IRenameSupport renameSupport = renameSupportFactory.create(renameElementContext, newName);

		// HACK, use reflection to obtain the private field 'renameRefactoring' since we need it to verify the
		// conditions
		// Field field = renameSupport.getClass().getDeclaredField("renameRefactoring");
		// field.setAccessible(true);
		ProcessorBasedRefactoring refactoring = (ProcessorBasedRefactoring) ReflectionUtil.getFieldValue(
				renameSupport,
				"renameRefactoring");

		RefactoringStatus status = refactoring.checkAllConditions(new NullProgressMonitor());
		// If rename refactoring's conditions are not satisfied, validate the error message
		if (status.hasError()) {
			RefactoringStatusEntry[] entries = status.getEntries();
			List<String> errorMessages = Arrays.stream(entries).map(statusEntry -> statusEntry.getMessage())
					.collect(Collectors.toList());

			expectedResult.assertEquals(errorMessages);
		} else {
			String beforeRenameContent = getResourceContentWithoutXpectComment(specifiedResourcePath, resource);
			renameSupport.startDirectRefactoring();
			String afterRenameContent = getResourceContentWithoutXpectComment(specifiedResourcePath, resource);

			expectation.assertDiffEquals(beforeRenameContent, afterRenameContent);
		}
	} finally {
		EditorsUtil.forceCloseAllEditors();
	}
}