|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 SAP S.E. and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * SAP S.E. - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.ui.internal.texteditor.codemining; |
| 15 | + |
| 16 | +import org.eclipse.swt.SWT; |
| 17 | +import org.eclipse.swt.custom.StyleRange; |
| 18 | +import org.eclipse.swt.custom.StyledText; |
| 19 | +import org.eclipse.swt.graphics.Color; |
| 20 | +import org.eclipse.swt.graphics.GC; |
| 21 | +import org.eclipse.swt.graphics.Point; |
| 22 | + |
| 23 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 24 | + |
| 25 | +import org.eclipse.jface.text.Position; |
| 26 | +import org.eclipse.jface.text.codemining.ICodeMiningProvider; |
| 27 | +import org.eclipse.jface.text.codemining.LineContentCodeMining; |
| 28 | + |
| 29 | +import org.eclipse.ui.texteditor.AbstractTextEditor; |
| 30 | + |
| 31 | +/** |
| 32 | + * A code mining that draws zero-width characters (like zero-width spaces) as |
| 33 | + * line content code minings. |
| 34 | + * |
| 35 | + * @see ZeroWidthCharactersLineContentCodeMiningProvider |
| 36 | + */ |
| 37 | +class ZeroWidthCharactersLineContentCodeMining extends LineContentCodeMining { |
| 38 | + |
| 39 | + private static final String ZW_CHARACTERS_MINING = "ZWSP"; //$NON-NLS-1$ |
| 40 | + private final IPreferenceStore store; |
| 41 | + private final int offset; |
| 42 | + |
| 43 | + public ZeroWidthCharactersLineContentCodeMining(int offset, ICodeMiningProvider provider, IPreferenceStore store) { |
| 44 | + super(new Position(offset, 1), false, provider); |
| 45 | + this.store = store; |
| 46 | + this.offset = offset; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean isResolved() { |
| 51 | + return true; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public String getLabel() { |
| 56 | + return ZW_CHARACTERS_MINING; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Point draw(GC gc, StyledText textWidget, Color color, int x, int y) { |
| 61 | + int oldAlpha = -1; |
| 62 | + boolean isAdvancedGraphicsPresent = gc.getAdvanced(); |
| 63 | + if (isAdvancedGraphicsPresent) { |
| 64 | + int alpha = store.getInt(AbstractTextEditor.PREFERENCE_WHITESPACE_CHARACTER_ALPHA_VALUE); |
| 65 | + oldAlpha = gc.getAlpha(); |
| 66 | + gc.setAlpha(alpha); |
| 67 | + } |
| 68 | + try { |
| 69 | + gc.setForeground(getColor(textWidget)); |
| 70 | + Point point = super.draw(gc, textWidget, color, x, y); |
| 71 | + gc.setForeground(color); |
| 72 | + return point; |
| 73 | + } finally { |
| 74 | + if (oldAlpha != -1) { |
| 75 | + gc.setAlpha(oldAlpha); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private Color getColor(StyledText textWidget) { |
| 81 | + int off = offset - 1; |
| 82 | + Color fg; |
| 83 | + boolean isFullSelectionStyle = (textWidget.getStyle() & SWT.FULL_SELECTION) != SWT.NONE; |
| 84 | + if (!textWidget.getBlockSelection() && isFullSelectionStyle && isOffsetSelected(textWidget, off)) { |
| 85 | + fg = textWidget.getSelectionForeground(); |
| 86 | + } else { |
| 87 | + if (off < 0 || off >= textWidget.getCharCount()) { |
| 88 | + fg = textWidget.getForeground(); |
| 89 | + } else { |
| 90 | + StyleRange styleRange = textWidget.getStyleRangeAtOffset(off); |
| 91 | + if (styleRange == null || styleRange.foreground == null) { |
| 92 | + fg = textWidget.getForeground(); |
| 93 | + } else { |
| 94 | + fg = styleRange.foreground; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + return fg; |
| 99 | + } |
| 100 | + |
| 101 | + private static final boolean isOffsetSelected(StyledText widget, int offset) { |
| 102 | + Point selection = widget.getSelection(); |
| 103 | + return offset >= selection.x && offset < selection.y; |
| 104 | + } |
| 105 | +} |
0 commit comments