use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class AddImportsOperation method evaluateEdits.
private TextEdit evaluateEdits(CompilationUnit root, ImportRewrite importRewrite, int offset, int length, IProgressMonitor monitor) throws JavaModelException {
SimpleName nameNode = null;
if (root != null) {
// got an AST
ASTNode node = NodeFinder.perform(root, offset, length);
if (node instanceof MarkerAnnotation) {
node = ((Annotation) node).getTypeName();
}
if (node instanceof QualifiedName) {
nameNode = ((QualifiedName) node).getName();
} else if (node instanceof SimpleName) {
nameNode = (SimpleName) node;
}
}
String name, simpleName, containerName;
int qualifierStart;
int simpleNameStart;
if (nameNode != null) {
simpleName = nameNode.getIdentifier();
simpleNameStart = nameNode.getStartPosition();
if (nameNode.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
Name qualifier = ((QualifiedName) nameNode.getParent()).getQualifier();
containerName = qualifier.getFullyQualifiedName();
name = JavaModelUtil.concatenateName(containerName, simpleName);
qualifierStart = qualifier.getStartPosition();
} else if (nameNode.getLocationInParent() == NameQualifiedType.NAME_PROPERTY) {
NameQualifiedType nameQualifiedType = (NameQualifiedType) nameNode.getParent();
Name qualifier = nameQualifiedType.getQualifier();
containerName = qualifier.getFullyQualifiedName();
name = JavaModelUtil.concatenateName(containerName, simpleName);
qualifierStart = qualifier.getStartPosition();
List<Annotation> annotations = nameQualifiedType.annotations();
if (!annotations.isEmpty()) {
// don't remove annotations
simpleNameStart = annotations.get(0).getStartPosition();
}
} else if (nameNode.getLocationInParent() == MethodInvocation.NAME_PROPERTY) {
ASTNode qualifier = ((MethodInvocation) nameNode.getParent()).getExpression();
if (qualifier instanceof Name) {
containerName = ASTNodes.asString(qualifier);
name = JavaModelUtil.concatenateName(containerName, simpleName);
qualifierStart = qualifier.getStartPosition();
} else {
return null;
}
} else {
//$NON-NLS-1$
containerName = "";
name = simpleName;
qualifierStart = simpleNameStart;
}
IBinding binding = nameNode.resolveBinding();
if (binding != null && !binding.isRecovered()) {
if (binding instanceof ITypeBinding) {
ITypeBinding typeBinding = ((ITypeBinding) binding).getTypeDeclaration();
String qualifiedBindingName = typeBinding.getQualifiedName();
if (containerName.length() > 0 && !qualifiedBindingName.equals(name)) {
return null;
}
ImportRewriteContext context = new ContextSensitiveImportRewriteContext(root, qualifierStart, importRewrite);
String res = importRewrite.addImport(typeBinding, context);
if (containerName.length() > 0 && !res.equals(simpleName)) {
// adding import failed
fStatus = JavaUIStatus.createError(IStatus.ERROR, CodeGenerationMessages.AddImportsOperation_error_importclash, null);
return null;
}
if (containerName.length() == 0 && res.equals(simpleName)) {
// no change necessary
return null;
}
return new ReplaceEdit(qualifierStart, simpleNameStart - qualifierStart, new String());
} else if (JavaModelUtil.is50OrHigher(fCompilationUnit.getJavaProject()) && (binding instanceof IVariableBinding || binding instanceof IMethodBinding)) {
boolean isField = binding instanceof IVariableBinding;
ITypeBinding declaringClass = isField ? ((IVariableBinding) binding).getDeclaringClass() : ((IMethodBinding) binding).getDeclaringClass();
if (declaringClass == null) {
// variableBinding.getDeclaringClass() is null for array.length
return null;
}
if (Modifier.isStatic(binding.getModifiers())) {
if (containerName.length() > 0) {
if (containerName.equals(declaringClass.getName()) || containerName.equals(declaringClass.getQualifiedName())) {
ASTNode node = nameNode.getParent();
boolean isDirectlyAccessible = false;
while (node != null) {
if (isTypeDeclarationSubTypeCompatible(node, declaringClass)) {
isDirectlyAccessible = true;
break;
}
node = node.getParent();
}
if (!isDirectlyAccessible) {
if (Modifier.isPrivate(declaringClass.getModifiers())) {
fStatus = JavaUIStatus.createError(IStatus.ERROR, Messages.format(CodeGenerationMessages.AddImportsOperation_error_not_visible_class, BasicElementLabels.getJavaElementName(declaringClass.getName())), null);
return null;
}
String res = importRewrite.addStaticImport(declaringClass.getQualifiedName(), binding.getName(), isField);
if (!res.equals(simpleName)) {
// adding import failed
return null;
}
}
//$NON-NLS-1$
return new ReplaceEdit(qualifierStart, simpleNameStart - qualifierStart, "");
}
}
}
// no static imports for packages
return null;
} else {
return null;
}
}
if (binding != null && binding.getKind() != IBinding.TYPE) {
// recovered binding
return null;
}
} else {
IBuffer buffer = fCompilationUnit.getBuffer();
qualifierStart = getNameStart(buffer, offset);
int nameEnd = getNameEnd(buffer, offset + length);
int len = nameEnd - qualifierStart;
name = buffer.getText(qualifierStart, len).trim();
if (name.length() == 0) {
return null;
}
simpleName = Signature.getSimpleName(name);
containerName = Signature.getQualifier(name);
IJavaProject javaProject = fCompilationUnit.getJavaProject();
if (simpleName.length() == 0 || JavaConventionsUtil.validateJavaTypeName(simpleName, javaProject).matches(IStatus.ERROR) || (containerName.length() > 0 && JavaConventionsUtil.validateJavaTypeName(containerName, javaProject).matches(IStatus.ERROR))) {
fStatus = JavaUIStatus.createError(IStatus.ERROR, CodeGenerationMessages.AddImportsOperation_error_invalid_selection, null);
return null;
}
simpleNameStart = getSimpleNameStart(buffer, qualifierStart, containerName);
int res = importRewrite.getDefaultImportRewriteContext().findInContext(containerName, simpleName, ImportRewriteContext.KIND_TYPE);
if (res == ImportRewriteContext.RES_NAME_CONFLICT) {
fStatus = JavaUIStatus.createError(IStatus.ERROR, CodeGenerationMessages.AddImportsOperation_error_importclash, null);
return null;
} else if (res == ImportRewriteContext.RES_NAME_FOUND) {
//$NON-NLS-1$
return new ReplaceEdit(qualifierStart, simpleNameStart - qualifierStart, "");
}
}
IJavaSearchScope searchScope = SearchEngine.createJavaSearchScope(new IJavaElement[] { fCompilationUnit.getJavaProject() });
TypeNameMatch[] types = findAllTypes(simpleName, searchScope, nameNode, new SubProgressMonitor(monitor, 1));
if (types.length == 0) {
fStatus = JavaUIStatus.createError(IStatus.ERROR, Messages.format(CodeGenerationMessages.AddImportsOperation_error_notresolved_message, BasicElementLabels.getJavaElementName(simpleName)), null);
return null;
}
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
TypeNameMatch chosen;
if (types.length > 1 && fQuery != null) {
chosen = fQuery.chooseImport(types, containerName);
if (chosen == null) {
throw new OperationCanceledException();
}
} else {
chosen = types[0];
}
ImportRewriteContext context = root == null ? null : new ContextSensitiveImportRewriteContext(root, simpleNameStart, importRewrite);
importRewrite.addImport(chosen.getFullyQualifiedName(), context);
//$NON-NLS-1$
return new ReplaceEdit(qualifierStart, simpleNameStart - qualifierStart, "");
}
use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class ASTNodes method getNodeSource.
/**
* 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;
}
use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class JavadocContentAccess2 method getHTMLContentFromSource.
private static String getHTMLContentFromSource(IMember member, String urlPrefix) throws JavaModelException {
IBuffer buf = member.getOpenable().getBuffer();
if (buf == null) {
// no source attachment found
return null;
}
ISourceRange javadocRange = member.getJavadocRange();
if (javadocRange == null) {
if (canInheritJavadoc(member)) {
// Try to use the inheritDoc algorithm.
//$NON-NLS-1$
String inheritedJavadoc = javadoc2HTML(member, "/***/", urlPrefix);
if (inheritedJavadoc != null && inheritedJavadoc.length() > 0) {
return inheritedJavadoc;
}
}
// getJavaFxPropertyDoc(member);
return null;
}
String rawJavadoc = buf.getText(javadocRange.getOffset(), javadocRange.getLength());
return javadoc2HTML(member, rawJavadoc, urlPrefix);
}
use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class CodeAssist method createOrganizeImportOperation.
private OrganizeImportResult createOrganizeImportOperation(ICompilationUnit compilationUnit, List<String> chosen) throws CoreException {
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(compilationUnit.getJavaProject());
OrganizeImportsOperation operation = new OrganizeImportsOperation(compilationUnit, null, settings.importIgnoreLowercase, !compilationUnit.isWorkingCopy(), true, chosen, null);
NullProgressMonitor monitor = new NullProgressMonitor();
TextEdit edit = operation.createTextEdit(monitor);
OrganizeImportResult result = DtoFactory.newDto(OrganizeImportResult.class);
TypeNameMatch[][] choices = operation.getChoices();
//or all conflicts were resolved (!chosen.isEmpty())
if ((chosen != null && !chosen.isEmpty()) || choices == null || choices.length == 0) {
IBuffer buffer = compilationUnit.getBuffer();
IDocument document = new Document(buffer.getContents());
DocumentChangeListener documentChangeListener = new DocumentChangeListener(document);
try {
edit.apply(document);
} catch (BadLocationException e) {
LOG.debug("Applying Organize import text edits goes wrong:", e);
}
result.setChanges(documentChangeListener.getChanges());
return result;
}
result.setConflicts(createListOfDTOMatches(choices));
return result;
}
use of org.eclipse.jdt.core.IBuffer in project che by eclipse.
the class StringFix method createFix.
public static StringFix createFix(CompilationUnit compilationUnit, IProblemLocation problem, boolean removeNLSTag, boolean addNLSTag) throws CoreException {
TextEdit addEdit = null;
ICompilationUnit cu = (ICompilationUnit) compilationUnit.getJavaElement();
//TODO NLS
// if (addNLSTag) {
// addEdit = NLSUtil.createNLSEdit(cu, problem.getOffset());
// }
ReplaceEdit removeEdit = null;
if (removeNLSTag) {
IBuffer buffer = cu.getBuffer();
if (buffer != null) {
removeEdit = getReplace(problem.getOffset(), problem.getLength(), buffer, true);
}
}
if (addEdit != null && removeEdit != null) {
String label = FixMessages.StringFix_AddRemoveNonNls_description;
return new StringFix(label, compilationUnit, new TextEditGroup[] { new TextEditGroup(label, addEdit), new TextEditGroup(label, removeEdit) });
} else if (addEdit != null) {
String label = FixMessages.StringFix_AddNonNls_description;
return new StringFix(label, compilationUnit, new TextEditGroup[] { new TextEditGroup(label, addEdit) });
} else if (removeEdit != null) {
String label = FixMessages.StringFix_RemoveNonNls_description;
return new StringFix(label, compilationUnit, new TextEditGroup[] { new TextEditGroup(label, removeEdit) });
} else {
return null;
}
}
Aggregations