refactor: fix lint issues

This commit is contained in:
Akash Yadav
2023-03-14 19:40:24 +05:30
parent b9bd8dd7df
commit fb4ec48940
5 changed files with 57 additions and 86 deletions
+2
View File
@@ -190,4 +190,6 @@ fun getEnvOrProp(key: String): String? {
afterEvaluate {
tasks.getByName("mergeDebugAssets").dependsOn(":subprojects:tooling-api-impl:copyJarToAssets")
tasks.getByName("mergeReleaseAssets").dependsOn(":subprojects:tooling-api-impl:copyJarToAssets")
tasks.getByName("lintAnalyzeDebug").dependsOn(":subprojects:tooling-api-impl:copyJarToAssets")
tasks.getByName("lintAnalyzeRelease").dependsOn(":subprojects:tooling-api-impl:copyJarToAssets")
}
@@ -17,41 +17,34 @@
package com.itsaky.androidide.editor.language.incremental;
import static com.itsaky.androidide.editor.language.incremental.LineState.INCOMPLETE;
import static com.itsaky.androidide.editor.language.incremental.LineState.NORMAL;
import static com.itsaky.androidide.syntax.colorschemes.SchemeAndroidIDE.FIXME_COMMENT;
import static com.itsaky.androidide.syntax.colorschemes.SchemeAndroidIDE.TODO_COMMENT;
import static com.itsaky.androidide.syntax.colorschemes.SchemeAndroidIDE.withoutCompletion;
import androidx.annotation.NonNull;
import com.blankj.utilcode.util.ArrayUtils;
import com.google.common.base.Preconditions;
import com.google.common.collect.EvictingQueue;
import com.itsaky.androidide.editor.language.java.JavaAnalyzer;
import com.itsaky.androidide.syntax.colorschemes.SchemeAndroidIDE;
import com.itsaky.androidide.utils.CharSequenceReader;
import com.itsaky.androidide.utils.ILogger;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Token;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Stack;
import java.util.stream.Collectors;
import io.github.rosemoe.sora.lang.analysis.AsyncIncrementalAnalyzeManager;
import io.github.rosemoe.sora.lang.styling.CodeBlock;
import io.github.rosemoe.sora.lang.styling.Span;
import io.github.rosemoe.sora.lang.styling.TextStyle;
import io.github.rosemoe.sora.text.Content;
import io.github.rosemoe.sora.util.IntPair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Stack;
import java.util.stream.Collectors;
import kotlin.Pair;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Token;
/**
* Base class for implementing an {@link AsyncIncrementalAnalyzeManager} in AndroidIDE.
@@ -59,9 +52,8 @@ import kotlin.Pair;
* @author Akash Yadav
*/
public abstract class BaseIncrementalAnalyzeManager
extends AsyncIncrementalAnalyzeManager<LineState, IncrementalToken> {
extends AsyncIncrementalAnalyzeManager<LineState, IncrementalToken> {
private static final ILogger LOG = ILogger.newInstance("BaseIncrementalAnalyzeManager");
protected final Lexer lexer;
private final int[] multilineStartTypes;
private final int[] multilineEndTypes;
@@ -76,7 +68,13 @@ public abstract class BaseIncrementalAnalyzeManager
this.multilineStartTypes = multilineTokenTypes[0];
this.multilineEndTypes = multilineTokenTypes[1];
this.blockTokens = Objects.requireNonNullElseGet(getCodeBlockTokens(), () -> new int[] {});
var tokens = getCodeBlockTokens();
if (tokens == null) {
tokens = new int[0];
}
this.blockTokens = tokens;
}
@NonNull
@@ -104,7 +102,7 @@ public abstract class BaseIncrementalAnalyzeManager
private void verifyMultilineTypes(@NonNull final int[][] types) {
Preconditions.checkState(
types.length == 2, "There must be exact two inner int[] in multiline token types");
types.length == 2, "There must be exact two inner int[] in multiline token types");
final var start = types[0];
final var end = types[1];
@@ -116,9 +114,9 @@ public abstract class BaseIncrementalAnalyzeManager
* Returns the token types which start and end a multiline token.
*
* @return A <b>2xn</b> matrix where int[] at index 0 specifies token types which start a
* multiline token and int[] 1 specifies tokens which end the multiline token. For example,
* <p>[['/', '*'], ['*', '/']].
* <p>But instead of characters, there must be token types.
* multiline token and int[] 1 specifies tokens which end the multiline token. For example,
* <p>[['/', '*'], ['*', '/']].
* <p>But instead of characters, there must be token types.
* @see JavaAnalyzer
*/
protected abstract int[][] getMultilineTokenStartEndTypes();
@@ -142,7 +140,7 @@ public abstract class BaseIncrementalAnalyzeManager
@Override
public LineTokenizeResult<LineState, IncrementalToken> tokenizeLine(
final CharSequence lineText, final LineState state, final int line) {
final CharSequence lineText, final LineState state, final int line) {
final var tokens = new ArrayList<IncrementalToken>();
var newState = 0;
var stateObj = new LineState();
@@ -153,7 +151,7 @@ public abstract class BaseIncrementalAnalyzeManager
newState = IntPair.getFirst(result);
if (newState == LineState.NORMAL) {
newState =
tokenizeNormal(lineText, IntPair.getSecond(result), tokens, stateObj, state.lexerMode);
tokenizeNormal(lineText, IntPair.getSecond(result), tokens, stateObj, state.lexerMode);
} else {
newState = LineState.INCOMPLETE;
}
@@ -165,7 +163,7 @@ public abstract class BaseIncrementalAnalyzeManager
@Override
public List<Span> generateSpansForLine(
final LineTokenizeResult<LineState, IncrementalToken> tokens) {
final LineTokenizeResult<LineState, IncrementalToken> tokens) {
var result = generateSpans(tokens);
Objects.requireNonNull(result);
@@ -184,13 +182,13 @@ public abstract class BaseIncrementalAnalyzeManager
* @return The spans for the tokens.
*/
protected abstract List<Span> generateSpans(
final LineTokenizeResult<LineState, IncrementalToken> tokens);
final LineTokenizeResult<LineState, IncrementalToken> tokens);
@Override
public List<CodeBlock> computeBlocks(
final Content text,
final AsyncIncrementalAnalyzeManager<LineState, IncrementalToken>.CodeBlockAnalyzeDelegate
delegate) {
final Content text,
final AsyncIncrementalAnalyzeManager<LineState, IncrementalToken>.CodeBlockAnalyzeDelegate
delegate) {
final var stack = new Stack<CodeBlock>();
final var blocks = new ArrayList<CodeBlock>();
var line = 0;
@@ -199,8 +197,8 @@ public abstract class BaseIncrementalAnalyzeManager
while (delegate.isNotCancelled() && line < text.getLineCount()) {
final var tokens = getState(line);
final var checkForIdentifiers =
tokens.state.state == LineState.NORMAL
|| (tokens.state.state == LineState.INCOMPLETE && tokens.tokens.size() > 1);
tokens.state.state == LineState.NORMAL
|| (tokens.state.state == LineState.INCOMPLETE && tokens.tokens.size() > 1);
if (!tokens.state.hasBraces && !checkForIdentifiers) {
line++;
continue;
@@ -208,7 +206,6 @@ public abstract class BaseIncrementalAnalyzeManager
for (int i = 0; i < tokens.tokens.size(); i++) {
final var token = tokens.tokens.get(i);
final var type = token.getType();
var offset = token.getStartIndex();
if (isCodeBlockStart(token)) {
if (stack.isEmpty()) {
@@ -283,15 +280,15 @@ public abstract class BaseIncrementalAnalyzeManager
* <p>By default, this method removes only the last token.
*
* @param incompleteToken The token which is the start of the incomplete token.
* @param tokens The list of tokens from which extra tokens must be removed.
* @param tokens The list of tokens from which extra tokens must be removed.
*/
protected void popTokensAfterIncomplete(
@NonNull IncrementalToken incompleteToken, @NonNull List<IncrementalToken> tokens) {
@NonNull IncrementalToken incompleteToken, @NonNull List<IncrementalToken> tokens) {
tokens.remove(tokens.size() - 1);
}
protected void handleLineCommentSpan(
@NonNull IncrementalToken token, @NonNull List<Span> spans, int offset) {
@NonNull IncrementalToken token, @NonNull List<Span> spans, int offset) {
var commentType = SchemeAndroidIDE.COMMENT;
// highlight special line comments
@@ -309,23 +306,23 @@ public abstract class BaseIncrementalAnalyzeManager
}
/**
* Called when the <code>state</code> in {@link #tokenizeLine(CharSequence, LineState)} is {@link
* LineState#NORMAL}.
* Called when the <code>state</code> in {@link #tokenizeLine(CharSequence, LineState)} is
* {@link LineState#NORMAL}.
*
* @param line The line source.
* @param line The line source.
* @param column The column in <code>line</code>.
* @param tokens The list of tokens that must be updated as the <code>line</code> is scanned.
* @param st The state object whose state must be after after the <code>line</code> has been
* scanned.
* @param st The state object whose state must be after after the <code>line</code> has been
* scanned.
* @return The new state.
*/
@SuppressWarnings("UnstableApiUsage")
protected int tokenizeNormal(
final CharSequence line,
final int column,
final List<IncrementalToken> tokens,
final LineState st,
final int lexerMode) {
final CharSequence line,
final int column,
final List<IncrementalToken> tokens,
final LineState st,
final int lexerMode) {
lexer.setInputStream(createStream(line));
if (lexer._mode != lexerMode) {
lexer.pushMode(lexerMode);
@@ -390,16 +387,16 @@ public abstract class BaseIncrementalAnalyzeManager
}
/**
* Called when the <code>state</code> in {@link #tokenizeLine(CharSequence, LineState)} is {@link
* LineState#INCOMPLETE}.
* Called when the <code>state</code> in {@link #tokenizeLine(CharSequence, LineState)} is
* {@link LineState#INCOMPLETE}.
*
* @param line The line source.
* @param line The line source.
* @param tokens The list of tokens that must be updated as the <code>line</code> is scanned.
* @return The state and offset.
*/
@SuppressWarnings("UnstableApiUsage")
protected long fillIncomplete(
CharSequence line, final List<IncrementalToken> tokens, final int lexerMode) {
CharSequence line, final List<IncrementalToken> tokens, final int lexerMode) {
lexer.setInputStream(createStream(line));
if (lexer._mode != lexerMode) {
lexer.pushMode(lexerMode);
@@ -407,7 +404,7 @@ public abstract class BaseIncrementalAnalyzeManager
final var queue = createEvictingQueueForTokens();
final var end = queue.getSecond();
final var allTokens =
lexer.getAllTokens().stream().map(IncrementalToken::new).collect(Collectors.toList());
lexer.getAllTokens().stream().map(IncrementalToken::new).collect(Collectors.toList());
if (allTokens.isEmpty()) {
return IntPair.pack(LineState.INCOMPLETE, 0);
}
@@ -441,15 +438,15 @@ public abstract class BaseIncrementalAnalyzeManager
@SuppressWarnings("UnstableApiUsage")
@NonNull
private Pair<EvictingQueue<IncrementalToken>, EvictingQueue<IncrementalToken>>
createEvictingQueueForTokens() {
createEvictingQueueForTokens() {
return new Pair<>(
EvictingQueue.create(multilineStartTypes.length),
EvictingQueue.create(multilineEndTypes.length));
EvictingQueue.create(multilineStartTypes.length),
EvictingQueue.create(multilineEndTypes.length));
}
@SuppressWarnings("UnstableApiUsage")
private boolean matchTokenTypes(
@NonNull int[] types, @NonNull EvictingQueue<IncrementalToken> tokens) {
@NonNull int[] types, @NonNull EvictingQueue<IncrementalToken> tokens) {
final var arr = tokens.toArray(new IncrementalToken[0]);
for (int i = 0; i < types.length; i++) {
if (types[i] != arr[i].getType()) {
@@ -105,11 +105,7 @@ inline fun <T : View> T.afterMeasured(crossinline f: T.() -> Unit) {
object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (measuredWidth > 0 && measuredHeight > 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
viewTreeObserver.removeOnGlobalLayoutListener(this)
} else {
viewTreeObserver.removeGlobalOnLayoutListener(this)
}
viewTreeObserver.removeOnGlobalLayoutListener(this)
f()
}
}
+1 -1
View File
@@ -17,7 +17,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:theme="@style/Theme.AndroidIDE">
<application android:theme="@style/Theme.AndroidIDE" android:supportsRtl="true">
<activity
android:name=".UIDesignerActivity"
android:configChanges="orientation|screenSize"
@@ -1,24 +0,0 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ This file is part of AndroidIDE.
~
~ AndroidIDE is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ AndroidIDE is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
-->
<com.google.android.material.textview.MaterialTextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="@style/TextAppearance.Material3.BodySmall"
tools:text="No views have been added." />