Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: remove guava #611

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions org.eclipse.tm4e.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Bundle-Localization: plugin
Bundle-SymbolicName: org.eclipse.tm4e.core
Bundle-Version: 0.6.2.qualifier
Require-Bundle: com.google.gson;bundle-version="[2.10.1,3.0.0)",
com.google.guava;bundle-version="[32.1.0,33.0.0)",
org.apache.batik.css;bundle-version="[1.16.0,2.0.0)";resolution:=optional,
org.apache.batik.util;bundle-version="[1.16.0,2.0.0)";resolution:=optional,
org.jcodings;bundle-version="[1.0.58,2.0.0)",
Expand All @@ -23,7 +22,7 @@ Export-Package: org.eclipse.tm4e.core,
org.eclipse.tm4e.core.internal.matcher;x-friends:="org.eclipse.tm4e.core.tests",
org.eclipse.tm4e.core.internal.theme;x-friends:="org.eclipse.tm4e.core.tests",
org.eclipse.tm4e.core.internal.theme.raw;x-friends:="org.eclipse.tm4e.core.tests",
org.eclipse.tm4e.core.internal.utils;x-friends:="org.eclipse.tm4e.core.tests,org.eclipse.tm4e.registry,org.eclipse.tm4e.languageconfiguration,org.eclipse.tm4e.ui,org.eclipse.tm4e.ui.tests",
org.eclipse.tm4e.core.internal.utils;x-friends:="org.eclipse.tm4e.core.tests,org.eclipse.tm4e.registry,org.eclipse.tm4e.languageconfiguration,org.eclipse.tm4e.markdown,org.eclipse.tm4e.ui,org.eclipse.tm4e.ui.tests",
org.eclipse.tm4e.core.model,
org.eclipse.tm4e.core.registry,
org.eclipse.tm4e.core.theme,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -26,14 +27,13 @@
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.common.collect.Iterators;

final class PListContentHandler<T> extends DefaultHandler {

private static final Logger LOGGER = System.getLogger(PListContentHandler.class.getName());

private static final class PListPathImpl implements PListPath {
final LinkedList<String> keys = new LinkedList<>();
final List<String> keysUnmodifiable = Collections.unmodifiableList(keys);
final List<Integer> keysDepths = new ArrayList<>();
int depth = 0;

Expand Down Expand Up @@ -69,7 +69,7 @@ public String last() {

@Override
public Iterator<String> iterator() {
return Iterators.unmodifiableIterator(keys.iterator());
return keysUnmodifiable.iterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import org.eclipse.tm4e.core.internal.theme.raw.IRawThemeSetting;
import org.eclipse.tm4e.core.internal.utils.StringUtils;

import com.google.common.collect.Lists;

/**
* TextMate theme.
*
Expand Down Expand Up @@ -201,8 +199,8 @@ && isValidHexColor(stringSettingsBackground)) {
final var scope = getLastElement(segments);
List<String> parentScopes = null;
if (segments.size() > 1) {
parentScopes = segments.subList(0, segments.size() - 1);
parentScopes = Lists.reverse(parentScopes);
parentScopes = new ArrayList<>(segments.subList(0, segments.size() - 1));
Collections.reverse(parentScopes);
}

result.add(new ParsedThemeRule(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,15 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.Supplier;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

public final class ObjectCloner {

private static final LoadingCache<Class<?>, Optional<Method>> CLONE_METHODS = CacheBuilder.newBuilder().weakKeys()
.build(new CacheLoader<>() {
@Override
public Optional<Method> load(final Class<?> cls) {
try {
return Optional.of(cls.getMethod("clone"));
} catch (final Exception ex) {
return Optional.empty();
}
}
});
private static final WeakHashMap<Class<?>, Optional<Method>> CLONE_METHODS_CACHE = new WeakHashMap<>();

public static <@NonNull T> T deepClone(final T obj) {
return deepClone(obj, new IdentityHashMap<>());
Expand Down Expand Up @@ -105,8 +92,13 @@ public Optional<Method> load(final Class<?> cls) {
private static <@NonNull T> T shallowClone(final T obj, final Supplier<T> fallback) {
if (obj instanceof Cloneable) {
try {
final var objClass = obj.getClass();
final var cloneMethod = CLONE_METHODS.get(objClass);
final var cloneMethod = CLONE_METHODS_CACHE.computeIfAbsent(obj.getClass(), cls -> {
try {
return Optional.of(cls.getMethod("clone"));
} catch (final Exception ex) {
return Optional.empty();
}
});
if (cloneMethod.isPresent()) {
return (T) cloneMethod.get().invoke(obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public final class StringUtils {
private static final Pattern RGB = Pattern.compile("^#[0-9a-f]{3}", Pattern.CASE_INSENSITIVE);
private static final Pattern RGBA = Pattern.compile("^#[0-9a-f]{4}", Pattern.CASE_INSENSITIVE);

public static String nullToEmpty(@Nullable final String txt) {
return txt == null ? "" : txt;
}

public static boolean isNullOrEmpty(@Nullable final String txt) {
return txt == null || txt.isEmpty();
}

public static boolean isValidHexColor(final CharSequence hex) {
if (hex.isEmpty()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
Expand All @@ -33,10 +33,6 @@
import org.eclipse.tm4e.core.internal.utils.MoreCollections;
import org.eclipse.tm4e.core.internal.utils.StringUtils;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;

/**
* @see <a href=
* "https://github.com/microsoft/vscode/blob/b22c1ee2ac18b1446e8710edecd5b29cc4a3ed19/src/vs/workbench/services/textMate/browser/tokenizationSupport/textMateTokenizationSupport.ts">
Expand Down Expand Up @@ -74,15 +70,6 @@ public TokenizationResult tokenize(final String line, @Nullable final IStateStac
return tokenize(line, state, null, null);
}

private final LoadingCache<List<String>, String> tokenTypeOfScopesCache = CacheBuilder.newBuilder()
.expireAfterAccess(5, TimeUnit.SECONDS)
.build(new CacheLoader<List<String>, String>() {
@Override
public String load(final List<String> scopes) throws Exception {
return decodeTextMateToken(decodeMap, scopes);
}
});

@Override
public TokenizationResult tokenize(final String line,
@Nullable final IStateStack state,
Expand All @@ -97,7 +84,7 @@ public TokenizationResult tokenize(final String line,
final var tmTokens = new ArrayList<TMToken>(tokens.length < 10 ? tokens.length : 10);
String lastTokenType = null;
for (final var token : tokens) {
final String tokenType = tokenTypeOfScopesCache.getUnchecked(token.getScopes());
final String tokenType = decodeTextMateTokenCached.apply(decodeMap, token.getScopes());

// do not push a new token if the type is exactly the same (also helps with ligatures)
if (!tokenType.equals(lastTokenType)) {
Expand All @@ -121,6 +108,34 @@ public TokenizationResult tokenize(final String line,
tokenizationResult.isStoppedEarly());
}

private final BiFunction<DecodeMap, List<String>, String> decodeTextMateTokenCached = new BiFunction<>() {
private static final long EXPIRE_AFTER_ACCESS_MS = 5_000;

static final class CacheEntry {
CacheEntry(final String tokenType) {
this.tokenType = tokenType;
}

final String tokenType;
long lastAccessed;
}

private final Map<List<String>, CacheEntry> cache = new HashMap<>();
private long lastCacheCleanup = System.currentTimeMillis();

@Override
public String apply(final DecodeMap decodeMap, final List<String> scopes) {
final var entry = cache.computeIfAbsent(scopes, s -> new CacheEntry(decodeTextMateToken(decodeMap, s)));
final var now = System.currentTimeMillis();
entry.lastAccessed = now;
if (now - lastCacheCleanup > EXPIRE_AFTER_ACCESS_MS) {
lastCacheCleanup = now;
cache.values().removeIf(e -> now - e.lastAccessed > EXPIRE_AFTER_ACCESS_MS);
}
return entry.tokenType;
}
};

/**
* https://github.com/microsoft/vscode/blob/70d250824ef66ef09f04c4084b804d5f353fb704/src/vs/editor/node/textMate/TMSyntax.ts#L251
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import com.google.common.base.Splitter;

public final class TokenizationUtils {

private static final Splitter BY_LINE_SPLITTER = Splitter.onPattern("\\r?\\n");
private static final Pattern BY_LINE_SPLITTER = Pattern.compile("\\r?\\n");

/**
* Lazy tokenizes the given text.
Expand All @@ -37,7 +36,7 @@ public static Stream<ITokenizeLineResult<IToken[]>> tokenizeText(final CharSeque
}

final var prevStack = new AtomicReference<IStateStack>();
return BY_LINE_SPLITTER.splitToStream(text).map(line -> {
return BY_LINE_SPLITTER.splitAsStream(text).map(line -> {
final var tokenized = grammar.tokenizeLine(line, prevStack.get(), null);
prevStack.set(tokenized.getRuleStack());
return tokenized;
Expand Down
9 changes: 4 additions & 5 deletions org.eclipse.tm4e.markdown/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ Bundle-Name: %pluginName
Bundle-Vendor: %providerName
Bundle-Localization: plugin
Bundle-SymbolicName: org.eclipse.tm4e.markdown;singleton:=true
Bundle-Version: 0.5.3.qualifier
Bundle-Version: 0.5.4.qualifier
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.tm4e.core;bundle-version="0.6.1",
org.eclipse.tm4e.registry;bundle-version="0.6.5",
org.eclipse.tm4e.ui;bundle-version="0.7.1",
com.google.guava;bundle-version="[32.1.0,33.0.0)"
org.eclipse.tm4e.core;bundle-version="0.6.2",
org.eclipse.tm4e.registry;bundle-version="0.6.6",
org.eclipse.tm4e.ui;bundle-version="0.7.2"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Export-Package: org.eclipse.tm4e.markdown,
org.eclipse.tm4e.markdown.marked
Expand Down
2 changes: 1 addition & 1 deletion org.eclipse.tm4e.markdown/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

<artifactId>org.eclipse.tm4e.markdown</artifactId>
<packaging>eclipse-plugin</packaging>
<version>0.5.3-SNAPSHOT</version>
<version>0.5.4-SNAPSHOT</version>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import java.util.regex.Matcher;

import org.eclipse.jdt.annotation.Nullable;

import com.google.common.base.Strings;
import org.eclipse.tm4e.core.internal.utils.StringUtils;

public class InlineLexer {

Expand Down Expand Up @@ -57,13 +56,13 @@ public InlineLexer(@Nullable final Object links, @Nullable final Options options

public void output(String src) {
Matcher cap = null;
while (!Strings.isNullOrEmpty(src)) {
while (!StringUtils.isNullOrEmpty(src)) {

// strong
if ((cap = this.rules.strong.exec(src)) != null) {
src = src.substring(cap.group(0).length());
this.renderer.startStrong();
this.output(!Strings.isNullOrEmpty(cap.group(2)) ? cap.group(2) : cap.group(1));
this.output(!StringUtils.isNullOrEmpty(cap.group(2)) ? cap.group(2) : cap.group(1));
this.renderer.endStrong();
continue;
}
Expand All @@ -72,7 +71,7 @@ public void output(String src) {
if ((cap = this.rules.em.exec(src)) != null) {
src = src.substring(cap.group(0).length());
this.renderer.startEm();
this.output(!Strings.isNullOrEmpty(cap.group(2)) ? cap.group(2) : cap.group(1));
this.output(!StringUtils.isNullOrEmpty(cap.group(2)) ? cap.group(2) : cap.group(1));
this.renderer.endEm();
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import java.util.regex.Matcher;

import org.eclipse.jdt.annotation.Nullable;

import com.google.common.base.Strings;
import org.eclipse.tm4e.core.internal.utils.StringUtils;

public class Lexer {

Expand Down Expand Up @@ -61,7 +60,7 @@ private Tokens token(final String src, final boolean top) {
private Tokens token(String src, final boolean top, @Nullable final Object bq) {
src = src.replaceAll("^ +$", "");
Matcher cap;
while (!Strings.isNullOrEmpty(src)) {
while (!StringUtils.isNullOrEmpty(src)) {

// newline
if ((cap = this.rules.newline.exec(src)) != null) {
Expand All @@ -86,7 +85,7 @@ private Tokens token(String src, final boolean top, @Nullable final Object bq) {
if ((cap = this.rules.fences.exec(src)) != null) {
src = src.substring(cap.group(0).length());
final String lang = cap.group(2);
final String text = !Strings.isNullOrEmpty(cap.group(3)) ? cap.group(3) : "";
final String text = !StringUtils.isNullOrEmpty(cap.group(3)) ? cap.group(3) : "";
this.tokens.add(new Token(TokenType.code, lang, text));
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
package org.eclipse.tm4e.markdown.marked;

import org.eclipse.jdt.annotation.Nullable;

import com.google.common.base.Strings;
import org.eclipse.tm4e.core.internal.utils.StringUtils;

public class Parser {

Expand Down Expand Up @@ -75,14 +74,14 @@ private void tok(final InlineLexer inline) {
break;
case heading:
// this.renderer.heading(this.inline.output(token.text), token.depth, token.text);
this.renderer.heading(Strings.nullToEmpty(token.text), token.depth, Strings.nullToEmpty(token.text));
this.renderer.heading(StringUtils.nullToEmpty(token.text), token.depth, StringUtils.nullToEmpty(token.text));
break;
case code:
this.renderer.code(Strings.nullToEmpty(token.text), token.lang, token.escaped);
this.renderer.code(StringUtils.nullToEmpty(token.text), token.lang, token.escaped);
break;
case paragraph:
this.renderer.startParagraph();
inline.output(Strings.nullToEmpty(token.text));
inline.output(StringUtils.nullToEmpty(token.text));
this.renderer.endParagraph();
break;
}
Expand Down
3 changes: 1 addition & 2 deletions org.eclipse.tm4e.registry/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Bundle-RequiredExecutionEnvironment: JavaSE-17
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.equinox.preferences,
org.eclipse.tm4e.core;bundle-version="0.6.1",
com.google.gson;bundle-version="[2.10.1,3.0.0)",
com.google.guava;bundle-version="[32.1.0,33.0.0)"
com.google.gson;bundle-version="[2.10.1,3.0.0)"
Export-Package: org.eclipse.tm4e.registry
Bundle-Activator: org.eclipse.tm4e.registry.TMEclipseRegistryPlugin
Bundle-Localization: plugin
Expand Down
3 changes: 0 additions & 3 deletions org.eclipse.tm4e.repository/category.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
<bundle id="com.google.gson" version="0.0.0">
<category name="Dependencies"/>
</bundle>
<bundle id="com.google.guava" version="0.0.0">
<category name="Dependencies"/>
</bundle>
<bundle id="org.apache.batik.constants" version="0.0.0">
<category name="Dependencies"/>
</bundle>
Expand Down
3 changes: 1 addition & 2 deletions org.eclipse.tm4e.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ Require-Bundle: org.eclipse.core.expressions,
org.eclipse.e4.ui.css.swt.theme,
org.eclipse.tm4e.core;bundle-version="0.6.1",
org.eclipse.tm4e.registry;bundle-version="0.6.4",
com.google.gson;bundle-version="[2.10.1,3.0.0)",
com.google.guava;bundle-version="[32.1.0,33.0.0)"
com.google.gson;bundle-version="[2.10.1,3.0.0)"
Bundle-RequiredExecutionEnvironment: JavaSE-17
Export-Package: org.eclipse.tm4e.ui,
org.eclipse.tm4e.ui.internal.model;x-friends:="org.eclipse.tm4e.languageconfiguration,org.eclipse.tm4e.ui.tests",
Expand Down
Loading