下面列出了怎么用org.eclipse.jface.text.formatter.IContentFormatter的API类实例代码及写法,或者点击链接到github查看源代码。
public IContentFormatter getContentFormatter( ISourceViewer sourceViewer){
ContentFormatter formatter = new ContentFormatter();
XMLFormattingStrategy formattingStrategy = new XMLFormattingStrategy();
DefaultFormattingStrategy defaultStrategy = new DefaultFormattingStrategy();
TextFormattingStrategy textStrategy = new TextFormattingStrategy();
DocTypeFormattingStrategy doctypeStrategy = new DocTypeFormattingStrategy();
PIFormattingStrategy piStrategy = new PIFormattingStrategy();
formatter.setFormattingStrategy(defaultStrategy,
IDocument.DEFAULT_CONTENT_TYPE);
formatter.setFormattingStrategy(textStrategy,
XMLPartitionScanner.XML_TEXT);
formatter.setFormattingStrategy(doctypeStrategy,
XMLPartitionScanner.XML_DOCTYPE);
formatter.setFormattingStrategy(piStrategy, XMLPartitionScanner.XML_PI);
formatter.setFormattingStrategy(textStrategy,
XMLPartitionScanner.XML_CDATA);
formatter.setFormattingStrategy(formattingStrategy,
XMLPartitionScanner.XML_START_TAG);
formatter.setFormattingStrategy(formattingStrategy,
XMLPartitionScanner.XML_END_TAG);
return formatter;
}
public static IContentFormatter createFormatter(String partitioning) {
/*
* Ideally, we would have no master strategy and two slave strategies, one
* for XML and one for CSS. The problem is, XMLFormattingStrategy won't work
* properly since some paired opening and closing tags are spread across
* different partitions (for example, the <ui:style> tag since the CSS
* partition will be between the opening and closing tag.)
*/
IndependentMultiPassContentFormatter formatter = new IndependentMultiPassContentFormatter(
partitioning, IXMLPartitions.XML_DEFAULT, new StructuredDocumentCloner(
partitioning, UIBINDER_XML_PARTITIONER_FACTORY));
formatter.setMasterStrategy(new XMLFormattingStrategy());
formatter.setSlaveStrategy2(new InlinedCssFormattingStrategy(),
ICSSPartitions.STYLE);
/*
* If the <ui:style> contains a '%' (for example, in something like
* "width: 50%;"), the XML following the <ui:style> tag will not be
* formatted. For the '%', the region type is UNDEFINED, and there is no
* corresponding DOM node. Before formatting, the
* DefaultXMLPartitionFormatter ensures the current region matches the
* current DOM node's first region, and since there is no DOM node for the
* UNDEFINED region, the formatting abruptly stops. To workaround this
* issue, we replace the CSS with whitespace when the master formatter runs.
*/
formatter.setReplaceSlavePartitionsDuringMasterFormat(true);
formatter.setCheckForNonwhitespaceChanges(true);
return formatter;
}
@Override
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
IndependentMultiPassContentFormatter formatter = new IndependentMultiPassContentFormatter(
getConfiguredDocumentPartitioning(sourceViewer),
IDocument.DEFAULT_CONTENT_TYPE, JAVA_DOCUMENT_CLONER);
formatter.setMasterStrategy(new JavaFormattingStrategy());
formatter.setSlaveStrategy2(new JsniFormattingStrategy(),
GWTPartitions.JSNI_METHOD);
return formatter;
}
@Override
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
if (fConfigureFormatter)
return super.getContentFormatter(sourceViewer);
else
return null;
}
@Override
protected void doFormatPreview() {
if (fPreviewText == null) {
fPreviewDocument.set(""); //$NON-NLS-1$
return;
}
fPreviewDocument.set(fPreviewText);
fSourceViewer.setRedraw(false);
final IFormattingContext context = new JavaFormattingContext();
try {
final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer);
if (formatter instanceof IContentFormatterExtension) {
final IContentFormatterExtension extension = (IContentFormatterExtension) formatter;
context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, fWorkingValues);
context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true));
extension.format(fPreviewDocument, context);
} else
formatter.format(fPreviewDocument, new Region(0, fPreviewDocument.getLength()));
} catch (Exception e) {
final IStatus status= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR,
FormatterMessages.JavaPreview_formatter_exception, e);
JavaPlugin.log(status);
} finally {
context.dispose();
fSourceViewer.setRedraw(true);
}
}
@Override
public void bind(IXtextDocument document, IContentFormatter contentFormatter) {
assert document != null;
assert contentFormatter != null;
this.document = document;
this.contentFormatter = contentFormatter;
}
@Override
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
return null;
}
@Override
public IContentFormatter createConfiguredFormatter(SourceViewerConfiguration cfg, ISourceViewer sourceViewer) {
return formatter.get();
}
@Override
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
if (contentFormatterFactory != null)
return contentFormatterFactory.createConfiguredFormatter(this, sourceViewer);
return super.getContentFormatter(sourceViewer);
}
@Override
public IContentFormatter createConfiguredFormatter(
SourceViewerConfiguration configuration, ISourceViewer sourceViewer) {
return new ContentFormatter();
}
@Override
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
IEditorInput input = getEditor().getEditorInput();
return input != null ? new TypeScriptContentFormatter(EditorUtils.getResource(input)) : null;
}
@Override
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
return UiBinderFormatter.createFormatter(getConfiguredDocumentPartitioning(sourceViewer));
}
/**
* Collects the code formatters by the supported content-types and returns a new {@link import
* org.eclipse.jface.text.formatter.MultiPassContentFormatter} that holds them.<br>
* The returned content formatter is computed from the result of {@link #getTopContentTypes()}. The first element in
* the returned array should define the 'master' formatter. While the rest of the elements should contain the
* 'slave' formatter. <br>
* Note that each slave formatter is located in the last element of each inner-array that was returned from the
* getTopContentTypes call.
*/
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer)
{
final String[][] contentTypes = getTopContentTypes();
final CommonMultiPassContentFormatter formatter = new CommonMultiPassContentFormatter(
getConfiguredDocumentPartitioning(sourceViewer), IDocument.DEFAULT_CONTENT_TYPE);
boolean masterSet = false;
Set<String> addedFormatters = new HashSet<String>();
for (String contentTypeArr[] : contentTypes)
{
// The first item in the array should contain the master formatter strategy
// In case it starts with the HTML prefix (like in PHP, ERB etc.), we try to set
// the master to the HTML formatter.
if (!masterSet && contentTypeArr[0].startsWith(CONTENTTYPE_HTML_PREFIX))
{
if (ScriptFormatterManager.hasFormatterFor(CONTENTTYPE_HTML_PREFIX))
{
formatter.setMasterStrategy(CONTENTTYPE_HTML_PREFIX);
masterSet = true;
addedFormatters.add(CONTENTTYPE_HTML_PREFIX);
}
else
{
IdeLog.logWarning(CommonEditorPlugin.getDefault(), MessageFormat.format(
"Could not located an expected code formatter for ''{0}''", CONTENTTYPE_HTML_PREFIX)); //$NON-NLS-1$
}
}
String contentType = contentTypeArr[contentTypeArr.length - 1];
if (!addedFormatters.contains(contentType) && ScriptFormatterManager.hasFormatterFor(contentType))
{
if (!masterSet)
{
formatter.setMasterStrategy(contentType);
masterSet = true;
}
else
{
formatter.setSlaveStrategy(contentType);
}
}
}
return formatter;
}
@Override
public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
final MultiPassContentFormatter formatter= new MultiPassContentFormatter(getConfiguredDocumentPartitioning(sourceViewer), IDocument.DEFAULT_CONTENT_TYPE);
formatter.setMasterStrategy(new JavaFormattingStrategy());
return formatter;
}
/** Create an instance of document auto formatter.
*
* @param document the Xtext document associated to this auto-formatter.
* @param contentFormatter the formatter of content to be used.
*/
default void bind(IXtextDocument document, IContentFormatter contentFormatter) {
//
}
IContentFormatter createConfiguredFormatter(SourceViewerConfiguration configuration, ISourceViewer sourceViewer);