mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
Validate range before selecting in editor
This commit is contained in:
Binary file not shown.
@@ -35,6 +35,7 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import androidx.activity.result.ActivityResult;
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
@@ -50,6 +51,7 @@ import androidx.fragment.app.Fragment;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.transition.Slide;
|
||||
import androidx.transition.TransitionManager;
|
||||
|
||||
import com.blankj.utilcode.util.ClipboardUtils;
|
||||
import com.blankj.utilcode.util.FileUtils;
|
||||
import com.blankj.utilcode.util.IntentUtils;
|
||||
@@ -115,8 +117,9 @@ import com.itsaky.lsp.models.InitializeParams;
|
||||
import com.itsaky.lsp.models.Range;
|
||||
import com.itsaky.toaster.Toaster;
|
||||
import com.unnamed.b.atv.model.TreeNode;
|
||||
import io.github.rosemoe.sora.event.ContentChangeEvent;
|
||||
import io.github.rosemoe.sora.event.Unsubscribe;
|
||||
|
||||
import org.jetbrains.annotations.Contract;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@@ -131,9 +134,11 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.github.rosemoe.sora.event.ContentChangeEvent;
|
||||
import io.github.rosemoe.sora.event.Unsubscribe;
|
||||
import me.piruin.quickaction.ActionItem;
|
||||
import me.piruin.quickaction.QuickAction;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
|
||||
public class EditorActivity extends StudioActivity
|
||||
implements FileTreeFragment.FileActionListener,
|
||||
@@ -696,11 +701,7 @@ public class EditorActivity extends StudioActivity
|
||||
editor.setSelection(
|
||||
range.getStart().getLine(), range.getEnd().getColumn());
|
||||
} else {
|
||||
editor.setSelectionRegion(
|
||||
range.getStart().getLine(),
|
||||
range.getStart().getColumn(),
|
||||
range.getEnd().getLine(),
|
||||
range.getEnd().getColumn());
|
||||
editor.setSelection(range);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
/************************************************************************************
|
||||
/*
|
||||
* 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
|
||||
@@ -16,20 +14,23 @@
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
**************************************************************************************/
|
||||
*/
|
||||
|
||||
package com.itsaky.androidide.adapters;
|
||||
|
||||
import android.graphics.PorterDuff;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.itsaky.androidide.R;
|
||||
import com.itsaky.androidide.databinding.LayoutDiagnosticGroupBinding;
|
||||
import com.itsaky.androidide.interfaces.DiagnosticClickListener;
|
||||
import com.itsaky.androidide.models.DiagnosticGroup;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DiagnosticsAdapter extends RecyclerView.Adapter<DiagnosticsAdapter.VH> {
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
package com.itsaky.androidide.lsp;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import com.blankj.utilcode.util.FileIOUtils;
|
||||
import com.blankj.utilcode.util.FileUtils;
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||
@@ -47,7 +48,7 @@ import com.itsaky.lsp.models.Location;
|
||||
import com.itsaky.lsp.models.Range;
|
||||
import com.itsaky.lsp.models.TextEdit;
|
||||
import com.itsaky.toaster.Toaster;
|
||||
import io.github.rosemoe.sora.text.Content;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -55,15 +56,15 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import io.github.rosemoe.sora.text.Content;
|
||||
|
||||
/** AndroidIDE specific implementation of the LanguageClient */
|
||||
public class IDELanguageClientImpl implements ILanguageClient {
|
||||
|
||||
protected static final Gson gson = new Gson();
|
||||
protected static final Logger LOG = Logger.instance("AbstractLanguageClient");
|
||||
|
||||
private final Map<File, List<DiagnosticItem>> diagnostics = new HashMap<>();
|
||||
private static IDELanguageClientImpl mInstance;
|
||||
|
||||
private final Map<File, List<DiagnosticItem>> diagnostics = new HashMap<>();
|
||||
protected EditorActivityProvider activityProvider;
|
||||
|
||||
private IDELanguageClientImpl(EditorActivityProvider provider) {
|
||||
@@ -366,12 +367,7 @@ public class IDELanguageClientImpl implements ILanguageClient {
|
||||
.setSelection(
|
||||
range.getStart().getLine(), range.getStart().getColumn());
|
||||
} else {
|
||||
frag.getEditor()
|
||||
.setSelectionRegion(
|
||||
range.getStart().getLine(),
|
||||
range.getStart().getColumn(),
|
||||
range.getEnd().getLine(),
|
||||
range.getEnd().getColumn());
|
||||
frag.getEditor().setSelection(range);
|
||||
}
|
||||
} else {
|
||||
activity().openFileAndSelect(file, range);
|
||||
|
||||
@@ -38,8 +38,10 @@ import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.blankj.utilcode.util.FileIOUtils;
|
||||
import com.blankj.utilcode.util.SizeUtils;
|
||||
import com.itsaky.androidide.adapters.CompletionListAdapter;
|
||||
@@ -57,9 +59,10 @@ import com.itsaky.androidide.utils.Logger;
|
||||
import com.itsaky.androidide.utils.TypefaceUtils;
|
||||
import com.itsaky.inflater.values.ValuesTableFactory;
|
||||
import com.itsaky.lsp.models.Range;
|
||||
import io.github.rosemoe.sora.event.ContentChangeEvent;
|
||||
import io.github.rosemoe.sora.lang.EmptyLanguage;
|
||||
import io.github.rosemoe.sora.text.Content;
|
||||
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.StringReader;
|
||||
import java.nio.file.Files;
|
||||
@@ -67,8 +70,10 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
import io.github.rosemoe.sora.event.ContentChangeEvent;
|
||||
import io.github.rosemoe.sora.lang.EmptyLanguage;
|
||||
import io.github.rosemoe.sora.text.Content;
|
||||
|
||||
/**
|
||||
* A view that handles opened code editors.
|
||||
@@ -78,14 +83,12 @@ import org.antlr.v4.runtime.Token;
|
||||
@SuppressLint("ViewConstructor") // This view is always dynamically created.
|
||||
public class CodeEditorView extends FrameLayout {
|
||||
|
||||
private static final Logger LOG = Logger.instance("CodeEditorView");
|
||||
private final File file;
|
||||
private final LayoutCodeEditorBinding binding;
|
||||
|
||||
private boolean isModified;
|
||||
private boolean isFirstCreate;
|
||||
|
||||
private static final Logger LOG = Logger.instance("CodeEditorView");
|
||||
|
||||
public CodeEditorView(
|
||||
@NonNull Context context, @NonNull File file, final @NonNull Range selection) {
|
||||
super(context);
|
||||
@@ -122,12 +125,7 @@ public class CodeEditorView extends FrameLayout {
|
||||
selection.getStart().getLine(),
|
||||
selection.getStart().getColumn());
|
||||
} else {
|
||||
getEditor()
|
||||
.setSelectionRegion(
|
||||
selection.getStart().getLine(),
|
||||
selection.getStart().getColumn(),
|
||||
selection.getEnd().getLine(),
|
||||
selection.getEnd().getColumn());
|
||||
getEditor().setSelection(selection);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -126,17 +126,74 @@ public class IDEEditor extends CodeEditor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given range is valid for this editor's text.
|
||||
*
|
||||
* @param range The range to check.
|
||||
* @return <code>true</code> if valid, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isValidRange(final Range range) {
|
||||
if (range == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final var start = range.getStart();
|
||||
final var end = range.getEnd();
|
||||
|
||||
return isValidPosition(start)
|
||||
&& isValidPosition(end)
|
||||
&& start.compareTo(end) < 0; // make sure start position is before end position
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given position is valid for this editor's text.
|
||||
*
|
||||
* @param position The position to check.
|
||||
* @return <code>true</code> if valid, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isValidPosition(final Position position) {
|
||||
if (position == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return isValidLine(position.getLine())
|
||||
&& isValidColumn(position.getLine(), position.getColumn());
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given line is valid for this editor's text.
|
||||
*
|
||||
* @param line The line to check.
|
||||
* @return <code>true</code> if valid, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isValidLine(int line) {
|
||||
return line >= 0 && line < getText().getLineCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the given column is valid for this editor's text.
|
||||
*
|
||||
* @param line The line of the column to check.
|
||||
* @param column The column to check.
|
||||
* @return <code>true</code> if valid, <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean isValidColumn(int line, int column) {
|
||||
return column >= 0 && column < getText().getColumnCount(line);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set selection to the given range.
|
||||
*
|
||||
* @param range The range to select.
|
||||
*/
|
||||
public void setSelection(@NonNull Range range) {
|
||||
setSelectionRegion(
|
||||
range.getStart().getLine(),
|
||||
range.getStart().getColumn(),
|
||||
range.getEnd().getLine(),
|
||||
range.getEnd().getColumn());
|
||||
if (isValidRange(range)) {
|
||||
setSelectionRegion(
|
||||
range.getStart().getLine(),
|
||||
range.getStart().getColumn(),
|
||||
range.getEnd().getLine(),
|
||||
range.getEnd().getColumn());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user