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

The following examples show how to use org.eclipse.jdt.core.IBuffer#getChar() . 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: 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 2
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 3
Source File: AddImportsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int getNameStart(IBuffer buffer, int pos) {
	while (pos > 0) {
		char ch= buffer.getChar(pos - 1);
		if (!Character.isJavaIdentifierPart(ch) && ch != '.') {
			return pos;
		}
		pos--;
	}
	return pos;
}
 
Example 4
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 5
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 6
Source File: ImportRewriteAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int findInBuffer(IBuffer buffer, String str, int start, int end) {
	int pos= start;
	int len= str.length();
	if (pos + len > end || str.length() == 0) {
		return -1;
	}
	char first= str.charAt(0);
	int step= str.indexOf(first, 1);
	if (step == -1) {
		step= len;
	}
	while (pos + len <= end) {
		if (buffer.getChar(pos) == first) {
			int k= 1;
			while (k < len && buffer.getChar(pos + k) == str.charAt(k)) {
				k++;
			}
			if (k == len) {
				return pos; // found
			}
			if (k < step) {
				pos+= k;
			} else {
				pos+= step;
			}
		} else {
			pos++;
		}
	}
	return -1;
}
 
Example 7
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;
		}
	}