下面列出了怎么用org.eclipse.jface.text.formatter.FormattingContextProperties的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Update the fomatting context to reflect to the script formatter that should be used with the given content-type.
*
* @param context
* @param region
* @param lastContentType
*/
private void updateContex(IFormattingContext context, String contentType, int offset, int length)
{
IScriptFormatterFactory factory = ScriptFormatterManager.getSelected(contentType);
if (factory != null)
{
factory.setMainContentType(contentType);
if (context != null)
{
context.setProperty(ScriptFormattingContextProperties.CONTEXT_FORMATTER_ID, factory.getId());
context.setProperty(FormattingContextProperties.CONTEXT_PARTITION, new TypedPosition(offset, length,
contentType));
context.setProperty(ScriptFormattingContextProperties.CONTEXT_FORMATTER_CAN_CONSUME_INDENTATION,
factory.canConsumePreviousIndent());
}
}
}
@Override
public IFormattingContext createFormattingContext() {
IFormattingContext context= new JavaFormattingContext();
Map<String, String> preferences;
IJavaElement inputJavaElement= getInputJavaElement();
IJavaProject javaProject= inputJavaElement != null ? inputJavaElement.getJavaProject() : null;
if (javaProject == null)
preferences= new HashMap<String, String>(JavaCore.getOptions());
else
preferences= new HashMap<String, String>(javaProject.getOptions(true));
context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, preferences);
return context;
}
@Override
public void formatterStarts(IFormattingContext context) {
super.formatterStarts(context);
IDocument document = (IDocument) context.getProperty(FormattingContextProperties.CONTEXT_MEDIUM);
TypedPosition position = (TypedPosition) context.getProperty(FormattingContextProperties.CONTEXT_PARTITION);
partitions.addLast(position);
documents.addLast(document);
}
@Override
public IFormattingContext createFormattingContext() {
// it's ok to use instance preferences here as subclasses replace
// with project dependent versions (see CompilationUnitEditor.AdaptedSourceViewer)
IFormattingContext context= new JavaFormattingContext();
Map<String, String> map= new HashMap<String, String>(JavaCore.getOptions());
context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, map);
return context;
}
@Override
public void formatterStarts(final IFormattingContext context) {
super.formatterStarts(context);
fPartitions.addLast((TypedPosition) context.getProperty(FormattingContextProperties.CONTEXT_PARTITION));
fDocuments.addLast((IDocument) context.getProperty(FormattingContextProperties.CONTEXT_MEDIUM));
}
@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);
}
}
private void setToTempDocument(IFormattingContext context) {
context.setProperty(FormattingContextProperties.CONTEXT_MEDIUM,
tempDocument);
}
@SuppressWarnings("rawtypes")
@Override
public IFormattingContext createFormattingContext()
{
final IFormattingContext context = super.createFormattingContext();
try
{
QualifiedContentType contentType = CommonEditorPlugin.getDefault().getDocumentScopeManager()
.getContentType(getDocument(), 0);
if (contentType != null && contentType.getPartCount() > 0)
{
for (String ct : contentType.getParts())
{
String mainContentType = ct;
// We need to make sure that in case the given content type is actually a nested language in
// HTML, we look for the HTML formatter factory because it should be the 'Master' formatter.
if (mainContentType.startsWith(CommonSourceViewerConfiguration.CONTENTTYPE_HTML_PREFIX))
{
mainContentType = CommonSourceViewerConfiguration.CONTENTTYPE_HTML_PREFIX;
}
final IScriptFormatterFactory factory = ScriptFormatterManager.getSelected(mainContentType);
if (factory != null)
{
// The code above might change the content type that is used to
// get the formatter, but we still need to save the original content-type so that the
// IScriptFormatter instance will handle the any required parsing by calling the right
// IParser.
factory.setMainContentType(contentType.getParts()[0]);
ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class);
if (textEditor != null)
{
IResource file = (IResource) textEditor.getEditorInput().getAdapter(IResource.class);
context.setProperty(ScriptFormattingContextProperties.CONTEXT_FORMATTER_ID, factory.getId());
IProject project = (file != null) ? file.getProject() : null;
Map preferences = factory.retrievePreferences(new PreferencesLookupDelegate(project));
context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, preferences);
}
break;
}
}
}
}
catch (BadLocationException e)
{
}
return context;
}