Search in sources :

Example 46 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.

the class StatementAnalyzer method checkSelectedNodes.

protected void checkSelectedNodes() {
    ASTNode[] nodes = getSelectedNodes();
    if (nodes.length == 0) {
        return;
    }
    ASTNode node = nodes[0];
    int selectionOffset = getSelection().getOffset();
    try {
        int start = fScanner.getNextStartOffset(selectionOffset, true);
        if (start == node.getStartPosition()) {
            int lastNodeEnd = ASTNodes.getExclusiveEnd(nodes[nodes.length - 1]);
            int pos = fScanner.getNextStartOffset(lastNodeEnd, true);
            int selectionEnd = getSelection().getInclusiveEnd();
            if (pos <= selectionEnd) {
                IScanner scanner = fScanner.getScanner();
                // see https://bugs.eclipse.org/324237
                char[] token = scanner.getCurrentTokenSource();
                if (start < lastNodeEnd && token.length == 1 && (token[0] == ';' || token[0] == ',')) {
                    setSelection(Selection.createFromStartEnd(start, lastNodeEnd - 1));
                } else {
                    ISourceRange range = new SourceRange(lastNodeEnd, pos - lastNodeEnd);
                    invalidSelection(RefactoringCoreMessages.StatementAnalyzer_end_of_selection, JavaStatusContext.create(fCUnit, range));
                }
            }
            // success
            return;
        }
    } catch (CoreException e) {
    // fall through
    }
    ISourceRange range = new SourceRange(selectionOffset, node.getStartPosition() - selectionOffset + 1);
    invalidSelection(RefactoringCoreMessages.StatementAnalyzer_beginning_of_selection, JavaStatusContext.create(fCUnit, range));
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) CoreException(org.eclipse.core.runtime.CoreException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SourceRange(org.eclipse.jdt.core.SourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 47 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.

the class JDTUtils method toLocation.

/**
 * Creates a location for a given java element.
 * Element can be a {@link ICompilationUnit} or {@link IClassFile}
 *
 * @param element
 * @return location or null
 * @throws JavaModelException
 */
public static Location toLocation(IJavaElement element) throws JavaModelException {
    ICompilationUnit unit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
    IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
    if (unit == null && cf == null) {
        return null;
    }
    if (element instanceof ISourceReference) {
        ISourceRange nameRange = getNameRange(element);
        if (SourceRange.isAvailable(nameRange)) {
            if (cf == null) {
                return toLocation(unit, nameRange.getOffset(), nameRange.getLength());
            } else {
                return toLocation(cf, nameRange.getOffset(), nameRange.getLength());
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IClassFile(org.eclipse.jdt.core.IClassFile) ISourceReference(org.eclipse.jdt.core.ISourceReference) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 48 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project eclipse.jdt.ls by eclipse.

the class JDTUtils method getNameRange.

private static ISourceRange getNameRange(IJavaElement element) throws JavaModelException {
    ISourceRange nameRange = null;
    if (element instanceof IMember) {
        IMember member = (IMember) element;
        nameRange = member.getNameRange();
        if ((!SourceRange.isAvailable(nameRange))) {
            nameRange = member.getSourceRange();
        }
    } else if (element instanceof ITypeParameter || element instanceof ILocalVariable) {
        nameRange = ((ISourceReference) element).getNameRange();
    } else if (element instanceof ISourceReference) {
        nameRange = ((ISourceReference) element).getSourceRange();
    }
    if (!SourceRange.isAvailable(nameRange) && element.getParent() != null) {
        nameRange = getNameRange(element.getParent());
    }
    return nameRange;
}
Also used : ILocalVariable(org.eclipse.jdt.core.ILocalVariable) ITypeParameter(org.eclipse.jdt.core.ITypeParameter) ISourceReference(org.eclipse.jdt.core.ISourceReference) IMember(org.eclipse.jdt.core.IMember) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 49 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project eclipse.platform.runtime by eclipse.

the class FindUnusedSearchResult method unusedElementFound.

@Override
public void unusedElementFound(IMember member) throws CoreException {
    ISourceRange nameRange = member.getNameRange();
    addMatch(new Match(member, nameRange.getOffset(), nameRange.getLength()));
}
Also used : ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 50 with ISourceRange

use of org.eclipse.jdt.core.ISourceRange in project flux by eclipse.

the class NavigationService method computeNavigation.

public JSONObject computeNavigation(String username, String requestorResourcePath, int offset, int length) {
    try {
        ICompilationUnit liveEditUnit = liveEditUnits.getLiveEditUnit(username, requestorResourcePath);
        if (liveEditUnit != null) {
            IJavaElement[] elements = liveEditUnit.codeSelect(offset, length);
            if (elements != null && elements.length > 0) {
                JSONObject result = new JSONObject();
                IJavaElement element = elements[0];
                IResource resource = element.getResource();
                // if the selected element corresponds to a resource in workspace, navigate to it
                if (resource != null && resource.getProject() != null) {
                    String projectName = resource.getProject().getName();
                    String resourcePath = resource.getProjectRelativePath().toString();
                    result.put("project", projectName);
                    result.put("resource", resourcePath);
                    if (element instanceof ISourceReference) {
                        ISourceRange nameRange = ((ISourceReference) element).getNameRange();
                        result.put("offset", nameRange.getOffset());
                        result.put("length", nameRange.getLength());
                    }
                    return result;
                }
                // walk up the java model until we reach a class file
                while (element != null && !(element instanceof IClassFile)) {
                    element = element.getParent();
                }
                if (element instanceof IClassFile) {
                    IClassFile classFile = (IClassFile) element;
                    ISourceRange sourceRange = classFile.getSourceRange();
                    if (sourceRange != null) {
                        String projectName = element.getJavaProject().getProject().getName();
                        String resourcePath = classFile.getParent().getElementName().replace('.', '/');
                        resourcePath = "classpath:/" + resourcePath + "/" + classFile.getElementName();
                        result.put("project", projectName);
                        result.put("resource", resourcePath);
                        return result;
                    }
                }
            // we don't know how to navigate to this element
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ISourceReference(org.eclipse.jdt.core.ISourceReference) IResource(org.eclipse.core.resources.IResource) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Aggregations

ISourceRange (org.eclipse.jdt.core.ISourceRange)53 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)18 IJavaElement (org.eclipse.jdt.core.IJavaElement)14 JavaModelException (org.eclipse.jdt.core.JavaModelException)13 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)10 ISourceReference (org.eclipse.jdt.core.ISourceReference)9 IType (org.eclipse.jdt.core.IType)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)7 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)6 SourceRange (org.eclipse.jdt.core.SourceRange)6 IBuffer (org.eclipse.jdt.core.IBuffer)5 IMethod (org.eclipse.jdt.core.IMethod)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)5 ArrayList (java.util.ArrayList)4 IField (org.eclipse.jdt.core.IField)4 IMember (org.eclipse.jdt.core.IMember)4 CoreException (org.eclipse.core.runtime.CoreException)3 IClassFile (org.eclipse.jdt.core.IClassFile)3