Skip to content

Commit

Permalink
Rework preferences page.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbr committed Feb 18, 2023
1 parent a423d04 commit efe131c
Show file tree
Hide file tree
Showing 20 changed files with 675 additions and 460 deletions.
2 changes: 1 addition & 1 deletion net.certiv.tools.indentguide.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature
id="net.certiv.tools.indentguide.feature"
label="IndentGuide"
version="1.8.0.qualifier"
version="2.0.0.qualifier"
provider-name="Certiv Analytis">

<description>
Expand Down
2 changes: 1 addition & 1 deletion net.certiv.tools.indentguide.feature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>net.certiv</groupId>
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
<version>1.8.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>net.certiv.tools.indentguide.feature</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion net.certiv.tools.indentguide.plugin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: IndentGuide
Bundle-SymbolicName: net.certiv.tools.indentguide;singleton:=true
Bundle-Version: 1.8.0.qualifier
Bundle-Version: 2.0.0.qualifier
Bundle-Activator: net.certiv.tools.indentguide.Activator
Bundle-Vendor: Certiv Analytics
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.26.100,4.0.0)",
Expand Down
2 changes: 1 addition & 1 deletion net.certiv.tools.indentguide.plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>net.certiv</groupId>
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
<version>1.8.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>net.certiv.tools.indentguide</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,31 @@
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.jface.resource.StringConverter;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.themes.ColorUtil;
import org.osgi.framework.BundleContext;
import org.osgi.service.event.EventHandler;

import net.certiv.tools.indentguide.preferences.Settings;

@SuppressWarnings("restriction")
public class Activator extends AbstractUIPlugin {

public static final String PLUGIN_ID = "net.certiv.tools.indentguide"; //$NON-NLS-1$
private static final String EditorsID = "org.eclipse.ui.editors"; //$NON-NLS-1$
private static final String PREFIX = "Indent Guide: "; //$NON-NLS-1$

private static Activator plugin;

private final IEclipsePreferences[] scopes = new IEclipsePreferences[2];
private final EventHandler themeChange = event -> {
private final IEclipsePreferences[] editorScopes = new IEclipsePreferences[] {
InstanceScope.INSTANCE.getNode(EditorsID), DefaultScope.INSTANCE.getNode(EditorsID) };

private final IPropertyChangeListener themeChange = event -> {
disposeLineColor();
log("Theme change '%s'", event);
};
Expand All @@ -51,35 +50,24 @@ public Activator() {
}

/** Returns the shared instance */
public static Activator getDefault() { return plugin; }
public static Activator getDefault() {
return plugin;
}

@Override
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;

log("Indent guide: startup");

scopes[0] = InstanceScope.INSTANCE.getNode(EditorsID);
scopes[1] = DefaultScope.INSTANCE.getNode(EditorsID);

IWorkbench wb = PlatformUI.getWorkbench();
IEventBroker broker = wb.getService(IEventBroker.class);
if (broker != null) {
broker.subscribe(IThemeEngine.Events.THEME_CHANGED, themeChange);
}
PlatformUI.getWorkbench().getThemeManager().addPropertyChangeListener(themeChange);
log("Startup");
}

@Override
public void stop(BundleContext context) throws Exception {
IWorkbench wb = PlatformUI.getWorkbench();
IEventBroker broker = wb.getService(IEventBroker.class);
if (broker != null) {
broker.unsubscribe(themeChange);
}

PlatformUI.getWorkbench().getThemeManager().removePropertyChangeListener(themeChange);
disposeLineColor();
scopes[0] = scopes[1] = null;
editorScopes[0] = editorScopes[1] = null;
plugin = null;
super.stop(context);
}
Expand All @@ -92,11 +80,20 @@ public Color getColor() {
}
String spec = getPreferenceStore().getString(key);
color = new Color(PlatformUI.getWorkbench().getDisplay(), ColorUtil.getColorValue(spec));
log(String.format("Line color set %s -> %s", key, spec));
log("Line color set %s -> %s", key, spec);
}
return color;
}

public void setColor(Color color) {
disposeLineColor();
this.color = color;
}

public void setColor(RGB rgb) {
setColor(new Color(PlatformUI.getWorkbench().getDisplay(), rgb));
}

/**
* Returns {@code true} if the current theme is 'dark', defined as where the foreground color is
* relatively darker than the background color. (black -> '0'; white -> '255*3')
Expand All @@ -108,19 +105,14 @@ public boolean isDarkTheme() {
}

private RGB getRawRGB(String key) {
String value = Platform.getPreferencesService().get(key, null, scopes);
String value = Platform.getPreferencesService().get(key, null, editorScopes);
if (value == null) return PreferenceConverter.COLOR_DEFAULT_DEFAULT;

RGB rgb = StringConverter.asRGB(value, null);
if (rgb == null) return PreferenceConverter.COLOR_DEFAULT_DEFAULT;
return rgb;
}

public void setColor(Color color) {
disposeLineColor();
this.color = color;
}

private void disposeLineColor() {
if (color != null) {
color.dispose();
Expand All @@ -129,7 +121,7 @@ private void disposeLineColor() {
}

public static void log(String fmt, Object... args) {
plugin.getLog().log(new Status(IStatus.INFO, PLUGIN_ID, String.format(fmt, args)));
plugin.getLog().log(new Status(IStatus.INFO, PLUGIN_ID, PREFIX + String.format(fmt, args)));
}

public static void log(Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
public class IndentGuidePainter implements IPainter, PaintListener {

private static final Pattern COMMENT_LEAD = Pattern.compile("^ \\*([ \\t].*|/.*|)$"); // $NON-NLS-1$
private static final String SPACE = " "; //$NON-NLS-1$

private static final char TAB = '\t'; // $NON-NLS-1$
private static final char SPC = ' '; // $NON-NLS-1$
private static final String SPACE = " "; // $NON-NLS-1$

private boolean advanced; // advanced graphics subsystem
private boolean active; // painter state
Expand Down Expand Up @@ -114,7 +117,8 @@ public void paint(int reason) {
if (widgetOffset >= 0 && redrawLength > 0) {
widget.redrawRange(widgetOffset, redrawLength, true);
}
} catch (BadLocationException e) {}
}
catch (BadLocationException e) {}
}
}

Expand Down Expand Up @@ -182,7 +186,7 @@ private void handleDrawRequest(GC gc, int x, int y, int w, int h) {
*/
private void drawLineRange(GC gc, int begLine, int endLine, int x, int w) {
int tabWidth = widget.getTabs();
int spaceWidth = gc.stringExtent(SPACE).x - 1;
int spcWidth = Math.max(1, gc.stringExtent(SPACE).x - 1);

StyledTextContent content = widget.getContent();

Expand Down Expand Up @@ -216,26 +220,26 @@ private void drawLineRange(GC gc, int begLine, int endLine, int x, int w) {
}
int count = countSpaces(text, tabWidth) + extend;
for (int col = drawLeftEnd ? 0 : tabWidth; col < count; col += tabWidth) {
draw(gc, offset, col, spaceWidth);
draw(gc, offset, col, spcWidth);
}
}
}
}

private void draw(GC gc, int offset, int column, int spaceWidth) {
private void draw(GC gc, int offset, int col, int spcWidth) {
Point pos = widget.getLocationAtOffset(offset);
pos.x += column * spaceWidth + lineShift;
pos.x += col * spcWidth + lineShift;
gc.drawLine(pos.x, pos.y, pos.x, pos.y + widget.getLineHeight(offset));
}

private int countSpaces(String str, int tabs) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
switch (str.charAt(i)) {
case ' ':
case SPC:
count++;
break;
case '\t':
case TAB:
int z = tabs - count % tabs;
count += z;
break;
Expand All @@ -252,10 +256,10 @@ private boolean assumeCommentBlock(String text, int tabs) {
int index = 0;
for (int i = 0; i < count; i++) {
switch (text.charAt(index)) {
case ' ':
case SPC:
index++;
break;
case '\t':
case TAB:
index++;
int z = tabs - i % tabs;
i += z;
Expand Down Expand Up @@ -320,6 +324,7 @@ private int getDocumentOffset(int widgetOffset) {
ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
return extension.widgetOffset2ModelOffset(widgetOffset);
}

IRegion visible = viewer.getVisibleRegion();
if (widgetOffset > visible.getLength()) return -1;
return widgetOffset + visible.getOffset();
Expand Down
Loading

0 comments on commit efe131c

Please sign in to comment.