|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Vegard IT GmbH and others. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Sebastian Thomschke (Vegard IT) - initial implementation |
| 11 | + *******************************************************************************/ |
| 12 | +package org.eclipse.tm4e.ui.internal.hover; |
| 13 | + |
| 14 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 15 | +import org.eclipse.jdt.annotation.Nullable; |
| 16 | +import org.eclipse.jface.text.AbstractReusableInformationControlCreator; |
| 17 | +import org.eclipse.jface.text.BadLocationException; |
| 18 | +import org.eclipse.jface.text.DefaultInformationControl; |
| 19 | +import org.eclipse.jface.text.IDocument; |
| 20 | +import org.eclipse.jface.text.IInformationControl; |
| 21 | +import org.eclipse.jface.text.IInformationControlCreator; |
| 22 | +import org.eclipse.jface.text.IRegion; |
| 23 | +import org.eclipse.jface.text.ITextHover; |
| 24 | +import org.eclipse.jface.text.ITextHoverExtension; |
| 25 | +import org.eclipse.jface.text.ITextViewer; |
| 26 | +import org.eclipse.jface.text.Region; |
| 27 | +import org.eclipse.swt.widgets.Shell; |
| 28 | +import org.eclipse.tm4e.core.model.TMToken; |
| 29 | +import org.eclipse.tm4e.ui.internal.model.TMDocumentModel; |
| 30 | +import org.eclipse.tm4e.ui.internal.model.TMModelManager; |
| 31 | +import org.eclipse.tm4e.ui.internal.preferences.PreferenceHelper; |
| 32 | +import org.eclipse.ui.editors.text.EditorsUI; |
| 33 | + |
| 34 | +public class TMTokenTextHover implements ITextHover, ITextHoverExtension { |
| 35 | + |
| 36 | + private static final class RegionWithTMToken extends Region { |
| 37 | + final TMToken token; |
| 38 | + final String tokenText; |
| 39 | + |
| 40 | + RegionWithTMToken(final int offset, final int length, final String tokenText, final TMToken token) { |
| 41 | + super(offset, length); |
| 42 | + this.tokenText = tokenText; |
| 43 | + this.token = token; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public IInformationControlCreator getHoverControlCreator() { |
| 49 | + // setup a hover control that interprets basic HTML input |
| 50 | + return new AbstractReusableInformationControlCreator() { |
| 51 | + @Override |
| 52 | + protected IInformationControl doCreateInformationControl(final @NonNullByDefault({}) Shell parent) { |
| 53 | + return new DefaultInformationControl(parent, EditorsUI.getTooltipAffordanceString()); |
| 54 | + } |
| 55 | + }; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public @Nullable String getHoverInfo(final @NonNullByDefault({}) ITextViewer textViewer, |
| 60 | + final @NonNullByDefault({}) IRegion hoverRegion) { |
| 61 | + if (hoverRegion instanceof final RegionWithTMToken regionWithToken) { |
| 62 | + final var text = regionWithToken.tokenText.replace(' ', '·').replace('\t', '→'); |
| 63 | + return "<b>" + text + "</b> (" + text.length() |
| 64 | + + " chars)<br>" |
| 65 | + + "<br>" |
| 66 | + + "<b>Token Type:</b> " + regionWithToken.token.type + "<br>" |
| 67 | + + "<b>TextMate Scopes:</b> <li>" + String.join("<li>", regionWithToken.token.scopes); |
| 68 | + } |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public @Nullable IRegion getHoverRegion(final @NonNullByDefault({}) ITextViewer textViewer, final int offset) { |
| 74 | + if (!PreferenceHelper.isTMTokenHoverEnabled()) |
| 75 | + return null; |
| 76 | + |
| 77 | + final @Nullable IDocument doc = textViewer.getDocument(); |
| 78 | + if (doc == null) |
| 79 | + return null; |
| 80 | + |
| 81 | + final TMDocumentModel model = TMModelManager.INSTANCE.getConnectedModel(doc); |
| 82 | + if (model == null) |
| 83 | + return null; |
| 84 | + |
| 85 | + try { |
| 86 | + // retrieve parsed TM tokens of the hovered line |
| 87 | + final int lineIndex = doc.getLineOfOffset(offset); |
| 88 | + final var tokens = model.getLineTokens(lineIndex); |
| 89 | + if (tokens == null) |
| 90 | + return null; |
| 91 | + |
| 92 | + // find the TM token at the hover position |
| 93 | + final int lineStartOffset = doc.getLineOffset(lineIndex); |
| 94 | + TMToken hoveredToken = null; |
| 95 | + TMToken nextToken = null; |
| 96 | + for (final TMToken token : tokens) { |
| 97 | + if (token.startIndex <= offset - lineStartOffset) { |
| 98 | + hoveredToken = token; |
| 99 | + } else { |
| 100 | + nextToken = token; |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + if (hoveredToken == null) |
| 105 | + return null; |
| 106 | + |
| 107 | + final int regionOffset = lineStartOffset + hoveredToken.startIndex; |
| 108 | + final int regionLength = nextToken == null |
| 109 | + ? doc.getLineLength(lineIndex) - hoveredToken.startIndex |
| 110 | + : nextToken.startIndex - hoveredToken.startIndex; |
| 111 | + return new RegionWithTMToken(regionOffset, regionLength, doc.get(regionOffset, regionLength), hoveredToken); |
| 112 | + } catch (final BadLocationException e) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments