org.eclipse.jface.text.formatter.FormattingContextProperties Java Examples

The following examples show how to use org.eclipse.jface.text.formatter.FormattingContextProperties. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CommonMultiPassContentFormatter.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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());
		}
	}
}
 
Example #2
Source File: CompilationUnitEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: AbstractFormattingStrategy.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
@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);
}
 
Example #4
Source File: JavaSourceViewer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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;
}
 
Example #5
Source File: JavaFormattingStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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));
}
 
Example #6
Source File: CompilationUnitPreview.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@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);
	}
   }
 
Example #7
Source File: IndependentMultiPassContentFormatter.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 4 votes vote down vote up
private void setToTempDocument(IFormattingContext context) {
  context.setProperty(FormattingContextProperties.CONTEXT_MEDIUM,
      tempDocument);
}
 
Example #8
Source File: CommonProjectionViewer.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
@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;
}