org.eclipse.xtext.ui.editor.XtextEditor Java Examples

The following examples show how to use org.eclipse.xtext.ui.editor.XtextEditor. 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: AutoEditTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test 
public void testBug434717_02() throws Exception {
	XtextEditor editor = openEditor(
			"genPlainText(Object this){\n" + 
			"		'''\n" + 
			"		|\n" +
			"		'''\n" + 
			"}\n" + 
			"");
	pasteText(editor, "\tfoo\n" +
					  "\t\tbar\n"
					  + "\tfoo");
	assertState(
			"genPlainText(Object this){\n" + 
			"		'''\n" + 
			"			foo\n" +
			"				bar\n" +
			"			foo|\n" +
			"		'''\n" + 
			"}\n" + 
			"", editor);
}
 
Example #2
Source File: EObjectContentProvider.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Retrieve the object's values for the given EAttributes.
 * 
 * @param attributes
 *          the EAttributes
 * @return List<Pair<EAttribute, Object>> the attribute+values - possibly containing null entries
 */
private List<AttributeValuePair> valuesForAttributes(final EList<EAttribute> attributes) {
  final URI elementUri = xtextElementSelectionListener.getSelectedElementUri();
  XtextEditor editor = xtextElementSelectionListener.getEditor();
  if (editor != null && elementUri != null) {
    return editor.getDocument().readOnly(new IUnitOfWork<List<AttributeValuePair>, XtextResource>() {
      @SuppressWarnings("PMD.SignatureDeclareThrowsException")
      public List<AttributeValuePair> exec(final XtextResource state) throws Exception {
        final EObject eObject = state.getEObject(elementUri.fragment());
        List<AttributeValuePair> pairs = Lists.transform(attributes, new Function<EAttribute, AttributeValuePair>() {
          public AttributeValuePair apply(final EAttribute from) {
            return new AttributeValuePair(from, eObject.eGet(from));
          }
        });
        return pairs;
      }
    });
  }
  return newArrayList();
}
 
Example #3
Source File: AbstractCStyleLanguageAutoEditTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testMLComments_02() throws Exception {
	XtextEditor editor = openEditor("   |");
	pressKey(editor, '/');
	pressKey(editor, '*');
	assertState("   /*| */", editor);

	pressKey(editor, '\n');
	assertState("   /*\n    * |\n    */", editor);

	pressKey(editor, '\n');
	assertState("   /*\n    * \n    * |\n    */", editor);

	pressKeys(editor, "foo bar");
	pressKey(editor, '\n');
	assertState("   /*\n    * \n    * foo bar\n    * |\n    */", editor);
}
 
Example #4
Source File: CursorHandlingTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testToLineStartInMlComment_04() throws Exception { 
	XtextEditor editor = openEditor("  /** beg|in\n" + 
			"*/");
	toLineStart(editor);
	assertState("  /** |begin\n" +
			"*/", editor);
	toLineStart(editor);
	assertState("  |/** begin\n" +
			"*/", editor);
	toLineStart(editor);
	assertState("|  /** begin\n" +
			"*/", editor);
	toLineStart(editor);
	assertState("  |/** begin\n" +
			"*/", editor);
}
 
Example #5
Source File: XtendHoverInEditorTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
private void hasHoverOver(final CharSequence it, final String textUnderHover, final Class<? extends BrowserInformationControlInput> expectedPopupType, final String expectedHoverContent) {
  try {
    final IFile fileFoo = this.helper.createFile("Foo.xtend", it.toString());
    this._syncUtil.waitForBuild(null);
    final XtextEditor editor = this.helper.openEditor(fileFoo);
    int _indexOf = it.toString().indexOf(textUnderHover);
    int _length = textUnderHover.length();
    final Region region = new Region(_indexOf, _length);
    final Object info = ((ITextHoverExtension2) this.hoverer).getHoverInfo2(editor.getInternalSourceViewer(), region);
    Assert.assertTrue(expectedPopupType.isInstance(info));
    final String html = ((BrowserInformationControlInput) info).getHtml();
    Assert.assertTrue(html.contains(expectedHoverContent));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #6
Source File: ToSaveOrNotToSaveTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void renameFooToFooBar(final XtextEditor contextEditor) throws Exception {
		contextEditor.getEditorSite().getPage().activate(contextEditor);
		waitForDisplay();
		IXtextDocument document = contextEditor.getDocument();
		final int offset = document.get().indexOf("foo");
		contextEditor.selectAndReveal(offset, 3);
		
		EvaluationContext evaluationContext = new EvaluationContext(null, new Object());
		evaluationContext.addVariable(ISources.ACTIVE_EDITOR_NAME, contextEditor);
		ExecutionEvent executionEvent = new ExecutionEvent(null, newHashMap(), null, evaluationContext);
		renameElementHandler.execute(executionEvent);
//		syncUtil.totalSync(refactoringPreferences.isSaveAllBeforeRefactoring());
//		IRenameElementContext context = document.readOnly(new IUnitOfWork<IRenameElementContext, XtextResource>() {
//			public IRenameElementContext exec(XtextResource state) throws Exception {
//				EObject target = eObjectAtOffsetHelper.resolveElementAt(state, offset);
//				return renameContextFactory.createRenameElementContext(target, contextEditor, new TextSelection(offset,
//						3), state);
//			}
//		});
//		controller.initialize(context);
//		waitForDisplay();
//		controller.startRefactoring(RefactoringType.LINKED_EDITING);
//		waitForDisplay();
		pressKeys(contextEditor, "fooBar\n");
		waitForDisplay();
		waitForReconciler(fooEditor);
		waitForReconciler(barEditor);
		waitForDisplay();
	}
 
Example #7
Source File: AbstractCStyleLanguageAutoEditTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testDoubleQuotedStringLiteral_5() throws Exception {
	XtextEditor editor = openEditor("|foo");
	pressKey(editor, '\"');
	assertState("\"|foo", editor);
	pressKey(editor, '\"');
	assertState("\"\"|foo", editor);
	pressKey(editor, SWT.BS);
	assertState("\"|foo", editor);
	pressKey(editor, SWT.BS);
	assertState("|foo", editor);
}
 
Example #8
Source File: AbstractCursorHandlingTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected XtextEditor openEditor(String content) throws Exception {
	int cursor = content.indexOf('|');
	String fileExtension = getFileExtension();
	IFile file = createFile("foo/myfile" + files.size() + "." + fileExtension, content.replace("|", ""));
	files.add(file);
	XtextEditor editor = openEditor(file);
	editor.getInternalSourceViewer().setSelectedRange(cursor, 0);
	editor.getInternalSourceViewer().getTextWidget().setFocus();
	return editor;
}
 
Example #9
Source File: RenameRefactoringIntegrationTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testDirtyEditor() throws Exception {
	XtextEditor editor = openEditor(testFile0);
	String dirtyModel = "Y B A { ref B }";
	editor.getDocument().set(dirtyModel);
	waitForReconciler(editor);
	assertTrue(editor.isDirty());
	doRename();
	assertTrue(editor.isDirty());
	assertEquals(dirtyModel.replace("B", "C"), editor.getDocument().get());
}
 
Example #10
Source File: AbstractCStyleLanguageAutoEditTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSingleQuotedStringLiteral_7() throws Exception {
	XtextEditor editor = openEditor("'' '| '");
	pressKey(editor, '\'');
	assertState("'' ''| '", editor);
	pressKey(editor, SWT.BS);
	assertState("'' '| '", editor);
}
 
Example #11
Source File: InsertStringHandler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
	XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
	if (editor != null) {
		// Hack, would be nicer with document edits, but this way we don't loose auto edit
		StyledText textWidget = editor.getInternalSourceViewer().getTextWidget();
		Event e = new Event();
		e.character = replaceChar;
		e.type = SWT.KeyDown;
		e.doit = true;
		textWidget.notifyListeners(SWT.KeyDown, e);
	}
	return null;
}
 
Example #12
Source File: SARLNatureAddingEditorCallback.java    From sarl with Apache License 2.0 5 votes vote down vote up
private boolean canBuild(XtextEditor editor) {
	final IResource resource = editor.getResource();
	if (!(resource instanceof IStorage)) {
		return false;
	}
	final IStorage storage = (IStorage) resource;
	final URI uri = this.mapper.getUri(storage);
	return this.uriValidator.canBuild(uri, storage);
}
 
Example #13
Source File: DerivedSourceView.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected String computeDescription(IWorkbenchPartSelection workbenchPartSelection) {
	if (selectedSource == null) {
		return super.computeDescription(workbenchPartSelection);
	}
	XtextEditor xtextEditor = (XtextEditor) workbenchPartSelection.getWorkbenchPart();
	if (xtextEditor.isDirty()) {
		return Messages.DerivedSourceView_EditorDirty;
	} else {
		return selectedSource.getFullPath().toString();
	}
}
 
Example #14
Source File: AutoEditTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testRichStringLiteral_10() throws Exception {
	XtextEditor editor = openEditor("''' �foobar� |'''");
	pressKey(editor, '\'');
	assertState("''' �foobar� '|''", editor);
	pressKey(editor, '\'');
	assertState("''' �foobar� ''|'", editor);
	pressKey(editor, '\'');
	assertState("''' �foobar� '''|", editor);
}
 
Example #15
Source File: AutoEditTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testBug342030_03() throws Exception {
	XtextEditor editor = openEditor(
			"genPlainText(Object this){\n" + 
			"    '''|'''\n" + 
			"}\n" + 
			"");
	pressKeys(editor, "'''");
	assertState("genPlainText(Object this){\n" + 
			"    ''''''|\n" + 
			"}\n" + 
			"", editor);
}
 
Example #16
Source File: AbstractCStyleLanguageAutoEditTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testCurlyBracesWithSelection_4() throws Exception {
	XtextEditor editor = openEditor("{{|foo}}");
	selectText(editor,0,3);
	pressKey(editor, '}');
	assertState("{{}|}", editor);
	pressKey(editor, SWT.BS);
	assertState("{{|}", editor);
}
 
Example #17
Source File: MultipageEditorTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testOpenBlankFile() throws Exception {
	IFile file = createFile("foo/y.testlanguage", "/* multi line */\n" + "stuff foo\n" + "stuff bar\n" + "// end");
	XtextEditor openedEditor = openEditor(file);
	assertNotNull(openedEditor);
	
	ICommandService service = PlatformUI.getWorkbench().getService(ICommandService.class);
	Command command = service.getCommand("org.eclipse.xtext.ui.editor.hyperlinking.OpenDeclaration");
	assertTrue(command.isEnabled());
	
	openedEditor.close(false);
}
 
Example #18
Source File: AutoEditTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testShortcut_1() throws Exception {
	XtextEditor editor = openEditor("fb|");
	pressKey(editor, 'b');
	assertState("foobarbaz|", editor);
	// Don't know how to simulate the press ESC scenario. The following does not work.
	pressKey(editor, SWT.ESC);
	assertState("fbb|", editor);
}
 
Example #19
Source File: JavaRefactoringIntegrationTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testRenameXtendClass() throws Exception {
	String xtendModel = "class XtendClass { }";
	IFile xtendClass = createFile("XtendClass.xtend", xtendModel);
	IFile javaClass = createFile("JavaClass.java", "public class JavaClass extends XtendClass { }");
	final XtextEditor editor = openEditorSafely(xtendClass);
	renameXtendElement(editor, xtendModel.indexOf("XtendClass"), "NewXtendClass");
	fileAsserts.assertFileExists("src/NewXtendClass.xtend");
	fileAsserts.assertFileContains(javaClass, "JavaClass extends NewXtendClass");
}
 
Example #20
Source File: AbstractSarlLaunchShortcut.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public IResource getLaunchableResource(IEditorPart editorpart) {
	final XtextEditor xtextEditor = EditorUtils.getXtextEditor(editorpart);
	if (xtextEditor != null) {
		return xtextEditor.getResource();
	}
	return null;
}
 
Example #21
Source File: SyncUtil.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void reconcileAllEditors(IWorkbench workbench, final boolean saveAll, final IProgressMonitor monitor) {
	for (IWorkbenchWindow window : workbench.getWorkbenchWindows()) {
		for (IWorkbenchPage page : window.getPages()) {
			for (IEditorReference editorReference : page.getEditorReferences()) {
				if (monitor.isCanceled())
					return;
				final IEditorPart editor = editorReference.getEditor(false);
				if (editor != null) {
					if (editor instanceof XtextEditor) {
						waitForReconciler((XtextEditor) editor);
					}
					if (saveAll) {
						Display display = workbench.getDisplay();
						display.syncExec(new Runnable() {
							@Override
							public void run() {
								if (editor.isDirty()) {
									editor.doSave(monitor);
								}
							}
						});
					}
				}
			}
		}
	}
}
 
Example #22
Source File: JavaRefactoringIntegrationTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testRenameRefToXtendInterface() throws Exception {
	createFile("XtendInterface.xtend", "interface XtendInterface {}");
	String xtendModel = "class XtendRef { XtendInterface foo }";
	IFile xtendRef = createFile("XtendRef.xtend", xtendModel);
	final XtextEditor editor = openEditorSafely(xtendRef);
	renameXtendElement(editor, xtendModel.indexOf("XtendInterface"), "NewXtendInterface");
	assertDocumentContains(editor, xtendModel.replace("XtendInterface", "NewXtendInterface"));
	IFile newXtendInterface = fileAsserts.assertFileExists("src/NewXtendInterface.xtend");
	fileAsserts.assertFileContains(newXtendInterface, "interface NewXtendInterface {}");
}
 
Example #23
Source File: AbstractCStyleLanguageAutoEditTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testSingleQuotedStringLiteral_14() throws Exception {
	XtextEditor editor = openEditor("|'test'");
	pressKey(editor, '\'');
	assertState("'|''test'", editor);
	pressKey(editor, '\'');
	assertState("''|'test'", editor);
	pressKey(editor, '\'');
	assertState("'''|''test'", editor);
}
 
Example #24
Source File: AbstractHyperlinkingTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected XtextEditor openInEditor(IFile dslFile) {
	// Wait for the cross-reference resolution
	IResourcesSetupUtil.waitForBuild();
	try {
		return openEditor(dslFile);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example #25
Source File: AbstractCStyleLanguageAutoEditTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testParenthesis_6() throws Exception {
	XtextEditor editor = openEditor("(|\n)");
	pressKey(editor, ')');
	assertState("()|\n)", editor);
	pressKey(editor, SWT.BS);
	assertState("(|\n)", editor);
}
 
Example #26
Source File: AbstractCStyleLanguageAutoEditTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testDoubleQuotedStringLiteral_8() throws Exception {
	XtextEditor editor = openEditor("\"| \" \" \"");
	pressKey(editor, '\"');
	assertState("\"\"| \" \" \"", editor);
	pressKey(editor, SWT.BS);
	assertState("\"| \" \" \"", editor);
}
 
Example #27
Source File: AutoEditInCodeBlockTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
@Test public void testIndentationEdit_2() throws Exception {
	// indentation from prefix
	XtextEditor editor = openEditor("  |");
	pressKey(editor, '\n');
	assertState("  \n\t|", editor);
}
 
Example #28
Source File: PasteJavaCodeHandler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
	final XtextEditor activeXtextEditor = EditorUtils.getActiveXtextEditor(event);
	if (activeXtextEditor == null || !activeXtextEditor.isEditable()) {
		return null;
	}

	String clipboardText = ClipboardUtil.getTextFromClipboard();
	if (!Strings.isEmpty(clipboardText)) {
		JavaImportData javaImports = ClipboardUtil.getJavaImportsContent();
		doPasteJavaCode(activeXtextEditor, clipboardText, javaImports);
	}
	return null;
}
 
Example #29
Source File: JavaRefactoringIntegrationTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testRenameXtendField() throws Exception {
	String xtendModel = "class XtendClass { protected int foo }";
	IFile xtendClass = createFile("XtendClass.xtend", xtendModel);
	IFile javaClass = createFile("JavaClass.java",
			"public class JavaClass extends XtendClass { int bar = foo; }");
	final XtextEditor editor = openEditorSafely(xtendClass);
	renameXtendElement(editor, xtendModel.indexOf("foo"), "baz");
	assertDocumentContains(editor, xtendModel.replace("foo", "baz"));
	fileAsserts.assertFileContains(javaClass, "int bar = baz");
}
 
Example #30
Source File: AbstractCStyleLanguageAutoEditTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testDoubleQuotedStringLiteral_3() throws Exception {
	XtextEditor editor = openEditor("|\"");
	pressKey(editor, '\"');
	assertState("\"|\"", editor);
	pressKey(editor, '\"');
	assertState("\"\"|", editor);
	pressKey(editor, '\"');
	assertState("\"\"\"|\"", editor);
}