use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class StubUtility method fixEmptyVariables.
// remove lines for empty variables
private static String fixEmptyVariables(TemplateBuffer buffer, String[] variables) throws MalformedTreeException, BadLocationException {
IDocument doc = new Document(buffer.getString());
int nLines = doc.getNumberOfLines();
MultiTextEdit edit = new MultiTextEdit();
HashSet<Integer> removedLines = new HashSet<Integer>();
for (int i = 0; i < variables.length; i++) {
// look if Javadoc tags have to be added
TemplateVariable position = findVariable(buffer, variables[i]);
if (position == null || position.getLength() > 0) {
continue;
}
int[] offsets = position.getOffsets();
for (int k = 0; k < offsets.length; k++) {
int line = doc.getLineOfOffset(offsets[k]);
IRegion lineInfo = doc.getLineInformation(line);
int offset = lineInfo.getOffset();
String str = doc.get(offset, lineInfo.getLength());
if (Strings.containsOnlyWhitespaces(str) && nLines > line + 1 && removedLines.add(new Integer(line))) {
int nextStart = doc.getLineOffset(line + 1);
edit.addChild(new DeleteEdit(offset, nextStart - offset));
}
}
}
edit.apply(doc, 0);
return doc.get();
}
use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class StubUtility method insertTag.
private static void insertTag(IDocument textBuffer, int offset, int length, String[] paramNames, String[] exceptionNames, String returnType, String[] typeParameterNames, boolean isDeprecated, String lineDelimiter) throws BadLocationException {
IRegion region = textBuffer.getLineInformationOfOffset(offset);
if (region == null) {
return;
}
String lineStart = textBuffer.get(region.getOffset(), offset - region.getOffset());
StringBuffer buf = new StringBuffer();
for (int i = 0; i < typeParameterNames.length; i++) {
if (buf.length() > 0) {
buf.append(lineDelimiter).append(lineStart);
}
//$NON-NLS-1$
buf.append("@param <").append(typeParameterNames[i]).append('>');
}
for (int i = 0; i < paramNames.length; i++) {
if (buf.length() > 0) {
buf.append(lineDelimiter).append(lineStart);
}
//$NON-NLS-1$
buf.append("@param ").append(paramNames[i]);
}
if (returnType != null && !returnType.equals("void")) {
//$NON-NLS-1$
if (buf.length() > 0) {
buf.append(lineDelimiter).append(lineStart);
}
//$NON-NLS-1$
buf.append("@return");
}
if (exceptionNames != null) {
for (int i = 0; i < exceptionNames.length; i++) {
if (buf.length() > 0) {
buf.append(lineDelimiter).append(lineStart);
}
//$NON-NLS-1$
buf.append("@throws ").append(exceptionNames[i]);
}
}
if (isDeprecated) {
if (buf.length() > 0) {
buf.append(lineDelimiter).append(lineStart);
}
//$NON-NLS-1$
buf.append("@deprecated");
}
if (buf.length() == 0 && isAllCommentWhitespace(lineStart)) {
int prevLine = textBuffer.getLineOfOffset(offset) - 1;
if (prevLine > 0) {
IRegion prevRegion = textBuffer.getLineInformation(prevLine);
int prevLineEnd = prevRegion.getOffset() + prevRegion.getLength();
// clear full line
//$NON-NLS-1$
textBuffer.replace(prevLineEnd, offset + length - prevLineEnd, "");
return;
}
}
textBuffer.replace(offset, length, buf.toString());
}
use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class RenameAnalyzeUtil method existsInNewOccurrences.
private static boolean existsInNewOccurrences(SearchMatch searchResult, SearchResultGroup[] newOccurrences, TextChangeManager manager) {
SearchResultGroup newGroup = findOccurrenceGroup(searchResult.getResource(), newOccurrences);
if (newGroup == null)
return false;
IRegion oldEditRange = getCorrespondingEditChangeRange(searchResult, manager);
if (oldEditRange == null)
return false;
SearchMatch[] newSearchResults = newGroup.getSearchResults();
int oldRangeOffset = oldEditRange.getOffset();
for (int i = 0; i < newSearchResults.length; i++) {
if (newSearchResults[i].getOffset() == oldRangeOffset)
return true;
}
return false;
}
use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class RenameAnalyzeUtil method analyzeLocalRenames.
/**
* This method analyzes a set of local variable renames inside one cu. It checks whether
* any new compile errors have been introduced by the rename(s) and whether the correct
* node(s) has/have been renamed.
*
* @param analyzePackages the LocalAnalyzePackages containing the information about the local renames
* @param cuChange the TextChange containing all local variable changes to be applied.
* @param oldCUNode the fully (incl. bindings) resolved AST node of the original compilation unit
* @param recovery whether statements and bindings recovery should be performed when parsing the changed CU
* @return a RefactoringStatus containing errors if compile errors or wrongly renamed nodes are found
* @throws CoreException thrown if there was an error greating the preview content of the change
*/
public static RefactoringStatus analyzeLocalRenames(LocalAnalyzePackage[] analyzePackages, TextChange cuChange, CompilationUnit oldCUNode, boolean recovery) throws CoreException {
RefactoringStatus result = new RefactoringStatus();
ICompilationUnit compilationUnit = (ICompilationUnit) oldCUNode.getJavaElement();
String newCuSource = cuChange.getPreviewContent(new NullProgressMonitor());
CompilationUnit newCUNode = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(newCuSource, compilationUnit, true, recovery, null);
result.merge(analyzeCompileErrors(newCuSource, newCUNode, oldCUNode));
if (result.hasError())
return result;
for (int i = 0; i < analyzePackages.length; i++) {
ASTNode enclosing = getEnclosingBlockOrMethodOrLambda(analyzePackages[i].fDeclarationEdit, cuChange, newCUNode);
// get new declaration
IRegion newRegion = RefactoringAnalyzeUtil.getNewTextRange(analyzePackages[i].fDeclarationEdit, cuChange);
ASTNode newDeclaration = NodeFinder.perform(newCUNode, newRegion.getOffset(), newRegion.getLength());
Assert.isTrue(newDeclaration instanceof Name);
VariableDeclaration declaration = getVariableDeclaration((Name) newDeclaration);
Assert.isNotNull(declaration);
SimpleName[] problemNodes = ProblemNodeFinder.getProblemNodes(enclosing, declaration, analyzePackages[i].fOccurenceEdits, cuChange);
result.merge(RefactoringAnalyzeUtil.reportProblemNodes(newCuSource, problemNodes));
}
return result;
}
use of org.eclipse.jface.text.IRegion in project che by eclipse.
the class JavaWordFinder method findWord.
public static IRegion findWord(IDocument document, int offset) {
int start = -2;
int end = -1;
try {
int pos = offset;
char c;
while (pos >= 0) {
c = document.getChar(pos);
if (!Character.isJavaIdentifierPart(c)) {
// Check for surrogates
if (isSurrogate(c)) {
/*
* XXX: Here we should create the code point and test whether
* it is a Java identifier part. Currently this is not possible
* because java.lang.Character in 1.4 does not support surrogates
* and because com.ibm.icu.lang.UCharacter.isJavaIdentifierPart(int)
* is not correctly implemented.
*/
} else {
break;
}
}
--pos;
}
start = pos;
pos = offset;
int length = document.getLength();
while (pos < length) {
c = document.getChar(pos);
if (!Character.isJavaIdentifierPart(c)) {
if (isSurrogate(c)) {
/*
* XXX: Here we should create the code point and test whether
* it is a Java identifier part. Currently this is not possible
* because java.lang.Character in 1.4 does not support surrogates
* and because com.ibm.icu.lang.UCharacter.isJavaIdentifierPart(int)
* is not correctly implemented.
*/
} else {
break;
}
}
++pos;
}
end = pos;
} catch (BadLocationException x) {
}
if (start >= -1 && end > -1) {
if (start == offset && end == offset) {
try {
char c = document.getChar(offset);
switch(c) {
case '-':
if (document.getChar(offset + 1) == '>') {
return new Region(offset, 2);
}
break;
case '>':
if (document.getChar(offset - 1) == '-') {
return new Region(offset - 1, 2);
}
break;
case ':':
if (document.getChar(offset + 1) == ':') {
return new Region(offset, 2);
} else if (document.getChar(offset - 1) == ':') {
return new Region(offset - 1, 2);
}
break;
}
} catch (BadLocationException e) {
}
return new Region(offset, 0);
} else if (start == offset) {
//XXX: probably unused...
return new Region(start, end - start);
} else {
return new Region(start + 1, end - start - 1);
}
}
return null;
}
Aggregations