blob: 07c80ef2d3725d6e5dc2061ff2b7f14c6b33d187 [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;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.part.ShowInContext;
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;
/**
* Data/state of LTK based input of a view/editor.
*
* E.g. used for {@link ISelectionWithElementInfoListener} or {@link ShowInContext}
*/
public class LTKInputData implements ISelection {
protected ISourceUnit inputElement;
protected ISourceUnitModelInfo inputInfo;
protected ISelectionProvider selectionProvider;
protected ISelection selection;
protected AstSelection astSelection;
protected ISourceStructElement modelSelection;
public LTKInputData(final ISourceUnit inputElement, final ISelection selection) {
this.inputElement= inputElement;
this.selection= selection;
}
public LTKInputData(final ISourceUnit inputElement, final ISelectionProvider selectionProvider) {
this.inputElement= inputElement;
this.selectionProvider= selectionProvider;
this.selection= selectionProvider.getSelection();
}
public boolean update() {
if (this.selectionProvider != null) {
final ISelection selection= this.selectionProvider.getSelection();
if (!selection.equals(this.selection)) {
this.astSelection= null;
this.modelSelection= null;
}
return true;
}
return false;
}
public IModelElement getInputElement() {
return this.inputElement;
}
public ISourceUnitModelInfo getInputInfo() {
if (this.inputInfo == null) {
this.inputInfo= this.inputElement.getModelInfo(null, IModelManager.NONE, new NullProgressMonitor());
}
return this.inputInfo;
}
@Override
public boolean isEmpty() {
return this.selection.isEmpty();
}
public ISelection getSelection() {
return this.selection;
}
public AstSelection getAstSelection() {
if (this.astSelection == null) {
if (this.selection instanceof ITextSelection && getInputInfo() != null) {
final ITextSelection textSelection= (ITextSelection) this.selection;
this.astSelection= AstSelection.search(getInputInfo().getAst().getRoot(),
textSelection.getOffset(), textSelection.getOffset()+textSelection.getLength(),
AstSelection.MODE_COVERING_SAME_LAST );
}
}
return this.astSelection;
}
public ISourceStructElement getModelSelection() {
if (this.modelSelection == null) {
if (this.selection instanceof ITextSelection && getInputInfo() != null) {
final ITextSelection textSelection= (ITextSelection) this.selection;
this.modelSelection= LTKUtils.getCoveringSourceElement(getInputInfo().getSourceElement(), textSelection.getOffset(), textSelection.getOffset()+textSelection.getLength());
}
}
return this.modelSelection;
}
public boolean isStillValid() {
return true;
}
}