<commit_before>package org.python.pydev.debug.ui.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.IVerticalRulerInfo;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.IUpdate;
import org.eclipse.ui.texteditor.MarkerUtilities;
import org.python.copiedfromeclipsesrc.PydevFileEditorInput;
import org.python.pydev.core.REF;
import org.python.pydev.debug.core.PydevDebugPlugin;
import org.python.pydev.debug.model.PyBreakpoint;
import org.python.pydev.debug.model.PyDebugModelPresentation;

public abstract class AbstractBreakpointRulerAction extends Action implements IUpdate {

	protected IVerticalRulerInfo fInfo;
	protected ITextEditor fTextEditor;
	private IBreakpoint fBreakpoint;

	protected IBreakpoint getBreakpoint() {
		return fBreakpoint;
	}

	protected void setBreakpoint(IBreakpoint breakpoint) {
		fBreakpoint = breakpoint;
	}

	protected ITextEditor getTextEditor() {
		return fTextEditor;
	}

	protected void setTextEditor(ITextEditor textEditor) {
		fTextEditor = textEditor;
	}

	protected IVerticalRulerInfo getInfo() {
		return fInfo;
	}

	protected void setInfo(IVerticalRulerInfo info) {
		fInfo = info;
	}

	protected IBreakpoint determineBreakpoint() {
		IBreakpoint[] breakpoints= DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(PyDebugModelPresentation.PY_DEBUG_MODEL_ID);
		for (int i= 0; i < breakpoints.length; i++) {
			IBreakpoint breakpoint= breakpoints[i];
			if (breakpoint instanceof PyBreakpoint ) {
				PyBreakpoint pyBreakpoint= (PyBreakpoint)breakpoint;
				try {
					if (breakpointAtRulerLine(pyBreakpoint, getPydevFileEditorInput())) {
						return pyBreakpoint;
					}
				} catch (CoreException ce) {
					PydevDebugPlugin.log(IStatus.ERROR,ce.getLocalizedMessage(),ce);
					continue;
				}
			}
		}
		return null;
	}

    /**
     * @return the document of the editor's input
     */
    protected IDocument getDocument() {
        IDocumentProvider provider = fTextEditor.getDocumentProvider();
        return provider.getDocument(fTextEditor.getEditorInput());
    }

	protected boolean breakpointAtRulerLine(PyBreakpoint pyBreakpoint, PydevFileEditorInput pydevFileEditorInput) throws CoreException {
        IDocument doc = getDocument();
		IMarker marker = pyBreakpoint.getMarker();
        Position position= getMarkerPosition(doc, marker);
		if (position != null) {
			try {
				int markerLineNumber= doc.getLineOfOffset(position.getOffset());
                if(getResourceForDebugMarkers() instanceof IFile){
                    //workspace file
                    int rulerLine= getInfo().getLineOfLastMouseButtonActivity();
                    if (rulerLine == markerLineNumber) {
                        if (getTextEditor().isDirty()) {
                            return pyBreakpoint.getLineNumber() == markerLineNumber + 1;
                        }
                        return true;
                    }
                }else if(isInSameExternalEditor(marker, pydevFileEditorInput)){
                    return true;
                }
			} catch (BadLocationException x) {
			}
		}
		
		return false;
	}
	
	protected IResource getResourceForDebugMarkers() {
	    return getResourceForDebugMarkers(fTextEditor);
	}

	public boolean isExternalFileEditor() {
	    return isExternalFileEditor(fTextEditor);
	}
    
	public PydevFileEditorInput getPydevFileEditorInput() {
	    return getPydevFileEditorInput(fTextEditor);
    }
    
    // utilities -------------------------------------------------------------------------------------------------------
    // utilities -------------------------------------------------------------------------------------------------------
    // utilities -------------------------------------------------------------------------------------------------------
    // utilities -------------------------------------------------------------------------------------------------------
    // utilities -------------------------------------------------------------------------------------------------------
    
	
	/**
	 * @return whether we're in an external editor or not.
	 */
	public static boolean isExternalFileEditor(ITextEditor editor) {
	    PydevFileEditorInput pydevFileEditorInput = getPydevFileEditorInput(editor);
	    if(pydevFileEditorInput != null){
	        return true;
	    }
	    return false;
	}
	
	
	
	/**
	 * @return the PydevFileEditorInput if we're dealing with an external file (or null otherwise)
	 */
	public static PydevFileEditorInput getPydevFileEditorInput(ITextEditor editor) {
	    IEditorInput input = editor.getEditorInput();
	    PydevFileEditorInput pydevFileEditorInput = null;
        
	    if (input instanceof PydevFileEditorInput) {
	        pydevFileEditorInput = (PydevFileEditorInput) input;
	    } else {
            if(input instanceof IPathEditorInput){
                IPathEditorInput pathEditorInput = (IPathEditorInput) input;
                IPath path = pathEditorInput.getPath();
                File file = path.toFile();
                if(file != null && file.exists()){<commit_msg>Fix in annotations sync.<commit_after>package org.python.pydev.debug.ui.actions;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.IVerticalRulerInfo;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.IUpdate;
import org.eclipse.ui.texteditor.MarkerUtilities;
import org.python.copiedfromeclipsesrc.PydevFileEditorInput;
import org.python.pydev.core.REF;
import org.python.pydev.debug.core.PydevDebugPlugin;
import org.python.pydev.debug.model.PyBreakpoint;
import org.python.pydev.debug.model.PyDebugModelPresentation;

public abstract class AbstractBreakpointRulerAction extends Action implements IUpdate {

	protected IVerticalRulerInfo fInfo;
	protected ITextEditor fTextEditor;
	private IBreakpoint fBreakpoint;

	protected IBreakpoint getBreakpoint() {
		return fBreakpoint;
	}

	protected void setBreakpoint(IBreakpoint breakpoint) {
		fBreakpoint = breakpoint;
	}

	protected ITextEditor getTextEditor() {
		return fTextEditor;
	}

	protected void setTextEditor(ITextEditor textEditor) {
		fTextEditor = textEditor;
	}

	protected IVerticalRulerInfo getInfo() {
		return fInfo;
	}

	protected void setInfo(IVerticalRulerInfo info) {
		fInfo = info;
	}

	protected IBreakpoint determineBreakpoint() {
		IBreakpoint[] breakpoints= DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(PyDebugModelPresentation.PY_DEBUG_MODEL_ID);
		for (int i= 0; i < breakpoints.length; i++) {
			IBreakpoint breakpoint= breakpoints[i];
			if (breakpoint instanceof PyBreakpoint ) {
				PyBreakpoint pyBreakpoint= (PyBreakpoint)breakpoint;
				try {
					if (breakpointAtRulerLine(pyBreakpoint, getPydevFileEditorInput())) {
						return pyBreakpoint;
					}
				} catch (CoreException ce) {
					PydevDebugPlugin.log(IStatus.ERROR,ce.getLocalizedMessage(),ce);
					continue;
				}
			}
		}
		return null;
	}

    /**
     * @return the document of the editor's input
     */
    protected IDocument getDocument() {
        IDocumentProvider provider = fTextEditor.getDocumentProvider();
        return provider.getDocument(fTextEditor.getEditorInput());
    }

	protected boolean breakpointAtRulerLine(PyBreakpoint pyBreakpoint, PydevFileEditorInput pydevFileEditorInput) throws CoreException {
        IDocument doc = getDocument();
		IMarker marker = pyBreakpoint.getMarker();
        Position position= getMarkerPosition(doc, marker);
		if (position != null) {
			try {
				int markerLineNumber= doc.getLineOfOffset(position.getOffset());
                if(getResourceForDebugMarkers() instanceof IFile){
                    //workspace file
                    int rulerLine= getInfo().getLineOfLastMouseButtonActivity();
                    if (rulerLine == markerLineNumber) {
                        if (getTextEditor().isDirty()) {
                            return pyBreakpoint.getLineNumber() == markerLineNumber + 1;
                        }
                        return true;
                    }
                }else if(isInSameExternalEditor(marker, pydevFileEditorInput)){
                    return true;
                }
			} catch (BadLocationException x) {
			}
		}
		
		return false;
	}
	
	protected IResource getResourceForDebugMarkers() {
	    return getResourceForDebugMarkers(fTextEditor);
	}

	public boolean isExternalFileEditor() {
	    return isExternalFileEditor(fTextEditor);
	}
    
	public PydevFileEditorInput getPydevFileEditorInput() {
	    return getPydevFileEditorInput(fTextEditor);
    }
    
    // utilities -------------------------------------------------------------------------------------------------------
    // utilities -------------------------------------------------------------------------------------------------------
    // utilities -------------------------------------------------------------------------------------------------------
    // utilities -------------------------------------------------------------------------------------------------------
    // utilities -------------------------------------------------------------------------------------------------------
    
	
	/**
	 * @return whether we're in an external editor or not.
	 */
	public static boolean isExternalFileEditor(ITextEditor editor) {
	    PydevFileEditorInput pydevFileEditorInput = getPydevFileEditorInput(editor);
	    if(pydevFileEditorInput != null){
	        return true;
	    }
	    return false;
	}
	
	
	
	/**
	 * @return the PydevFileEditorInput if we're dealing with an external file (or null otherwise)
	 */
	public static PydevFileEditorInput getPydevFileEditorInput(ITextEditor editor) {
	    IEditorInput input = editor.getEditorInput();
	    PydevFileEditorInput pydevFileEditorInput = null;
        
	    //only return not null if it's an external file (FileEditorInput marks a workspace file) 
        if(input instanceof FileEditorInput){
            return null;
        }
        
	    if (input instanceof PydevFileEditorInput) {
	        pydevFileEditorInput = (PydevFileEditorInput) input;
            
	    } else {
            if(input instanceof IPathEditorInput && !(input instanceof FileEditorInput)){
                IPathEditorInput pathEditorInput = (IPathEditorInput) input;
                IPath path = pathEditorInput.getPath();
                File file = path.toFile();
                if(file != null && file.exists()){
{'fraction_non_alphanumeric': 0.16557538058602062, 'fraction_numerical': 0.0003273858241938124, 'mean_word_length': 2.4274894810659187, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '6546172', 'n_tokens_mistral': 3403, 'n_tokens_neox': 3151, 'n_words': 646}