Skip to content

Commit

Permalink
refact: ensure consistent placement of @Nullable annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Dec 18, 2024
1 parent 7ea2e74 commit 075d8b6
Show file tree
Hide file tree
Showing 76 changed files with 256 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ final class AttributedScopeStack {
record Frame(int encodedTokenAttributes, List<String> scopeNames) {
}

@Nullable
static AttributedScopeStack fromExtension(final @Nullable AttributedScopeStack namesScopeList,
static @Nullable AttributedScopeStack fromExtension(final @Nullable AttributedScopeStack namesScopeList,
final List<AttributedScopeStack.Frame> contentNameScopesList) {
var current = namesScopeList;
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ final class BasicScopeAttributesProvider {

private final ConcurrentMap<String /*scopeName*/, BasicScopeAttributes> cache = new ConcurrentHashMap<>();

BasicScopeAttributesProvider(final int initialLanguage, @Nullable final Map<String, Integer> embeddedLanguages) {
BasicScopeAttributesProvider(final int initialLanguage, final @Nullable Map<String, Integer> embeddedLanguages) {
this._defaultAttributes = new BasicScopeAttributes(initialLanguage, OptionalStandardTokenType.NotSet);
this._embeddedLanguagesMatcher = new ScopeMatcher<>(defaultIfNull(embeddedLanguages, Collections.emptyMap()));
}
Expand All @@ -50,7 +50,7 @@ BasicScopeAttributes getDefaultAttributes() {
return this._defaultAttributes;
}

BasicScopeAttributes getBasicScopeAttributes(@Nullable final String scopeName) {
BasicScopeAttributes getBasicScopeAttributes(final @Nullable String scopeName) {
if (scopeName == null) {
return BasicScopeAttributesProvider._NULL_SCOPE_METADATA;
}
Expand Down Expand Up @@ -92,8 +92,7 @@ private int _scopeToLanguage(final String scopeName) {
private static final class ScopeMatcher<TValue> {

private final Map<String, TValue> values;
@Nullable
private final Pattern scopesRegExp;
private final @Nullable Pattern scopesRegExp;

ScopeMatcher(final Map<String, TValue> values) {
if (values.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,27 @@ public final class Grammar implements IGrammar, IRuleFactoryHelper {

private final String rootScopeName;

@Nullable
private RuleId _rootId;
private @Nullable RuleId _rootId;
private int _lastRuleId = 0;
private final Map<RuleId, @Nullable Rule> _ruleId2desc = new HashMap<>();
private final Map<String /*scopeName*/, IRawGrammar> includedGrammars = new HashMap<>();
private final IGrammarRepository _grammarRepository;
private final IRawGrammar _grammar;
final IThemeProvider themeProvider;

@Nullable
private List<Injection> _injections;
private @Nullable List<Injection> _injections;
private final BasicScopeAttributesProvider _basicScopeAttributesProvider;
private final List<TokenTypeMatcher> _tokenTypeMatchers = new ArrayList<>();

@Nullable
private final BalancedBracketSelectors balancedBracketSelectors;
private final @Nullable BalancedBracketSelectors balancedBracketSelectors;

public Grammar(
final String rootScopeName,
final IRawGrammar grammar,
final int initialLanguage,
@Nullable final Map<String, Integer> embeddedLanguages,
@Nullable final Map<String, Integer> tokenTypes,
@Nullable final BalancedBracketSelectors balancedBracketSelectors,
final @Nullable Map<String, Integer> embeddedLanguages,
final @Nullable Map<String, Integer> tokenTypes,
final @Nullable BalancedBracketSelectors balancedBracketSelectors,
final IGrammarRepository grammarRepository,
final IThemeProvider themeProvider) {

Expand Down Expand Up @@ -220,8 +217,7 @@ public Rule getRule(final RuleId ruleId) {
}

@Override
@Nullable
public IRawGrammar getExternalGrammar(final String scopeName, @Nullable final IRawRepository repository) {
public @Nullable IRawGrammar getExternalGrammar(final String scopeName, final @Nullable IRawRepository repository) {
if (this.includedGrammars.containsKey(scopeName)) {
return this.includedGrammars.get(scopeName);
}
Expand All @@ -236,7 +232,7 @@ public IRawGrammar getExternalGrammar(final String scopeName, @Nullable final IR
return null;
}

private IRawGrammar initGrammar(IRawGrammar grammar, @Nullable final IRawRule base) {
private IRawGrammar initGrammar(IRawGrammar grammar, final @Nullable IRawRule base) {
grammar = ObjectCloner.deepClone(grammar);

final var repo = grammar.getRepository();
Expand All @@ -254,8 +250,8 @@ public ITokenizeLineResult<IToken[]> tokenizeLine(final String lineText) {

@Override
public ITokenizeLineResult<IToken[]> tokenizeLine(final String lineText,
@Nullable final IStateStack prevState,
@Nullable final Duration timeLimit) {
final @Nullable IStateStack prevState,
final @Nullable Duration timeLimit) {
return _tokenize(lineText, (StateStack) prevState, false, timeLimit);
}

Expand All @@ -265,8 +261,8 @@ public ITokenizeLineResult<int[]> tokenizeLine2(final String lineText) {
}

@Override
public ITokenizeLineResult<int[]> tokenizeLine2(final String lineText, @Nullable final IStateStack prevState,
@Nullable final Duration timeLimit) {
public ITokenizeLineResult<int[]> tokenizeLine2(final String lineText, final @Nullable IStateStack prevState,
final @Nullable Duration timeLimit) {
return _tokenize(lineText, (StateStack) prevState, true, timeLimit);
}

Expand All @@ -275,7 +271,7 @@ private <T> T _tokenize(
String lineText,
@Nullable StateStack prevState,
final boolean emitBinaryTokens,
@Nullable final Duration timeLimit) {
final @Nullable Duration timeLimit) {
var rootId = this._rootId;
if (rootId == null) {
rootId = this._rootId = RuleFactory.getCompiledRuleId(
Expand Down Expand Up @@ -358,8 +354,7 @@ private <T> T _tokenize(
}

@Override
@Nullable
public String getName() {
public @Nullable String getName() {
return _grammar.getName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ private void scanNext() {
}
}

@Nullable
private MatchResult matchRule(final Grammar grammar, final OnigString lineText, final boolean isFirstLine, final int linePos,
private @Nullable MatchResult matchRule(final Grammar grammar, final OnigString lineText, final boolean isFirstLine, final int linePos,
final StateStack stack, final int anchorPosition) {
final var rule = stack.getRule(grammar);
final var ruleScanner = rule.compileAG(grammar, stack.endRule, isFirstLine, linePos == anchorPosition);
Expand All @@ -319,8 +318,7 @@ private MatchResult matchRule(final Grammar grammar, final OnigString lineText,
return null;
}

@Nullable
private MatchResult matchRuleOrInjections(final Grammar grammar, final OnigString lineText, final boolean isFirstLine,
private @Nullable MatchResult matchRuleOrInjections(final Grammar grammar, final OnigString lineText, final boolean isFirstLine,
final int linePos, final StateStack stack, final int anchorPosition) {
// Look for normal grammar rule
final MatchResult matchResult = matchRule(grammar, lineText, isFirstLine, linePos, stack, anchorPosition);
Expand Down Expand Up @@ -355,9 +353,8 @@ private MatchResult matchRuleOrInjections(final Grammar grammar, final OnigStrin
return matchResult;
}

@Nullable
private MatchInjectionsResult matchInjections(final List<Injection> injections, final Grammar grammar, final OnigString lineText,
final boolean isFirstLine, final int linePos, final StateStack stack, final int anchorPosition) {
private @Nullable MatchInjectionsResult matchInjections(final List<Injection> injections, final Grammar grammar,
final OnigString lineText, final boolean isFirstLine, final int linePos, final StateStack stack, final int anchorPosition) {

// The lower the better
var bestMatchRating = Integer.MAX_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,12 @@ public String toString() {

private final List<TokenTypeMatcher> _tokenTypeOverrides;

@Nullable
private final BalancedBracketSelectors balancedBracketSelectors;
private final @Nullable BalancedBracketSelectors balancedBracketSelectors;

LineTokens(final boolean emitBinaryTokens,
final String lineText,
final List<TokenTypeMatcher> tokenTypeOverrides,
@Nullable final BalancedBracketSelectors balancedBracketSelectors) {
final @Nullable BalancedBracketSelectors balancedBracketSelectors) {

this._emitBinaryTokens = emitBinaryTokens;
this._tokenTypeOverrides = tokenTypeOverrides;
Expand All @@ -130,7 +129,7 @@ void produce(final StateStack stack, final int endIndex) {
this.produceFromScopes(stack.contentNameScopesList, endIndex);
}

void produceFromScopes(@Nullable final AttributedScopeStack scopesList, final int endIndex) {
void produceFromScopes(final @Nullable AttributedScopeStack scopesList, final int endIndex) {
if (this._lastTokenEndIndex >= endIndex) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
*/
public final class ScopeStack {

@Nullable
static ScopeStack push(@Nullable ScopeStack path, final List<String> scopeNames) {
static @Nullable ScopeStack push(@Nullable ScopeStack path, final List<String> scopeNames) {
for (final var name : scopeNames) {
path = new ScopeStack(path, name);
}
Expand All @@ -41,20 +40,18 @@ public static ScopeStack from(final String first) {
return new ScopeStack(null, first);
}

@Nullable
public static ScopeStack from(final String... segments) {
public static @Nullable ScopeStack from(final String... segments) {
ScopeStack result = null;
for (final String segment : segments) {
result = new ScopeStack(result, segment);
}
return result;
}

@Nullable
public final ScopeStack parent;
public final @Nullable ScopeStack parent;
public final String scopeName;

ScopeStack(@Nullable final ScopeStack parent, final String scopeName) {
ScopeStack(final @Nullable ScopeStack parent, final String scopeName) {
this.parent = parent;
this.scopeName = scopeName;
}
Expand Down Expand Up @@ -92,7 +89,7 @@ public boolean isExtending(final ScopeStack other) {
return parent.isExtending(other);
}

List<String> getExtensionIfDefined(@Nullable final ScopeStack base) {
List<String> getExtensionIfDefined(final @Nullable ScopeStack base) {
final var result = new ArrayList<String>();
@Nullable
ScopeStack item = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ record Frame(
/**
* The previous state on the stack (or null for the root state).
*/
@Nullable
private final StateStack parent;
private final @Nullable StateStack parent;

/**
* The state (rule) that this element represents.
Expand All @@ -97,31 +96,28 @@ record Frame(
/**
* The "pop" (end) condition for this state in case that it was dynamically generated through captured text.
*/
@Nullable
final String endRule;
final @Nullable String endRule;

/**
* The list of scopes containing the "name" for this state.
*/
@Nullable
final AttributedScopeStack nameScopesList;
final @Nullable AttributedScopeStack nameScopesList;

/**
* The list of scopes containing the "contentName" (besides "name") for this state.
* This list **must** contain as an element `scopeName`.
*/
@Nullable
final AttributedScopeStack contentNameScopesList;
final @Nullable AttributedScopeStack contentNameScopesList;

StateStack(
@Nullable final StateStack parent,
final @Nullable StateStack parent,
final RuleId ruleId,
final int enterPos,
final int anchorPos,
final boolean beginRuleCapturedEOL,
@Nullable final String endRule,
@Nullable final AttributedScopeStack nameScopesList,
@Nullable final AttributedScopeStack contentNameScopesList) {
final @Nullable String endRule,
final @Nullable AttributedScopeStack nameScopesList,
final @Nullable AttributedScopeStack contentNameScopesList) {

this.parent = parent;
this.ruleId = ruleId;
Expand All @@ -135,7 +131,7 @@ record Frame(
}

@Override
public boolean equals(@Nullable final Object other) {
public boolean equals(final @Nullable Object other) {
if (other instanceof final StateStack otherState) {
return _equals(this, otherState);
}
Expand Down Expand Up @@ -224,9 +220,9 @@ StateStack push(
final int enterPos,
final int anchorPos,
final boolean beginRuleCapturedEOL,
@Nullable final String endRule,
@Nullable final AttributedScopeStack nameScopesList,
@Nullable final AttributedScopeStack contentNameScopesList) {
final @Nullable String endRule,
final @Nullable AttributedScopeStack nameScopesList,
final @Nullable AttributedScopeStack contentNameScopesList) {
return new StateStack(
this,
ruleId,
Expand Down Expand Up @@ -324,7 +320,7 @@ Frame toStateStackFrame() {
: Collections.emptyList());
}

public static StateStack pushFrame(@Nullable final StateStack self, final Frame frame) {
public static StateStack pushFrame(final @Nullable StateStack self, final Frame frame) {
final var namesScopeList = AttributedScopeStack.fromExtension(self == null ? null : self.nameScopesList,
frame.nameScopesList);
final var enterPos = frame.enterPos;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ private static class Context {
}

private static final class ContextWithRepository extends Context {
@Nullable
final IRawRepository repository;

ContextWithRepository(final Context context, @Nullable final IRawRepository repository) {
final @Nullable IRawRepository repository;

ContextWithRepository(final Context context, final @Nullable IRawRepository repository) {
super(context.baseGrammar, context.selfGrammar);
this.repository = repository;
}

ContextWithRepository(final IRawGrammar baseGrammar, final IRawGrammar selfGrammar, @Nullable final IRawRepository repository) {
ContextWithRepository(final IRawGrammar baseGrammar, final IRawGrammar selfGrammar, final @Nullable IRawRepository repository) {
super(baseGrammar, selfGrammar);
this.repository = repository;
}
Expand Down Expand Up @@ -227,8 +227,7 @@ private void collectExternalReferencesInRules(
break;
case TopLevelReference:
case TopLevelRepositoryReference:
@Nullable
final IRawGrammar selfGrammar = reference.scopeName.equals(context.selfGrammar.getScopeName())
final @Nullable IRawGrammar selfGrammar = reference.scopeName.equals(context.selfGrammar.getScopeName())
? context.selfGrammar
: reference.scopeName.equals(context.baseGrammar.getScopeName())
? context.baseGrammar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public interface IRawRepository {

static IRawRepository merge(@Nullable final IRawRepository... sources) {
static IRawRepository merge(final @Nullable IRawRepository... sources) {
final var merged = new RawRepository();
for (final var source : sources) {
if (source == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public String getScopeName() {
}

@Override
public @Nullable Object put(final String key, @Nullable final Object value) {
public @Nullable Object put(final String key, final @Nullable Object value) {
if (FILE_TYPES.equals(key))
fileTypes = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ private IRawRule getOrThrow(final String key) {
}

@Override
@Nullable
public IRawRule getRule(final String name) {
public @Nullable IRawRule getRule(final String name) {
try {
return get(name);
} catch (final ClassCastException ex) {
Expand Down
Loading

0 comments on commit 075d8b6

Please sign in to comment.