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

import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
import org.eclipse.jdt.debug.core.JDIDebugModel;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.ide.FileStoreEditorInput;

import com.redhat.ceylon.compiler.typechecker.model.Unit;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.compiler.typechecker.tree.Visitor;
import com.redhat.ceylon.eclipse.core.builder.CeylonBuilder;
import com.redhat.ceylon.eclipse.core.external.ExternalSourceArchiveManager;
import com.redhat.ceylon.eclipse.core.model.IResourceAware;

public class ToggleBreakpointAdapter implements IToggleBreakpointsTarget {

    private static final String JDT_DEBUG_PLUGIN_ID = "org.eclipse.jdt.debug";

    public ToggleBreakpointAdapter() {}

    public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
        if (selection instanceof ITextSelection) {
            ITextSelection textSel= (ITextSelection) selection;
            IEditorPart editorPart= (IEditorPart) part.getAdapter(IEditorPart.class);
            //TODO: handle org.eclipse.ui.ide.FileStoreEditorInput
            //      to set breakpoints in code from archives
            IEditorInput editorInput = editorPart.getEditorInput();
            final IFile origSrcFile = getSourceFile(editorInput);
            int line = textSel.getStartLine();
            boolean emptyLine = false;
            Node node = null;
            try {
                CeylonEditor editor = (CeylonEditor) editorPart;
                IDocument document = editor.getCeylonSourceViewer().getDocument();
                Tree.CompilationUnit rootNode = editor.getParseController().getRootNode();
                final IRegion lineInformation = document.getLineInformation(line);
                String text = 
                        document.get(lineInformation.getOffset(), 
                                lineInformation.getLength()).trim();
                emptyLine = text.isEmpty();
                if (!emptyLine) {
                    node = getNode(rootNode, lineInformation);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            final boolean breakable = !emptyLine && node!=null;
            final int lineNumber = line+1;
            IWorkspaceRunnable wr = new IWorkspaceRunnable() {
                public void run(IProgressMonitor monitor) throws CoreException {
                    IMarker marker = findBreakpointMarker(origSrcFile, lineNumber);
                    if (marker!=null) {
                        // The following will delete the associated marker
                        clearLineBreakpoint(origSrcFile, lineNumber);
                    } else if (breakable) {
                        // The following will create a marker as a side-effect
                        setLineBreakpoint(origSrcFile, lineNumber);
                    }
                }
            };
            try {
                getWorkspace().run(wr, null);
            } 
            catch (CoreException e) {
                throw new DebugException(e.getStatus());
            }
        }
    }

    private IFile getSourceFile(IEditorInput editorInput) {
        final IFile origSrcFile;
        if (editorInput instanceof IFileEditorInput) {
            origSrcFile= ((IFileEditorInput)editorInput).getFile();
        } else if (editorInput instanceof FileStoreEditorInput) {
            URI uri = ((FileStoreEditorInput) editorInput).getURI();
            IResource resource = ExternalSourceArchiveManager.toResource(uri);
            if (resource instanceof IFile) {
                origSrcFile = (IFile) resource;
            } else {
                origSrcFile = null;
            }
        } else {
            origSrcFile = null;
        }
        return origSrcFile;
    }

    private static Node getNode(Tree.CompilationUnit rootNode,
            final IRegion lineInformation) {
        class BreakpointVisitor extends Visitor {
            Node result;
            int start = lineInformation.getOffset();
            int end = start + lineInformation.getLength()+1;
            boolean in(Node node) {
                Integer startIndex = node.getStartIndex();
                int stopIndex = node.getStopIndex()+1;
                return startIndex<=start && stopIndex>=end ||
                        startIndex>=start && startIndex<end ||
                        stopIndex>=start && stopIndex<end;
            }
            @Override
            public void visit(Tree.Annotation that) {}
//            @Override
//            public void visit(Tree.MethodDefinition that) {
//                if (in(that.getIdentifier())) {
//                    result = that;
//                }
//                super.visit(that);
//            }
//            @Override<commit_msg>Fix NPE<commit_after>package com.redhat.ceylon.eclipse.code.editor;

import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.ui.actions.IToggleBreakpointsTarget;
import org.eclipse.jdt.debug.core.IJavaLineBreakpoint;
import org.eclipse.jdt.debug.core.JDIDebugModel;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.ide.FileStoreEditorInput;

import com.redhat.ceylon.compiler.typechecker.model.Unit;
import com.redhat.ceylon.compiler.typechecker.tree.Node;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.compiler.typechecker.tree.Visitor;
import com.redhat.ceylon.eclipse.core.builder.CeylonBuilder;
import com.redhat.ceylon.eclipse.core.external.ExternalSourceArchiveManager;
import com.redhat.ceylon.eclipse.core.model.IResourceAware;

public class ToggleBreakpointAdapter implements IToggleBreakpointsTarget {

    private static final String JDT_DEBUG_PLUGIN_ID = "org.eclipse.jdt.debug";

    public ToggleBreakpointAdapter() {}

    public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
        if (selection instanceof ITextSelection) {
            ITextSelection textSel= (ITextSelection) selection;
            IEditorPart editorPart= (IEditorPart) part.getAdapter(IEditorPart.class);
            //TODO: handle org.eclipse.ui.ide.FileStoreEditorInput
            //      to set breakpoints in code from archives
            IEditorInput editorInput = editorPart.getEditorInput();
            final IFile origSrcFile = getSourceFile(editorInput);
            int line = textSel.getStartLine();
            boolean emptyLine = false;
            Node node = null;
            try {
                CeylonEditor editor = (CeylonEditor) editorPart;
                IDocument document = editor.getCeylonSourceViewer().getDocument();
                Tree.CompilationUnit rootNode = editor.getParseController().getRootNode();
                final IRegion lineInformation = document.getLineInformation(line);
                String text = 
                        document.get(lineInformation.getOffset(), 
                                lineInformation.getLength()).trim();
                emptyLine = text.isEmpty();
                if (!emptyLine) {
                    node = getNode(rootNode, lineInformation);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            final boolean breakable = !emptyLine && node!=null;
            final int lineNumber = line+1;
            IWorkspaceRunnable wr = new IWorkspaceRunnable() {
                public void run(IProgressMonitor monitor) throws CoreException {
                    IMarker marker = findBreakpointMarker(origSrcFile, lineNumber);
                    if (marker!=null) {
                        // The following will delete the associated marker
                        clearLineBreakpoint(origSrcFile, lineNumber);
                    } else if (breakable) {
                        // The following will create a marker as a side-effect
                        setLineBreakpoint(origSrcFile, lineNumber);
                    }
                }
            };
            try {
                getWorkspace().run(wr, null);
            } 
            catch (CoreException e) {
                throw new DebugException(e.getStatus());
            }
        }
    }

    private IFile getSourceFile(IEditorInput editorInput) {
        final IFile origSrcFile;
        if (editorInput instanceof IFileEditorInput) {
            origSrcFile= ((IFileEditorInput)editorInput).getFile();
        } else if (editorInput instanceof FileStoreEditorInput) {
            URI uri = ((FileStoreEditorInput) editorInput).getURI();
            IResource resource = ExternalSourceArchiveManager.toResource(uri);
            if (resource instanceof IFile) {
                origSrcFile = (IFile) resource;
            } else {
                origSrcFile = null;
            }
        } else {
            origSrcFile = null;
        }
        return origSrcFile;
    }

    private static Node getNode(Tree.CompilationUnit rootNode,
            final IRegion lineInformation) {
        class BreakpointVisitor extends Visitor {
            Node result;
            int start = lineInformation.getOffset();
            int end = start + lineInformation.getLength()+1;
            boolean in(Node node) {
                Integer startIndex = node.getStartIndex();
                Integer stopIndex = node.getStopIndex();
                if (startIndex != null && stopIndex != null) {
                    stopIndex ++;
                    return startIndex<=start && stopIndex>=end ||
                            startIndex>=start && startIndex<end ||
                            stopIndex>=start && stopIndex<end;
                }
                return false;
            }
            @Override
            public void visit(Tree.Annotation that) {}
//            @Override
//            public void visit(Tree.MethodDefinition that) {
//                if (in(that.getIdentifier())) {
//                    result = that;
//                }
//                super.visit(that);
//            }
//            @Override
{'fraction_non_alphanumeric': 0.08892770984111138, 'fraction_numerical': 0.0004159387738124948, 'mean_word_length': 2.1854795972443033, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2283103', 'n_tokens_mistral': 3113, 'n_tokens_neox': 3016, 'n_words': 624}