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

The following examples show how to use org.eclipse.jdt.core.IBuffer#getLength() . 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: ModelBasedCompletionEngine.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static void codeComplete(ICompilationUnit cu, int position, CompletionRequestor requestor, WorkingCopyOwner owner, IProgressMonitor monitor) throws JavaModelException {
	if (!(cu instanceof CompilationUnit)) {
		return;
	}

	if (requestor == null) {
		throw new IllegalArgumentException("Completion requestor cannot be null"); //$NON-NLS-1$
	}

	IBuffer buffer = cu.getBuffer();
	if (buffer == null) {
		return;
	}

	if (position < -1 || position > buffer.getLength()) {
		throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INDEX_OUT_OF_BOUNDS));
	}

	JavaProject project = (JavaProject) cu.getJavaProject();
	ModelBasedSearchableEnvironment environment = new ModelBasedSearchableEnvironment(project, owner, requestor.isTestCodeExcluded());
	environment.setUnitToSkip((CompilationUnit) cu);

	// code complete
	CompletionEngine engine = new CompletionEngine(environment, requestor, project.getOptions(true), project, owner, monitor);
	engine.complete((CompilationUnit) cu, position, 0, cu);
}
 
Example 2
Source File: JavaCodeFormatterImpl.java    From jenerate with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Examines a string and returns the first line delimiter found.
 */
private static String getLineDelimiterUsed(IJavaElement elem) throws JavaModelException {
    ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT);
    if (cu != null && cu.exists()) {
        IBuffer buf = cu.getBuffer();
        int length = buf.getLength();
        for (int i = 0; i < length; i++) {
            char ch = buf.getChar(i);
            if (ch == SWT.CR) {
                if (i + 1 < length) {
                    if (buf.getChar(i + 1) == SWT.LF) {
                        return "\r\n";
                    }
                }
                return "\r";
            } else if (ch == SWT.LF) {
                return "\n";
            }
        }
    }
    return System.getProperty("line.separator", "\n");
}
 
Example 3
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 4
Source File: AddImportsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private int getNameEnd(IBuffer doc, int pos) {
	if (pos > 0) {
		if (Character.isWhitespace(doc.getChar(pos - 1))) {
			return pos;
		}
	}
	int len= doc.getLength();
	while (pos < len) {
		char ch= doc.getChar(pos);
		if (!Character.isJavaIdentifierPart(ch)) {
			return pos;
		}
		pos++;
	}
	return pos;
}
 
Example 5
Source File: NLSUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static int findLineEnd(ICompilationUnit cu, int position) throws JavaModelException {
	IBuffer buffer= cu.getBuffer();
	int length= buffer.getLength();
	for (int i= position; i < length; i++) {
		if (IndentManipulation.isLineDelimiterChar(buffer.getChar(i))) {
			return i;
		}
	}
	return length;
}
 
Example 6
Source File: NLSUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static int findLineEnd(ICompilationUnit cu, int position) throws JavaModelException {
	IBuffer buffer= cu.getBuffer();
	int length= buffer.getLength();
	for (int i= position; i < length; i++) {
		if (IndentManipulation.isLineDelimiterChar(buffer.getChar(i))) {
			return i;
		}
	}
	return length;
}
 
Example 7
Source File: NLSSourceModifier.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getLineEnd(IBuffer buffer, int offset) {
	int pos= offset;
	int length= buffer.getLength();
	while (pos < length && !isDelemiter(buffer.getChar(pos))) {
		pos++;
	}
	return pos;
}
 
Example 8
Source File: AddImportsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getSimpleNameStart(IBuffer buffer, int nameStart, String containerName) {
	int containerLen= containerName.length();
	int docLen= buffer.getLength();
	if (containerLen > 0 && nameStart + containerLen + 1 < docLen) {
		for (int k= 0; k < containerLen; k++) {
			if (buffer.getChar(nameStart + k) != containerName.charAt(k)) {
				return nameStart;
			}
		}
		if (buffer.getChar(nameStart + containerLen) == '.') {
			return nameStart + containerLen + 1;
		}
	}
	return nameStart;
}
 
Example 9
Source File: JavaElementLine.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param element either an ICompilationUnit or an IClassFile
 * @param lineNumber the line number, starting at 0
 * @param lineStartOffset the start offset of the line
 * @throws CoreException thrown when accessing of the buffer failed
 */
public JavaElementLine(ITypeRoot element, int lineNumber, int lineStartOffset) throws CoreException {
	fElement= element;
	fFlags= 0;

	IBuffer buffer= element.getBuffer();
	if (buffer == null) {
		throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, Messages.format( SearchMessages.JavaElementLine_error_nobuffer, BasicElementLabels.getFileName(element))));
	}

	int length= buffer.getLength();
	int i= lineStartOffset;

	char ch= buffer.getChar(i);
	while (lineStartOffset < length && IndentManipulation.isIndentChar(ch)) {
		ch= buffer.getChar(++i);
	}
	fLineStartOffset= i;

	StringBuffer buf= new StringBuffer();

	while (i < length && !IndentManipulation.isLineDelimiterChar(ch)) {
		if (Character.isISOControl(ch)) {
			buf.append(' ');
		} else {
			buf.append(ch);
		}
		i++;
		if (i < length)
			ch= buffer.getChar(i);
	}
	fLineContents= buf.toString();
	fLineNumber= lineNumber;
}
 
Example 10
Source File: CompletionHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private CompletionList computeContentAssist(ICompilationUnit unit, int line, int column, IProgressMonitor monitor) throws JavaModelException {
	CompletionResponses.clear();
	if (unit == null) {
		return null;
	}
	List<CompletionItem> proposals = new ArrayList<>();

	final int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
	CompletionProposalRequestor collector = new CompletionProposalRequestor(unit, offset, manager);
	// Allow completions for unresolved types - since 3.3
	collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_REF, true);
	collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.TYPE_IMPORT, true);
	collector.setAllowsRequiredProposals(CompletionProposal.FIELD_REF, CompletionProposal.FIELD_IMPORT, true);

	collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_REF, true);
	collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.TYPE_IMPORT, true);
	collector.setAllowsRequiredProposals(CompletionProposal.METHOD_REF, CompletionProposal.METHOD_IMPORT, true);

	collector.setAllowsRequiredProposals(CompletionProposal.CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);

	collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, CompletionProposal.TYPE_REF, true);
	collector.setAllowsRequiredProposals(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, CompletionProposal.TYPE_REF, true);

	collector.setAllowsRequiredProposals(CompletionProposal.TYPE_REF, CompletionProposal.TYPE_REF, true);
	collector.setFavoriteReferences(getFavoriteStaticMembers());

	if (offset >-1 && !monitor.isCanceled()) {
		IBuffer buffer = unit.getBuffer();
		if (buffer != null && buffer.getLength() >= offset) {
			IProgressMonitor subMonitor = new ProgressMonitorWrapper(monitor) {
				private long timeLimit;
				private final long TIMEOUT = Long.getLong("completion.timeout", 5000);

				@Override
				public void beginTask(String name, int totalWork) {
					timeLimit = System.currentTimeMillis() + TIMEOUT;
				}

				@Override
				public boolean isCanceled() {
					return super.isCanceled() || timeLimit <= System.currentTimeMillis();
				}

			};
			try {
				if (isIndexEngineEnabled()) {
					unit.codeComplete(offset, collector, subMonitor);
				} else {
					ModelBasedCompletionEngine.codeComplete(unit, offset, collector, DefaultWorkingCopyOwner.PRIMARY, subMonitor);
				}
				proposals.addAll(collector.getCompletionItems());
				if (isSnippetStringSupported() && !UNSUPPORTED_RESOURCES.contains(unit.getResource().getName())) {
					proposals.addAll(SnippetCompletionProposal.getSnippets(unit, collector.getContext(), subMonitor));
				}
				proposals.addAll(new JavadocCompletionProposal().getProposals(unit, offset, collector, subMonitor));
			} catch (OperationCanceledException e) {
				monitor.setCanceled(true);
			}
		}
	}
	proposals.sort(PROPOSAL_COMPARATOR);
	CompletionList list = new CompletionList(proposals);
	list.setIsIncomplete(!collector.isComplete());
	return list;
}
 
Example 11
Source File: StringFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static ReplaceEdit getReplace(int offset, int length, IBuffer buffer, boolean removeLeadingIndents) {

		String replaceString= new String();
		boolean hasMoreInComment= false;

		// look after the tag
		int next= offset + length;
		while (next < buffer.getLength()) {
			char ch= buffer.getChar(next);
			if (IndentManipulation.isIndentChar(ch)) {
				next++; // remove all whitespace
			} else if (IndentManipulation.isLineDelimiterChar(ch)) {
				length= next - offset;
				break;
			} else if (ch == '/') {
				next++;
				if (next == buffer.getLength() || buffer.getChar(next) != '/') {
					replaceString= "//"; //$NON-NLS-1$
				} else {
					length= next - offset - 1;
				}
				hasMoreInComment= true;
				break;
			} else {
				replaceString= "//"; //$NON-NLS-1$
				hasMoreInComment= true;
				break;
			}
		}
		if (!hasMoreInComment && removeLeadingIndents) {
			while (offset > 0 && IndentManipulation.isIndentChar(buffer.getChar(offset - 1))) {
				offset--;
				length++;
			}
		}
		if (length > 0) {
			ReplaceEdit replaceEdit= new ReplaceEdit(offset, length, replaceString);
			return replaceEdit;
		} else {
			return null;
		}
	}