use of org.eclipse.jface.text.formatter.IContentFormatterExtension in project webtools.sourceediting by eclipse.
the class TestJSPContentFormatter method formatAndAssertEquals.
private void formatAndAssertEquals(String beforePath, String afterPath, boolean resetPreferences) throws UnsupportedEncodingException, IOException, CoreException {
IStructuredModel beforeModel = null, afterModel = null;
try {
beforeModel = getModelForEdit(beforePath);
assertNotNull("could not retrieve structured model for : " + beforePath, beforeModel);
afterModel = getModelForEdit(afterPath);
assertNotNull("could not retrieve structured model for : " + afterPath, afterModel);
if (resetPreferences) {
resetPreferencesToDefault();
}
SourceViewerConfiguration configuration = (SourceViewerConfiguration) ExtendedConfigurationBuilder.getInstance().getConfiguration(ExtendedConfigurationBuilder.SOURCEVIEWERCONFIGURATION, "org.eclipse.jst.jsp.core.jspsource");
IContentFormatterExtension formatter = (IContentFormatterExtension) configuration.getContentFormatter(null);
IDocument document = beforeModel.getStructuredDocument();
Region region = new Region(0, document.getLength());
fContext.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
formatter.format(document, fContext);
ByteArrayOutputStream formattedBytes = new ByteArrayOutputStream();
// "beforeModel" should now be
beforeModel.save(formattedBytes);
// after the formatter
ByteArrayOutputStream afterBytes = new ByteArrayOutputStream();
afterModel.save(afterBytes);
String expectedContents = new String(afterBytes.toByteArray(), UTF_8);
expectedContents = StringUtils.replace(expectedContents, "\r\n", "\r");
expectedContents = StringUtils.replace(expectedContents, "\r", "\n");
String actualContents = new String(formattedBytes.toByteArray(), UTF_8);
actualContents = StringUtils.replace(actualContents, "\r\n", "\r");
actualContents = StringUtils.replace(actualContents, "\r", "\n");
assertTrue(onlyWhiteSpaceDiffers(expectedContents, actualContents));
assertEquals("Formatted document differs from the expected.", expectedContents, actualContents);
} finally {
if (beforeModel != null)
beforeModel.releaseFromEdit();
if (afterModel != null)
afterModel.releaseFromEdit();
}
}
use of org.eclipse.jface.text.formatter.IContentFormatterExtension 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.formatter.IContentFormatterExtension in project webtools.sourceediting by eclipse.
the class TestContentFormatter method formatAndAssertEquals.
private void formatAndAssertEquals(String beforePath, String afterPath, boolean resetPreferences) throws UnsupportedEncodingException, IOException, CoreException {
IStructuredModel beforeModel = null, afterModel = null;
try {
beforeModel = getModelForEdit(beforePath);
assertNotNull("could not retrieve structured model for : " + beforePath, beforeModel);
afterModel = getModelForEdit(afterPath);
assertNotNull("could not retrieve structured model for : " + afterPath, afterModel);
if (resetPreferences) {
resetPreferencesToDefault();
}
SourceViewerConfiguration configuration = new StructuredTextViewerConfigurationJSP();
IContentFormatterExtension formatter = (IContentFormatterExtension) configuration.getContentFormatter(null);
IDocument document = beforeModel.getStructuredDocument();
Region region = new Region(0, document.getLength());
fContext.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
formatter.format(document, fContext);
ByteArrayOutputStream formattedBytes = new ByteArrayOutputStream();
// "beforeModel" should now be
beforeModel.save(formattedBytes);
// after the formatter
ByteArrayOutputStream afterBytes = new ByteArrayOutputStream();
afterModel.save(afterBytes);
String expectedContents = new String(afterBytes.toByteArray(), UTF_8);
expectedContents = StringUtils.replace(expectedContents, "\r\n", "\r");
expectedContents = StringUtils.replace(expectedContents, "\r", "\n");
String actualContents = new String(formattedBytes.toByteArray(), UTF_8);
actualContents = StringUtils.replace(actualContents, "\r\n", "\r");
actualContents = StringUtils.replace(actualContents, "\r", "\n");
assertTrue(onlyWhiteSpaceDiffers(expectedContents, actualContents));
assertEquals("Formatted document differs from the expected.", expectedContents, actualContents);
} finally {
if (beforeModel != null)
beforeModel.releaseFromEdit();
if (afterModel != null)
afterModel.releaseFromEdit();
}
}
use of org.eclipse.jface.text.formatter.IContentFormatterExtension in project webtools.sourceediting by eclipse.
the class FormattingTests method formatAndAssertEquals.
/**
* @param beforePath - the path of the before file
* @param afterPath - the path of the after file, <b>must not be the same as the before file</b>
* @param configuration
* @throws UnsupportedEncodingException
* @throws IOException
* @throws CoreException
*
* @see org.eclipse.wst.xml.core.tests.format.TestPartitionFormatterXML#formatAndAssertEquals
*/
private void formatAndAssertEquals(String beforePath, String afterPath, SourceViewerConfiguration configuration) throws UnsupportedEncodingException, IOException, CoreException {
IStructuredModel beforeModel = null, afterModel = null;
ISourceViewer viewer = null;
try {
beforeModel = getModelForEdit(beforePath);
assertNotNull("could not retrieve structured model for : " + beforePath, beforeModel);
JsTranslationAdapterFactory.setupAdapterFactory(beforeModel);
afterModel = getModelForEdit(afterPath);
assertNotNull("could not retrieve structured model for : " + afterPath, afterModel);
// normalize contents
IStructuredDocument document = beforeModel.getStructuredDocument();
String normalizedContents = document.get();
normalizedContents = StringUtils.replace(normalizedContents, "\r\n", "\n");
normalizedContents = StringUtils.replace(normalizedContents, "\r", "\n");
document.set(normalizedContents);
viewer = getConfiguredViewer(document, configuration);
assertNotNull("Could not get viewer to run test", viewer);
// do the format
IContentFormatterExtension formatter = (IContentFormatterExtension) configuration.getContentFormatter(viewer);
IFormattingContext fContext = new FormattingContext();
Region region = new Region(0, document.getLength());
fContext.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
fContext.setProperty(FormattingContextProperties.CONTEXT_REGION, region);
formatter.format(document, fContext);
// get the contents
String actualContents = beforeModel.getStructuredDocument().get();
String expectedContents = afterModel.getStructuredDocument().get();
/* Make some adjustments to ignore cross platform line delimiter issues */
expectedContents = StringUtils.replace(expectedContents, "\r\n", "\n");
expectedContents = StringUtils.replace(expectedContents, "\r", "\n");
actualContents = StringUtils.replace(actualContents, "\r\n", "\n");
actualContents = StringUtils.replace(actualContents, "\r", "\n");
onlyWhiteSpaceDiffers(expectedContents, actualContents);
assertEquals("Formatted document differs from the expected.", expectedContents, actualContents);
} finally {
if (beforeModel != null) {
try {
beforeModel.releaseFromEdit();
} catch (Exception e) {
// ignore
}
}
if (afterModel != null) {
try {
afterModel.releaseFromEdit();
} catch (Exception e) {
// ignore
}
}
if (viewer != null) {
StyledText text = viewer.getTextWidget();
if (text != null && !text.isDisposed()) {
text.dispose();
}
}
}
}
Aggregations