use of org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings in project che by eclipse.
the class ConstructorFromSuperclassProposal method getRewrite.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
*/
@Override
protected ASTRewrite getRewrite() throws CoreException {
AST ast = fTypeNode.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
createImportRewrite((CompilationUnit) fTypeNode.getRoot());
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(getCompilationUnit().getJavaProject());
if (!settings.createComments) {
settings = null;
}
ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(fTypeNode, getImportRewrite());
MethodDeclaration newMethodDecl = createNewMethodDeclaration(ast, fSuperConstructor, rewrite, importRewriteContext, settings);
rewrite.getListRewrite(fTypeNode, TypeDeclaration.BODY_DECLARATIONS_PROPERTY).insertFirst(newMethodDecl, null);
addLinkedRanges(rewrite, newMethodDecl);
return rewrite;
}
use of org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings 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.internal.corext.codemanipulation.CodeGenerationSettings in project che by eclipse.
the class AddUnimplementedMethodsOperation method rewriteAST.
/**
* {@inheritDoc}
*/
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
IMethodBinding[] unimplementedMethods = getUnimplementedMethods(fTypeNode);
if (unimplementedMethods.length == 0)
return;
ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fTypeNode.getRoot(), fTypeNode.getStartPosition(), cuRewrite.getImportRewrite());
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ICompilationUnit unit = cuRewrite.getCu();
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(unit.getJavaProject());
ListRewrite listRewrite;
if (fTypeNode instanceof AnonymousClassDeclaration) {
AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
} else if (fTypeNode instanceof AbstractTypeDeclaration) {
AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, decl.getBodyDeclarationsProperty());
} else if (fTypeNode instanceof EnumConstantDeclaration) {
EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) fTypeNode;
AnonymousClassDeclaration anonymousClassDeclaration = enumConstantDeclaration.getAnonymousClassDeclaration();
if (anonymousClassDeclaration == null) {
anonymousClassDeclaration = rewrite.getAST().newAnonymousClassDeclaration();
rewrite.set(enumConstantDeclaration, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, anonymousClassDeclaration, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
listRewrite = rewrite.getListRewrite(anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
} else {
//$NON-NLS-1$
Assert.isTrue(false, "Unknown type node");
return;
}
ImportRewrite imports = cuRewrite.getImportRewrite();
for (int i = 0; i < unimplementedMethods.length; i++) {
IMethodBinding curr = unimplementedMethods[i];
MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(unit, rewrite, imports, context, curr, curr.getDeclaringClass().getName(), settings, false);
listRewrite.insertLast(newMethodDecl, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
}
use of org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings in project che by eclipse.
the class MethodDeclarationCompletionProposal method updateReplacementString.
/* (non-Javadoc)
* @see JavaTypeCompletionProposal#updateReplacementString(IDocument, char, int, ImportRewrite)
*/
@Override
protected boolean updateReplacementString(IDocument document, char trigger, int offset, ImportRewrite impRewrite) throws CoreException, BadLocationException {
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(fType.getJavaProject());
boolean addComments = settings.createComments;
String[] empty = new String[0];
String lineDelim = TextUtilities.getDefaultLineDelimiter(document);
String declTypeName = fType.getTypeQualifiedName('.');
boolean isInterface = fType.isInterface();
StringBuffer buf = new StringBuffer();
if (addComments) {
String comment = CodeGeneration.getMethodComment(fType.getCompilationUnit(), declTypeName, fMethodName, empty, empty, fReturnTypeSig, empty, null, lineDelim);
if (comment != null) {
buf.append(comment);
buf.append(lineDelim);
}
}
if (fReturnTypeSig != null) {
if (!isInterface) {
//$NON-NLS-1$
buf.append("private ");
}
} else {
if (fType.isEnum())
//$NON-NLS-1$
buf.append("private ");
else
//$NON-NLS-1$
buf.append("public ");
}
if (fReturnTypeSig != null) {
buf.append(Signature.toString(fReturnTypeSig));
}
buf.append(' ');
buf.append(fMethodName);
if (isInterface) {
//$NON-NLS-1$
buf.append("();");
buf.append(lineDelim);
} else {
//$NON-NLS-1$
buf.append("() {");
buf.append(lineDelim);
String body = CodeGeneration.getMethodBodyContent(fType.getCompilationUnit(), declTypeName, fMethodName, fReturnTypeSig == null, "", //$NON-NLS-1$
lineDelim);
if (body != null) {
buf.append(body);
buf.append(lineDelim);
}
//$NON-NLS-1$
buf.append("}");
buf.append(lineDelim);
}
String stub = buf.toString();
// use the code formatter
IRegion region = document.getLineInformationOfOffset(getReplacementOffset());
int lineStart = region.getOffset();
int indent = Strings.computeIndentUnits(document.get(lineStart, getReplacementOffset() - lineStart), settings.tabWidth, settings.indentWidth);
String replacement = CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, stub, indent, lineDelim, fType.getJavaProject());
if (replacement.endsWith(lineDelim)) {
replacement = replacement.substring(0, replacement.length() - lineDelim.length());
}
setReplacementString(Strings.trimLeadingTabsAndSpaces(replacement));
return true;
}
use of org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings in project che by eclipse.
the class JavaPreferencesSettings method getCodeGenerationSettings.
public static CodeGenerationSettings getCodeGenerationSettings(IJavaProject project) {
CodeGenerationSettings res = new CodeGenerationSettings();
//Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_ADD_COMMENTS, project)).booleanValue();
res.createComments = false;
//Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_KEYWORD_THIS, project)).booleanValue();
res.useKeywordThis = false;
//Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.CODEGEN_USE_OVERRIDE_ANNOTATION, project)).booleanValue();
res.overrideAnnotation = true;
//Boolean.valueOf(PreferenceConstants.getPreference(PreferenceConstants.ORGIMPORTS_IGNORELOWERCASE, project)).booleanValue();
res.importIgnoreLowercase = true;
res.tabWidth = CodeFormatterUtil.getTabWidth(project);
res.indentWidth = CodeFormatterUtil.getIndentWidth(project);
return res;
}
Aggregations