<commit_before>package com.redhat.ceylon.eclipse.code.editor;

/*******************************************************************************
* Copyright (c) 2007 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    <NAME> (<EMAIL>) - initial API and implementation
*******************************************************************************/

import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.PARSE_ANNOTATION_TYPE;
import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.PARSE_ANNOTATION_TYPE_ERROR;
import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.PARSE_ANNOTATION_TYPE_INFO;
import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.PARSE_ANNOTATION_TYPE_WARNING;
import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.isParseAnnotation;
import static org.eclipse.core.resources.IMarker.SEVERITY_ERROR;
import static org.eclipse.core.resources.IMarker.SEVERITY_INFO;
import static org.eclipse.core.resources.IMarker.SEVERITY_WARNING;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelExtension;
import org.eclipse.ui.texteditor.IDocumentProvider;

import com.redhat.ceylon.compiler.typechecker.tree.Message;
import com.redhat.ceylon.eclipse.util.ErrorVisitor;

/**
 * An implementation of the IMessageHandler interface that creates editor annotations
 * directly from messages. Used for live parsing within a source editor (cf. building,
 * which uses the class MarkerCreator to create markers).
 * @author rmfuhrer
 */
public class AnnotationCreator extends ErrorVisitor {
	
    private static class PositionedMessage {
        public final String message;
        public final Position pos;
        public final int code;
        public final int severity;
        
        private PositionedMessage(String msg, Position pos, 
        		int severity, int code) {
            this.message= msg;
            this.pos= pos;
            this.code = code;
            this.severity = severity;
        }

        @Override
        public String toString() {
        	return pos.toString() + " - "+ message;
        }
    }

    private final CeylonEditor editor;
    private final List<PositionedMessage> messages= new LinkedList<PositionedMessage>();
    private final List<Annotation> annotations= new LinkedList<Annotation>();

    public AnnotationCreator(CeylonEditor textEditor) {
        editor= textEditor;
    }

    @Override
    public void handleMessage(int startOffset, int endOffset,
            int startCol, int startLine, Message error) {
        messages.add(new PositionedMessage(error.getMessage(), 
        		new Position(startOffset, endOffset-startOffset+1), 
        		getSeverity(error, warnForErrors), 
        		error.getCode()));
    }

    public void updateAnnotations() {
    	if (editor.isBackgroundParsingPaused()) {
    		messages.clear();
    		return;
    	}
        IDocumentProvider docProvider= editor.getDocumentProvider();
        if (docProvider!=null) {
            IAnnotationModel model= docProvider.getAnnotationModel(editor.getEditorInput());
            if (model instanceof IAnnotationModelExtension) {
                IAnnotationModelExtension modelExt= (IAnnotationModelExtension) model;
                Annotation[] oldAnnotations= annotations.toArray(new Annotation[annotations.size()]);
                Map<Annotation,Position> newAnnotations= new HashMap<Annotation,Position>(messages.size());
                for (PositionedMessage pm: messages) {
                    Annotation a= createAnnotation(pm);
                    newAnnotations.put(a, pm.pos);
                    annotations.add(a);
                }
                modelExt.replaceAnnotations(oldAnnotations, newAnnotations);
            } 
            else if (model != null) { // model could be null if, e.g., we're directly browsing a file version in a src repo
                for (Iterator i= model.getAnnotationIterator(); i.hasNext(); ) {
                    Annotation a= (Annotation) i.next();
                    if (isParseAnnotation(a)) {
                        model.removeAnnotation(a);
                    }
                }
                for (PositionedMessage pm: messages) {
                    Annotation a= createAnnotation(pm);
                    model.addAnnotation(a, pm.pos);
                    annotations.add(a);
                }
            }
        }
        messages.clear();
    }

	private Annotation createAnnotation(PositionedMessage pm) {
        return new CeylonAnnotation(getAnnotationType(pm), 
        		pm.message, editor, pm.code, pm.severity);
    }<commit_msg>suppress typechecker anns on lines with parse/lex error anns<commit_after>package com.redhat.ceylon.eclipse.code.editor;

/*******************************************************************************
* Copyright (c) 2007 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*    <NAME> (<EMAIL>) - initial API and implementation
*******************************************************************************/

import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.PARSE_ANNOTATION_TYPE;
import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.PARSE_ANNOTATION_TYPE_ERROR;
import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.PARSE_ANNOTATION_TYPE_INFO;
import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.PARSE_ANNOTATION_TYPE_WARNING;
import static com.redhat.ceylon.eclipse.code.editor.CeylonEditor.isParseAnnotation;
import static org.eclipse.core.resources.IMarker.SEVERITY_ERROR;
import static org.eclipse.core.resources.IMarker.SEVERITY_INFO;
import static org.eclipse.core.resources.IMarker.SEVERITY_WARNING;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelExtension;
import org.eclipse.ui.texteditor.IDocumentProvider;

import com.redhat.ceylon.compiler.typechecker.parser.RecognitionError;
import com.redhat.ceylon.compiler.typechecker.tree.Message;
import com.redhat.ceylon.eclipse.util.ErrorVisitor;

/**
 * An implementation of the IMessageHandler interface that creates editor annotations
 * directly from messages. Used for live parsing within a source editor (cf. building,
 * which uses the class MarkerCreator to create markers).
 * @author rmfuhrer
 */
public class AnnotationCreator extends ErrorVisitor {
	
    private static class PositionedMessage {
        public final String message;
        public final Position pos;
        public final int code;
        public final int severity;
        public final boolean syntaxError;
        public final int line;
        
        private PositionedMessage(String msg, Position pos, 
        		int severity, int code, boolean syntaxError,
        		int line) {
            this.message = msg;
            this.pos = pos;
            this.code = code;
            this.severity = severity;
            this.syntaxError = syntaxError;
            this.line = line;
        }

        @Override
        public String toString() {
        	return pos.toString() + " - "+ message;
        }
    }

    private final CeylonEditor editor;
    private final List<PositionedMessage> messages= new LinkedList<PositionedMessage>();
    private final List<Annotation> annotations= new LinkedList<Annotation>();

    public AnnotationCreator(CeylonEditor textEditor) {
        editor= textEditor;
    }

    @Override
    public void handleMessage(int startOffset, int endOffset,
            int startCol, int startLine, Message error) {
        messages.add(new PositionedMessage(error.getMessage(), 
        		new Position(startOffset, endOffset-startOffset+1), 
        		getSeverity(error, warnForErrors), 
        		error.getCode(), error instanceof RecognitionError,
        		error.getLine()));
    }

    public void updateAnnotations() {
    	if (editor.isBackgroundParsingPaused()) {
    		messages.clear();
    		return;
    	}
        IDocumentProvider docProvider= editor.getDocumentProvider();
        if (docProvider!=null) {
            IAnnotationModel model= docProvider.getAnnotationModel(editor.getEditorInput());
            if (model instanceof IAnnotationModelExtension) {
                IAnnotationModelExtension modelExt= (IAnnotationModelExtension) model;
                Annotation[] oldAnnotations= annotations.toArray(new Annotation[annotations.size()]);
                Map<Annotation,Position> newAnnotations= new HashMap<Annotation,Position>(messages.size());
                for (PositionedMessage pm: messages) {
                	if (!suppressAnnotation(pm)) {
                		Annotation a= createAnnotation(pm);
                		newAnnotations.put(a, pm.pos);
                		annotations.add(a);
                	}
                }
                modelExt.replaceAnnotations(oldAnnotations, newAnnotations);
            } 
            else if (model != null) { // model could be null if, e.g., we're directly browsing a file version in a src repo
                for (Iterator i= model.getAnnotationIterator(); i.hasNext(); ) {
                    Annotation a= (Annotation) i.next();
                    if (isParseAnnotation(a)) {
                        model.removeAnnotation(a);
                    }
                }
                for (PositionedMessage pm: messages) {
                	if (!suppressAnnotation(pm)) {
                		Annotation a= createAnnotation(pm);
                		model.addAnnotation(a, pm.pos);
                		annotations.add(a);
                	}
                }
            }
        }
        messages.clear();
    }

	public boolean suppressAnnotation(PositionedMessage pm) {
		boolean suppress = false;
		if (!pm.syntaxError && pm.line>=0) {
			for (PositionedMessage m: messages) {
				if (m.syntaxError && m.line==pm.line) {
					suppress = true;
					break;
				}
			}
		}
		return suppress;
	}

	private Annotation createAnnotation(PositionedMessage pm) {
        return new CeylonAnnotation(getAnnotationType(pm), 
        		pm.message, editor, pm.code, pm.severity);
    }
{'fraction_non_alphanumeric': 0.1158997722095672, 'fraction_numerical': 0.0017312072892938497, 'mean_word_length': 2.84179208960448, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6966559', 'n_tokens_mistral': 2985, 'n_tokens_neox': 2811, 'n_words': 682}