use of org.eclipse.jface.text.DocumentRewriteSession in project che by eclipse.
the class TextChange method performEdits.
/**
* Executes the text edits on the given document.
* Subclasses that override this method should call <code>super.performEdits(document)</code>.
*
* @param document the document
* @return an object representing the undo of the executed edits
* @exception MalformedTreeException is thrown if the edit tree isn't
* in a valid state. This exception is thrown before any edit is executed.
* So the document is still in its original state.
* @exception BadLocationException is thrown if one of the edits in the
* tree can't be executed. The state of the document is undefined if this
* exception is thrown.
* @since 3.5
*/
protected UndoEdit performEdits(IDocument document) throws BadLocationException, MalformedTreeException {
DocumentRewriteSession session = null;
try {
if (document instanceof IDocumentExtension4) {
session = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
}
LinkedModeModel.closeAllModels(document);
TextEditProcessor processor = createTextEditProcessor(document, TextEdit.CREATE_UNDO, false);
return processor.performEdits();
} finally {
if (session != null) {
((IDocumentExtension4) document).stopRewriteSession(session);
}
}
}
use of org.eclipse.jface.text.DocumentRewriteSession in project tdi-studio-se by Talend.
the class JavaProcessor method formatCode.
/**
* DOC nrousseau Comment method "formatCode".
*
* from SourceViewer.doOperation for FORMAT.
*
* @param processCode
* @return
*/
@SuppressWarnings({ "unchecked" })
private String formatCode(String processCode) {
// we cannot make calls to Ui in headless mode
if (ProcessorUtilities.isExportConfig() || CommonsPlugin.isHeadless()) {
// nothing to do
return processCode;
}
final IDocument document = new Document(processCode);
JavaTextTools tools = JavaPlugin.getDefault().getJavaTextTools();
tools.setupJavaDocumentPartitioner(document, IJavaPartitions.JAVA_PARTITIONING);
IFormattingContext context = null;
DocumentRewriteSession rewriteSession = null;
if (document instanceof IDocumentExtension4) {
rewriteSession = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL);
}
try {
final String rememberedContents = document.get();
try {
final MultiPassContentFormatter formatter = new MultiPassContentFormatter(IJavaPartitions.JAVA_PARTITIONING, IDocument.DEFAULT_CONTENT_TYPE);
formatter.setMasterStrategy(new JavaFormattingStrategy());
// formatter.setSlaveStrategy(new CommentFormattingStrategy(),
// IJavaPartitions.JAVA_DOC);
// formatter.setSlaveStrategy(new CommentFormattingStrategy(),
// IJavaPartitions.JAVA_SINGLE_LINE_COMMENT);
// formatter.setSlaveStrategy(new CommentFormattingStrategy(),
// IJavaPartitions.JAVA_MULTI_LINE_COMMENT);
context = new FormattingContext();
context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.TRUE);
Map<String, String> preferences;
if (this.getTalendJavaProject() == null) {
preferences = new HashMap<String, String>(JavaCore.getOptions());
} else {
// use project options
preferences = new HashMap<String, String>(this.getTalendJavaProject().getJavaProject().getOptions(true));
}
context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, preferences);
formatter.format(document, context);
} catch (RuntimeException x) {
// fire wall for
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=47472
// if something went wrong we undo the changes we just did
// TODO to be removed after 3.0 M8
document.set(rememberedContents);
throw x;
}
} finally {
if (rewriteSession != null && document instanceof IDocumentExtension4) {
((IDocumentExtension4) document).stopRewriteSession(rewriteSession);
}
if (context != null) {
context.dispose();
}
}
return document.get();
}
use of org.eclipse.jface.text.DocumentRewriteSession in project che by eclipse.
the class MultiStateTextFileChange method performChangesInSynchronizationContext.
private void performChangesInSynchronizationContext(final IDocument document, final LinkedList undoList, final boolean preview) throws BadLocationException {
DocumentRewriteSession session = null;
try {
if (document instanceof IDocumentExtension4)
session = ((IDocumentExtension4) document).startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED);
for (final Iterator iterator = fChanges.iterator(); iterator.hasNext(); ) {
final ComposableBufferChange change = (ComposableBufferChange) iterator.next();
final UndoEdit edit = createTextEditProcessor(change, document, undoList != null ? TextEdit.CREATE_UNDO : TextEdit.NONE, preview).performEdits();
if (undoList != null)
undoList.addFirst(edit);
}
} finally {
if (session != null)
((IDocumentExtension4) document).stopRewriteSession(session);
}
}
Aggregations