Java Code Examples for org.eclipse.jdt.core.IBuffer#getText()

The following examples show how to use org.eclipse.jdt.core.IBuffer#getText() . 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: CallLocation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initCallTextAndLineNumber() {
	if (fCallText != null)
		return;

    IBuffer buffer= getBufferForMember();
    if (buffer == null || buffer.getLength() < fEnd) { //binary, without source attachment || buffer contents out of sync (bug 121900)
    	fCallText= ""; //$NON-NLS-1$
    	fLineNumber= UNKNOWN_LINE_NUMBER;
    	return;
    }

    fCallText= buffer.getText(fStart, (fEnd - fStart));

    if (fLineNumber == UNKNOWN_LINE_NUMBER) {
        Document document= new Document(buffer.getContents());
        try {
            fLineNumber= document.getLineOfOffset(fStart) + 1;
        } catch (BadLocationException e) {
            JavaPlugin.log(e);
        }
    }
}
 
Example 2
Source File: ASTNodes.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the source of the given node from the location where it was parsed.
 * @param node the node to get the source from
 * @param extendedRange if set, the extended ranges of the nodes should ne used
 * @param removeIndent if set, the indentation is removed.
 * @return return the source for the given node or null if accessing the source failed.
 */
public static String getNodeSource(ASTNode node, boolean extendedRange, boolean removeIndent) {
	ASTNode root= node.getRoot();
	if (root instanceof CompilationUnit) {
		CompilationUnit astRoot= (CompilationUnit) root;
		ITypeRoot typeRoot= astRoot.getTypeRoot();
		try {
			if (typeRoot != null && typeRoot.getBuffer() != null) {
				IBuffer buffer= typeRoot.getBuffer();
				int offset= extendedRange ? astRoot.getExtendedStartPosition(node) : node.getStartPosition();
				int length= extendedRange ? astRoot.getExtendedLength(node) : node.getLength();
				String str= buffer.getText(offset, length);
				if (removeIndent) {
					IJavaProject project= typeRoot.getJavaProject();
					int indent= StubUtility.getIndentUsed(buffer, node.getStartPosition(), project);
					str= Strings.changeIndent(str, indent, project, new String(), typeRoot.findRecommendedLineSeparator());
				}
				return str;
			}
		} catch (JavaModelException e) {
			// ignore
		}
	}
	return null;
}
 
Example 3
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static String getHTMLContentFromSource(IMember member) throws JavaModelException {
	IBuffer buf= member.getOpenable().getBuffer();
	if (buf == null) {
		return null; // no source attachment found
	}

	ISourceRange javadocRange= member.getJavadocRange();
	if (javadocRange == null) {
		if (canInheritJavadoc(member)) {
			// Try to use the inheritDoc algorithm.
			String inheritedJavadoc= javadoc2HTML(member, "/***/"); //$NON-NLS-1$
			if (inheritedJavadoc != null && inheritedJavadoc.length() > 0) {
				return inheritedJavadoc;
			}
		}
		return getJavaFxPropertyDoc(member);
	}

	String rawJavadoc= buf.getText(javadocRange.getOffset(), javadocRange.getLength());
	return javadoc2HTML(member, rawJavadoc);
}
 
Example 4
Source File: MoveCuUpdateCreator.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
	if (filterMatch(match)) {
		return;
	}

	/*
	 * Processing is done in collector to reuse the buffer which was
	 * already required by the search engine to locate the matches.
	 */
	// [start, end[ include qualification.
	IJavaElement element = SearchUtils.getEnclosingJavaElement(match);
	int accuracy = match.getAccuracy();
	int start = match.getOffset();
	int length = match.getLength();
	boolean insideDocComment = match.isInsideDocComment();
	IResource res = match.getResource();
	if (element.getAncestor(IJavaElement.IMPORT_DECLARATION) != null) {
		collectMatch(TypeReference.createImportReference(element, accuracy, start, length, insideDocComment, res));
	} else {
		ICompilationUnit unit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
		if (unit != null) {
			IBuffer buffer = unit.getBuffer();
			String matchText = buffer.getText(start, length);
			if (fSource.isDefaultPackage()) {
				collectMatch(TypeReference.createSimpleReference(element, accuracy, start, length, insideDocComment, res, matchText));
			} else {
				// assert: matchText doesn't start nor end with comment
				int simpleNameStart = getLastSimpleNameStart(matchText);
				if (simpleNameStart != 0) {
					collectMatch(TypeReference.createQualifiedReference(element, accuracy, start, length, insideDocComment, res, start + simpleNameStart));
				} else {
					collectMatch(TypeReference.createSimpleReference(element, accuracy, start, length, insideDocComment, res, matchText));
				}
			}
		}
	}
}
 
Example 5
Source File: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param method
 *            the method
 * @return the Javadoc content access for the given method, or <code>null</code>
 *         if no Javadoc could be found in source
 * @throws JavaModelException
 *             unexpected problem
 */
private JavadocContentAccess2 getJavadocContentAccess(IMethod method) throws JavaModelException {
	Object cached = fContentAccesses.get(method);
	if (cached != null) {
		return (JavadocContentAccess2) cached;
	}
	if (fContentAccesses.containsKey(method)) {
		return null;
	}

	IBuffer buf = method.getOpenable().getBuffer();
	if (buf == null) { // no source attachment found
		fContentAccesses.put(method, null);
		return null;
	}

	ISourceRange javadocRange = method.getJavadocRange();
	if (javadocRange == null) {
		fContentAccesses.put(method, null);
		return null;
	}

	String rawJavadoc = buf.getText(javadocRange.getOffset(), javadocRange.getLength());
	Javadoc javadoc = getJavadocNode(method, rawJavadoc);
	if (javadoc == null) {
		fContentAccesses.put(method, null);
		return null;
	}

	JavadocContentAccess2 contentAccess = new JavadocContentAccess2(method, javadoc, rawJavadoc, this);
	fContentAccesses.put(method, contentAccess);
	return contentAccess;
}
 
Example 6
Source File: MoveCuUpdateCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
	if (filterMatch(match))
		return;

	/*
	 * Processing is done in collector to reuse the buffer which was
	 * already required by the search engine to locate the matches.
	 */
	// [start, end[ include qualification.
	IJavaElement element= SearchUtils.getEnclosingJavaElement(match);
	int accuracy= match.getAccuracy();
	int start= match.getOffset();
	int length= match.getLength();
	boolean insideDocComment= match.isInsideDocComment();
	IResource res= match.getResource();
	if (element.getAncestor(IJavaElement.IMPORT_DECLARATION) != null) {
		collectMatch(TypeReference.createImportReference(element, accuracy, start, length, insideDocComment, res));
	} else {
		ICompilationUnit unit= (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
		if (unit != null) {
			IBuffer buffer= unit.getBuffer();
			String matchText= buffer.getText(start, length);
			if (fSource.isDefaultPackage()) {
				collectMatch(TypeReference.createSimpleReference(element, accuracy, start, length, insideDocComment, res, matchText));
			} else {
				// assert: matchText doesn't start nor end with comment
				int simpleNameStart= getLastSimpleNameStart(matchText);
				if (simpleNameStart != 0) {
					collectMatch(TypeReference.createQualifiedReference(element, accuracy, start, length, insideDocComment, res, start + simpleNameStart));
				} else {
					collectMatch(TypeReference.createSimpleReference(element, accuracy, start, length, insideDocComment, res, matchText));
				}
			}
		}
	}
}
 
Example 7
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param method the method
 * @return the Javadoc content access for the given method, or
 * 		<code>null</code> if no Javadoc could be found in source
 * @throws JavaModelException unexpected problem
 */
private JavadocContentAccess2 getJavadocContentAccess(IMethod method) throws JavaModelException {
	Object cached= fContentAccesses.get(method);
	if (cached != null)
		return (JavadocContentAccess2) cached;
	if (fContentAccesses.containsKey(method))
		return null;

	IBuffer buf= method.getOpenable().getBuffer();
	if (buf == null) { // no source attachment found
		fContentAccesses.put(method, null);
		return null;
	}

	ISourceRange javadocRange= method.getJavadocRange();
	if (javadocRange == null) {
		fContentAccesses.put(method, null);
		return null;
	}

	String rawJavadoc= buf.getText(javadocRange.getOffset(), javadocRange.getLength());
	Javadoc javadoc= getJavadocNode(method, rawJavadoc);
	if (javadoc == null) {
		fContentAccesses.put(method, null);
		return null;
	}

	JavadocContentAccess2 contentAccess= new JavadocContentAccess2(method, javadoc, rawJavadoc, this);
	fContentAccesses.put(method, contentAccess);
	return contentAccess;
}
 
Example 8
Source File: SelectionAwareSourceRangeComputer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public SelectionAwareSourceRangeComputer(ASTNode[] selectedNodes, IBuffer buffer, int selectionStart, int selectionLength) {
	fSelectedNodes = selectedNodes;
	fSelectionStart = selectionStart;
	fSelectionLength = selectionLength;
	fDocumentPortionToScan = buffer.getText(fSelectionStart, fSelectionLength);
}
 
Example 9
Source File: SelectionAwareSourceRangeComputer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public SelectionAwareSourceRangeComputer(ASTNode[] selectedNodes, IBuffer buffer, int selectionStart, int selectionLength) {
	fSelectedNodes= selectedNodes;
	fSelectionStart= selectionStart;
	fSelectionLength= selectionLength;
	fDocumentPortionToScan= buffer.getText(fSelectionStart, fSelectionLength);
}
 
Example 10
Source File: NewPackageWizardPage.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void createPackageInfoJava(IProgressMonitor monitor) throws CoreException {
	String lineDelimiter= StubUtility.getLineDelimiterUsed(fCreatedPackageFragment.getJavaProject());
	StringBuilder content = new StringBuilder();
	String fileComment= getFileComment(lineDelimiter);
	String typeComment= getTypeComment(lineDelimiter);
	
	if (fileComment != null) {
		content.append(fileComment);
		content.append(lineDelimiter);
	}

	if (typeComment != null) {
		content.append(typeComment);
		content.append(lineDelimiter);
	} else if (fileComment != null) {
		// insert an empty file comment to avoid that the file comment becomes the type comment
		content.append("/**");  //$NON-NLS-1$
		content.append(lineDelimiter);
		content.append(" *"); //$NON-NLS-1$
		content.append(lineDelimiter);
		content.append(" */"); //$NON-NLS-1$
		content.append(lineDelimiter);
	}

	content.append("package "); //$NON-NLS-1$
	content.append(fCreatedPackageFragment.getElementName());
	content.append(";"); //$NON-NLS-1$

	ICompilationUnit compilationUnit= fCreatedPackageFragment.createCompilationUnit(PACKAGE_INFO_JAVA_FILENAME, content.toString(), true, monitor);

	JavaModelUtil.reconcile(compilationUnit);

	compilationUnit.becomeWorkingCopy(monitor);
	try {
		IBuffer buffer= compilationUnit.getBuffer();
		ISourceRange sourceRange= compilationUnit.getSourceRange();
		String originalContent= buffer.getText(sourceRange.getOffset(), sourceRange.getLength());

		String formattedContent= CodeFormatterUtil.format(CodeFormatter.K_COMPILATION_UNIT, originalContent, 0, lineDelimiter, fCreatedPackageFragment.getJavaProject());
		formattedContent= Strings.trimLeadingTabsAndSpaces(formattedContent);
		buffer.replace(sourceRange.getOffset(), sourceRange.getLength(), formattedContent);
		compilationUnit.commitWorkingCopy(true, new SubProgressMonitor(monitor, 1));
	} finally {
		compilationUnit.discardWorkingCopy();
	}
}