use of org.eclipse.jface.text.Document in project flux by eclipse.
the class ASTNodes method asFormattedString.
public static String asFormattedString(ASTNode node, int indent, String lineDelim, Map<String, String> options) {
String unformatted = asString(node);
TextEdit edit = CodeFormatterUtil.format2(node, unformatted, indent, lineDelim, options);
if (edit != null) {
Document document = new Document(unformatted);
try {
edit.apply(document, TextEdit.NONE);
} catch (BadLocationException e) {
JavaPlugin.log(e);
}
return document.get();
}
// unknown node
return unformatted;
}
use of org.eclipse.jface.text.Document in project flux by eclipse.
the class CUCorrectionProposal method createTextChange.
protected TextChange createTextChange() throws CoreException {
ICompilationUnit cu = getCompilationUnit();
String name = getName();
TextChange change;
if (!cu.getResource().exists()) {
String source;
try {
source = cu.getSource();
} catch (JavaModelException e) {
JavaPlugin.log(e);
// empty
source = new String();
}
Document document = new Document(source);
document.setInitialLineDelimiter(StubUtility.getLineDelimiterUsed(cu));
change = new DocumentChange(name, document);
} else {
CompilationUnitChange cuChange = new CompilationUnitChange(name, cu);
cuChange.setSaveMode(TextFileChange.LEAVE_DIRTY);
change = cuChange;
}
TextEdit rootEdit = new MultiTextEdit();
change.setEdit(rootEdit);
// initialize text change
IDocument document = change.getCurrentDocument(new NullProgressMonitor());
addEdits(document, rootEdit);
return change;
}
use of org.eclipse.jface.text.Document in project flux by eclipse.
the class StubUtility method getMethodComment.
/*
* Don't use this method directly, use CodeGeneration.
* @see org.eclipse.jdt.ui.CodeGeneration#getMethodComment(ICompilationUnit, String, String, String[], String[], String, String[], IMethod, String)
*/
public static String getMethodComment(ICompilationUnit cu, String typeName, String methodName, String[] paramNames, String[] excTypeSig, String retTypeSig, String[] typeParameterNames, IMethod target, boolean delegate, String lineDelimiter) throws CoreException {
String templateName = CodeTemplateContextType.METHODCOMMENT_ID;
if (retTypeSig == null) {
templateName = CodeTemplateContextType.CONSTRUCTORCOMMENT_ID;
} else if (target != null) {
if (delegate)
templateName = CodeTemplateContextType.DELEGATECOMMENT_ID;
else
templateName = CodeTemplateContextType.OVERRIDECOMMENT_ID;
}
Template template = getCodeTemplate(templateName, cu.getJavaProject());
if (template == null) {
return null;
}
CodeTemplateContext context = new CodeTemplateContext(template.getContextTypeId(), cu.getJavaProject(), lineDelimiter);
context.setCompilationUnitVariables(cu);
context.setVariable(CodeTemplateContextType.ENCLOSING_TYPE, typeName);
context.setVariable(CodeTemplateContextType.ENCLOSING_METHOD, methodName);
if (retTypeSig != null) {
context.setVariable(CodeTemplateContextType.RETURN_TYPE, Signature.toString(retTypeSig));
}
if (target != null) {
String targetTypeName = target.getDeclaringType().getFullyQualifiedName('.');
String[] targetParamTypeNames = getParameterTypeNamesForSeeTag(target);
if (delegate)
context.setVariable(CodeTemplateContextType.SEE_TO_TARGET_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
else
context.setVariable(CodeTemplateContextType.SEE_TO_OVERRIDDEN_TAG, getSeeTag(targetTypeName, methodName, targetParamTypeNames));
}
TemplateBuffer buffer;
try {
buffer = context.evaluate(template);
} catch (BadLocationException e) {
throw new CoreException(Status.CANCEL_STATUS);
} catch (TemplateException e) {
throw new CoreException(Status.CANCEL_STATUS);
}
if (buffer == null) {
return null;
}
String str = buffer.getString();
if (Strings.containsOnlyWhitespaces(str)) {
return null;
}
// look if Javadoc tags have to be added
TemplateVariable position = findVariable(buffer, CodeTemplateContextType.TAGS);
if (position == null) {
return str;
}
IDocument document = new Document(str);
String[] exceptionNames = new String[excTypeSig.length];
for (int i = 0; i < excTypeSig.length; i++) {
exceptionNames[i] = Signature.toString(excTypeSig[i]);
}
String returnType = retTypeSig != null ? Signature.toString(retTypeSig) : null;
int[] tagOffsets = position.getOffsets();
for (int i = tagOffsets.length - 1; i >= 0; i--) {
// from last to first
try {
insertTag(document, tagOffsets[i], position.getLength(), paramNames, exceptionNames, returnType, typeParameterNames, false, lineDelimiter);
} catch (BadLocationException e) {
throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
}
}
return document.get();
}
use of org.eclipse.jface.text.Document in project generator by mybatis.
the class ExistingJavaFileVisitorTest method testRegularClass.
@Test
public void testRegularClass() throws Exception {
InputStream resource = getClass().getResourceAsStream("/org/mybatis/generator/eclipse/core/tests/merge/resources/AwfulTable.java.src");
String source = getResourceAsString(resource);
IDocument document = new Document(source);
CompilationUnit cu = getCompilationUnitFromSource(source);
cu.recordModifications();
ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(MergeConstants.OLD_ELEMENT_TAGS);
// delete all the old generated stuff
cu.accept(visitor);
assertThat(visitor.getTypeDeclaration(), is(notNullValue()));
assertThat(visitor.getTypeDeclaration().isInterface(), is(false));
assertThat(visitor.getTypeDeclaration().getName().getFullyQualifiedName(), is("AwfulTable"));
// generate a new compilation unit that is stripped of old stuff
TextEdit textEdit = cu.rewrite(document, null);
textEdit.apply(document);
CompilationUnitSummary summary = getCompilationUnitSummaryFromSource(document.get());
assertThat(summary, hasImportCount(1));
assertThat(summary, hasClass("AwfulTable", withSuperClass("AwfulTableKey")));
assertThat(summary, hasClass("AwfulTable", withSuperInterfaceCount(0)));
assertThat(summary, hasClass("AwfulTable", withMethodCount(0)));
assertThat(summary, hasClass("AwfulTable", withFieldCount(0)));
}
use of org.eclipse.jface.text.Document in project generator by mybatis.
the class ExistingJavaFileVisitorTest method testComplexClass.
@Test
public void testComplexClass() throws Exception {
InputStream resource = getClass().getResourceAsStream("/org/mybatis/generator/eclipse/core/tests/merge/resources/AwfulTableExample.java.src");
String source = getResourceAsString(resource);
IDocument document = new Document(source);
CompilationUnit cu = getCompilationUnitFromSource(source);
cu.recordModifications();
ExistingJavaFileVisitor visitor = new ExistingJavaFileVisitor(MergeConstants.OLD_ELEMENT_TAGS);
// delete all the old generated stuff
cu.accept(visitor);
assertThat(visitor.getTypeDeclaration(), is(notNullValue()));
assertThat(visitor.getTypeDeclaration().isInterface(), is(false));
assertThat(visitor.getTypeDeclaration().getName().getFullyQualifiedName(), is("AwfulTableExample"));
assertThat(visitor.containsInnerClass("Criteria"), is(true));
// generate a new compilation unit that is stripped of old stuff
TextEdit textEdit = cu.rewrite(document, null);
textEdit.apply(document);
CompilationUnitSummary summary = getCompilationUnitSummaryFromSource(document.get());
assertThat(summary, hasImportCount(2));
assertThat(summary, hasClass("AwfulTableExample", withSuperClass(nullValue())));
assertThat(summary, hasClass("AwfulTableExample", withSuperInterfaceCount(0)));
assertThat(summary, hasClass("AwfulTableExample", withMethodCount(0)));
assertThat(summary, hasClass("AwfulTableExample", withFieldCount(0)));
assertThat(summary, hasClass("AwfulTableExample", withClass("Criteria")));
}
Aggregations