| /******************************************************************************* |
| * Copyright (c) 2000, 2018 IBM Corporation and others. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.dltk.internal.ui.text; |
| |
| import org.eclipse.dltk.core.IModelElement; |
| import org.eclipse.dltk.core.ModelException; |
| import org.eclipse.dltk.internal.ui.actions.SelectionConverter; |
| import org.eclipse.dltk.internal.ui.editor.EditorUtility; |
| import org.eclipse.dltk.internal.ui.editor.ScriptEditor; |
| import org.eclipse.jface.text.IRegion; |
| import org.eclipse.jface.text.ITextViewer; |
| import org.eclipse.jface.text.Region; |
| import org.eclipse.jface.text.information.IInformationProvider; |
| import org.eclipse.jface.text.information.IInformationProviderExtension; |
| import org.eclipse.jface.viewers.IStructuredSelection; |
| import org.eclipse.ui.IEditorPart; |
| |
| /** |
| * Provides a Script element to be displayed in by an information presenter. |
| */ |
| public class ScriptElementProvider |
| implements IInformationProvider, IInformationProviderExtension { |
| |
| private ScriptEditor fEditor; |
| private boolean fUseCodeResolve; |
| |
| public ScriptElementProvider(IEditorPart editor) { |
| fUseCodeResolve = false; |
| if (editor instanceof ScriptEditor) |
| fEditor = (ScriptEditor) editor; |
| } |
| |
| public ScriptElementProvider(IEditorPart editor, boolean useCodeResolve) { |
| this(editor); |
| fUseCodeResolve = useCodeResolve; |
| } |
| |
| @Override |
| public IRegion getSubject(ITextViewer textViewer, int offset) { |
| if (textViewer != null && fEditor != null) { |
| IRegion region = ScriptWordFinder.findWord(textViewer.getDocument(), |
| offset); |
| if (region != null) { |
| return region; |
| } |
| return new Region(offset, 0); |
| } |
| return null; |
| } |
| |
| @Override |
| public String getInformation(ITextViewer textViewer, IRegion subject) { |
| return getInformation2(textViewer, subject).toString(); |
| } |
| |
| @Override |
| public Object getInformation2(ITextViewer textViewer, IRegion subject) { |
| if (fEditor == null) |
| return null; |
| |
| try { |
| if (fUseCodeResolve) { |
| IStructuredSelection sel = SelectionConverter |
| .getStructuredSelection(fEditor); |
| if (!sel.isEmpty()) |
| return sel.getFirstElement(); |
| } |
| IModelElement element = SelectionConverter |
| .getElementAtOffset(fEditor); |
| if (element != null) |
| return element; |
| |
| return EditorUtility.getEditorInputModelElement(fEditor, false); |
| } catch (ModelException e) { |
| return null; |
| } |
| } |
| } |