Java Code Examples for org.eclipse.xtext.ui.editor.XtextEditor#close()

The following examples show how to use org.eclipse.xtext.ui.editor.XtextEditor#close() . 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: EditorResourceAccessTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testAccess() throws Exception {
	EditorResourceAccess access = TestsActivator.getInstance().getInjector(getEditorId())
			.getInstance(EditorResourceAccess.class);
	IUnitOfWork<Boolean, ResourceSet> isEmpty = new IUnitOfWork<Boolean, ResourceSet>(){
		@Override
		public Boolean exec(ResourceSet state) throws Exception {
			return state.getResources().get(0).getContents().isEmpty();
		}
	};
	assertTrue(access.readOnly(uri, isEmpty));
	
	XtextEditor editor = openEditor(file);
	runAsyncJobs();
	assertTrue(access.readOnly(uri, isEmpty));

	editor.getDocument().set("A");
	runAsyncJobs();
	waitForReconciler(editor);
	runAsyncJobs();
	assertFalse(access.readOnly(uri, isEmpty));
	
	editor.close(false);
	runAsyncJobs();
	assertTrue(access.readOnly(uri, isEmpty));
}
 
Example 2
Source File: OpenDocumentTrackerTests.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testTracker() throws Exception {
	assertNull(tracker.getOpenDocument(uri));
	XtextEditor editor = openEditor(file);
	runAsyncJobs();
	assertNotNull(tracker.getOpenDocument(uri));
	editor.close(false);
	runAsyncJobs();
	assertNull(tracker.getOpenDocument(uri));
	editor = openEditor(file);
	runAsyncJobs();
	assertNotNull(tracker.getOpenDocument(uri));
}
 
Example 3
Source File: SimpleEditorTest.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/x.testlanguage", "");
	XtextEditor openedEditor = openEditor(file);
	assertNotNull(openedEditor);
	IXtextDocument document = openedEditor.getDocument();
	document.readOnly(new IUnitOfWork.Void<XtextResource>() {

		@Override
		public void process(XtextResource resource) throws Exception {
			assertNotNull(resource);
			assertTrue(resource.getContents().isEmpty());
		}
	});
	openedEditor.close(false);
}
 
Example 4
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 5
Source File: EncodingTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void openEditorAndCheckEncoding(IFile file, final String charset) throws CoreException, Exception {
	XtextEditor openedEditor = openEditor(file);
	assertNotNull(openedEditor);
	IXtextDocument document = openedEditor.getDocument();
	document.readOnly(new IUnitOfWork.Void<XtextResource>() {
		@Override
		public void process(XtextResource resource) throws Exception {
			assertEquals(charset, resource.getEncoding());
		}
	});
	openedEditor.close(false);
	openedEditor.dispose();
}
 
Example 6
Source File: ExtractVariableIntegrationTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertAfterExtract(final CharSequence input, final CharSequence expected, final boolean isFinal) {
  try {
    final String inputString = input.toString();
    final String model = inputString.replace("$", "");
    final IFile file = this.workbenchTestHelper.createFile("Foo", model);
    final XtextEditor editor = this.workbenchTestHelper.openEditor(file);
    try {
      final IUnitOfWork<Change, XtextResource> _function = (XtextResource it) -> {
        Change _xblockexpression = null;
        {
          final int offset = inputString.indexOf("$");
          int _lastIndexOf = inputString.lastIndexOf("$");
          int _minus = (_lastIndexOf - 1);
          final int length = (_minus - offset);
          final TextSelection textSelection = new TextSelection(offset, length);
          final XExpression selection = this.util.findSelectedExpression(it, textSelection);
          final ExtractVariableRefactoring refactoring = this.refactoringProvider.get();
          refactoring.setFinal(isFinal);
          refactoring.initialize(editor, selection);
          NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
          final RefactoringStatus status = refactoring.checkAllConditions(_nullProgressMonitor);
          Assert.assertTrue(status.toString(), status.isOK());
          NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
          Change _createChange = refactoring.createChange(_nullProgressMonitor_1);
          NullProgressMonitor _nullProgressMonitor_2 = new NullProgressMonitor();
          _xblockexpression = _createChange.perform(_nullProgressMonitor_2);
        }
        return _xblockexpression;
      };
      editor.getDocument().<Change>readOnly(_function);
      Assert.assertEquals(expected.toString(), editor.getDocument().get());
    } finally {
      editor.close(false);
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 7
Source File: ExtractMethodIntegrationTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertExtractForbidden(final CharSequence input, final Procedure1<? super ExtractMethodRefactoring> initializer, final String messageFragment) {
  try {
    final String inputString = input.toString();
    final IFile file = this.workbenchTestHelper.createFile("Foo", inputString.replace("$", ""));
    final XtextEditor editor = this.workbenchTestHelper.openEditor(file);
    try {
      final IUnitOfWork<String, XtextResource> _function = (XtextResource it) -> {
        String _xblockexpression = null;
        {
          int _indexOf = inputString.indexOf("$");
          int _lastIndexOf = inputString.lastIndexOf("$");
          int _indexOf_1 = inputString.indexOf("$");
          int _minus = (_lastIndexOf - _indexOf_1);
          int _minus_1 = (_minus - 1);
          TextSelection _textSelection = new TextSelection(_indexOf, _minus_1);
          final List<XExpression> selection = this.util.findSelectedSiblingExpressions(it, _textSelection);
          final ExtractMethodRefactoring refactoring = this.refactoringProvider.get();
          refactoring.initialize(editor, selection, true);
          refactoring.setMethodName("bar");
          NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
          final RefactoringStatus status = refactoring.checkInitialConditions(_nullProgressMonitor);
          initializer.apply(refactoring);
          NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
          status.merge(refactoring.checkFinalConditions(_nullProgressMonitor_1));
          Assert.assertTrue(status.toString(), status.hasError());
          final String message = status.getMessageMatchingSeverity(RefactoringStatus.ERROR);
          Assert.assertTrue(message, message.toLowerCase().contains(messageFragment.toLowerCase()));
          _xblockexpression = "";
        }
        return _xblockexpression;
      };
      editor.getDocument().<String>readOnly(_function);
    } finally {
      editor.close(false);
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example 8
Source File: WorkbenchMarkerResolutionGenerator.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * Note : this method is largely copied from the superclass, all we need to override is the call to
 * getAdaptedResolutions to provider the marker.
 */
@Override
public IMarkerResolution[] getResolutions(final IMarker marker) {
  final IMarkerResolution[] emptyResult = new IMarkerResolution[0];
  try {
    if (!marker.isSubtypeOf(MarkerTypes.ANY_VALIDATION)) {
      return emptyResult;
    }
  } catch (CoreException e) {
    return emptyResult;
  }
  if (!languageResourceHelper.isLanguageResource(marker.getResource())) {
    return emptyResult;
  }

  final XtextEditor editor = getEditor(marker.getResource());
  if (editor != null) {
    final IAnnotationModel annotationModel = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
    if (annotationModel != null && !isMarkerStillValid(marker, annotationModel)) {
      return emptyResult;
    }
  }

  final Iterable<IssueResolution> resolutions = getResolutionProvider().getResolutions(getIssueUtil().createIssue(marker));
  if (editor != null && editor.isEditorInputReadOnly()) {
    editor.close(false);
  }

  return getAdaptedResolutions(resolutions, marker);
}
 
Example 9
Source File: BuilderParticipantPluginUITest.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 *
 * 01. Class0 uses Class1 in require statement and in isa function
 * 02. Class1 file is renamed
 * 05. Class0 should get error marker at require statement and at isa function
 * 06. Class1 is renamed back
 * 07. Class0 should have no error markers
 */
//@formatter:on
@Test
public void testSuperClassRenamed() throws Exception {
	logger.info("BuilderParticipantPluginUITest.testSuperClassRenamed");
	// create project and test files
	final IProject project = createJSProject("testSuperClassRenamed");
	IFolder folder = configureProjectWithXtext(project);

	IFolder moduleFolder = createFolder(folder, InheritanceTestFiles.inheritanceModule());

	IFile parentFile = createTestFile(moduleFolder, "Parent", InheritanceTestFiles.Parent());
	assertMarkers("Parent file should have no errors", parentFile, 0);
	IFile childFile = createTestFile(moduleFolder, "Child", InheritanceTestFiles.Child());
	assertMarkers("Child file should have no errors", childFile, 0);

	// open editors of test files
	IWorkbenchPage page = EclipseUIUtils.getActivePage();
	XtextEditor parentFileXtextEditor = openAndGetXtextEditor(parentFile, page);
	List<Resource.Diagnostic> errors = getEditorErrors(parentFileXtextEditor);
	assertEquals("Editor of parent should have no errors", 0, errors.size());
	XtextEditor childFileXtextEditor = openAndGetXtextEditor(childFile, page);
	errors = getEditorErrors(childFileXtextEditor);
	assertEquals("Editor of child should have no errors", 0, errors.size());

	parentFileXtextEditor.close(true);

	parentFile.move(new Path("Parent2" + "." + N4JSGlobals.N4JS_FILE_EXTENSION), true, true, monitor());
	moduleFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor());
	waitForAutoBuild();
	waitForUpdateEditorJob();

	assertFalse("Parent.n4js doesn't exist anymore",
			moduleFolder.getFile(new Path("Parent" + "." + N4JSGlobals.N4JS_FILE_EXTENSION)).exists());
	IFile movedParentFile = moduleFolder.getFile("Parent2" + "." + N4JSGlobals.N4JS_FILE_EXTENSION);
	assertTrue("Parent2.n4js does exist", movedParentFile.exists());

	errors = getEditorErrors(childFileXtextEditor);
	assertEquals("Editor of child should have got error markers",
			Sets.newHashSet(
					"line 1: Cannot resolve plain module specifier (without project name as first segment): no matching module found.",
					"line 2: Couldn't resolve reference to Type 'ParentObjectLiteral'."),
			toSetOfStrings(errors));

	movedParentFile.move(new Path("Parent" + "." + N4JSGlobals.N4JS_FILE_EXTENSION), true, true, monitor());
	moduleFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor());
	waitForAutoBuild();
	waitForUpdateEditorJob();
	assertTrue("Parent.n4js does exist",
			moduleFolder.getFile(new Path("Parent" + "." + N4JSGlobals.N4JS_FILE_EXTENSION)).exists());

	errors = getEditorErrors(childFileXtextEditor);
	assertEquals("Editor of child should have no errors", 0, errors.size());
}
 
Example 10
Source File: BuilderParticipantPluginUITest.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 *
 * 01. Class0 uses Class1 in require statement and in isa function
 * 02. Class1 is deleted
 * 05. Class0 should get error marker at require statement and at isa function
 * 06. recreate Class1 file
 * 07. Class0 should have no error markers
 *
 */
//@formatter:on
@Test
public void testSuperClassDeleted() throws Exception {
	logger.info("BuilderParticipantPluginUITest.testSuperClassDeleted");
	// create project and test files
	final IProject project = createJSProject("testSuperClassDeleted");
	IFolder folder = configureProjectWithXtext(project);

	IFolder moduleFolder = createFolder(folder, InheritanceTestFiles.inheritanceModule());

	IFile parentFile = createTestFile(moduleFolder, "Parent", InheritanceTestFiles.Parent());
	assertMarkers("Parent file should have no errors", parentFile, 0);
	IFile childFile = createTestFile(moduleFolder, "Child", InheritanceTestFiles.Child());
	assertMarkers("Child file should have no errors", childFile, 0);

	// open editors of test files
	IWorkbenchPage page = EclipseUIUtils.getActivePage();
	XtextEditor parentFileXtextEditor = openAndGetXtextEditor(parentFile, page);
	List<Resource.Diagnostic> errors = getEditorErrors(parentFileXtextEditor);
	assertEquals("Editor of parent should have no errors", 0, errors.size());
	XtextEditor childFileXtextEditor = openAndGetXtextEditor(childFile, page);
	errors = getEditorErrors(childFileXtextEditor);
	assertEquals("Editor of child should have no errors", 0, errors.size());

	parentFileXtextEditor.close(true);

	parentFile.delete(true, true, monitor());
	moduleFolder.refreshLocal(IResource.DEPTH_INFINITE, monitor());
	waitForAutoBuild();
	waitForUpdateEditorJob();
	assertFalse("Parent.n4js doesn't exist anymore",
			moduleFolder.getFile(new Path("Parent" + "." + N4JSGlobals.N4JS_FILE_EXTENSION)).exists());
	errors = getEditorErrors(childFileXtextEditor);
	assertEquals("Editor of child should have error markers",
			Sets.newHashSet(
					"line 1: Cannot resolve plain module specifier (without project name as first segment): no matching module found.",
					"line 2: Couldn't resolve reference to Type 'ParentObjectLiteral'."),
			toSetOfStrings(errors));

	IFile recreatedParentFile = createTestFile(moduleFolder, "Parent", InheritanceTestFiles.Parent());
	assertMarkers("File1 should have no errors", recreatedParentFile, 0);
	waitForUpdateEditorJob();

	errors = getEditorErrors(childFileXtextEditor);
	assertEquals("Editor of child should have no errors", 0, errors.size());
}
 
Example 11
Source File: ExtractMethodIntegrationTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void assertAfterExtract(final CharSequence input, final Procedure1<? super ExtractMethodRefactoring> initializer, final CharSequence expected) {
  try {
    final String inputString = input.toString();
    final IFile file = this.workbenchTestHelper.createFile("Foo", inputString.replace("$", ""));
    final XtextEditor editor = this.workbenchTestHelper.openEditor(file);
    try {
      final IUnitOfWork<Change, XtextResource> _function = (XtextResource it) -> {
        Change _xblockexpression = null;
        {
          int _indexOf = inputString.indexOf("$");
          int _lastIndexOf = inputString.lastIndexOf("$");
          int _indexOf_1 = inputString.indexOf("$");
          int _minus = (_lastIndexOf - _indexOf_1);
          int _minus_1 = (_minus - 1);
          TextSelection _textSelection = new TextSelection(_indexOf, _minus_1);
          final List<XExpression> selection = this.util.findSelectedSiblingExpressions(it, _textSelection);
          final ExtractMethodRefactoring refactoring = this.refactoringProvider.get();
          refactoring.initialize(editor, selection, true);
          refactoring.setExplicitlyDeclareReturnType(false);
          refactoring.setVisibility(JvmVisibility.PUBLIC);
          refactoring.setMethodName("bar");
          NullProgressMonitor _nullProgressMonitor = new NullProgressMonitor();
          RefactoringStatus status = refactoring.checkInitialConditions(_nullProgressMonitor);
          Assert.assertTrue(status.toString(), status.isOK());
          initializer.apply(refactoring);
          NullProgressMonitor _nullProgressMonitor_1 = new NullProgressMonitor();
          status = refactoring.checkFinalConditions(_nullProgressMonitor_1);
          Assert.assertTrue(status.toString(), status.isOK());
          NullProgressMonitor _nullProgressMonitor_2 = new NullProgressMonitor();
          Change _createChange = refactoring.createChange(_nullProgressMonitor_2);
          NullProgressMonitor _nullProgressMonitor_3 = new NullProgressMonitor();
          _xblockexpression = _createChange.perform(_nullProgressMonitor_3);
        }
        return _xblockexpression;
      };
      editor.getDocument().<Change>readOnly(_function);
      Assert.assertEquals(expected.toString(), editor.getDocument().get());
    } finally {
      editor.close(false);
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}