org.eclipse.jdt.core.IOpenable Java Examples

The following examples show how to use org.eclipse.jdt.core.IOpenable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GenerateMethodAbstractAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
static RefactoringStatusContext createRefactoringStatusContext(IJavaElement element) {
	if (element instanceof IMember) {
		return JavaStatusContext.create((IMember) element);
	}
	if (element instanceof ISourceReference) {
		IOpenable openable= element.getOpenable();
		try {
			if (openable instanceof ICompilationUnit) {
				return JavaStatusContext.create((ICompilationUnit) openable, ((ISourceReference) element).getSourceRange());
			} else if (openable instanceof IClassFile) {
				return JavaStatusContext.create((IClassFile) openable, ((ISourceReference) element).getSourceRange());
			}
		} catch (JavaModelException e) {
			// ignore
		}
	}
	return null;
}
 
Example #2
Source File: DeleteResourceElementsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * @see MultiOperation This method delegate to <code>deleteResource</code> or
 * <code>deletePackageFragment</code> depending on the type of <code>element</code>.
 */
protected void processElement(IJavaElement element) throws JavaModelException {
	switch (element.getElementType()) {
		case IJavaElement.CLASS_FILE :
		case IJavaElement.COMPILATION_UNIT :
			deleteResource(element.getResource(), this.force ? IResource.FORCE | IResource.KEEP_HISTORY : IResource.KEEP_HISTORY);
			break;
		case IJavaElement.PACKAGE_FRAGMENT :
			deletePackageFragment((IPackageFragment) element);
			break;
		default :
			throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element));
	}
	// ensure the element is closed
	if (element instanceof IOpenable) {
		((IOpenable)element).close();
	}
}
 
Example #3
Source File: BaseDiagnosticsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings("restriction")
private static Range convertRange(IOpenable openable, IProblem problem) {
	try {
		return JDTUtils.toRange(openable, problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1);
	} catch (CoreException e) {
		// In case failed to open the IOpenable's buffer, use the IProblem's information to calculate the range.
		Position start = new Position();
		Position end = new Position();

		start.setLine(problem.getSourceLineNumber() - 1);// The protocol is 0-based.
		end.setLine(problem.getSourceLineNumber() - 1);
		if (problem instanceof DefaultProblem) {
			DefaultProblem dProblem = (DefaultProblem) problem;
			start.setCharacter(dProblem.getSourceColumnNumber() - 1);
			int offset = 0;
			if (dProblem.getSourceStart() != -1 && dProblem.getSourceEnd() != -1) {
				offset = dProblem.getSourceEnd() - dProblem.getSourceStart() + 1;
			}
			end.setCharacter(dProblem.getSourceColumnNumber() - 1 + offset);
		}
		return new Range(start, end);
	}
}
 
Example #4
Source File: BaseDiagnosticsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static List<Diagnostic> toDiagnosticsArray(IOpenable openable, List<IProblem> problems, boolean isDiagnosticTagSupported) {
	List<Diagnostic> array = new ArrayList<>(problems.size());
	for (IProblem problem : problems) {
		Diagnostic diag = new Diagnostic();
		diag.setSource(JavaLanguageServerPlugin.SERVER_SOURCE_ID);
		diag.setMessage(problem.getMessage());
		diag.setCode(Integer.toString(problem.getID()));
		diag.setSeverity(convertSeverity(problem));
		diag.setRange(convertRange(openable, problem));
		if (isDiagnosticTagSupported) {
			diag.setTags(getDiagnosticTag(problem.getID()));
		}
		array.add(diag);
	}
	return array;
}
 
Example #5
Source File: CallHierarchyHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private List<Range> toCallRanges(Collection<CallLocation> callLocations) {
	List<Range> ranges = new ArrayList<>();
	if (callLocations != null) {
		for (CallLocation location : callLocations) {
			IOpenable openable = location.getMember().getCompilationUnit();
			if (openable == null) {
				openable = location.getMember().getTypeRoot();
			}
			int[] start = JsonRpcHelpers.toLine(openable, location.getStart());
			int[] end = JsonRpcHelpers.toLine(openable, location.getEnd());
			Assert.isNotNull(start, "start");
			Assert.isNotNull(end, "end");
			// Assert.isLegal(start[0] == end[0], "Expected equal start and end lines. Start was: " + Arrays.toString(start) + " End was:" + Arrays.toString(end));
			Range callRange = new Range(new Position(start[0], start[1]), new Position(end[0], end[1]));
			ranges.add(callRange);
		}
	}

	return ranges;
}
 
Example #6
Source File: JsonRpcHelpers.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static <T> T convert(IOpenable openable, Function<IDocument, T> consumer) throws JavaModelException {
	Assert.isNotNull(openable, "openable");
	boolean mustClose = false;
	try {
		if (!openable.isOpen()) {
			openable.open(new NullProgressMonitor());
			mustClose = openable.isOpen();
		}
		IBuffer buffer = openable.getBuffer();
		return consumer.apply(toDocument(buffer));
	} finally {
		if (mustClose) {
			try {
				openable.close();
			} catch (JavaModelException e) {
				JavaLanguageServerPlugin.logException("Error when closing openable: " + openable, e);
			}
		}
	}
}
 
Example #7
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates a range for the given offset and length for an {@link IOpenable}
 *
 * @param openable
 * @param offset
 * @param length
 * @return
 * @throws JavaModelException
 */
public static Range toRange(IOpenable openable, int offset, int length) throws JavaModelException{
	Range range = newRange();
	if (offset > 0 || length > 0) {
		int[] loc = null;
		int[] endLoc = null;
		IBuffer buffer = openable.getBuffer();
		if (buffer != null) {
			loc = JsonRpcHelpers.toLine(buffer, offset);
			endLoc = JsonRpcHelpers.toLine(buffer, offset + length);
		}
		if (loc == null) {
			loc = new int[2];
		}
		if (endLoc == null) {
			endLoc = new int[2];
		}
		setPosition(range.getStart(), loc);
		setPosition(range.getEnd(), endLoc);
	}
	return range;
}
 
Example #8
Source File: JarPackageWizard.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void addJavaElement(List<Object> selectedElements, IJavaElement je) {
	if (je.getElementType() == IJavaElement.COMPILATION_UNIT)
		selectedElements.add(je);
	else if (je.getElementType() == IJavaElement.CLASS_FILE)
		selectedElements.add(je);
	else if (je.getElementType() == IJavaElement.JAVA_PROJECT)
		selectedElements.add(je);
	else if (je.getElementType() == IJavaElement.PACKAGE_FRAGMENT) {
		if (!isInArchiveOrExternal(je))
			selectedElements.add(je);
	} else if (je.getElementType() == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
		if (!isInArchiveOrExternal(je))
			selectedElements.add(je);
	} else {
		IOpenable openable= je.getOpenable();
		if (openable instanceof ICompilationUnit)
			selectedElements.add(((ICompilationUnit) openable).getPrimary());
		else if (openable instanceof IClassFile && !isInArchiveOrExternal(je))
			selectedElements.add(openable);
	}
}
 
Example #9
Source File: LapseView.java    From lapse-plus with GNU General Public License v3.0 6 votes vote down vote up
private CompilationUnit internalSetInput(IOpenable input) throws CoreException {
	IBuffer buffer = input.getBuffer();
	if (buffer == null) {
		JavaPlugin.logErrorMessage("Input has no buffer"); //$NON-NLS-1$
	}
	if (input instanceof ICompilationUnit) {
		fParser.setSource((ICompilationUnit) input);
	} else {
		fParser.setSource((IClassFile) input);
	}

	try {
		CompilationUnit root = (CompilationUnit) fParser.createAST(null);
		log("Recomputed the AST for " + buffer.getUnderlyingResource().getName());
						
		if (root == null) {
			JavaPlugin.logErrorMessage("Could not create AST"); //$NON-NLS-1$
		}

		return root;
	} catch (RuntimeException e) {
		JavaPlugin.logErrorMessage("Could not create AST:\n" + e.getMessage()); //$NON-NLS-1$
		return null;
	}
}
 
Example #10
Source File: LapseView.java    From lapse-plus with GNU General Public License v3.0 6 votes vote down vote up
public void setInput(ITextEditor editor) throws CoreException {
	if (fEditor != null) {
		uninstallModificationListener();
	}

	fEditor = null;
	fRoot   = null;
	if(editor == null) {
		logError("editor is set to null");
		return;
	}

	IOpenable openable = getJavaInput(editor);
	if (openable == null) {
		throw new RuntimeException("Editor not showing a classfile");
	}
	// reset the fields
	fOpenable = openable;
	fRoot 	  = internalSetInput(openable);
	fEditor	  = editor;
	
	installModificationListener();
}
 
Example #11
Source File: PackageExplorerActionGroup.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
void handleDoubleClick(DoubleClickEvent event) {
	TreeViewer viewer= fPart.getTreeViewer();
	IStructuredSelection selection= (IStructuredSelection)event.getSelection();
	Object element= selection.getFirstElement();
	if (viewer.isExpandable(element)) {
		if (doubleClickGoesInto()) {
			// don't zoom into compilation units and class files
			if (element instanceof ICompilationUnit || element instanceof IClassFile)
				return;
			if (element instanceof IOpenable || element instanceof IContainer || element instanceof IWorkingSet) {
				fZoomInAction.run();
			}
		} else {
			IAction openAction= fNavigateActionGroup.getOpenAction();
			if (openAction != null && openAction.isEnabled() && OpenStrategy.getOpenMethod() == OpenStrategy.DOUBLE_CLICK)
				return;
			if (selection instanceof ITreeSelection) {
				TreePath[] paths= ((ITreeSelection)selection).getPathsFor(element);
				for (int i= 0; i < paths.length; i++) {
					viewer.setExpandedState(paths[i], !viewer.getExpandedState(paths[i]));
				}
			} else {
				viewer.setExpandedState(element, !viewer.getExpandedState(element));
			}
		}
	} else if (element instanceof IProject && !((IProject) element).isOpen()) {
		OpenProjectAction openProjectAction= fProjectActionGroup.getOpenProjectAction();
		if (openProjectAction.isEnabled()) {
			openProjectAction.run();
		}
	}
}
 
Example #12
Source File: CustomBufferFactory.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public IBuffer createBuffer(IOpenable owner) {
	if (owner instanceof ICompilationUnit) {
		ICompilationUnit unit= (ICompilationUnit) owner;
		ICompilationUnit original= unit.getPrimary();
		IResource resource= original.getResource();
		if (resource instanceof IFile) {
			return new DocumentAdapter(unit, (IFile) resource);
		}

	}
	return DocumentAdapter.NULL;
}
 
Example #13
Source File: DocumentAdapter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Constructs a new document adapter.
 *
 * @param owner the owner of this buffer
 * @param file the <code>IFile</code> that backs the buffer
 */
public DocumentAdapter(IOpenable owner, IFile file) {
	fOwner= owner;
	fFile= file;
	fPath= fFile.getFullPath();
	fLocationKind= LocationKind.IFILE;

	initialize();
}
 
Example #14
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Evaluates the indentation used by a Java element. (in tabulators)
 * 
 * @param elem the element to get the indent of
 * @return return the indent unit
 * @throws JavaModelException thrown if the element could not be accessed
 */
public static int getIndentUsed(IJavaElement elem) throws JavaModelException {
	IOpenable openable= elem.getOpenable();
	if (openable instanceof ITypeRoot) {
		IBuffer buf= openable.getBuffer();
		if (buf != null) {
			int offset= ((ISourceReference)elem).getSourceRange().getOffset();
			return getIndentUsed(buf, offset, elem.getJavaProject());
		}
	}
	return 0;
}
 
Example #15
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param elem a Java element (doesn't have to exist)
 * @return the existing or default line delimiter for the element
 */
public static String getLineDelimiterUsed(IJavaElement elem) {
	IOpenable openable= elem.getOpenable();
	if (openable instanceof ITypeRoot) {
		try {
			return openable.findRecommendedLineSeparator();
		} catch (JavaModelException exception) {
			// Use project setting
		}
	}
	IJavaProject project= elem.getJavaProject();
	return getProjectLineDelimiter(project.exists() ? project : null);
}
 
Example #16
Source File: JavaDocLocations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the reason for why the Javadoc of the Java element could not be retrieved.
 * 
 * @param element whose Javadoc could not be retrieved
 * @param root the root of the Java element
 * @return the String message for why the Javadoc could not be retrieved for the Java element or
 *         <code>null</code> if the Java element is from a source container
 * @since 3.9
 */
public static String getExplanationForMissingJavadoc(IJavaElement element, IPackageFragmentRoot root) {
	String message= null;
	try {
		boolean isBinary= (root.exists() && root.getKind() == IPackageFragmentRoot.K_BINARY);
		if (isBinary) {
			boolean hasAttachedJavadoc= JavaDocLocations.getJavadocBaseLocation(element) != null;
			boolean hasAttachedSource= root.getSourceAttachmentPath() != null;
			IOpenable openable= element.getOpenable();
			boolean hasSource= openable.getBuffer() != null;

			// Provide hint why there's no Java doc
			if (!hasAttachedSource && !hasAttachedJavadoc)
				message= CorextMessages.JavaDocLocations_noAttachments;
			else if (!hasAttachedJavadoc && !hasSource)
				message= CorextMessages.JavaDocLocations_noAttachedJavadoc;
			else if (!hasAttachedSource)
				message= CorextMessages.JavaDocLocations_noAttachedSource;
			else if (!hasSource)
				message= CorextMessages.JavaDocLocations_noInformation;

		}
	} catch (JavaModelException e) {
		message= CorextMessages.JavaDocLocations_error_gettingJavadoc;
		JavaPlugin.log(e);
	}
	return message;
}
 
Example #17
Source File: CallLocation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the IBuffer for the IMember represented by this CallLocation.
 *
 * @return IBuffer for the IMember or null if the member doesn't have a buffer (for
 *          example if it is a binary file without source attachment).
 */
private IBuffer getBufferForMember() {
    IBuffer buffer = null;
    try {
        IOpenable openable = fMember.getOpenable();
        if (openable != null && fMember.exists()) {
            buffer = openable.getBuffer();
        }
    } catch (JavaModelException e) {
        JavaPlugin.log(e);
    }
    return buffer;
}
 
Example #18
Source File: SelectionEngine.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Object findMethodWithAttachedDocInHierarchy(final MethodBinding method) throws JavaModelException {
	ReferenceBinding type= method.declaringClass;
	final SelectionRequestor requestor1 = (SelectionRequestor) this.requestor;
	return new InheritDocVisitor() {
		public Object visit(ReferenceBinding currType) throws JavaModelException {
			MethodBinding overridden =  findOverriddenMethodInType(currType, method);
			if (overridden == null)
				return InheritDocVisitor.CONTINUE;
			TypeBinding args[] = overridden.parameters;
			String names[] = new String[args.length];
			for (int i = 0; i < args.length; i++) {
				names[i] = Signature.createTypeSignature(args[i].sourceName(), false);
			}
			IMember member = (IMember) requestor1.findMethodFromBinding(overridden, names, overridden.declaringClass);
			if (member == null)
				return InheritDocVisitor.CONTINUE;
			if (member.getAttachedJavadoc(null) != null ) {  
				// for binary methods with attached javadoc and no source attached
				return overridden;
			}
			IOpenable openable = member.getOpenable();
			if (openable == null)
				return InheritDocVisitor.CONTINUE;
			IBuffer buf= openable.getBuffer();
			if (buf == null) {
				// no source attachment found. This method maybe the one. Stop.
				return InheritDocVisitor.STOP_BRANCH;
			}

			ISourceRange javadocRange= member.getJavadocRange();
			if (javadocRange == null)
				return InheritDocVisitor.CONTINUE;	// this method doesn't have javadoc, continue to look.
			String rawJavadoc= buf.getText(javadocRange.getOffset(), javadocRange.getLength());
			if (rawJavadoc != null) {
				return overridden;
			}
			return InheritDocVisitor.CONTINUE;
		}
	}.visitInheritDoc(type);
}
 
Example #19
Source File: DocumentAdapter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Constructs a new document adapter.
 *
 * @param owner the owner of this buffer
 * @param path the path of the file that backs the buffer
 * @since 3.2
 */
public DocumentAdapter(IOpenable owner, IPath path) {
	Assert.isLegal(path != null);
	fOwner= owner;
	fPath= path;
	fLocationKind= LocationKind.NORMALIZE;

	initialize();
}
 
Example #20
Source File: ResourceUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static IResource getResource(IJavaElement element){
	if (element.getElementType() == IJavaElement.COMPILATION_UNIT)
		return ((ICompilationUnit) element).getResource();
	else if (element instanceof IOpenable)
		return element.getResource();
	else
		return null;
}
 
Example #21
Source File: DocumentAdapter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Constructs a new document adapter.
 * 
 * @param owner the owner of this buffer
 * @param fileStore the file store of the file that backs the buffer
 * @param path the path of the file that backs the buffer
 * @since 3.6
 */
public DocumentAdapter(IOpenable owner, IFileStore fileStore, IPath path) {
	Assert.isLegal(fileStore != null);
	Assert.isLegal(path != null);
	fOwner= owner;
	fFileStore= fileStore;
	fPath= path;
	fLocationKind= LocationKind.NORMALIZE;

	initialize();
}
 
Example #22
Source File: MarkerUtil.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SuppressWarnings("restriction")
private static char[] getContent(IType source) throws JavaModelException {
    char[] charContent = null;
    IOpenable op = source.getOpenable();
    if (op instanceof CompilationUnit) {
        charContent = ((CompilationUnit) (op)).getContents();
    }
    if (charContent == null) {
        String content = source.getSource();
        if (content != null) {
            charContent = content.toCharArray();
        }
    }
    return charContent;
}
 
Example #23
Source File: BufferManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IBuffer createBuffer(IOpenable owner) {
	JavaElement element = (JavaElement) owner;
	IResource resource = element.resource();
	return
		new Buffer(
			resource instanceof IFile ? (IFile)resource : null,
			owner,
			element.isReadOnly());
}
 
Example #24
Source File: JsonRpcHelpers.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Convert line, column to a document offset.
 *
 * @param openable
 * @param line
 * @param column
 * @return
 */
public static int toOffset(IOpenable openable, int line, int column) {
	if (openable != null) {
		try {
			return convert(openable, (IDocument document) -> toOffset(document, line, column));
		} catch (JavaModelException e) {
			JavaLanguageServerPlugin.log(e);
		}
	}

	return -1;
}
 
Example #25
Source File: ResourceUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static IResource getResource(IJavaElement element) {
	if (element.getElementType() == IJavaElement.COMPILATION_UNIT) {
		return ((ICompilationUnit) element).getResource();
	} else if (element instanceof IOpenable) {
		return element.getResource();
	} else {
		return null;
	}
}
 
Example #26
Source File: BufferManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IBuffer createNullBuffer(IOpenable owner) {
	JavaElement element = (JavaElement) owner;
	IResource resource = element.resource();
	return
		new NullBuffer(
			resource instanceof IFile ? (IFile)resource : null,
			owner,
			element.isReadOnly());
}
 
Example #27
Source File: DocumentAdapter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public DocumentAdapter(IOpenable owner, IFile file) {
	fOwner = owner;
	fFile = file;
	fBufferListeners = new ArrayList<>(3);
	fIsClosed = false;

	ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
	try {
		manager.connect(file.getFullPath(), LocationKind.IFILE, null);
		fTextFileBuffer= manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
	} catch (CoreException e) {
	}
}
 
Example #28
Source File: LapseView.java    From lapse-plus with GNU General Public License v3.0 5 votes vote down vote up
public static IOpenable getJavaInput(IEditorPart part) {
    IEditorInput editorInput= part.getEditorInput();
    if (editorInput != null) {
      IJavaElement input= (IJavaElement)editorInput.getAdapter(IJavaElement.class);
      if (input instanceof IOpenable) {
        return (IOpenable) input;
      }
    }
    return null;  
}
 
Example #29
Source File: BufferManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
    * @deprecated
    */
public IBuffer createBuffer(IOpenable owner) {
	return BufferManager.createBuffer(owner);
}
 
Example #30
Source File: BufferManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the open buffer associated with the given owner,
 * or <code>null</code> if the owner does not have an open
 * buffer associated with it.
 */
public IBuffer getBuffer(IOpenable owner) {
	synchronized (this.openBuffers) {
		return (IBuffer)this.openBuffers.get(owner);
	}
}