@Override public IRegion getHoverRegion(final ITextViewer textViewer, final int offset) { IXtextDocument xtextDocument = XtextDocumentUtil.get(textViewer); if(xtextDocument == null) return null; //TODO this is being called on change in the UI-thread. Not a good idea to do such expensive stuff. // returning the region on a per token basis would be better. return xtextDocument.readOnly(new IUnitOfWork<IRegion, XtextResource>() { public IRegion exec(XtextResource state) throws Exception { // resource can be null e.g. read only zip/jar entry if (state == null) { return null; } Pair<EObject, IRegion> element = getXtextElementAt(state, offset); if (element != null) { return element.getSecond(); } else { return null; } } }); }
public Object getHoverInfo2(final ITextViewer textViewer, final IRegion hoverRegion) { if (hoverRegion == null) return null; IXtextDocument xtextDocument = XtextDocumentUtil.get(textViewer); if(xtextDocument == null) return null; return xtextDocument.readOnly(new IUnitOfWork<Object, XtextResource>() { public Object exec(XtextResource state) throws Exception { // resource can be null e.g. read only zip/jar entry if (state == null) { return null; } Pair<EObject, IRegion> element = getXtextElementAt(state, hoverRegion.getOffset()); if (element != null && element.getFirst() != null) { return getHoverInfo(element.getFirst(), textViewer, hoverRegion); } return null; } }); }
/** * @since 2.3 */ protected List<ICompletionProposal> createQuickfixes(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations) { List<ICompletionProposal> result = Lists.newArrayList(); ISourceViewer sourceViewer = invocationContext.getSourceViewer(); IAnnotationModel annotationModel = sourceViewer.getAnnotationModel(); IXtextDocument xtextDocument = XtextDocumentUtil.get(sourceViewer); for(Annotation annotation : applicableAnnotations) { if (annotation instanceof SpellingAnnotation) { SpellingProblem spellingProblem = ((SpellingAnnotation) annotation).getSpellingProblem(); result.addAll(asList(spellingProblem.getProposals())); } else { final Issue issue = issueUtil.getIssueFromAnnotation(annotation); if (issue != null) { Iterable<IssueResolution> resolutions = getResolutions(issue, xtextDocument); if (resolutions.iterator().hasNext()) { Position pos = annotationModel.getPosition(annotation); for (IssueResolution resolution : resolutions) { result.add(create(pos, resolution)); } } } } } return result; }
protected void handleInputDocumentChanged(IDocument oldInput, IDocument newInput) { if (shouldInstallCompletionListener) { ContentAssistantFacade facade = ((ISourceViewerExtension4) textViewer).getContentAssistantFacade(); if (facade != null) { facade.addCompletionListener(documentListener); } shouldInstallCompletionListener = false; } if (oldInput instanceof IXtextDocument) { ((IXtextDocument) oldInput).removeXtextDocumentContentObserver(documentListener); } if (newInput instanceof IXtextDocument) { ((IXtextDocument) newInput).addXtextDocumentContentObserver(documentListener); final IXtextDocument document = XtextDocumentUtil.get(textViewer); strategy.setDocument(document); if (!initalProcessDone && strategy instanceof IReconcilingStrategyExtension) { initalProcessDone = true; IReconcilingStrategyExtension reconcilingStrategyExtension = (IReconcilingStrategyExtension) strategy; reconcilingStrategyExtension.initialReconcile(); } } if (oldInput != null && newInput != null) { handleDocumentChanged(new InputChangedDocumentEvent(oldInput, newInput)); } }
@Override protected IStatus run(final IProgressMonitor monitor) { if (monitor.isCanceled() || paused) return Status.CANCEL_STATUS; long start = System.currentTimeMillis(); final IXtextDocument document = XtextDocumentUtil.get(textViewer); if (document instanceof XtextDocument) { ((XtextDocument) document).internalModify(new IUnitOfWork.Void<XtextResource>() { @Override public void process(XtextResource state) throws Exception { doRun(state, monitor); } }); } if (log.isDebugEnabled()) log.debug("Reconciliation finished. Time required: " + (System.currentTimeMillis() - start)); //$NON-NLS-1$ return Status.OK_STATUS; }
public void setSourceViewer(ISourceViewer sourceViewer) { this.sourceViewer = sourceViewer; IDocument document = sourceViewer.getDocument(); xtextDocument = XtextDocumentUtil.get(document); Assert.isNotNull(xtextDocument); configureTextInputListener(); }
public IXtextDocument getXtextDocument(IResource resource) { IXtextDocument result = XtextDocumentUtil.get(resource); if(result == null) { IWorkbenchPage page = workbench.getActiveWorkbenchWindow().getActivePage(); try { IFile file = ResourceUtil.getFile(resource); IEditorInput input = new FileEditorInput(file); page.openEditor(input, getEditorId()); } catch (PartInitException e) { return null; } } return XtextDocumentUtil.get(resource); }
protected ICompletionProposal createTemplateProposal( TemplateIssueResolution res) { try { TemplateContextSupplier fac = res.getContextFactory(); IDocument doc = XtextDocumentUtil.get(qaCtx.getSourceViewer()); Position pos = fac.getPosition(doc, qaCtx); String prefix = fac.getPrefix(doc, qaCtx); String postfix = fac.getPostfix(doc, qaCtx); Map<String, String> vars = fac.getVariables(doc, qaCtx); Region r = new Region(pos.getOffset(), pos.getLength()); ContentAssistContext cactx = builderProvider.get() .setOffset(pos.getOffset()).setPrefix(prefix) .setViewer(qaCtx.getSourceViewer()).toContext(); // create a TemplateProposalContext Image i = ((DefaultTemplateProposalProvider) templatePP) .getImage(res.getTemplate()); XtextTemplateContextType type = templateCtxTypeProvider.get(); XtextTemplateContext tplctx = new QuickfixTemplateContext(type, doc, pos, cactx, scopeProvider, prefix, postfix); for (Map.Entry<String, String> e : vars.entrySet()) tplctx.setVariable(e.getKey(), e.getValue()); // create a TemplateProposal XtextTemplateProposal proposal = new XtextTemplateProposal( res.getTemplate(), tplctx, r, i); return proposal; } catch (RuntimeException e1) { throw e1; } catch (Exception e2) { throw new WrappedException(e2); } }
public IXtextDocument getDocument() { return XtextDocumentUtil.get(getSourceViewer()); }