Skip to content

Commit

Permalink
refact: minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Jan 25, 2024
1 parent 1cff26b commit 3d8e19e
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private static class MatchResult {
}

private static final class MatchInjectionsResult extends MatchResult {
boolean isPriorityMatch;
final boolean isPriorityMatch;

MatchInjectionsResult(final RuleId matchedRuleId, final OnigCaptureIndex[] captureIndices, final boolean isPriorityMatch) {
super(matchedRuleId, captureIndices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ void setIndex(final int index) {

int locationAt(final int index) {
final int bytes = region.getBeg(index);
if (bytes > 0) {
return bytes;
}
return 0;
return bytes > 0 ? bytes : 0;
}

int count() {
Expand All @@ -54,9 +51,6 @@ int count() {

int lengthAt(final int index) {
final int bytes = region.getEnd(index) - region.getBeg(index);
if (bytes > 0) {
return bytes;
}
return 0;
return bytes > 0 ? bytes : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private OnigString(final String content, final byte[] bytesUTF8) {
bytesCount = bytesUTF8.length;
}

protected final String throwOutOfBoundsException(final String indexName, final int index, final int minIndex, final int maxIndex) {
protected final void throwOutOfBoundsException(final String indexName, final int index, final int minIndex, final int maxIndex) {
throw new ArrayIndexOutOfBoundsException(
indexName + " index " + index + " is out of range " + minIndex + ".." + maxIndex + " of " + this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected Map<String, Object> loadRaw(final Reader source) {
}

@Override
public final <T extends PropertySettable<?>> T parse(final Reader source, final ObjectFactory<T> factory) throws Exception {
public final <T extends PropertySettable<?>> T parse(final Reader source, final ObjectFactory<T> factory) {
final Map<String, Object> rawRoot = loadRaw(source);
return transform(rawRoot, factory);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.eclipse.jdt.annotation.Nullable;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

Expand Down Expand Up @@ -89,14 +88,13 @@ public <T extends PropertySettable<?>> T parse(final Reader source, final Object
final StringBuilder text = new StringBuilder();

@Override
public void characters(final char @Nullable [] chars, final int start, final int count) throws SAXException {
public void characters(final char @Nullable [] chars, final int start, final int count) {
text.append(chars, start, count);
}

@Override
@NonNullByDefault({})
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes)
throws SAXException {
public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) {
text.setLength(0);
switch (localName) {
case PLIST_DICT: {
Expand All @@ -121,7 +119,7 @@ public void startElement(final String uri, final String localName, final String

@Override
@NonNullByDefault({})
public void endElement(final String uri, final String localName, final String qName) throws SAXException {
public void endElement(final String uri, final String localName, final String qName) {
switch (localName) {
case PLIST_ARRAY: {
final var parentRef = parents.remove(parents.size() - 1);
Expand Down Expand Up @@ -199,7 +197,7 @@ public void endElement(final String uri, final String localName, final String qN
}

@SuppressWarnings("unchecked")
protected void setCurrentProperty(final Object value) {
private void setCurrentProperty(final Object value) {
path.removeLast();
final var obj = parents.get(parents.size() - 1);
switch (obj.sourceKind) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public final class ColorMap {

private boolean _isFrozen;
private final boolean _isFrozen;
private int _lastColorId = -1; // -1 and not 0 as in upstream project on purpose
private final List<String> _id2color = new ArrayList<>();
private final Map<String /*color*/, @Nullable Integer /*ID color*/> _color2id = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -105,6 +106,13 @@ public static String toStringWithIndex(final List<?> list) {
return sb.append(']').toString();
}

/**
* @return a new list without null elements
*/
public static <T> List<T> noNulls(final @Nullable List<T> coll) {
return coll == null || coll.isEmpty() ? Collections.emptyList() : coll.stream().filter(Objects::nonNull).toList();
}

private MoreCollections() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static IGrammarSource fromResource(final Class<?> clazz, final String resourceNa
final var contentType1 = contentType == null ? guessFileFormat(resourceName) : contentType;
return new IGrammarSource() {
@Override
public Reader getReader() throws IOException {
public Reader getReader() {
return new BufferedReader(new InputStreamReader(
clazz.getResourceAsStream(resourceName),
charset == null ? StandardCharsets.UTF_8 : charset));
Expand All @@ -107,7 +107,7 @@ static IGrammarSource fromString(final ContentType contentType, final String con

return new IGrammarSource() {
@Override
public Reader getReader() throws IOException {
public Reader getReader() {
return new StringReader(content);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static IThemeSource fromResource(final Class<?> clazz, final String resourceName
final var contentType1 = contentType == null ? guessFileFormat(resourceName) : contentType;
return new IThemeSource() {
@Override
public Reader getReader() throws IOException {
public Reader getReader() {
return new BufferedReader(new InputStreamReader(
clazz.getResourceAsStream(resourceName),
charset == null ? StandardCharsets.UTF_8 : charset));
Expand All @@ -107,7 +107,7 @@ static IThemeSource fromString(final ContentType contentType, final String conte

return new IThemeSource() {
@Override
public Reader getReader() throws IOException {
public Reader getReader() {
return new StringReader(content);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
package org.eclipse.tm4e.languageconfiguration.internal.model;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tm4e.core.internal.utils.StringUtils;

/**
Expand All @@ -25,11 +26,10 @@ public final class CompleteEnterAction extends EnterAction {
*/
public final String indentation;

public CompleteEnterAction(final EnterAction action, final String indentation) {
super(action.indentAction);
public CompleteEnterAction(IndentAction indentAction, @Nullable String appendText, @Nullable Integer removeText,
final String indentation) {
super(indentAction, appendText, removeText);
this.indentation = indentation;
this.appendText = action.appendText;
this.removeText = action.removeText;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,21 @@ public static IndentAction get(final @Nullable String value) {
/**
* Describes text to be appended after the new line and after the indentation.
*/
@Nullable
public String appendText;
public final @Nullable String appendText;

/**
* Describes the number of characters to remove from the new line's indentation.
*/
@Nullable
public Integer removeText;
public final @Nullable Integer removeText;

public EnterAction(final IndentAction indentAction) {
this.indentAction = indentAction;
this(indentAction, null, null);
}

/**
* @param appendText the appendText to set
*/
EnterAction withAppendText(@Nullable final String appendText) {
public EnterAction(final IndentAction indentAction, final @Nullable String appendText, final @Nullable Integer removeText) {
this.indentAction = indentAction;
this.appendText = appendText;
return this;
}

/**
* @param removeText the removeText to set
*/
EnterAction withRemoveText(@Nullable final Integer removeText) {
this.removeText = removeText;
return this;
}

@Override
Expand Down
Loading

0 comments on commit 3d8e19e

Please sign in to comment.