blob: ff76d107fdbb510946512104107a8a72ee18b2d6 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2008, 2019 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.ltk.ui.util;
import java.util.Iterator;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.statet.jcommons.lang.NonNull;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.Nullable;
import org.eclipse.statet.jcommons.text.core.TextRegion;
import org.eclipse.statet.ecommons.text.core.JFaceTextRegion;
import org.eclipse.statet.ltk.ast.core.AstInfo;
import org.eclipse.statet.ltk.ast.core.AstNode;
import org.eclipse.statet.ltk.ast.core.util.AstSelection;
import org.eclipse.statet.ltk.core.LTKUtils;
import org.eclipse.statet.ltk.model.core.IModelManager;
import org.eclipse.statet.ltk.model.core.elements.IModelElement;
import org.eclipse.statet.ltk.model.core.elements.ISourceStructElement;
import org.eclipse.statet.ltk.model.core.elements.ISourceUnit;
import org.eclipse.statet.ltk.model.core.elements.ISourceUnitModelInfo;
import org.eclipse.statet.ltk.ui.sourceediting.ISourceEditor;
@NonNullByDefault
public class LTKSelectionUtils {
public static TextRegion toTextRegion(final ITextSelection selection) {
return JFaceTextRegion.newByStartLength(selection.getOffset(), selection.getLength());
}
public static TextRegion toTextRegion(final Point selectedRange) {
return JFaceTextRegion.newByStartLength(selectedRange.x, selectedRange.y);
}
public static TextRegion toTextRegion(final IRegion region) {
return JFaceTextRegion.toTextRegion(region);
}
public static TextRegion toTextRegion(final Position position) {
return JFaceTextRegion.newByStartLength(position.getOffset(), position.getLength());
}
public static IModelElement @Nullable [] getSelectedElements(final @Nullable ISelection selection) {
if (selection instanceof IStructuredSelection) {
return getSelectedElements((IStructuredSelection) selection);
}
return null;
}
public static IModelElement @Nullable [] getSelectedElements(final IStructuredSelection selection) {
final IModelElement[] elements= new @NonNull IModelElement[selection.size()];
final Iterator<?> iter= selection.iterator();
for (int i= 0; i < elements.length; i++) {
final Object next= iter.next();
if (next instanceof IModelElement) {
elements[i]= (IModelElement) next;
}
else {
return null;
}
}
return elements;
}
public static @Nullable ISourceUnit getSingleSourceUnit(final IWorkbenchPart part) {
final ISourceEditor editor= part.getAdapter(ISourceEditor.class);
if (editor == null) {
return null;
}
return editor.getSourceUnit();
}
public static @Nullable AstNode getSelectedAstNode(final ISourceUnit su, final String type,
final @Nullable ISelection selection, final IProgressMonitor monitor) {
if (selection instanceof ITextSelection) {
final ITextSelection textSelection= (ITextSelection) selection;
final ISourceUnitModelInfo modelInfo= su.getModelInfo(type, IModelManager.MODEL_FILE, monitor);
if (modelInfo == null) {
return null;
}
final AstInfo info= modelInfo.getAst();
if (info == null || info.getRoot() == null) {
return null;
}
final AstSelection astSelection= AstSelection.search(info.getRoot(),
textSelection.getOffset(), textSelection.getOffset()+textSelection.getLength(),
AstSelection.MODE_COVERING_SAME_LAST );
return astSelection.getCovering();
}
return null;
}
public static ISourceStructElement @Nullable [] getSelectedSourceStructElements(
final @Nullable ISelection selection) {
if (selection instanceof IStructuredSelection) {
return getSelectedSourceStructElements((IStructuredSelection) selection);
}
return null;
}
public static ISourceStructElement @Nullable [] getSelectedSourceStructElements(
final IStructuredSelection selection) {
final ISourceStructElement[] elements= new @NonNull ISourceStructElement[selection.size()];
final Iterator<?> iter= selection.iterator();
for (int i= 0; i < elements.length; i++) {
final Object next= iter.next();
if (next instanceof ISourceStructElement) {
elements[i]= (ISourceStructElement) next;
continue;
}
return null;
}
return elements;
}
public static IFile @Nullable [] getSelectedFiles(final @Nullable ISelection selection) {
if (selection instanceof IStructuredSelection) {
return getSelectedFiles((IStructuredSelection) selection);
}
return null;
}
public static IFile @Nullable [] getSelectedFiles(final IStructuredSelection selection) {
final IFile[] elements= new @NonNull IFile[selection.size()];
final Iterator<?> iter= selection.iterator();
for (int i= 0; i < elements.length; i++) {
final Object next= iter.next();
if (next instanceof IFile) {
elements[i]= (IFile) next;
continue;
}
if (next instanceof IAdaptable) {
elements[i]= ((IAdaptable) next).getAdapter(IFile.class);
if (elements[i] != null) {
continue;
}
}
return null;
}
return elements;
}
public static ISourceStructElement @Nullable [] getSelectedSourceStructElement(
final @Nullable ISourceUnitModelInfo suModel, final ITextSelection selection) {
if (suModel != null) {
final ISourceStructElement root= suModel.getSourceElement();
final int selectionStart= selection.getOffset();
final int selectionEnd= selectionStart + selection.getLength();
if (selectionStart >= root.getSourceRange().getStartOffset()
&& selectionEnd <= root.getSourceRange().getEndOffset()) {
return new ISourceStructElement[] {
LTKUtils.getCoveringSourceElement(root, selectionStart, selectionEnd),
};
}
}
return null;
}
}