<commit_before>package com.reprezen.swagedit.editor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dadacoalition.yedit.YEditLog;
import org.dadacoalition.yedit.editor.IDocumentIdleListener;
import org.dadacoalition.yedit.editor.YEdit;
import org.dadacoalition.yedit.editor.YEditSourceViewerConfiguration;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
import org.eclipse.jface.text.source.projection.ProjectionSupport;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.yaml.snakeyaml.error.YAMLException;

import com.reprezen.swagedit.validation.SwaggerError;
import com.reprezen.swagedit.validation.Validator;

/**
 * SwagEdit editor.
 * 
 */
public class SwaggerEditor extends YEdit {

	public static final String ID = "com.reprezen.swagedit.editor";

	private final Validator validator = new Validator();
	private ProjectionSupport projectionSupport;
	private Annotation[] oldAnnotations;
	private ProjectionAnnotationModel annotationModel;

	private final IDocumentListener changeListener = new IDocumentListener() {
		@Override
		public void documentAboutToBeChanged(DocumentEvent event) {}
		@Override
		public void documentChanged(DocumentEvent event) {
			if (event.getDocument() instanceof SwaggerDocument) {
				final SwaggerDocument document = (SwaggerDocument) event.getDocument();

				Display.getCurrent().asyncExec(new Runnable() {
					@Override
					public void run() {
						document.onChange();
						validate();
					}
				});
			}
		}
	};

	public SwaggerEditor() {
		super();
		setDocumentProvider(new SwaggerDocumentProvider());
	}

	@Override
	protected YEditSourceViewerConfiguration createSourceViewerConfiguration() {
		final SwaggerSourceViewerConfiguration configuration = new SwaggerSourceViewerConfiguration();
		configuration.setEditor(this);
		return configuration;
	}

	@Override
	protected void doSetInput(IEditorInput input) throws CoreException {
		super.doSetInput(input);
		
		getDocumentProvider().getDocument(getEditorInput()).addDocumentListener(changeListener);
	}
	
	public ProjectionViewer getProjectionViewer() {
	    return (ProjectionViewer) getSourceViewer();
	}

	@Override
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);

		ProjectionViewer viewer = getProjectionViewer();

		projectionSupport = new ProjectionSupport(viewer, getAnnotationAccess(), getSharedColors());
		projectionSupport.install();

		// turn projection mode on
		viewer.doOperation(ProjectionViewer.TOGGLE);

		annotationModel = viewer.getProjectionAnnotationModel();
	}

	@Override
	protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
		ISourceViewer viewer = new ProjectionViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(),
				styles);
		getSourceViewerDecorationSupport(viewer);
		return viewer;
	}

	public void updateFoldingStructure(List<Position> positions) {
		final Map<Annotation, Position> newAnnotations = new HashMap<Annotation, Position>();
		for (Position position: positions) {
			newAnnotations.put(new ProjectionAnnotation(), position);<commit_msg>ZEN-1979: Provide placeholder for external contributions<commit_after>package com.reprezen.swagedit.editor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dadacoalition.yedit.YEditLog;
import org.dadacoalition.yedit.editor.IDocumentIdleListener;
import org.dadacoalition.yedit.editor.YEdit;
import org.dadacoalition.yedit.editor.YEditSourceViewerConfiguration;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
import org.eclipse.jface.text.source.projection.ProjectionSupport;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.yaml.snakeyaml.error.YAMLException;

import com.reprezen.swagedit.validation.SwaggerError;
import com.reprezen.swagedit.validation.Validator;

/**
 * SwagEdit editor.
 * 
 */
public class SwaggerEditor extends YEdit {

	public static final String ID = "com.reprezen.swagedit.editor";

	private final Validator validator = new Validator();
	private ProjectionSupport projectionSupport;
	private Annotation[] oldAnnotations;
	private ProjectionAnnotationModel annotationModel;
	private Composite topPanel;

	private final IDocumentListener changeListener = new IDocumentListener() {
		@Override
		public void documentAboutToBeChanged(DocumentEvent event) {}
		@Override
		public void documentChanged(DocumentEvent event) {
			if (event.getDocument() instanceof SwaggerDocument) {
				final SwaggerDocument document = (SwaggerDocument) event.getDocument();

				Display.getCurrent().asyncExec(new Runnable() {
					@Override
					public void run() {
						document.onChange();
						validate();
					}
				});
			}
		}
	};

	public SwaggerEditor() {
		super();
		setDocumentProvider(new SwaggerDocumentProvider());
	}

	@Override
	protected YEditSourceViewerConfiguration createSourceViewerConfiguration() {
		final SwaggerSourceViewerConfiguration configuration = new SwaggerSourceViewerConfiguration();
		configuration.setEditor(this);
		return configuration;
	}

	@Override
	protected void doSetInput(IEditorInput input) throws CoreException {
		super.doSetInput(input);
		
		getDocumentProvider().getDocument(getEditorInput()).addDocumentListener(changeListener);
	}
	
	public ProjectionViewer getProjectionViewer() {
	    return (ProjectionViewer) getSourceViewer();
	}

	@Override
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);

		ProjectionViewer viewer = getProjectionViewer();

		projectionSupport = new ProjectionSupport(viewer, getAnnotationAccess(), getSharedColors());
		projectionSupport.install();

		// turn projection mode on
		viewer.doOperation(ProjectionViewer.TOGGLE);

		annotationModel = viewer.getProjectionAnnotationModel();
	}

    @Override
    protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
        Composite composite = new Composite(parent, SWT.NONE);
        GridLayout compositeLayout = new GridLayout(1, false);
        compositeLayout.marginHeight = 0;
        compositeLayout.marginWidth = 0;
        compositeLayout.horizontalSpacing = 0;
        compositeLayout.verticalSpacing = 0;
        composite.setLayout(compositeLayout);

        topPanel = new Composite(composite, SWT.NONE);
        topPanel.setLayout(new StackLayout());
        topPanel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

        Composite editorComposite = new Composite(composite, SWT.NONE);
        editorComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
        fillLayout.marginHeight = 0;
        fillLayout.marginWidth = 0;
        fillLayout.spacing = 0;
        editorComposite.setLayout(fillLayout);

        ISourceViewer result = doCreateSourceViewer(editorComposite, ruler, styles);

        return result;
        
    }
    
    /**
     * SwaggerEditor provides additional hidden panel on top of the source viewer, where external integrations can put
     * their UI.
     * <p/>
     * The panel is only a placeholder, that is:
     * <ul>
     * <li>it is not visible by default</li>
     * <li>it has a {@link StackLayout} and expect single composite to be created per contribution</li>
     * <li>if there are more than one contributors, it is their responsibility to manage {@link StackLayout#topControl}</li>
     * </ul>
     */
    public Composite getTopPanel() {
        return topPanel;
    }

	protected ISourceViewer doCreateSourceViewer(Composite parent, IVerticalRuler ruler, int styles) {
		ISourceViewer viewer = new ProjectionViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible(),
				styles);
		getSourceViewerDecorationSupport(viewer);
		return viewer;
	}

	public void updateFoldingStructure(List<Position> positions) {
		final Map<Annotation, Position> newAnnotations = new HashMap<Annotation, Position>();
		for (Position position: positions) {
			newAnnotations.put(new ProjectionAnnotation(), position);
{'fraction_non_alphanumeric': 0.0949367088607595, 'fraction_numerical': 0.0012055455093429777, 'mean_word_length': 5.187072715972654, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '831360', 'n_tokens_mistral': 3050, 'n_tokens_neox': 2832, 'n_words': 586}