use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-xtend by eclipse.
the class ExtractMethodHandler method execute.
@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;
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-xtend by eclipse.
the class CodeBuilderQuickfix method getXtendModification.
protected IModification getXtendModification(final ICodeBuilder.Xtend builder) {
final IModification _function = (IModificationContext it) -> {
final XtendTypeDeclaration xtendClass = builder.getXtendType();
final IEditorPart editor = this.editorOpener.open(EcoreUtil.getURI(xtendClass), false);
if ((!(editor instanceof XtextEditor))) {
return;
}
final XtextEditor xtextEditor = ((XtextEditor) editor);
final IXtextDocument document = xtextEditor.getDocument();
final Wrapper<Integer> wrapper = Wrapper.<Integer>forType(Integer.class);
final IUnitOfWork<ReplacingAppendable, XtextResource> _function_1 = (XtextResource resource) -> {
ReplacingAppendable _xblockexpression = null;
{
int offset = builder.getInsertOffset(resource);
wrapper.set(Integer.valueOf(offset));
final int typeIndentation = this.getTypeIndentation(resource, document, xtendClass);
DocumentSourceAppender.Factory.OptionalParameters _optionalParameters = new DocumentSourceAppender.Factory.OptionalParameters();
final Procedure1<DocumentSourceAppender.Factory.OptionalParameters> _function_2 = (DocumentSourceAppender.Factory.OptionalParameters it_1) -> {
int _indentationLevel = builder.getIndentationLevel();
int _plus = (_indentationLevel + typeIndentation);
it_1.baseIndentationLevel = _plus;
it_1.ensureEmptyLinesAround = true;
};
DocumentSourceAppender.Factory.OptionalParameters _doubleArrow = ObjectExtensions.<DocumentSourceAppender.Factory.OptionalParameters>operator_doubleArrow(_optionalParameters, _function_2);
_xblockexpression = this.appendableFactory.create(document, resource, offset, 0, _doubleArrow);
}
return _xblockexpression;
};
final ReplacingAppendable appendable = document.<ReplacingAppendable>readOnly(_function_1);
Integer offset = wrapper.get();
builder.build(appendable);
appendable.commitChanges();
xtextEditor.setHighlightRange(((offset).intValue() + 1), appendable.length(), true);
};
return _function;
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-xtend by eclipse.
the class FieldInitializerUtil method getSelectedResource.
public IJavaElement getSelectedResource(IStructuredSelection selection) {
IJavaElement elem = null;
if (selection != null && !selection.isEmpty()) {
Object o = selection.getFirstElement();
elem = Adapters.adapt(o, IJavaElement.class);
if (elem == null) {
elem = getPackage(o);
}
}
if (elem == null) {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IWorkbenchPart part = activePage.getActivePart();
if (part instanceof ContentOutline) {
part = activePage.getActiveEditor();
}
if (part instanceof XtextEditor) {
IXtextDocument doc = ((XtextEditor) part).getDocument();
IFile file = Adapters.adapt(doc, IFile.class);
elem = getPackage(file);
}
}
if (elem == null || elem.getElementType() == IJavaElement.JAVA_MODEL) {
try {
IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
if (projects.length == 1) {
elem = projects[0];
}
} catch (JavaModelException e) {
throw new RuntimeException(e.getMessage());
}
}
return elem;
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class ContentAssistProcessorTestBuilder method assertMatchString.
public ContentAssistProcessorTestBuilder assertMatchString(String matchString) throws Exception {
String currentModelToParse = getModel();
final XtextResource xtextResource = loadHelper.getResourceFor(new StringInputStream(currentModelToParse));
final IXtextDocument xtextDocument = getDocument(xtextResource, currentModelToParse);
XtextSourceViewerConfiguration configuration = get(XtextSourceViewerConfiguration.class);
Shell shell = new Shell();
try {
ISourceViewer sourceViewer = getSourceViewer(shell, xtextDocument, configuration);
IContentAssistant contentAssistant = configuration.getContentAssistant(sourceViewer);
String contentType = xtextDocument.getContentType(currentModelToParse.length());
if (contentAssistant.getContentAssistProcessor(contentType) != null) {
ContentAssistContext.Factory factory = get(ContentAssistContext.Factory.class);
ContentAssistContext[] contexts = factory.create(sourceViewer, currentModelToParse.length(), xtextResource);
for (ContentAssistContext context : contexts) {
Assert.assertTrue("matchString = '" + matchString + "', actual: '" + context.getPrefix() + "'", "".equals(context.getPrefix()) || matchString.equals(context.getPrefix()));
}
} else {
Assert.fail("No content assistant for content type " + contentType);
}
return this;
} finally {
shell.dispose();
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class ContentAssistProcessorTestBuilder method applyProposal.
public ContentAssistProcessorTestBuilder applyProposal(int position, String proposalString) throws Exception {
IXtextDocument document = getDocument(getModel());
Shell shell = new Shell();
try {
ICompletionProposal[] proposals = computeCompletionProposals(document, position, shell);
ICompletionProposal proposal = findProposal(proposalString, proposals);
return applyProposal(proposal, position, document);
} finally {
shell.dispose();
}
}
Aggregations