Search in sources :

Example 11 with IRegion

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

the class FastPartitioner method documentChanged2.

/**
	 * {@inheritDoc}
	 * <p>
	 * May be extended by subclasses.
	 * </p>
	 */
public IRegion documentChanged2(DocumentEvent e) {
    if (!fIsInitialized)
        return null;
    try {
        Assert.isTrue(e.getDocument() == fDocument);
        Position[] category = getPositions();
        IRegion line = fDocument.getLineInformationOfOffset(e.getOffset());
        int reparseStart = line.getOffset();
        int partitionStart = -1;
        String contentType = null;
        int newLength = e.getText() == null ? 0 : e.getText().length();
        int first = fDocument.computeIndexInCategory(fPositionCategory, reparseStart);
        if (first > 0) {
            TypedPosition partition = (TypedPosition) category[first - 1];
            if (partition.includes(reparseStart)) {
                partitionStart = partition.getOffset();
                contentType = partition.getType();
                if (e.getOffset() == partition.getOffset() + partition.getLength())
                    reparseStart = partitionStart;
                --first;
            } else if (reparseStart == e.getOffset() && reparseStart == partition.getOffset() + partition.getLength()) {
                partitionStart = partition.getOffset();
                contentType = partition.getType();
                reparseStart = partitionStart;
                --first;
            } else {
                partitionStart = partition.getOffset() + partition.getLength();
                contentType = IDocument.DEFAULT_CONTENT_TYPE;
            }
        }
        fPositionUpdater.update(e);
        for (int i = first; i < category.length; i++) {
            Position p = category[i];
            if (p.isDeleted) {
                rememberDeletedOffset(e.getOffset());
                break;
            }
        }
        clearPositionCache();
        category = getPositions();
        fScanner.setPartialRange(fDocument, reparseStart, fDocument.getLength() - reparseStart, contentType, partitionStart);
        int behindLastScannedPosition = reparseStart;
        IToken token = fScanner.nextToken();
        while (!token.isEOF()) {
            contentType = getTokenContentType(token);
            if (!isSupportedContentType(contentType)) {
                token = fScanner.nextToken();
                continue;
            }
            int start = fScanner.getTokenOffset();
            int length = fScanner.getTokenLength();
            behindLastScannedPosition = start + length;
            int lastScannedPosition = behindLastScannedPosition - 1;
            // remove all affected positions
            while (first < category.length) {
                TypedPosition p = (TypedPosition) category[first];
                if (lastScannedPosition >= p.offset + p.length || (p.overlapsWith(start, length) && (!fDocument.containsPosition(fPositionCategory, start, length) || !contentType.equals(p.getType())))) {
                    rememberRegion(p.offset, p.length);
                    fDocument.removePosition(fPositionCategory, p);
                    ++first;
                } else
                    break;
            }
            // area covered by the event, we are done
            if (fDocument.containsPosition(fPositionCategory, start, length)) {
                if (lastScannedPosition >= e.getOffset() + newLength)
                    return createRegion();
                ++first;
            } else {
                // insert the new type position
                try {
                    fDocument.addPosition(fPositionCategory, new TypedPosition(start, length, contentType));
                    rememberRegion(start, length);
                } catch (BadPositionCategoryException x) {
                } catch (BadLocationException x) {
                }
            }
            token = fScanner.nextToken();
        }
        first = fDocument.computeIndexInCategory(fPositionCategory, behindLastScannedPosition);
        clearPositionCache();
        category = getPositions();
        TypedPosition p;
        while (first < category.length) {
            p = (TypedPosition) category[first++];
            fDocument.removePosition(fPositionCategory, p);
            rememberRegion(p.offset, p.length);
        }
    } catch (BadPositionCategoryException x) {
    // should never happen on connected documents
    } catch (BadLocationException x) {
    } finally {
        clearPositionCache();
    }
    return createRegion();
}
Also used : TypedPosition(org.eclipse.jface.text.TypedPosition) Position(org.eclipse.jface.text.Position) TypedPosition(org.eclipse.jface.text.TypedPosition) BadPositionCategoryException(org.eclipse.jface.text.BadPositionCategoryException) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 12 with IRegion

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

the class TextEditGroupNode method feedInput.

public ChangePreview feedInput(IChangePreviewViewer viewer, List categories) throws CoreException {
    InternalTextEditChangeNode element = getTextEditChangeNode();
    if (element != null) {
        Change change = element.getChange();
        if (change instanceof TextEditBasedChange) {
            IRegion range = getTextRange(this);
            ChangePreviewViewerInput input = null;
            if (range != null) {
                input = TextEditChangePreviewViewer.createInput(change, new TextEditBasedChangeGroup[] { fChangeGroup }, range);
            } else {
                input = TextEditChangePreviewViewer.createInput(change, fChangeGroup, 2);
            }
            return viewer.setInput(input);
        }
    } else {
        return viewer.setInput(null);
    }
    return null;
}
Also used : TextEditBasedChange(org.eclipse.ltk.core.refactoring.TextEditBasedChange) ChangePreviewViewerInput(org.eclipse.ltk.ui.refactoring.ChangePreviewViewerInput) TextEditBasedChange(org.eclipse.ltk.core.refactoring.TextEditBasedChange) Change(org.eclipse.ltk.core.refactoring.Change) IRegion(org.eclipse.jface.text.IRegion) TextEditBasedChangeGroup(org.eclipse.ltk.core.refactoring.TextEditBasedChangeGroup)

Example 13 with IRegion

use of org.eclipse.jface.text.IRegion 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 14 with IRegion

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

the class TaskMarkerProposal method getUpdatedPosition.

private Position getUpdatedPosition(IDocument document) throws BadLocationException {
    IScanner scanner = ToolFactory.createScanner(true, false, false, false);
    scanner.setSource(document.get().toCharArray());
    int token = getSurroundingComment(scanner);
    if (token == ITerminalSymbols.TokenNameEOF) {
        return null;
    }
    int commentStart = scanner.getCurrentTokenStartPosition();
    int commentEnd = scanner.getCurrentTokenEndPosition() + 1;
    int contentStart = commentStart + 2;
    int contentEnd = commentEnd;
    if (token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
        contentStart = commentStart + 3;
        contentEnd = commentEnd - 2;
    } else if (token == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
        contentEnd = commentEnd - 2;
    }
    if (hasContent(document, contentStart, fLocation.getOffset()) || hasContent(document, contentEnd, fLocation.getOffset() + fLocation.getLength())) {
        return new Position(fLocation.getOffset(), fLocation.getLength());
    }
    IRegion startRegion = document.getLineInformationOfOffset(commentStart);
    int start = startRegion.getOffset();
    boolean contentAtBegining = hasContent(document, start, commentStart);
    if (contentAtBegining) {
        start = commentStart;
    }
    int end;
    if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
        if (contentAtBegining) {
            // only to the end of the line
            end = startRegion.getOffset() + startRegion.getLength();
        } else {
            // includes new line
            end = commentEnd;
        }
    } else {
        int endLine = document.getLineOfOffset(commentEnd - 1);
        if (endLine + 1 == document.getNumberOfLines() || contentAtBegining) {
            IRegion endRegion = document.getLineInformation(endLine);
            end = endRegion.getOffset() + endRegion.getLength();
        } else {
            IRegion endRegion = document.getLineInformation(endLine + 1);
            end = endRegion.getOffset();
        }
    }
    if (hasContent(document, commentEnd, end)) {
        end = commentEnd;
        // only remove comment
        start = commentStart;
    }
    return new Position(start, end - start);
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) Position(org.eclipse.jface.text.Position) IRegion(org.eclipse.jface.text.IRegion)

Example 15 with IRegion

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

the class AnonymousTypeCompletionProposal method updateReplacementString.

/*
	 * @see org.eclipse.jdt.internal.ui.text.java.JavaTypeCompletionProposal#updateReplacementString(org.eclipse.jface.text.IDocument, char, int, org.eclipse.jdt.core.dom.rewrite.ImportRewrite)
	 */
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
    fImportRewrite = impRewrite;
    String newBody = createNewBody(impRewrite);
    if (newBody == null)
        return false;
    CompletionProposal coreProposal = ((MemberProposalInfo) getProposalInfo()).fProposal;
    boolean isAnonymousConstructorInvoc = coreProposal.getKind() == CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION;
    //$NON-NLS-1$
    boolean replacementStringEndsWithParentheses = isAnonymousConstructorInvoc || getReplacementString().endsWith(")");
    // construct replacement text: an expression to be formatted
    //$NON-NLS-1$
    StringBuffer buf = new StringBuffer("new A(");
    if (!replacementStringEndsWithParentheses || isAnonymousConstructorInvoc)
        buf.append(')');
    buf.append(newBody);
    // use the code formatter
    String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
    final IJavaProject project = fCompilationUnit.getJavaProject();
    IRegion lineInfo = document.getLineInformationOfOffset(getReplacementOffset());
    int indent = Strings.computeIndentUnits(document.get(lineInfo.getOffset(), lineInfo.getLength()), project);
    Map<String, String> options = project != null ? project.getOptions(true) : JavaCore.getOptions();
    options.put(DefaultCodeFormatterConstants.FORMATTER_INDENT_EMPTY_LINES, DefaultCodeFormatterConstants.TRUE);
    String replacementString = CodeFormatterUtil.format(CodeFormatter.K_EXPRESSION, buf.toString(), 0, lineDelim, options);
    int lineEndOffset = lineInfo.getOffset() + lineInfo.getLength();
    int p = offset;
    char ch = document.getChar(p);
    while (p < lineEndOffset) {
        if (ch == '(' || ch == ')' || ch == ';' || ch == ',')
            break;
        ch = document.getChar(++p);
    }
    if (ch != ';' && ch != ',' && ch != ')')
        replacementString = replacementString + ';';
    replacementString = Strings.changeIndent(replacementString, 0, project, CodeFormatterUtil.createIndentString(indent, project), lineDelim);
    int beginIndex = replacementString.indexOf('(');
    if (!isAnonymousConstructorInvoc)
        beginIndex++;
    replacementString = replacementString.substring(beginIndex);
    int pos = offset;
    if (isAnonymousConstructorInvoc && (insertCompletion() ^ isInsertModeToggled())) {
        // Keep existing code
        int endPos = pos;
        ch = document.getChar(endPos);
        while (endPos < lineEndOffset && ch != '(' && ch != ')' && ch != ';' && ch != ',' && !Character.isWhitespace(ch)) ch = document.getChar(++endPos);
        int keepLength = endPos - pos;
        if (keepLength > 0) {
            String keepStr = document.get(pos, keepLength);
            replacementString = replacementString + keepStr;
            setCursorPosition(replacementString.length() - keepLength);
        }
    } else
        setCursorPosition(replacementString.length());
    setReplacementString(replacementString);
    if (pos < document.getLength() && document.getChar(pos) == ')') {
        int currentLength = getReplacementLength();
        if (replacementStringEndsWithParentheses)
            setReplacementLength(currentLength + pos - offset);
        else
            setReplacementLength(currentLength + pos - offset + 1);
    }
    return false;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) CompletionProposal(org.eclipse.jdt.core.CompletionProposal) StyledString(org.eclipse.jface.viewers.StyledString) IRegion(org.eclipse.jface.text.IRegion)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)668 BadLocationException (org.eclipse.jface.text.BadLocationException)291 Region (org.eclipse.jface.text.Region)265 IDocument (org.eclipse.jface.text.IDocument)143 Test (org.junit.Test)108 Point (org.eclipse.swt.graphics.Point)76 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)57 ITypedRegion (org.eclipse.jface.text.ITypedRegion)55 Position (org.eclipse.jface.text.Position)50 ArrayList (java.util.ArrayList)37 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)35 IFile (org.eclipse.core.resources.IFile)33 ITextSelection (org.eclipse.jface.text.ITextSelection)32 IEditorPart (org.eclipse.ui.IEditorPart)29 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)24 StyledText (org.eclipse.swt.custom.StyledText)22 CoreException (org.eclipse.core.runtime.CoreException)19 List (java.util.List)18 Document (org.eclipse.jface.text.Document)17 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)17