Search in sources :

Example 1 with IRegion

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

the class StubUtility method fixEmptyVariables.

// remove lines for empty variables
private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException {
    IDocument doc = new Document(buffer.getString());
    int nLines = doc.getNumberOfLines();
    MultiTextEdit edit = new MultiTextEdit();
    HashSet<Integer> removedLines = new HashSet<Integer>();
    for (int i = 0; i < variables.length; i++) {
        // look if Javadoc tags have to be added
        TemplateVariable position = findVariable(buffer, variables[i]);
        if (position == null || position.getLength() > 0) {
            continue;
        }
        int[] offsets = position.getOffsets();
        for (int k = 0; k < offsets.length; k++) {
            int line = doc.getLineOfOffset(offsets[k]);
            IRegion lineInfo = doc.getLineInformation(line);
            int offset = lineInfo.getOffset();
            String str = doc.get(offset, lineInfo.getLength());
            if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) {
                int nextStart = doc.getLineOffset(line + 1);
                edit.addChild(new DeleteEdit(offset, nextStart - offset));
            }
        }
    }
    edit.apply(doc, 0);
    return doc.get();
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DeleteEdit(org.eclipse.text.edits.DeleteEdit) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IRegion(org.eclipse.jface.text.IRegion) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with IRegion

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

the class StubUtility method insertTag.

private static void insertTag(IDocument textBuffer, int offset, int length, String[] paramNames, String[] exceptionNames, String returnType, String[] typeParameterNames, boolean isDeprecated, String lineDelimiter) throws BadLocationException {
    IRegion region = textBuffer.getLineInformationOfOffset(offset);
    if (region == null) {
        return;
    }
    String lineStart = textBuffer.get(region.getOffset(), offset - region.getOffset());
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < typeParameterNames.length; i++) {
        if (buf.length() > 0) {
            buf.append(lineDelimiter).append(lineStart);
        }
        //$NON-NLS-1$
        buf.append("@param <").append(typeParameterNames[i]).append('>');
    }
    for (int i = 0; i < paramNames.length; i++) {
        if (buf.length() > 0) {
            buf.append(lineDelimiter).append(lineStart);
        }
        //$NON-NLS-1$
        buf.append("@param ").append(paramNames[i]);
    }
    if (returnType != null && !returnType.equals("void")) {
        //$NON-NLS-1$
        if (buf.length() > 0) {
            buf.append(lineDelimiter).append(lineStart);
        }
        //$NON-NLS-1$
        buf.append("@return");
    }
    if (exceptionNames != null) {
        for (int i = 0; i < exceptionNames.length; i++) {
            if (buf.length() > 0) {
                buf.append(lineDelimiter).append(lineStart);
            }
            //$NON-NLS-1$
            buf.append("@throws ").append(exceptionNames[i]);
        }
    }
    if (isDeprecated) {
        if (buf.length() > 0) {
            buf.append(lineDelimiter).append(lineStart);
        }
        //$NON-NLS-1$
        buf.append("@deprecated");
    }
    if (buf.length() == 0 && isAllCommentWhitespace(lineStart)) {
        int prevLine = textBuffer.getLineOfOffset(offset) - 1;
        if (prevLine > 0) {
            IRegion prevRegion = textBuffer.getLineInformation(prevLine);
            int prevLineEnd = prevRegion.getOffset() + prevRegion.getLength();
            // clear full line
            //$NON-NLS-1$
            textBuffer.replace(prevLineEnd, offset + length - prevLineEnd, "");
            return;
        }
    }
    textBuffer.replace(offset, length, buf.toString());
}
Also used : IRegion(org.eclipse.jface.text.IRegion)

Example 3 with IRegion

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

the class RenameAnalyzeUtil method existsInNewOccurrences.

private static boolean existsInNewOccurrences(SearchMatch searchResult, SearchResultGroup[] newOccurrences, TextChangeManager manager) {
    SearchResultGroup newGroup = findOccurrenceGroup(searchResult.getResource(), newOccurrences);
    if (newGroup == null)
        return false;
    IRegion oldEditRange = getCorrespondingEditChangeRange(searchResult, manager);
    if (oldEditRange == null)
        return false;
    SearchMatch[] newSearchResults = newGroup.getSearchResults();
    int oldRangeOffset = oldEditRange.getOffset();
    for (int i = 0; i < newSearchResults.length; i++) {
        if (newSearchResults[i].getOffset() == oldRangeOffset)
            return true;
    }
    return false;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IRegion(org.eclipse.jface.text.IRegion)

Example 4 with IRegion

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

the class RenameAnalyzeUtil method analyzeLocalRenames.

/**
	 * This method analyzes a set of local variable renames inside one cu. It checks whether
	 * any new compile errors have been introduced by the rename(s) and whether the correct
	 * node(s) has/have been renamed.
	 *
	 * @param analyzePackages the LocalAnalyzePackages containing the information about the local renames
	 * @param cuChange the TextChange containing all local variable changes to be applied.
	 * @param oldCUNode the fully (incl. bindings) resolved AST node of the original compilation unit
	 * @param recovery whether statements and bindings recovery should be performed when parsing the changed CU
	 * @return a RefactoringStatus containing errors if compile errors or wrongly renamed nodes are found
	 * @throws CoreException thrown if there was an error greating the preview content of the change
	 */
public static RefactoringStatus analyzeLocalRenames(LocalAnalyzePackage[] analyzePackages, TextChange cuChange, CompilationUnit oldCUNode, boolean recovery) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    ICompilationUnit compilationUnit = (ICompilationUnit) oldCUNode.getJavaElement();
    String newCuSource = cuChange.getPreviewContent(new NullProgressMonitor());
    CompilationUnit newCUNode = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(newCuSource, compilationUnit, true, recovery, null);
    result.merge(analyzeCompileErrors(newCuSource, newCUNode, oldCUNode));
    if (result.hasError())
        return result;
    for (int i = 0; i < analyzePackages.length; i++) {
        ASTNode enclosing = getEnclosingBlockOrMethodOrLambda(analyzePackages[i].fDeclarationEdit, cuChange, newCUNode);
        // get new declaration
        IRegion newRegion = RefactoringAnalyzeUtil.getNewTextRange(analyzePackages[i].fDeclarationEdit, cuChange);
        ASTNode newDeclaration = NodeFinder.perform(newCUNode, newRegion.getOffset(), newRegion.getLength());
        Assert.isTrue(newDeclaration instanceof Name);
        VariableDeclaration declaration = getVariableDeclaration((Name) newDeclaration);
        Assert.isNotNull(declaration);
        SimpleName[] problemNodes = ProblemNodeFinder.getProblemNodes(enclosing, declaration, analyzePackages[i].fOccurenceEdits, cuChange);
        result.merge(RefactoringAnalyzeUtil.reportProblemNodes(newCuSource, problemNodes));
    }
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SimpleName(org.eclipse.jdt.core.dom.SimpleName) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IRegion(org.eclipse.jface.text.IRegion) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration)

Example 5 with IRegion

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

the class JavaWordFinder method findWord.

public static IRegion findWord(IDocument document, int offset) {
    int start = -2;
    int end = -1;
    try {
        int pos = offset;
        char c;
        while (pos >= 0) {
            c = document.getChar(pos);
            if (!Character.isJavaIdentifierPart(c)) {
                // Check for surrogates
                if (isSurrogate(c)) {
                /*
						 * XXX: Here we should create the code point and test whether
						 * it is a Java identifier part. Currently this is not possible
						 * because java.lang.Character in 1.4 does not support surrogates
						 * and because com.ibm.icu.lang.UCharacter.isJavaIdentifierPart(int)
						 * is not correctly implemented.
						 */
                } else {
                    break;
                }
            }
            --pos;
        }
        start = pos;
        pos = offset;
        int length = document.getLength();
        while (pos < length) {
            c = document.getChar(pos);
            if (!Character.isJavaIdentifierPart(c)) {
                if (isSurrogate(c)) {
                /*
						 * XXX: Here we should create the code point and test whether
						 * it is a Java identifier part. Currently this is not possible
						 * because java.lang.Character in 1.4 does not support surrogates
						 * and because com.ibm.icu.lang.UCharacter.isJavaIdentifierPart(int)
						 * is not correctly implemented.
						 */
                } else {
                    break;
                }
            }
            ++pos;
        }
        end = pos;
    } catch (BadLocationException x) {
    }
    if (start >= -1 && end > -1) {
        if (start == offset && end == offset) {
            try {
                char c = document.getChar(offset);
                switch(c) {
                    case '-':
                        if (document.getChar(offset + 1) == '>') {
                            return new Region(offset, 2);
                        }
                        break;
                    case '>':
                        if (document.getChar(offset - 1) == '-') {
                            return new Region(offset - 1, 2);
                        }
                        break;
                    case ':':
                        if (document.getChar(offset + 1) == ':') {
                            return new Region(offset, 2);
                        } else if (document.getChar(offset - 1) == ':') {
                            return new Region(offset - 1, 2);
                        }
                        break;
                }
            } catch (BadLocationException e) {
            }
            return new Region(offset, 0);
        } else if (start == offset) {
            //XXX: probably unused...
            return new Region(start, end - start);
        } else {
            return new Region(start + 1, end - start - 1);
        }
    }
    return null;
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)671 BadLocationException (org.eclipse.jface.text.BadLocationException)291 Region (org.eclipse.jface.text.Region)268 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)56 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