blob: b6d1e71e55d17e2420427f3bf0a127cd9e644a9b [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2010, 2021 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.internal.r.ui.editors;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.statet.ltk.ui.sourceediting.EditorTextInfoHoverProxy;
import org.eclipse.statet.ltk.ui.sourceediting.SourceEditorViewerConfiguration;
import org.eclipse.statet.ltk.ui.sourceediting.assist.AssistInvocationContext;
import org.eclipse.statet.ltk.ui.sourceediting.assist.InfoHoverDescriptor;
import org.eclipse.statet.r.core.source.RDocumentConstants;
import org.eclipse.statet.r.core.source.RHeuristicTokenScanner;
import org.eclipse.statet.r.ui.editors.IRSourceEditor;
import org.eclipse.statet.r.ui.sourceediting.RAssistInvocationContext;
public class REditorTextHover extends EditorTextInfoHoverProxy {
private RHeuristicTokenScanner scanner;
public REditorTextHover(final IRSourceEditor editor,
final InfoHoverDescriptor descriptor, final SourceEditorViewerConfiguration config) {
super(descriptor, config);
}
@Override
public IRegion getHoverRegion(final ITextViewer textViewer, final int offset) {
if (this.scanner == null) {
this.scanner= RHeuristicTokenScanner.create(getEditor().getDocumentContentInfo());
}
try {
final IDocument document= getEditor().getViewer().getDocument();
this.scanner.configure(document);
final IRegion word= this.scanner.findRWord(offset, false, true);
if (word != null) {
final ITypedRegion partition= this.scanner.getPartition(word.getOffset());
if (RDocumentConstants.R_DEFAULT_CONTENT_CONSTRAINT.matches(partition.getType())
|| partition.getType() == RDocumentConstants.R_STRING_CONTENT_TYPE
|| partition.getType() == RDocumentConstants.R_QUOTED_SYMBOL_CONTENT_TYPE) {
return word;
}
}
final char c= document.getChar(offset);
if (c == '[') {
final ITypedRegion partition= this.scanner.getPartition(offset);
if (RDocumentConstants.R_DEFAULT_CONTENT_CONSTRAINT.matches(partition.getType())) {
return new Region(offset, 1);
}
}
}
catch (final Exception e) {
}
return null;
}
@Override
protected AssistInvocationContext createContext(final IRegion region, final String contentType,
final IProgressMonitor monitor) {
// we are not in UI thread
final RAssistInvocationContext context= new RAssistInvocationContext(
(IRSourceEditor) getEditor(), region, contentType, this.scanner, monitor );
if (context.getAstInfo() == null) {
return null;
}
return context;
}
}