Skip to content

Commit

Permalink
feat: auto-reload modified CSS themes
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Feb 8, 2024
1 parent d44487b commit 9dab037
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.URL;
import java.nio.charset.StandardCharsets;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -27,7 +28,7 @@
*/
public abstract class TMResource implements ITMResource {

private static final String PLATFORM_PLUGIN = "platform:/plugin/"; //$NON-NLS-1$
private static final String PLATFORM_PLUGIN = "platform:/plugin/";

private String path;
private @Nullable String pluginId;
Expand All @@ -48,7 +49,7 @@ protected TMResource(final String path) {
this.path = path;
}

protected TMResource(final String path, @Nullable final String pluginId) {
protected TMResource(final String path, final @Nullable String pluginId) {
this.path = path;
this.pluginId = pluginId;
}
Expand All @@ -66,13 +67,24 @@ public String getPath() {
@Override
@SuppressWarnings("resource")
public InputStream getInputStream() throws IOException {
return new BufferedInputStream(pluginId != null
? new URL(PLATFORM_PLUGIN + pluginId + "/" + path).openStream()
: new FileInputStream(new File(path)));
return new BufferedInputStream(pluginId == null
? new FileInputStream(new File(path))
: new URL(PLATFORM_PLUGIN + pluginId + '/' + path).openStream());
}

protected long getLastModified() {
try {
return new File(pluginId == null
? path
: FileLocator.resolve(new URL(PLATFORM_PLUGIN + pluginId + '/' + path)).getFile() //
).lastModified();
} catch (final Exception ex) {
return 0;
}
}

protected @Nullable String getResourceContent() {
try (InputStream in = this.getInputStream()) {
try (InputStream in = getInputStream()) {
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
} catch (final Exception ex) {
TMEclipseRegistryPlugin.logError(ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

/**
* {@link ITheme} implementation.
*
*/
public class Theme extends TMResource implements ITheme {

Expand Down Expand Up @@ -137,7 +136,7 @@ public Color getEditorCurrentLineHighlight() {

@Nullable
private ITokenProvider getTokenProvider() {
if (tokenProvider == null) {
if (tokenProvider == null || isModified()) {
try (InputStream in = super.getInputStream()) {
tokenProvider = new CSSTokenProvider(in);
} catch (final Exception ex) {
Expand All @@ -148,6 +147,20 @@ private ITokenProvider getTokenProvider() {
return tokenProvider;
}

private long lastModified = -1;
private long lastModifiedRecheck = -1;

private boolean isModified() {
final long now = System.currentTimeMillis();
if (now > lastModifiedRecheck) {
lastModifiedRecheck = now + 5_000;
final long oldModified = lastModified;
lastModified = getLastModified();
return lastModified != oldModified;
}
return false;
}

@Nullable
@Override
public String toCSSStyleSheet() {
Expand Down

0 comments on commit 9dab037

Please sign in to comment.