Search in sources :

Example 61 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class ShowQuickOutlineActionHandler method execute.

@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;
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) Event(org.eclipse.swt.widgets.Event) XtextResource(org.eclipse.xtext.resource.XtextResource) ExecutionException(org.eclipse.core.commands.ExecutionException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 62 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class OrganizeImportsHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
    if (editor != null) {
        final IXtextDocument document = editor.getDocument();
        doOrganizeImports(document);
    }
    return null;
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 63 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class JavaTypeQuickfixes method createConstructorProposals.

@SuppressWarnings("restriction")
protected boolean createConstructorProposals(final JvmDeclaredType contextType, final Issue issue, String typeName, IJavaSearchScope searchScope, final IssueResolutionAcceptor acceptor) throws JavaModelException {
    final boolean[] result = new boolean[] { false };
    if (contextType != null) {
        final IVisibilityHelper visibilityHelper = getVisibilityHelper(contextType);
        final Pair<String, String> packageAndType = typeNameGuesser.guessPackageAndTypeName(contextType, typeName);
        final String wantedPackageName = packageAndType.getFirst();
        final String wantedTypeName = packageAndType.getSecond();
        if (typeName.endsWith(wantedTypeName)) {
            return false;
        }
        org.eclipse.jdt.internal.core.search.BasicSearchEngine searchEngine = new org.eclipse.jdt.internal.core.search.BasicSearchEngine();
        final char[] wantedPackageChars = (isEmpty(wantedPackageName)) ? null : wantedPackageName.toCharArray();
        searchEngine.searchAllTypeNames(wantedPackageChars, SearchPattern.R_EXACT_MATCH, wantedTypeName.toCharArray(), SearchPattern.R_EXACT_MATCH, IJavaSearchConstants.TYPE, searchScope, new org.eclipse.jdt.internal.core.search.IRestrictedAccessTypeRequestor() {

            @Override
            public void acceptType(int modifiers, char[] packageName, char[] simpleTypeName, char[][] enclosingTypeNames, String path, org.eclipse.jdt.internal.compiler.env.AccessRestriction access) {
                final String qualifiedTypeName = getQualifiedTypeName(packageName, enclosingTypeNames, simpleTypeName);
                if (access == null || (access.getProblemId() != IProblem.ForbiddenReference && !access.ignoreIfBetter())) {
                    JvmType importType = services.getTypeReferences().findDeclaredType(qualifiedTypeName, contextType);
                    if (importType instanceof JvmDeclaredType && visibilityHelper.isVisible((JvmDeclaredType) importType)) {
                        result[0] = true;
                        StringBuilder label = new StringBuilder("Change to constructor call 'new ");
                        label.append(simpleTypeName);
                        label.append("()'");
                        if (!equal(wantedPackageName, new String(packageName))) {
                            label.append(" (");
                            label.append(packageName);
                            if (enclosingTypeNames.length > 0) {
                                for (char[] enclosingTypeName : enclosingTypeNames) {
                                    label.append(".");
                                    label.append(enclosingTypeName);
                                }
                            }
                            label.append(")");
                        }
                        acceptor.accept(issue, label.toString(), label.toString(), "impc_obj.gif", new ISemanticModification() {

                            @Override
                            public void apply(EObject element, IModificationContext context) throws Exception {
                                IXtextDocument document = context.getXtextDocument();
                                DocumentRewriter rewriter = rewriterFactory.create(document, (XtextResource) element.eResource());
                                final int typeEndOffset = document.get().indexOf(wantedTypeName, issue.getOffset() + wantedPackageName.length()) + wantedTypeName.length();
                                final Section section = rewriter.newSection(issue.getOffset(), typeEndOffset - issue.getOffset());
                                section.append(services.getTypeReferences().findDeclaredType(qualifiedTypeName, element));
                                section.append("()");
                                final TextEdit textEdit = replaceConverter.convertToTextEdit(rewriter.getChanges());
                                textEdit.apply(document);
                            }
                        }, jdtTypeRelevance.getRelevance(qualifiedTypeName, wantedTypeName) + 100);
                    }
                }
            }
        }, IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH, new NullProgressMonitor());
    }
    return result[0];
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) JvmType(org.eclipse.xtext.common.types.JvmType) EObject(org.eclipse.emf.ecore.EObject) ISemanticModification(org.eclipse.xtext.ui.editor.model.edit.ISemanticModification) IVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.IVisibilityHelper) XImportSection(org.eclipse.xtext.xtype.XImportSection) Section(org.eclipse.xtext.xbase.ui.document.DocumentRewriter.Section) TextEdit(org.eclipse.text.edits.TextEdit) DocumentRewriter(org.eclipse.xtext.xbase.ui.document.DocumentRewriter) IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 64 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.

the class XbaseDispatchingEObjectTextHover method getHoverRegion.

@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
    IXtextDocument xtextDocument = xtextDocumentUtil.getXtextDocument(textViewer);
    if (xtextDocument == null || offset < 0 || xtextDocument.getLength() < offset)
        return null;
    @SuppressWarnings("restriction") IRegion word = org.eclipse.jdt.internal.ui.text.JavaWordFinder.findWord(xtextDocument, offset);
    if (word != null)
        return word;
    // TODO return null for non-operators.
    return new Region(offset, 0);
}
Also used : Region(org.eclipse.jface.text.Region) ITextRegion(org.eclipse.xtext.util.ITextRegion) IRegion(org.eclipse.jface.text.IRegion) IRegion(org.eclipse.jface.text.IRegion) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 65 with IXtextDocument

use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-xtend by eclipse.

the class XtendFoldingRegionProviderTest method openFileAndReturnDocument.

protected IXtextDocument openFileAndReturnDocument(IFile iFile) throws Exception {
    XtextEditor editor = testHelper.openEditor(iFile);
    IXtextDocument document = editor.getDocument();
    return document;
}
Also used : XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)147 XtextResource (org.eclipse.xtext.resource.XtextResource)51 Test (org.junit.Test)46 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)38 IFile (org.eclipse.core.resources.IFile)37 BadLocationException (org.eclipse.jface.text.BadLocationException)26 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)26 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)25 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)23 EObject (org.eclipse.emf.ecore.EObject)22 DefaultFoldingRegionProvider (org.eclipse.xtext.ui.editor.folding.DefaultFoldingRegionProvider)18 FoldedPosition (org.eclipse.xtext.ui.editor.folding.FoldedPosition)18 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)16 List (java.util.List)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 ArrayList (java.util.ArrayList)11 Issue (org.eclipse.xtext.validation.Issue)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)10 URI (org.eclipse.emf.common.util.URI)9 IRegion (org.eclipse.jface.text.IRegion)9