Search in sources :

Example 16 with IDocument

use of org.eclipse.jface.text.IDocument in project che by eclipse.

the class TextChange method getCurrentDocument.

//---- Method to access the current content of the text change ---------
/**
	 * Returns the document this text change is associated to. The
	 * document returned is computed at the point in time when this
	 * method is called. So calling this method multiple times may
	 * return different document instances.
	 * <p>
	 * The returned document must not be modified by the client. Doing
	 * so will result in an unexpected behavior when the change is
	 * performed.
	 * </p>
	 *
	 * @param pm a progress monitor to report progress or <code>null</code>
	 *  if no progress reporting is desired
	 * @return the document this change is working on
	 *
	 * @throws CoreException if the document can't be acquired
	 */
public IDocument getCurrentDocument(IProgressMonitor pm) throws CoreException {
    if (pm == null)
        pm = new NullProgressMonitor();
    IDocument result = null;
    //$NON-NLS-1$
    pm.beginTask("", 2);
    try {
        result = acquireDocument(new SubProgressMonitor(pm, 1));
    } finally {
        if (result != null)
            releaseDocument(result, new SubProgressMonitor(pm, 1));
    }
    pm.done();
    return result;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IDocument(org.eclipse.jface.text.IDocument) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 17 with IDocument

use of org.eclipse.jface.text.IDocument in project che by eclipse.

the class SearchManager method performFindUsageSearch.

private FindUsagesResponse performFindUsageSearch(IJavaElement element) throws JavaModelException, BadLocationException {
    JavaSearchScopeFactory factory = JavaSearchScopeFactory.getInstance();
    boolean isInsideJRE = factory.isInsideJRE(element);
    JavaSearchQuery query = new JavaSearchQuery(new ElementQuerySpecification(element, IJavaSearchConstants.REFERENCES, factory.createWorkspaceScope(isInsideJRE), "workspace scope"));
    NewSearchUI.runQueryInForeground(null, query);
    ISearchResult result = query.getSearchResult();
    JavaSearchResult javaResult = ((JavaSearchResult) result);
    FindUsagesResponse response = DtoFactory.newDto(FindUsagesResponse.class);
    Map<String, List<org.eclipse.che.ide.ext.java.shared.dto.search.Match>> mapMaches = new HashMap<>();
    JavaElementToDtoConverter converter = new JavaElementToDtoConverter(javaResult);
    for (Object o : javaResult.getElements()) {
        IJavaElement javaElement = (IJavaElement) o;
        IDocument document = null;
        if (javaElement instanceof IMember) {
            IMember member = ((IMember) javaElement);
            if (member.isBinary()) {
                if (member.getClassFile().getSource() != null) {
                    document = new Document(member.getClassFile().getSource());
                }
            } else {
                document = getDocument(member.getCompilationUnit());
            }
        } else if (javaElement instanceof IPackageDeclaration) {
            ICompilationUnit ancestor = (ICompilationUnit) (javaElement).getAncestor(IJavaElement.COMPILATION_UNIT);
            document = getDocument(ancestor);
        }
        converter.addElementToProjectHierarchy(javaElement);
        Match[] matches = javaResult.getMatches(o);
        List<org.eclipse.che.ide.ext.java.shared.dto.search.Match> matchList = new ArrayList<>();
        for (Match match : matches) {
            org.eclipse.che.ide.ext.java.shared.dto.search.Match dtoMatch = DtoFactory.newDto(org.eclipse.che.ide.ext.java.shared.dto.search.Match.class);
            if (document != null) {
                IRegion lineInformation = document.getLineInformationOfOffset(match.getOffset());
                int offsetInLine = match.getOffset() - lineInformation.getOffset();
                Region matchInLine = DtoFactory.newDto(Region.class).withOffset(offsetInLine).withLength(match.getLength());
                dtoMatch.setMatchInLine(matchInLine);
                dtoMatch.setMatchLineNumber(document.getLineOfOffset(match.getOffset()));
                dtoMatch.setMatchedLine(document.get(lineInformation.getOffset(), lineInformation.getLength()));
            }
            dtoMatch.setFileMatchRegion(DtoFactory.newDto(Region.class).withOffset(match.getOffset()).withLength(match.getLength()));
            matchList.add(dtoMatch);
        }
        mapMaches.put(javaElement.getHandleIdentifier(), matchList);
    }
    List<JavaProject> projects = converter.getProjects();
    response.setProjects(projects);
    response.setMatches(mapMaches);
    response.setSearchElementLabel(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT));
    return response;
}
Also used : ISearchResult(org.eclipse.search.ui.ISearchResult) FindUsagesResponse(org.eclipse.che.ide.ext.java.shared.dto.search.FindUsagesResponse) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IMember(org.eclipse.jdt.core.IMember) IRegion(org.eclipse.jface.text.IRegion) Match(org.eclipse.search.ui.text.Match) JavaSearchResult(org.eclipse.jdt.internal.ui.search.JavaSearchResult) ArrayList(java.util.ArrayList) List(java.util.List) JavaSearchScopeFactory(org.eclipse.jdt.internal.ui.search.JavaSearchScopeFactory) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaProject(org.eclipse.che.ide.ext.java.shared.dto.model.JavaProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) ElementQuerySpecification(org.eclipse.jdt.ui.search.ElementQuerySpecification) JavaSearchQuery(org.eclipse.jdt.internal.ui.search.JavaSearchQuery) Region(org.eclipse.che.ide.ext.java.shared.dto.Region) IRegion(org.eclipse.jface.text.IRegion) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) IDocument(org.eclipse.jface.text.IDocument)

Example 18 with IDocument

use of org.eclipse.jface.text.IDocument in project che by eclipse.

the class SearchManager method getDocument.

private IDocument getDocument(ICompilationUnit ancestor) throws JavaModelException {
    IDocument document;
    IBuffer buffer = ancestor.getBuffer();
    if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
        document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
    } else {
        document = new DocumentAdapter(buffer);
    }
    return document;
}
Also used : DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) IDocument(org.eclipse.jface.text.IDocument) IBuffer(org.eclipse.jdt.core.IBuffer)

Example 19 with IDocument

use of org.eclipse.jface.text.IDocument in project che by eclipse.

the class CompletionJavadocTest method computeProposals.

private static List<ICompletionProposal> computeProposals(ICompilationUnit compilationUnit, int offset) throws JavaModelException {
    IBuffer buffer = compilationUnit.getBuffer();
    IDocument document;
    if (buffer instanceof org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) {
        document = ((org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter) buffer).getDocument();
    } else {
        document = new DocumentAdapter(buffer);
    }
    TextViewer viewer = new TextViewer(document, new Point(offset, 0));
    JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, compilationUnit);
    List<ICompletionProposal> proposals = new ArrayList<>();
    proposals.addAll(new JavaAllCompletionProposalComputer().computeCompletionProposals(context, null));
    //        proposals.addAll(new TemplateCompletionProposalComputer().computeCompletionProposals(context, null));
    Collections.sort(proposals, new RelevanceSorter());
    return proposals;
}
Also used : JavaContentAssistInvocationContext(org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.jdt.internal.core.DocumentAdapter) Point(org.eclipse.swt.graphics.Point) RelevanceSorter(org.eclipse.jdt.internal.ui.text.java.RelevanceSorter) IBuffer(org.eclipse.jdt.core.IBuffer) TextViewer(org.eclipse.che.jdt.javaeditor.TextViewer) ICompletionProposal(org.eclipse.che.jface.text.contentassist.ICompletionProposal) JavaAllCompletionProposalComputer(org.eclipse.jdt.internal.ui.text.java.JavaAllCompletionProposalComputer) IDocument(org.eclipse.jface.text.IDocument)

Example 20 with IDocument

use of org.eclipse.jface.text.IDocument in project che by eclipse.

the class FileStoreTextFileBuffer method revert.

/*
	 * @see org.eclipse.core.filebuffers.IFileBuffer#revert(org.eclipse.core.runtime.IProgressMonitor)
	 */
public void revert(IProgressMonitor monitor) throws CoreException {
    if (isDisconnected())
        return;
    IDocument original = null;
    fStatus = null;
    try {
        original = fManager.createEmptyDocument(getLocationOrName(), LocationKind.LOCATION);
        cacheEncodingState();
        setDocumentContent(original, fFileStore, fEncoding, fHasBOM, monitor);
    } catch (CoreException x) {
        fStatus = x.getStatus();
    }
    if (original == null)
        return;
    String originalContents = original.get();
    boolean replaceContents = !originalContents.equals(fDocument.get());
    if (!replaceContents && !fCanBeSaved)
        return;
    fManager.fireStateChanging(this);
    try {
        if (replaceContents) {
            fManager.fireBufferContentAboutToBeReplaced(this);
            fDocument.set(original.get());
        }
        boolean fireDirtyStateChanged = fCanBeSaved;
        if (fCanBeSaved) {
            fCanBeSaved = false;
            addFileBufferContentListeners();
        }
        if (replaceContents)
            fManager.fireBufferContentReplaced(this);
        IFileInfo info = fFileStore.fetchInfo();
        if (info.exists())
            fSynchronizationStamp = fFileStore.fetchInfo().getLastModified();
        if (fireDirtyStateChanged)
            fManager.fireDirtyStateChanged(this, fCanBeSaved);
    } catch (RuntimeException x) {
        fManager.fireStateChangeFailed(this);
        throw x;
    }
}
Also used : IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

IDocument (org.eclipse.jface.text.IDocument)1055 BadLocationException (org.eclipse.jface.text.BadLocationException)379 Document (org.eclipse.jface.text.Document)189 IRegion (org.eclipse.jface.text.IRegion)150 Test (org.junit.Test)107 CoreException (org.eclipse.core.runtime.CoreException)102 Point (org.eclipse.swt.graphics.Point)94 IFile (org.eclipse.core.resources.IFile)90 ArrayList (java.util.ArrayList)85 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)84 Position (org.eclipse.jface.text.Position)74 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)74 IEditorPart (org.eclipse.ui.IEditorPart)62 ITextFileBufferManager (org.eclipse.core.filebuffers.ITextFileBufferManager)54 IPath (org.eclipse.core.runtime.IPath)54 ITextSelection (org.eclipse.jface.text.ITextSelection)53 TextEdit (org.eclipse.text.edits.TextEdit)49 Region (org.eclipse.jface.text.Region)47 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)44 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)41