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

The following examples show how to use org.eclipse.xtext.ui.editor.XtextEditor#getResource() . 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: OwnResourceValidatorAwareValidatingEditorCallback.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private ValidationJob newValidationJob(final XtextEditor editor) {

		final IXtextDocument document = editor.getDocument();
		final IAnnotationModel annotationModel = editor.getInternalSourceViewer().getAnnotationModel();

		final IssueResolutionProvider issueResolutionProvider = getService(editor, IssueResolutionProvider.class);
		final MarkerTypeProvider markerTypeProvider = getService(editor, MarkerTypeProvider.class);
		final MarkerCreator markerCreator = getService(editor, MarkerCreator.class);

		final IValidationIssueProcessor issueProcessor = new CompositeValidationIssueProcessor(
				new AnnotationIssueProcessor(document, annotationModel, issueResolutionProvider),
				new MarkerIssueProcessor(editor.getResource(), annotationModel, markerCreator, markerTypeProvider));

		return editor.getDocument().modify(resource -> {
			final IResourceServiceProvider serviceProvider = resource.getResourceServiceProvider();
			final IResourceValidator resourceValidator = serviceProvider.getResourceValidator();
			return new ValidationJob(resourceValidator, editor.getDocument(), issueProcessor, ALL);
		});
	}
 
Example 2
Source File: NatureAddingEditorCallback.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void afterCreatePartControl(XtextEditor editor) {
	IResource resource = editor.getResource();
	if (resource != null && !toggleNature.hasNature(resource.getProject()) && resource.getProject().isAccessible()
			&& !resource.getProject().isHidden()) {
		String title = Messages.NatureAddingEditorCallback_MessageDialog_Title;
		String message = Messages.NatureAddingEditorCallback_MessageDialog_Msg0 + resource.getProject().getName()
				+ Messages.NatureAddingEditorCallback_MessageDialog_Msg1;
		boolean addNature = false;
		if (MessageDialogWithToggle.PROMPT.equals(dialogs.getUserDecision(ADD_XTEXT_NATURE))) {
			int userAnswer = dialogs.askUser(message, title, ADD_XTEXT_NATURE, editor.getEditorSite().getShell());
			if (userAnswer == IDialogConstants.YES_ID) {
				addNature = true;
			} else if (userAnswer == IDialogConstants.CANCEL_ID) {
				return;
			}
		} else if (MessageDialogWithToggle.ALWAYS.equals(dialogs.getUserDecision(ADD_XTEXT_NATURE))) {
			addNature = true;
		}
		if (addNature) {
			toggleNature.toggleNature(resource.getProject());
		}
	}
}
 
Example 3
Source File: OrganizeImportsService.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/** Organize provided editor. */
public static void organizeImportsInEditor(XtextEditor editor, final Interaction interaction) {
	try {
		IResource resource = editor.getResource();
		DocumentImportsOrganizer imortsOrganizer = getOrganizeImports(resource);
		imortsOrganizer.organizeDocument(editor.getDocument(), interaction);
	} catch (RuntimeException re) {
		if (re.getCause() instanceof BreakException) {
			LOGGER.debug("user canceled");
		} else {
			LOGGER.warn("Unrecognized RT-exception", re);
		}

	}
}
 
Example 4
Source File: AlwaysAddNatureCallback.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void afterCreatePartControl(XtextEditor editor) {
	IResource resource = editor.getResource();
	if (resource != null && !toggleNature.hasNature(resource.getProject()) && resource.getProject().isAccessible()
			&& !resource.getProject().isHidden()) {
		toggleNature.toggleNature(resource.getProject());
	}
}
 
Example 5
Source File: ValidateActionHandlerTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testExpensiveMarkerCreation() throws Exception {
	IFile iFile = createFile("foo/bar.testlanguage", "stuff foo");
	XtextEditor xtextEditor = openEditor(iFile);
	IHandlerService handlerService = xtextEditor.getSite().getService(IHandlerService.class);
	handlerService.executeCommand("org.eclipse.xtext.ui.tests.TestLanguage.validate", null);
	closeEditors();
	Job[] find = Job.getJobManager().find(ValidationJob.XTEXT_VALIDATION_FAMILY);
	for (Job job : find) {
		job.join();
	}
	IResource file = xtextEditor.getResource();
	IMarker[] markers = file.findMarkers(MarkerTypes.EXPENSIVE_VALIDATION, true, IResource.DEPTH_ZERO);
	assertEquals(1, markers.length);

}
 
Example 6
Source File: XtextGrammarQuickfixProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private void assertAndApplyAllResolutions(XtextEditor xtextEditor, String issueCode, int issueDataCount, int issueCount,
		String resolutionLabel) throws CoreException {
	InternalBuilderTest.setAutoBuild(true);
	if (xtextEditor.isDirty()) {
		xtextEditor.doSave(new NullProgressMonitor());
	}
	InternalBuilderTest.fullBuild();
	IXtextDocument document = xtextEditor.getDocument();
	validateInEditor(document);
	List<Issue> issues = getIssues(document);
	assertFalse("Document has no issues, but should.", issues.isEmpty());

	issues.iterator().forEachRemaining((issue) -> {
		assertEquals(issueCode, issue.getCode());
		assertNotNull(issue.getData());
		assertEquals(issueDataCount, issue.getData().length);
	});
	IResource resource = xtextEditor.getResource();
	IMarker[] problems = resource.findMarkers(MarkerTypes.FAST_VALIDATION, true, IResource.DEPTH_INFINITE);
	assertEquals("Resource should have " + issueCount + " error marker.", issueCount, problems.length);
	validateInEditor(document);
	MarkerResolutionGenerator instance = injector.getInstance(MarkerResolutionGenerator.class);
	List<IMarkerResolution> resolutions = Lists.newArrayList(instance.getResolutions(problems[0]));
	assertEquals(1, resolutions.size());
	IMarkerResolution resolution = resolutions.iterator().next();
	assertTrue(resolution instanceof WorkbenchMarkerResolution);
	WorkbenchMarkerResolution workbenchResolution = (WorkbenchMarkerResolution) resolution;
	IMarker primaryMarker = problems[0];
	List<IMarker> others = Lists.newArrayList(workbenchResolution.findOtherMarkers(problems));
	assertFalse(others.contains(primaryMarker));
	assertEquals(problems.length - 1, others.size());
	others.add(primaryMarker);
	workbenchResolution.run(others.toArray(new IMarker[others.size()]), new NullProgressMonitor());
	if (xtextEditor.isDirty()) {
		xtextEditor.doSave(null);
	}
	InternalBuilderTest.cleanBuild();
	problems = resource.findMarkers(MarkerTypes.FAST_VALIDATION, true, IResource.DEPTH_INFINITE);
	assertEquals("Resource should have no error marker.", 0, problems.length);
}
 
Example 7
Source File: ValidatingEditorCallback.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private ValidationJob newValidationJob(XtextEditor editor) {
	IValidationIssueProcessor issueProcessor;
	if (editor.getResource() == null) {
		issueProcessor = new AnnotationIssueProcessor(editor.getDocument(), editor.getInternalSourceViewer().getAnnotationModel(),
				issueResolutionProvider);
	} else {
		issueProcessor = new MarkerIssueProcessor(editor.getResource(), editor.getInternalSourceViewer().getAnnotationModel(),
				markerCreator, markerTypeProvider);
	}
	ValidationJob validationJob = new ValidationJob(resourceValidator, editor.getDocument(), issueProcessor, CheckMode.NORMAL_AND_FAST);
	return validationJob;
}
 
Example 8
Source File: XtendNatureAddingEditorCallback.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void afterCreatePartControl(XtextEditor editor) {
	IResource resource = editor.getResource();
	if (resource!=null && !toggleNature.hasNature(resource.getProject()) 
			&& resource.getProject().isAccessible() && !resource.getProject().isHidden() && canBuild(editor)) {
		toggleNature.toggleNature(resource.getProject());
	}
}
 
Example 9
Source File: XtendNatureAddingEditorCallback.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
private boolean canBuild(XtextEditor editor) {
	IResource resource = editor.getResource();
	if (!(resource instanceof IStorage)) {
		return false;
	}
	IStorage storage = (IStorage) resource;
	URI uri = mapper.getUri(storage);
	return uriValidator.canBuild(uri, storage);
}
 
Example 10
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 11
Source File: SARLNatureAddingEditorCallback.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void afterCreatePartControl(XtextEditor editor) {
	final IResource resource = editor.getResource();
	if (resource != null && !this.toggleNature.hasNature(resource.getProject())
			&& resource.getProject().isAccessible() && !resource.getProject().isHidden() && canBuild(editor)) {
		this.toggleNature.toggleNature(resource.getProject());
	}
}
 
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: IssueDataTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testIssueData() throws Exception {
	IFile dslFile = dslFile(getProjectName(), getFileName(), getFileExtension(), MODEL_WITH_LINKING_ERROR);
	XtextEditor xtextEditor = openEditor(dslFile);
	IXtextDocument document = xtextEditor.getDocument();
	IResource file = xtextEditor.getResource();
	List<Issue> issues = getAllValidationIssues(document);
	assertEquals(1, issues.size());
	Issue issue = issues.get(0);
	assertEquals(2, issue.getLineNumber().intValue());
	assertEquals(3, issue.getColumn().intValue());
	assertEquals(PREFIX.length(), issue.getOffset().intValue());
	assertEquals(QuickfixCrossrefTestLanguageValidator.TRIGGER_VALIDATION_ISSUE.length(), issue.getLength().intValue());
	String[] expectedIssueData = new String[]{ QuickfixCrossrefTestLanguageValidator.ISSUE_DATA_0,
		QuickfixCrossrefTestLanguageValidator.ISSUE_DATA_1};
	assertTrue(Arrays.equals(expectedIssueData, issue.getData()));
	Thread.sleep(1000);

	IAnnotationModel annotationModel = xtextEditor.getDocumentProvider().getAnnotationModel(
			xtextEditor.getEditorInput());
	AnnotationIssueProcessor annotationIssueProcessor = 
			new AnnotationIssueProcessor(document, annotationModel, new IssueResolutionProvider.NullImpl());
	annotationIssueProcessor.processIssues(issues, new NullProgressMonitor());
	Iterator<?> annotationIterator = annotationModel.getAnnotationIterator();
	// filter QuickDiffAnnotations
	List<Object> allAnnotations = Lists.newArrayList(annotationIterator);
	List<XtextAnnotation> annotations = newArrayList(filter(allAnnotations, XtextAnnotation.class));
	assertEquals(annotations.toString(), 1, annotations.size());
	XtextAnnotation annotation = annotations.get(0);
	assertTrue(Arrays.equals(expectedIssueData, annotation.getIssueData()));
	IssueUtil issueUtil = new IssueUtil();
	Issue issueFromAnnotation = issueUtil.getIssueFromAnnotation(annotation);
	assertTrue(Arrays.equals(expectedIssueData, issueFromAnnotation.getData()));

	new MarkerCreator().createMarker(issue, file, MarkerTypes.FAST_VALIDATION);
	IMarker[] markers = file.findMarkers(MarkerTypes.FAST_VALIDATION, true, IResource.DEPTH_ZERO);
	String errorMessage = new AnnotatedTextToString().withFile(dslFile).withMarkers(markers).toString().trim();
	assertEquals(errorMessage, 1, markers.length);
	String attribute = (String) markers[0].getAttribute(Issue.DATA_KEY);
	assertNotNull(attribute);
	assertTrue(Arrays.equals(expectedIssueData, Strings.unpack(attribute)));

	Issue issueFromMarker = issueUtil.createIssue(markers[0]);
	assertEquals(issue.getColumn(), issueFromMarker.getColumn());
	assertEquals(issue.getLineNumber(), issueFromMarker.getLineNumber());
	assertEquals(issue.getOffset(), issueFromMarker.getOffset());
	assertEquals(issue.getLength(), issueFromMarker.getLength());
	assertTrue(Arrays.equals(expectedIssueData, issueFromMarker.getData()));
}