Java Code Examples for org.eclipse.xtext.ui.editor.model.IXtextDocument#priorityReadOnly()

The following examples show how to use org.eclipse.xtext.ui.editor.model.IXtextDocument#priorityReadOnly() . 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: FixedContentFormatter.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * This method makes sure that no changes are applied (no dirty state), if there are no changes. This fixes bug
 * GHOLD-272
 */
@Override
public void format(IDocument document, IRegion region) {
	IXtextDocument doc = (IXtextDocument) document;
	TextEdit e = doc.priorityReadOnly(new FormattingUnitOfWork(doc, region));

	if (e == null)
		return;
	if (e instanceof ReplaceEdit) {
		ReplaceEdit r = (ReplaceEdit) e;
		if ((r.getOffset() == 0) && (r.getLength() == 0) && (r.getText().isEmpty())) {
			return;
		}
	}
	try {
		e.apply(document);
	} catch (BadLocationException ex) {
		throw new RuntimeException(ex);
	}

}
 
Example 2
Source File: ShowQuickOutlineActionHandler.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
	final XtextEditor xtextEditor = EditorUtils.getActiveXtextEditor(event);
	if (xtextEditor != null) {
		final IXtextDocument document = xtextEditor.getDocument();
		document.priorityReadOnly(new IUnitOfWork.Void<XtextResource>()  {
			@Override
			public void process(XtextResource state) throws Exception {
				final QuickOutlinePopup quickOutlinePopup = createPopup(xtextEditor.getEditorSite().getShell());
				quickOutlinePopup.setEditor(xtextEditor);
				quickOutlinePopup.setInput(document);
				if (event.getTrigger() != null) {
					quickOutlinePopup.setEvent((Event) event.getTrigger());
				}
				quickOutlinePopup.open();
			}
		});
	}
	return null;
}
 
Example 3
Source File: XtextInformationProvider.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IRegion getSubject(ITextViewer textViewer, final int offset) {
	if(textViewer instanceof XtextSourceViewer){
		IXtextDocument document = ((XtextSourceViewer) textViewer).getXtextDocument();
		if (document != null) {
			Object resolvedObject = document.priorityReadOnly(new IUnitOfWork<Object, XtextResource>() {
				@Override
				public Object exec(XtextResource state) throws Exception {
					return eObjectAtOffsetHelper.resolveElementAt(state, offset);
				}
			});
			if(resolvedObject != null && resolvedObject instanceof EObject){
				this.contextObject = (EObject) resolvedObject;
			}
		}
	}
	this.contextRegion = computeRegion(textViewer, offset);
	this.textViewer = textViewer;
	return contextRegion;
}
 
Example 4
Source File: ContentFormatterFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void format(IDocument document, IRegion region) {
	IXtextDocument doc = xtextDocumentUtil.getXtextDocument(document);
	ReplaceRegion r = doc.priorityReadOnly(new FormattingUnitOfWork(region));
	try {
		if (r != null) {
			TextEdit edit = createTextEdit(doc, r);
			if (edit != null) {
				edit.apply(doc);
			}
			
		}
	} catch (BadLocationException e) {
		throw new RuntimeException(e);
	}
}
 
Example 5
Source File: OrganizeImportsHandler.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public void doOrganizeImports(final IXtextDocument document) {
	List<ReplaceRegion> result = document.priorityReadOnly(new IUnitOfWork<List<ReplaceRegion>, XtextResource>() {
		@Override
		public List<ReplaceRegion> exec(XtextResource state) throws Exception {
			return importOrganizer.getOrganizedImportChanges(state);
		}
	});
	if (result == null || result.isEmpty())
		return;
	try {
		DocumentRewriteSession session = null;
		if(document instanceof IDocumentExtension4) {
			session = ((IDocumentExtension4)document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
		}
		replaceConverter.convertToTextEdit(result).apply(document);
		if(session != null) {
			((IDocumentExtension4)document).stopRewriteSession(session);
		}
	} catch (BadLocationException e) {
		LOG.error(Messages.OrganizeImportsHandler_organizeImportsErrorMessage, e);
	}
}
 
Example 6
Source File: JSDocContentAssistProcessor.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
	if (viewer instanceof XtextSourceViewer) {
		IXtextDocument document = (IXtextDocument) viewer.getDocument();
		return document.priorityReadOnly(createCompletionProposalComputer(viewer, offset));
	}

	return new ICompletionProposal[0];
}
 
Example 7
Source File: RepeatedContentAssistProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.7
 */
protected ICompletionProposal[] computeCompletionProposals(IXtextDocument document, CompletionProposalComputer proposalComputer) {
	if (getContentProposalProvider() == null)
		return null;
	
	ICompletionProposal[] result = document.priorityReadOnly(proposalComputer);
	Arrays.sort(result, getCompletionProposalComparator());
	result = getCompletionProposalPostProcessor().postProcess(result);
	return result;
}
 
Example 8
Source File: XtextContentAssistProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
	if (contentProposalProvider == null)
		return null;
	
	IXtextDocument document = xtextDocumentUtil.getXtextDocument(viewer);
	ICompletionProposal[] result = document.priorityReadOnly(createCompletionProposalComputer(viewer, offset));
	Arrays.sort(result, completionProposalComparator);
	result = completionProposalPostProcessor.postProcess(result);
	return result;
}
 
Example 9
Source File: XtextContentAssistProcessor.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public IContextInformation[] computeContextInformation(final ITextViewer viewer, final int offset) {
	if (contextInformationProvider == null)
		return null;
	
	IXtextDocument document = xtextDocumentUtil.getXtextDocument(viewer);
	return document.priorityReadOnly(createContextInformationComputer(viewer, offset));
}
 
Example 10
Source File: ContentFormatter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void format(IDocument document, IRegion region) {
	IXtextDocument doc = xtextDocumentUtil.getXtextDocument(document);
	TextEdit r = doc.priorityReadOnly(new FormattingUnitOfWork(doc, region));
	try {
		if (r != null)
			r.apply(document);
	} catch (BadLocationException e) {
		throw new RuntimeException(e);
	}
}
 
Example 11
Source File: XbaseTemplateContext.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private List<ReplaceRegion> createImports(final List<String> types, IXtextDocument document) {
	return document.priorityReadOnly(new IUnitOfWork<List<ReplaceRegion>, XtextResource>() {
		@Override
		public List<ReplaceRegion> exec(XtextResource state) throws Exception {
			RewritableImportSection impSection = importSectionFactory.parse(state);
			for (String fqName : types) {
				JvmDeclaredType jvmType = findJvmDeclaredType(fqName, state.getResourceSet());
				if (jvmType != null) {
					impSection.addImport(jvmType);
				}
			}
			return impSection.rewrite();
		}
	});
}
 
Example 12
Source File: XbaseTemplateContext.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private boolean checkImports(final List<String> types, IXtextDocument document) {
	return document.priorityReadOnly(new IUnitOfWork<Boolean, XtextResource>() {
		@Override
		public Boolean exec(XtextResource state) throws Exception {
			for (String fqName : types) {
				JvmDeclaredType jvmType = findJvmDeclaredType(fqName, state.getResourceSet());
				if (jvmType == null) {
					return false;
				}
			}
			return true;
		}
	});
}
 
Example 13
Source File: ExtractVariableHandler.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
	try {
		syncUtil.totalSync(false);
		final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
		if (editor != null) {
			final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
			final IXtextDocument document = editor.getDocument();
			XtextResource resource = document.priorityReadOnly(new IUnitOfWork<XtextResource, XtextResource>() {
				@Override
				public XtextResource exec(XtextResource state) throws Exception {
					return resourceCopier.loadIntoNewResourceSet(state);
				}
			});
			XExpression expression = expressionUtil.findSelectedExpression(resource, selection);
			if(expression != null) {
				ExtractVariableRefactoring introduceVariableRefactoring = refactoringProvider.get();
				if(introduceVariableRefactoring.initialize(editor, expression)) {
					ITextRegion region = locationInFileProvider.getFullTextRegion(expression);
					editor.selectAndReveal(region.getOffset(), region.getLength());
					ExtractVariableWizard wizard = new ExtractVariableWizard(introduceVariableRefactoring);
					RefactoringWizardOpenOperation_NonForking openOperation = new RefactoringWizardOpenOperation_NonForking(
							wizard);
					openOperation.run(editor.getSite().getShell(), "Extract Local Variable");
				}
			}
		}
	} catch (InterruptedException e) {
		return null;
	} catch (Exception exc) {
		LOG.error("Error during refactoring", exc);
		MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error during refactoring", exc.getMessage()
				+ "\nSee log for details");
	}
	return null;
}
 
Example 14
Source File: XtendJavaDocContentAssistProcessor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
	IXtextDocument document = xtextDocumentUtil.getXtextDocument(viewer);
	if (document != null) {
		return document.priorityReadOnly(createCompletionProposalComputer(viewer, offset));
	}
	return new ICompletionProposal[0];
}
 
Example 15
Source File: ExtractMethodHandler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
	try {
		syncUtil.totalSync(false);
		final XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
		if (editor != null) {
			final ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
			final IXtextDocument document = editor.getDocument();
			XtextResource copiedResource = document.priorityReadOnly(new IUnitOfWork<XtextResource, XtextResource>() {
				@Override
				public XtextResource exec(XtextResource state) throws Exception {
					return resourceCopier.loadIntoNewResourceSet(state);
				}
			});
			List<XExpression> expressions = expressionUtil.findSelectedSiblingExpressions(copiedResource,
					selection);
			if (!expressions.isEmpty()) {
				ExtractMethodRefactoring extractMethodRefactoring = refactoringProvider.get();
				if (extractMethodRefactoring.initialize(editor, expressions, true)) {
					updateSelection(editor, expressions);
					ExtractMethodWizard wizard = wizardFactory.create(extractMethodRefactoring);
					RefactoringWizardOpenOperation_NonForking openOperation = new RefactoringWizardOpenOperation_NonForking(
							wizard);
					openOperation.run(editor.getSite().getShell(), "Extract Method");
				}
			}
		}
	} catch (InterruptedException e) {
		return null;
	} catch (Exception exc) {
		LOG.error("Error during refactoring", exc);
		MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error during refactoring", exc.getMessage()
				+ "\nSee log for details");
	}
	return null;
}