use of org.eclipse.jface.text.DocumentRewriteSessionType in project webtools.sourceediting by eclipse.
the class StructuredTextViewer method doOperation.
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.ITextOperationTarget#doOperation(int)
*/
public void doOperation(int operation) {
Point selection = getTextWidget().getSelection();
int cursorPosition = selection.x;
int selectionLength = selection.y - selection.x;
switch(operation) {
case CUT:
beginRecording(TEXT_CUT, TEXT_CUT, cursorPosition, selectionLength);
super.doOperation(operation);
selection = getTextWidget().getSelection();
cursorPosition = selection.x;
selectionLength = selection.y - selection.x;
endRecording(cursorPosition, selectionLength);
break;
case PASTE:
beginRecording(TEXT_PASTE, TEXT_PASTE, cursorPosition, selectionLength);
super.doOperation(operation);
selection = getTextWidget().getSelection();
cursorPosition = selection.x;
selectionLength = selection.y - selection.x;
endRecording(cursorPosition, selectionLength);
break;
case CONTENTASSIST_PROPOSALS:
// maybe not configured?
if (fContentAssistant != null && isEditable()) {
// position
if (canDoOperation(CONTENTASSIST_PROPOSALS)) {
String err = fContentAssistant.showPossibleCompletions();
if (err != null) {
// don't wanna beep if there is no error
PlatformStatusLineUtil.displayTemporaryErrorMessage(this, err);
}
} else
beep();
}
break;
case CONTENTASSIST_CONTEXT_INFORMATION:
if (fContentAssistant != null) {
String err = fContentAssistant.showContextInformation();
if (err != null) {
// don't wanna beep if there is no error
PlatformStatusLineUtil.displayTemporaryErrorMessage(this, err);
}
}
break;
case SHIFT_RIGHT:
beginRecording(TEXT_SHIFT_RIGHT, TEXT_SHIFT_RIGHT, cursorPosition, selectionLength);
updateIndentationPrefixes();
doModelOperation(SHIFT_RIGHT);
selection = getTextWidget().getSelection();
cursorPosition = selection.x;
selectionLength = selection.y - selection.x;
endRecording(cursorPosition, selectionLength);
break;
case SHIFT_LEFT:
beginRecording(TEXT_SHIFT_LEFT, TEXT_SHIFT_LEFT, cursorPosition, selectionLength);
updateIndentationPrefixes();
doModelOperation(SHIFT_LEFT);
selection = getTextWidget().getSelection();
cursorPosition = selection.x;
selectionLength = selection.y - selection.x;
endRecording(cursorPosition, selectionLength);
break;
case FORMAT_DOCUMENT:
DocumentRewriteSession rewriteSession = null;
IDocument document = getDocument();
try {
/*
* This command will actually format selection if text is
* selected, otherwise format entire document
*/
// begin recording
beginRecording(FORMAT_DOCUMENT_TEXT, FORMAT_DOCUMENT_TEXT, cursorPosition, selectionLength);
boolean formatDocument = false;
IRegion region = null;
Point s = getSelectedRange();
if (s.y > 0) {
// only format currently selected text
region = new Region(s.x, s.y);
} else {
// no selection, so format entire document
region = getModelCoverage();
formatDocument = true;
}
if (document instanceof IDocumentExtension4) {
IDocumentExtension4 extension = (IDocumentExtension4) document;
DocumentRewriteSessionType type = (selection.y == 0 || selection.y > MAX_SMALL_FORMAT_LENGTH) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
rewriteSession = (extension.getActiveRewriteSession() != null) ? null : extension.startRewriteSession(type);
} else {
setRedraw(false);
}
if (fContentFormatter instanceof IContentFormatterExtension) {
IContentFormatterExtension extension = (IContentFormatterExtension) fContentFormatter;
IFormattingContext context = new FormattingContext();
context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(formatDocument));
context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
extension.format(document, context);
} else {
fContentFormatter.format(document, region);
}
} finally {
try {
if (rewriteSession != null) {
IDocumentExtension4 extension = (IDocumentExtension4) document;
extension.stopRewriteSession(rewriteSession);
} else {
setRedraw(true);
}
} finally {
// end recording
selection = getTextWidget().getSelection();
cursorPosition = selection.x;
selectionLength = selection.y - selection.x;
endRecording(cursorPosition, selectionLength);
}
}
break;
case FORMAT_ACTIVE_ELEMENTS:
rewriteSession = null;
document = getDocument();
try {
/*
* This command will format the node at cursor position
* (and all its children)
*/
// begin recording
beginRecording(FORMAT_ACTIVE_ELEMENTS_TEXT, FORMAT_ACTIVE_ELEMENTS_TEXT, cursorPosition, selectionLength);
IRegion region = null;
Point s = getSelectedRange();
if (s.y > -1) {
// only format node at cursor position
region = new Region(s.x, s.y);
}
if (document instanceof IDocumentExtension4) {
IDocumentExtension4 extension = (IDocumentExtension4) document;
DocumentRewriteSessionType type = (selection.y == 0 || selection.y > MAX_SMALL_FORMAT_LENGTH) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
rewriteSession = (extension.getActiveRewriteSession() != null) ? null : extension.startRewriteSession(type);
} else {
setRedraw(false);
}
if (fContentFormatter instanceof IContentFormatterExtension) {
IContentFormatterExtension extension = (IContentFormatterExtension) fContentFormatter;
IFormattingContext context = new FormattingContext();
context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.FALSE);
context.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
extension.format(getDocument(), context);
} else {
fContentFormatter.format(getDocument(), region);
}
} finally {
try {
if (rewriteSession != null) {
IDocumentExtension4 extension = (IDocumentExtension4) document;
extension.stopRewriteSession(rewriteSession);
} else {
setRedraw(true);
}
} finally {
// end recording
selection = getTextWidget().getSelection();
cursorPosition = selection.x;
selectionLength = selection.y - selection.x;
endRecording(cursorPosition, selectionLength);
}
}
break;
default:
super.doOperation(operation);
}
}
use of org.eclipse.jface.text.DocumentRewriteSessionType in project webtools.sourceediting by eclipse.
the class AbstractStructuredFormatProcessor method formatModel.
public void formatModel(IStructuredModel structuredModel, int start, int length) {
if (structuredModel != null) {
// for debugging purposes
long startTime = System.currentTimeMillis();
IDocumentExtension4 docExt4 = null;
if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
}
DocumentRewriteSession rewriteSession = null;
try {
// whenever formatting model, fire abouttochange/modelchanged
structuredModel.aboutToChangeModel();
DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
if ((start == 0) && (length == structuredModel.getStructuredDocument().getLength()))
setFormatWithSiblingIndent(structuredModel, false);
else
setFormatWithSiblingIndent(structuredModel, true);
if ((start >= 0) && (length >= 0) && (start + length <= structuredModel.getStructuredDocument().getLength())) {
List activeNodes = getAllActiveNodes(structuredModel, start, length);
if (activeNodes.size() > 0) {
Node firstNode = (Node) activeNodes.get(0);
Node lastNode = (Node) activeNodes.get(activeNodes.size() - 1);
boolean done = false;
Node eachNode = firstNode;
Node nextNode = null;
while (!done) {
// update "done"
done = (eachNode == lastNode);
/*
* get next sibling before format because eachNode
* may be deleted, for example when it's an empty
* text node
*/
nextNode = eachNode.getNextSibling();
// format each node
formatNode(eachNode);
// update each node
if ((nextNode != null) && (nextNode.getParentNode() == null))
// nextNode is deleted during format
eachNode = eachNode.getNextSibling();
else
eachNode = nextNode;
// We don't want an infinite loop here.
if (eachNode == null)
done = true;
}
}
}
} finally {
// we need two finally's, just in case first fails
try {
if ((docExt4 != null) && (rewriteSession != null))
docExt4.stopRewriteSession(rewriteSession);
} finally {
// always make sure to fire changedmodel when done
structuredModel.changedModel();
}
}
if (Logger.DEBUG_FORMAT) {
long endTime = System.currentTimeMillis();
// $NON-NLS-1$
System.out.println("formatModel time: " + (endTime - startTime));
}
}
}
use of org.eclipse.jface.text.DocumentRewriteSessionType in project webtools.sourceediting by eclipse.
the class FormatProcessorCSS method formatModel.
public void formatModel(IStructuredModel structuredModel, int start, int length) {
CSSFormatUtil formatUtil = CSSFormatUtil.getInstance();
if (structuredModel instanceof ICSSModel) {
// BUG102822 take advantage of IDocumentExtension4
IDocumentExtension4 docExt4 = null;
if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
}
DocumentRewriteSession rewriteSession = null;
try {
DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
ICSSDocument doc = ((ICSSModel) structuredModel).getDocument();
IStructuredDocumentRegion startRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(start);
IStructuredDocumentRegion endRegion = structuredModel.getStructuredDocument().getRegionAtCharacterOffset(start + length);
if (startRegion != null && endRegion != null) {
start = startRegion.getStart();
CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
StringBuffer buf = formatter.format(doc, new Region(start, (endRegion.getEnd() - start)));
if (buf != null) {
formatUtil.replaceSource(doc.getModel(), start, endRegion.getEnd() - start, buf.toString());
}
}
} finally {
// BUG102822 take advantage of IDocumentExtension4
if (docExt4 != null && rewriteSession != null)
docExt4.stopRewriteSession(rewriteSession);
}
} else if (structuredModel instanceof IDOMModel) {
List cssnodes = formatUtil.collectCSSNodes(structuredModel, start, length);
if (cssnodes != null && !cssnodes.isEmpty()) {
ICSSModel model = null;
// BUG102822 take advantage of IDocumentExtension4
IDocumentExtension4 docExt4 = null;
if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) {
docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument();
}
DocumentRewriteSession rewriteSession = null;
try {
DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType);
for (int i = 0; i < cssnodes.size(); i++) {
ICSSNode node = (ICSSNode) cssnodes.get(i);
CSSSourceFormatter formatter = CSSSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) node);
StringBuffer buf = formatter.format(node);
if (buf != null) {
int startOffset = ((IndexedRegion) node).getStartOffset();
int endOffset = ((IndexedRegion) node).getEndOffset();
if (model == null) {
model = node.getOwnerDocument().getModel();
}
formatUtil.replaceSource(model, startOffset, endOffset - startOffset, buf.toString());
}
}
} finally {
// BUG102822 take advantage of IDocumentExtension4
if (docExt4 != null && rewriteSession != null)
docExt4.stopRewriteSession(rewriteSession);
}
}
}
}
use of org.eclipse.jface.text.DocumentRewriteSessionType in project webtools.sourceediting by eclipse.
the class BasicStructuredDocument method internalStopRewriteSession.
/**
* NOT-API. Final protected so clients may call this method if needed, but
* cannot override.
*
* @param session
*/
protected final void internalStopRewriteSession(DocumentRewriteSession session) {
if (fActiveRewriteSession == session) {
DocumentRewriteSessionType sessionType = session.getSessionType();
if (DocumentRewriteSessionType.SEQUENTIAL == sessionType || DocumentRewriteSessionType.STRICTLY_SEQUENTIAL == sessionType)
stopSequentialRewrite();
stopRewriteSessionOnPartitioners(session);
ILineTracker tracker = getTracker();
if (tracker instanceof ILineTrackerExtension) {
ILineTrackerExtension extension = (ILineTrackerExtension) tracker;
extension.stopRewriteSession(session, get());
}
fActiveRewriteSession = null;
DocumentRewriteSessionEvent event = new DocumentRewriteSessionEvent(this, session, DocumentRewriteSessionEvent.SESSION_STOP);
fireDocumentRewriteSessionEvent(event);
}
}
use of org.eclipse.jface.text.DocumentRewriteSessionType in project webtools.sourceediting by eclipse.
the class JobSafeStructuredDocument method startRewriteSession.
public DocumentRewriteSession startRewriteSession(DocumentRewriteSessionType sessionType) throws IllegalStateException {
DocumentRewriteSession session = null;
IExecutionDelegate executionDelegate = getExecutionDelegate();
if (executionDelegate == null) {
session = internalStartRewriteSession(sessionType);
} else {
final Object[] resultSlot = new Object[1];
final DocumentRewriteSessionType finalSessionType = sessionType;
JobSafeRunnable runnable = new JobSafeRunnable() {
public void run() throws Exception {
resultSlot[0] = internalStartRewriteSession(finalSessionType);
}
};
executionDelegate.execute(runnable);
if (resultSlot[0] instanceof Throwable) {
throw new RuntimeException((Throwable) resultSlot[0]);
} else {
session = (DocumentRewriteSession) resultSlot[0];
}
}
return session;
}
Aggregations