| /*=============================================================================# |
| # Copyright (c) 2008, 2020 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.model.core.LtkModelUtils; |
| import org.eclipse.statet.ltk.model.core.ModelManager; |
| import org.eclipse.statet.ltk.model.core.element.LtkModelElement; |
| import org.eclipse.statet.ltk.model.core.element.SourceStructElement; |
| import org.eclipse.statet.ltk.model.core.element.SourceUnit; |
| import org.eclipse.statet.ltk.model.core.element.SourceUnitModelInfo; |
| |
| |
| /** |
| * 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 SourceUnit inputElement; |
| protected SourceUnitModelInfo inputInfo; |
| |
| protected ISelectionProvider selectionProvider; |
| protected ISelection selection; |
| protected AstSelection astSelection; |
| protected SourceStructElement modelSelection; |
| |
| |
| public LTKInputData(final SourceUnit inputElement, final ISelection selection) { |
| this.inputElement= inputElement; |
| this.selection= selection; |
| } |
| |
| public LTKInputData(final SourceUnit 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 LtkModelElement getInputElement() { |
| return this.inputElement; |
| } |
| |
| public SourceUnitModelInfo getInputInfo() { |
| if (this.inputInfo == null) { |
| this.inputInfo= this.inputElement.getModelInfo(null, ModelManager.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 SourceStructElement getModelSelection() { |
| if (this.modelSelection == null) { |
| if (this.selection instanceof ITextSelection && getInputInfo() != null) { |
| final ITextSelection textSelection= (ITextSelection) this.selection; |
| this.modelSelection= LtkModelUtils.getCoveringSourceElement(getInputInfo().getSourceElement(), textSelection.getOffset(), textSelection.getOffset()+textSelection.getLength()); |
| } |
| } |
| return this.modelSelection; |
| } |
| |
| |
| public boolean isStillValid() { |
| return true; |
| } |
| |
| } |