package com.redhat.ceylon.eclipse.core.builder;

import static com.redhat.ceylon.eclipse.core.builder.CeylonBuilder.PROBLEM_MARKER_ID;
import static com.redhat.ceylon.eclipse.core.builder.CeylonBuilder.MODULE_DEPENDENCY_PROBLEM_MARKER_ID;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;

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

/**
 * This class provides a message handler that creates markers in
 * response to received messages.
 * 
 * MarkerCreators are instantiated with a file (IFile) and a parse
 * controller (IParseController).  The parse controller should
 * be parsing the file and generating the messages that are
 * received by the MarkerCreator.  The MarkerCreator, in turn,
 * creates a problem marker for each error message received,
 * uses the parse controller to compute a line number for the
 * token provided with each message, and attaches the marker to
 * the given file at the computed line.
 */
public class MarkerCreator extends ErrorVisitor {
    
    protected IFile file;
    public static final String ERROR_CODE_KEY= "errorCode";

    public MarkerCreator(IFile file) {
        this.file = file;
    }

    @Override
    public void handleMessage(int startOffset, int endOffset,
            int startCol, int startLine, Message message) {
        
        String[] attributeNames= new String[] {
                IMarker.LINE_NUMBER, 
                IMarker.CHAR_START, IMarker.CHAR_END, 
                IMarker.MESSAGE, 
                IMarker.PRIORITY, 
                IMarker.SEVERITY,
                ERROR_CODE_KEY,
                IMarker.SOURCE_ID
            };
        Object[] values = new Object[] {
                startLine, 
                startOffset, endOffset+1, 
                message.getMessage(), 
                IMarker.PRIORITY_HIGH, 
                getSeverity(message, warnForErrors),
                message.getCode(),
                CeylonBuilder.SOURCE
            };
        try {
            file.createMarker(message instanceof ModuleManager.ModuleDependencyAnalysisError ?
                    MODULE_DEPENDENCY_PROBLEM_MARKER_ID:PROBLEM_MARKER_ID)
                .setAttributes(attributeNames, values);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }
//
//    private static boolean isCompilerError(String msg) {
//        //TODO: we need a MUCH better way to distinguish 
//        //      compiler errors from typechecker errors
//        return msg.startsWith("cannot find module") || 
//                msg.startsWith("unable to read source artifact for") ||
//                msg.startsWith("invalid JDK module") /* ||
//                msg.contains("compiled for an incompatible version")*/;
//    }
//    
}

@@ -2440,468 +2440,6 @@
   %7D%0A
-//%0A//    private static boolean isCompilerError(String msg) %7B%0A//        //TODO: we need a MUCH better way to distinguish %0A//        //      compiler errors from typechecker errors%0A//        return msg.startsWith(%22cannot find module%22) %7C%7C %0A//                msg.startsWith(%22unable to read source artifact for%22) %7C%7C%0A//                msg.startsWith(%22invalid JDK module%22) /* %7C%7C%0A//                msg.contains(%22compiled for an incompatible version%22)*/;%0A//    %7D%0A//    %0A
 %7D%0A

clean unnecessary commented code