Skip to content

Commit

Permalink
perf: avoid null check of parser in CSSTokenProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jun 8, 2023
1 parent dcf19b0 commit 19a562e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
*/
public class CSSParser {

private final CSSDocumentHandler handler;
protected final CSSDocumentHandler handler = new CSSDocumentHandler();

protected CSSParser() {
}

public CSSParser(final InputStream source) throws Exception {
this(toSource(source));
Expand All @@ -55,7 +58,6 @@ public CSSParser(final String source) throws Exception {
}

public CSSParser(final InputSource source, final Parser parser) throws CSSException, IOException {
this.handler = new CSSDocumentHandler();
parser.setDocumentHandler(handler);
parser.setConditionFactory(CSSConditionFactory.INSTANCE);
parser.setSelectorFactory(CSSSelectorFactory.INSTANCE);
Expand Down Expand Up @@ -84,5 +86,4 @@ public IStyle getBestStyle(final String... names) {
public List<IStyle> getStyles() {
return handler.getList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
package org.eclipse.tm4e.ui.themes.css;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -31,12 +33,24 @@

public class CSSTokenProvider extends AbstractTokenProvider {

private static class NoopCSSParser extends CSSParser {
@Override
public List<IStyle> getStyles() {
return Collections.emptyList();
}

@Override
public @Nullable IStyle getBestStyle(String... names) {
return null;
}
}

private final Map<IStyle, @Nullable IToken> tokenMaps = new HashMap<>();

@Nullable
private CSSParser parser;
private final CSSParser parser;

public CSSTokenProvider(final InputStream in) {
CSSParser parser = null;
try {
parser = new CSSParser(in);
for (final IStyle style : parser.getStyles()) {
Expand All @@ -62,6 +76,8 @@ public CSSTokenProvider(final InputStream in) {
} catch (final Exception ex) {
TMUIPlugin.logError(ex);
}

this.parser = parser == null ? new NoopCSSParser() : parser;
}

@Nullable
Expand All @@ -70,10 +86,6 @@ public IToken getToken(@Nullable final String type) {
if (type == null)
return null;

final var parser = this.parser;
if (parser == null)
return null;

final IStyle style = parser.getBestStyle(StringUtils.splitToArray(type, '.'));
if (style == null)
return null;
Expand All @@ -83,10 +95,6 @@ public IToken getToken(@Nullable final String type) {

@Nullable
private Color getColor(final boolean isForeground, final String... styles) {
final var parser = this.parser;
if (parser == null)
return null;

final var style = parser.getBestStyle(styles);
if (style == null)
return null;
Expand Down Expand Up @@ -120,7 +128,6 @@ public Color getEditorSelectionForeground() {
@Override
public Color getEditorSelectionBackground() {
return getColor(false, "editor", "selection");

}

@Nullable
Expand Down

0 comments on commit 19a562e

Please sign in to comment.