<commit_before>package org.xtuml.bp.ui.text.activity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.BadLocationException;
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.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.Bundle;
import org.xtuml.bp.core.Attribute_c;
import org.xtuml.bp.core.Body_c;
import org.xtuml.bp.core.BridgeParameter_c;
import org.xtuml.bp.core.Bridge_c;
import org.xtuml.bp.core.CorePlugin;
import org.xtuml.bp.core.Enumerator_c;
import org.xtuml.bp.core.FunctionParameter_c;
import org.xtuml.bp.core.Function_c;
import org.xtuml.bp.core.InterfaceOperation_c;
import org.xtuml.bp.core.InterfaceSignal_c;
import org.xtuml.bp.core.OperationParameter_c;
import org.xtuml.bp.core.Operation_c;
import org.xtuml.bp.core.Package_c;
import org.xtuml.bp.core.PropertyParameter_c;
import org.xtuml.bp.core.StateMachineEventDataItem_c;
import org.xtuml.bp.core.StateMachineEvent_c;
import org.xtuml.bp.core.VariableLocation_c;
import org.xtuml.bp.core.common.NonRootModelElement;
import org.xtuml.bp.core.common.OALPersistenceUtil;
import org.xtuml.bp.core.util.EditorUtil;
import org.xtuml.bp.core.util.UIUtil;
import org.xtuml.bp.ui.text.TextPlugin;

/**
 * This action opens the declaration of model element references
 *
 */
public class OpenDeclarationAction implements IEditorActionDelegate {
	private IEditorPart targetEditor;

	@Override
	public void run(IAction arg0) {
		ActivityEditor editor;
		if (targetEditor instanceof ActivityEditor) {
			editor = (ActivityEditor) targetEditor;
		} else {
			throw new RuntimeException("Expecting Activity editor. Found: " + targetEditor.getClass().getName());
		}

		ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
		IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());

		try {
			int startLine = selection.getStartLine() + 1;
			int startOffset = selection.getOffset();
			IRegion findWord = editor.findWord(doc, startOffset);
			startOffset = findWord.getOffset();
			int lineOffset = doc.getLineOffset(startLine - 1);
			int offsetWithinLine = startOffset - lineOffset + 1;
			ActivityEditorInput input = (ActivityEditorInput) editor.getEditorInput();
			Body_c bdy = OALPersistenceUtil.getOALModelElement(input.getModelElement());
			if (bdy == null) {
				// not parsed just return
				return;
			}
			NonRootModelElement declarationElement = (NonRootModelElement) bdy.Finddeclaration(offsetWithinLine,
					startLine);
			if (declarationElement != null) {
				if (declarationElement instanceof VariableLocation_c) {
					VariableLocation_c location = (VariableLocation_c) declarationElement;
				    lineOffset = doc.getLineOffset(location.getLinenumber() - 1);
					editor.selectAndReveal(lineOffset + location.getStartposition() - 1,
							location.getEndposition() + 1 - location.getStartposition());
				} else {
					boolean showInME = shouldShowInME(declarationElement);
					if (showInME) {
						String explorerViewId = "org.xtuml.bp.ui.explorer.ExplorerView";
						IViewPart explorerView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
								.findView(explorerViewId);
						if(explorerView == null) {
							// not opened, open now
							explorerView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(explorerViewId);
						}
						StructuredViewer viewer = UIUtil.getViewer();
						viewer.setSelection(new StructuredSelection(declarationElement), true);
						PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(explorerView);
					} else {
						Package_c pkg = declarationElement.getFirstParentPackage();
						IEditorPart editorPart = EditorUtil.openEditorForElement(pkg);
						editorPart.getSite().getSelectionProvider()
								.setSelection(new StructuredSelection(declarationElement));
						zoomToSelected(editorPart);
					}
				}
			}
		} catch (BadLocationException | PartInitException e) {
			TextPlugin.logError("Unable to locate line information for the text editor selection.", e);
		}<commit_msg>Job: #9761 #9762 Open ME as well as canvas unless only ME is specified<commit_after>package org.xtuml.bp.ui.text.activity;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.BadLocationException;
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.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.osgi.framework.Bundle;
import org.xtuml.bp.core.Attribute_c;
import org.xtuml.bp.core.Body_c;
import org.xtuml.bp.core.BridgeParameter_c;
import org.xtuml.bp.core.Bridge_c;
import org.xtuml.bp.core.CorePlugin;
import org.xtuml.bp.core.Enumerator_c;
import org.xtuml.bp.core.FunctionParameter_c;
import org.xtuml.bp.core.Function_c;
import org.xtuml.bp.core.InterfaceOperation_c;
import org.xtuml.bp.core.InterfaceSignal_c;
import org.xtuml.bp.core.OperationParameter_c;
import org.xtuml.bp.core.Operation_c;
import org.xtuml.bp.core.Package_c;
import org.xtuml.bp.core.PropertyParameter_c;
import org.xtuml.bp.core.StateMachineEventDataItem_c;
import org.xtuml.bp.core.StateMachineEvent_c;
import org.xtuml.bp.core.VariableLocation_c;
import org.xtuml.bp.core.common.NonRootModelElement;
import org.xtuml.bp.core.common.OALPersistenceUtil;
import org.xtuml.bp.core.util.EditorUtil;
import org.xtuml.bp.core.util.UIUtil;
import org.xtuml.bp.ui.text.TextPlugin;

/**
 * This action opens the declaration of model element references
 *
 */
public class OpenDeclarationAction implements IEditorActionDelegate {
	private IEditorPart targetEditor;

	@Override
	public void run(IAction arg0) {
		ActivityEditor editor;
		if (targetEditor instanceof ActivityEditor) {
			editor = (ActivityEditor) targetEditor;
		} else {
			throw new RuntimeException("Expecting Activity editor. Found: " + targetEditor.getClass().getName());
		}

		ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
		IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());

		try {
			int startLine = selection.getStartLine() + 1;
			int startOffset = selection.getOffset();
			IRegion findWord = editor.findWord(doc, startOffset);
			startOffset = findWord.getOffset();
			int lineOffset = doc.getLineOffset(startLine - 1);
			int offsetWithinLine = startOffset - lineOffset + 1;
			ActivityEditorInput input = (ActivityEditorInput) editor.getEditorInput();
			Body_c bdy = OALPersistenceUtil.getOALModelElement(input.getModelElement());
			if (bdy == null) {
				// not parsed just return
				return;
			}
			NonRootModelElement declarationElement = (NonRootModelElement) bdy.Finddeclaration(offsetWithinLine,
					startLine);
			if (declarationElement != null) {
				if (declarationElement instanceof VariableLocation_c) {
					VariableLocation_c location = (VariableLocation_c) declarationElement;
				    lineOffset = doc.getLineOffset(location.getLinenumber() - 1);
					editor.selectAndReveal(lineOffset + location.getStartposition() - 1,
							location.getEndposition() + 1 - location.getStartposition());
				} else {
					boolean showInME = shouldShowInME(declarationElement);
					if (showInME) {
						showInModelExplorer(declarationElement);
					} else {
						showInModelExplorer(declarationElement);
						Package_c pkg = declarationElement.getFirstParentPackage();
						IEditorPart editorPart = EditorUtil.openEditorForElement(pkg);
						editorPart.getSite().getSelectionProvider()
								.setSelection(new StructuredSelection(declarationElement));
						zoomToSelected(editorPart);
					}
				}
			}
		} catch (BadLocationException | PartInitException e) {
			TextPlugin.logError("Unable to locate line information for the text editor selection.", e);
		}

	}

	private void showInModelExplorer(NonRootModelElement declarationElement) throws PartInitException {
		String explorerViewId = "org.xtuml.bp.ui.explorer.ExplorerView";
		IViewPart explorerView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
				.findView(explorerViewId);
		if(explorerView == null) {
			// not opened, open now
			explorerView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(explorerViewId);
		}
		StructuredViewer viewer = UIUtil.getViewer();
		viewer.setSelection(new StructuredSelection(declarationElement), true);
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().activate(explorerView);
{'fraction_non_alphanumeric': 0.1051589369463262, 'fraction_numerical': 0.0022928608650338717, 'mean_word_length': 5.135549872122762, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5741626', 'n_tokens_mistral': 3287, 'n_tokens_neox': 2926, 'n_words': 515}