use of org.eclipse.jface.text.Document in project AutoRefactor by JnRouvignac.
the class AllRefactoringRulesTest method testRefactoring0.
private void testRefactoring0() throws Exception {
final File samplesDir = new File(SAMPLES_ALL_BASE_DIR);
final File sampleIn = new File(samplesDir, "samples_in/" + sampleName);
assertTrue(sampleName + ": sample in file " + sampleIn + " should exist", sampleIn.exists());
final File sampleOut = new File(samplesDir, "samples_out/" + sampleName);
assertTrue(sampleName + ": sample out file " + sampleOut + " should exist", sampleOut.exists());
final String sampleInSource = readAll(sampleIn);
final String sampleOutSource = readAll(sampleOut);
final IPackageFragment packageFragment = JavaCoreHelper.getPackageFragment(PACKAGE_NAME);
final ICompilationUnit cu = packageFragment.createCompilationUnit(sampleName, sampleInSource, true, null);
cu.getBuffer().setContents(sampleInSource);
cu.save(null, true);
final IDocument doc = new Document(sampleInSource);
new ApplyRefactoringsJob(null, null, TEST_ENVIRONMENT).applyRefactoring(doc, cu, new AggregateASTVisitor(AllRefactoringRules.getAllRefactoringRules()), newJavaProjectOptions(Release.javaSE("1.7.0"), 4), new NullProgressMonitor());
final String actual = normalizeJavaSourceCode(doc.get().replaceAll("samples_in", "samples_out"));
final String expected = normalizeJavaSourceCode(sampleOutSource);
assertEquals(sampleName + ": wrong output;", expected, actual);
}
use of org.eclipse.jface.text.Document in project tdi-studio-se by Talend.
the class TalendJavaSourceViewer method createViewerForComponent.
public static ISourceViewer createViewerForComponent(Composite composite, int styles, CompilationUnitEditor editor, List<Variable> variableList, String uniqueName, String codePart) {
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String selectedPart = "[" + uniqueName + " " + codePart + " ] start";
IDocument originalDocument = editor.getDocumentProvider().getDocument(editor.getEditorInput());
int documentOffset = -1;
//$NON-NLS-1$
String globalFields = "";
//$NON-NLS-1$
String localFields = "";
//$NON-NLS-1$
globalFields = "\tprivate static java.util.Properties context = new java.util.Properties();\n";
//$NON-NLS-1$
globalFields += "\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n";
IDocument newDoc = new Document();
boolean checkCode = false;
FindReplaceDocumentAdapter frda = null;
// hywang modified for bug 9180
String documentText = originalDocument.get();
if (documentText != null && !documentText.equals("") && documentText.length() > 0) {
//$NON-NLS-1$
frda = new FindReplaceDocumentAdapter(originalDocument);
}
try {
Region region = null;
if (frda != null) {
region = (Region) frda.find(0, selectedPart, true, false, false, false);
}
if (region != null) {
checkCode = true;
documentOffset = region.getOffset();
documentOffset = originalDocument.getLineOffset(originalDocument.getLineOfOffset(documentOffset) + 2);
JavaCompletionProcessor processor = new JavaCompletionProcessor(editor, new ContentAssistant(), IDocument.DEFAULT_CONTENT_TYPE);
boolean globalFieldsDone = false;
//$NON-NLS-1$
globalFields = "";
String className = editor.getPartName().substring(0, editor.getPartName().indexOf('.') + 1);
ICompletionProposal[] proposals = processor.computeCompletionProposals(editor.getViewer(), documentOffset);
for (ICompletionProposal curProposal : proposals) {
if (curProposal instanceof JavaCompletionProposal) {
JavaCompletionProposal javaProposal = ((JavaCompletionProposal) curProposal);
if (javaProposal.getJavaElement() instanceof SourceField) {
globalFieldsDone = true;
SourceField sourceField = (SourceField) javaProposal.getJavaElement();
//$NON-NLS-1$ //$NON-NLS-2$
globalFields += "\t" + sourceField.getSource() + "\n";
// System.out.println();
}
if (javaProposal.getJavaElement() == null && !globalFieldsDone) {
//$NON-NLS-1$
String[] str = javaProposal.getSortString().split(" ");
//$NON-NLS-1$
String variable = "";
for (int i = str.length - 1; i >= 0; i--) {
//$NON-NLS-1$
variable += str[i].length() == 0 ? " " : str[i];
}
if (variable.contains(className)) {
continue;
}
//$NON-NLS-1$
variable += ";";
//$NON-NLS-1$ //$NON-NLS-2$
localFields += "\t\t" + variable + "\n";
// System.out.println(variable);
}
}
}
}
} catch (BadLocationException e) {
ExceptionHandler.process(e);
} catch (JavaModelException e) {
// e.printStackTrace();
ExceptionHandler.process(e);
}
StringBuffer buff = new StringBuffer();
//$NON-NLS-1$
buff.append("package internal;\n\n");
buff.append(getImports());
//$NON-NLS-1$ //$NON-NLS-2$
buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
buff.append(globalFields);
//$NON-NLS-1$
buff.append("\tpublic void myFunction(){\n");
buff.append(localFields);
documentOffset = buff.toString().length();
//$NON-NLS-1$
buff.append("\n\t\n}\n}");
newDoc.set(buff.toString());
return initializeViewer(composite, styles, checkCode, newDoc, documentOffset);
}
use of org.eclipse.jface.text.Document in project Main by SpartanRefactoring.
the class ASTTestClassGenerator method addStaticImports.
public static String addStaticImports(ASTNode root, String importName, String simpleName) {
if (!iz.compilationUnit(root)) {
return root.toString();
}
ListRewrite lr = ASTRewrite.create(root.getAST()).getListRewrite(root, CompilationUnit.IMPORTS_PROPERTY);
ImportDeclaration id = root.getAST().newImportDeclaration();
id.setStatic(true);
id.setName(root.getAST().newName(importName.split("\\.")));
lr.insertLast(id, null);
//irw.addStaticImport(importName, simpleName, false);
try {
IDocument doc = new Document(root.toString());
TextEdit te = lr.getASTRewrite().rewriteAST(doc, null);
// TextEdit te = irw.rewriteImports(new NullProgressMonitor()); //FIXME: @orenafek: Change null to something else...
te.apply(doc);
return doc.get();
} catch (BadLocationException e) {
note.bug(e);
}
//lr.insertLast(ImportDeclaration.
return root.toString();
}
use of org.eclipse.jface.text.Document in project bndtools by bndtools.
the class ComponentMarker method findAndMarkComponentAnnotations.
private static void findAndMarkComponentAnnotations(ICompilationUnit c) throws CoreException, JavaModelException {
Document document = null;
boolean found = false;
String key = null;
for (IType t : c.getTypes()) {
for (IAnnotation annot : t.getAnnotations()) {
if ("Component".equals(annot.getElementName())) {
if (document == null)
document = new Document(c.getBuffer().getContents());
found = true;
key = getNameFromComponent(annot);
int lineNumber;
try {
lineNumber = document.getLineOfOffset(t.getSourceRange().getOffset()) + 1;
String message = key == null ? "OSGi Component" : key;
IMarker marker = c.getResource().createMarker(BndtoolsConstants.MARKER_COMPONENT);
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_INFO);
marker.setAttribute(IMarker.MESSAGE, message);
marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
marker.setAttribute(IMarker.LOCATION, "line " + lineNumber);
} catch (BadLocationException e) {
logger.logError("Component Marker error", e);
lineNumber = -1;
}
}
}
}
if (!found) {
c.getResource().deleteMarkers(BndtoolsConstants.MARKER_COMPONENT, true, IResource.DEPTH_ONE);
}
}
Aggregations