Show quickfixes for diagnostic items

This commit is contained in:
Akash Yadav
2022-03-16 20:44:29 +05:30
parent d71e736ecf
commit 7575d12c95
7 changed files with 738 additions and 578 deletions
+1
View File
@@ -57,6 +57,7 @@
<entry key="..\:/Projects/AndroidStudioProjects/AndroidIDE/app/src/main/res/layout/layout_completion_item.xml" value="0.23854166666666668" />
<entry key="..\:/Projects/AndroidStudioProjects/AndroidIDE/app/src/main/res/layout/layout_crash_report.xml" value="0.25" />
<entry key="..\:/Projects/AndroidStudioProjects/AndroidIDE/app/src/main/res/layout/layout_diagnostic_info.xml" value="0.20512820512820512" />
<entry key="..\:/Projects/AndroidStudioProjects/AndroidIDE/app/src/main/res/layout/layout_diagnostic_window.xml" value="0.25" />
<entry key="..\:/Projects/AndroidStudioProjects/AndroidIDE/app/src/main/res/layout/layout_dimension_attr_editor.xml" value="0.25" />
<entry key="..\:/Projects/AndroidStudioProjects/AndroidIDE/app/src/main/res/layout/layout_divider.xml" value="0.25" />
<entry key="..\:/Projects/AndroidStudioProjects/AndroidIDE/app/src/main/res/layout/layout_editor_bottom_sheet.xml" value="0.19572649572649573" />
Binary file not shown.
@@ -17,18 +17,36 @@
package com.itsaky.androidide.views.editor;
import static com.itsaky.androidide.utils.Logger.instance;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.blankj.utilcode.util.ThreadUtils;
import com.itsaky.androidide.R;
import com.itsaky.androidide.databinding.LayoutDiagnosticWindowBinding;
import com.itsaky.androidide.utils.DialogUtils;
import com.itsaky.androidide.utils.Logger;
import com.itsaky.lsp.models.CodeActionItem;
import com.itsaky.lsp.models.DiagnosticItem;
import java.util.Collections;
/**
* Popup window used to show diagnostic messages.
*
* @author Akash Yadav
*/
public class DiagnosticWindow extends SimpleTextWindow {
private static final Logger LOG = instance ("DiagnosticWindow");
private LayoutDiagnosticWindowBinding binding;
/**
* Create a popup window for editor
*
@@ -37,27 +55,81 @@ public class DiagnosticWindow extends SimpleTextWindow {
* @see #FEATURE_SHOW_OUTSIDE_VIEW_ALLOWED
* @see #FEATURE_HIDE_WHEN_FAST_SCROLL
*/
public DiagnosticWindow(@NonNull IDEEditor editor) {
super(editor);
public DiagnosticWindow (@NonNull IDEEditor editor) {
super (editor);
}
@Override
protected View onCreateContentView (@NonNull Context context) {
this.binding = LayoutDiagnosticWindowBinding.inflate (LayoutInflater.from (context));
this.binding.getRoot ().setBackground (createBackground ());
return this.binding.getRoot ();
}
@Override
protected TextView onCreateTextView (@NonNull IDEEditor editor) {
return this.binding.text;
}
@Override
protected View getRootView () {
return this.binding.getRoot ();
}
/**
* Show the given diagnostic item.
*
* @param diagnostic The diagnostic item to show.
*/
public void showDiagnostic(@Nullable DiagnosticItem diagnostic) {
public void showDiagnostic (@Nullable DiagnosticItem diagnostic) {
if (diagnostic == null) {
if (isShowing()) {
dismiss();
if (isShowing ()) {
dismiss ();
}
if (this.binding != null) {
this.binding.fix.setVisibility (View.GONE);
}
return;
}
final var message = diagnostic.getMessage();
this.text.setText(message);
displayWindow();
final var message = diagnostic.getMessage ();
this.binding.text.setText (message);
this.binding.fix.setVisibility (View.VISIBLE);
this.binding.fix.setEnabled (false);
final var editor = (IDEEditor) getEditor ();
final var future = editor.codeActions (Collections.singletonList (diagnostic));
future.whenComplete ((result, throwable) -> {
ThreadUtils.runOnUiThread (() -> {
if (result == null || result.getActions ().isEmpty () || throwable != null) {
LOG.error ("No code actions were found for the given diagnostic item", throwable);
binding.fix.setVisibility (View.GONE);
return;
}
binding.fix.setVisibility (View.VISIBLE);
binding.fix.setEnabled (true);
binding.fix.setClickable (true);
binding.fix.setFocusable (true);
binding.fix.setOnClickListener (v -> {
final var actions = result.getActions ();
if (actions.size () == 1) {
editor.performCodeAction (actions.get (0));
} else {
final var builder = DialogUtils.newMaterialDialogBuilder (editor.getContext ());
builder.setItems (actions.stream ().map (CodeActionItem::getTitle).toArray (String[]::new), (dialog, which) -> {
editor.performCodeAction (actions.get (which));
});
builder.setPositiveButton (R.string.cancel, (dialog, which) -> dialog.dismiss ());
builder.show ();
}
});
});
});
displayWindow ();
}
}
File diff suppressed because it is too large Load Diff
@@ -17,6 +17,7 @@
package com.itsaky.androidide.views.editor;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.TypedValue;
@@ -38,9 +39,9 @@ import io.github.rosemoe.sora.widget.base.EditorPopupWindow;
* @author Akash Yadav
*/
public abstract class SimpleTextWindow extends EditorPopupWindow {
protected final TextView text;
/**
* Create a popup window for editor
*
@@ -49,53 +50,65 @@ public abstract class SimpleTextWindow extends EditorPopupWindow {
* @see #FEATURE_SHOW_OUTSIDE_VIEW_ALLOWED
* @see #FEATURE_HIDE_WHEN_FAST_SCROLL
*/
public SimpleTextWindow(@NonNull IDEEditor editor) {
super(editor, getFeatureFlags());
final var context = editor.getContext();
final var dp4 = SizeUtils.dp2px(4);
final var dp8 = dp4 * 2;
this.text = new TextView(context);
this.text.setBackground(createBackground());
this.text.setTextColor(ContextCompat.getColor(context, R.color.primaryTextColor));
this.text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
this.text.setClickable(false);
this.text.setFocusable(false);
this.text.setPaddingRelative(dp8, dp4, dp8, dp4);
this.text.setLayoutParams(
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
setContentView(this.text);
public SimpleTextWindow (@NonNull IDEEditor editor) {
super (editor, getFeatureFlags ());
setContentView (onCreateContentView (editor.getContext ()));
this.text = onCreateTextView (editor);
}
public void displayWindow() {
this.text.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
setSize(this.text.getMeasuredWidth(), this.text.getMeasuredHeight());
final var line = getEditor().getCursor().getLeftLine();
final var column = getEditor().getCursor().getLeftColumn();
int x = (int) ((getEditor().getOffset(line, column) - (getWidth() / 2)));
int y =
(int) (getEditor().getRowHeight() * line)
- getEditor().getOffsetY()
- getHeight()
- 5;
setLocationAbsolutely(x, y);
show();
}
protected Drawable createBackground() {
GradientDrawable background = new GradientDrawable();
background.setShape(GradientDrawable.RECTANGLE);
background.setColor(0xff212121);
background.setStroke(1, 0xffffffff);
background.setCornerRadius(8);
return background;
}
private static int getFeatureFlags() {
private static int getFeatureFlags () {
return FEATURE_SCROLL_AS_CONTENT | FEATURE_SHOW_OUTSIDE_VIEW_ALLOWED;
}
protected View onCreateContentView (@NonNull Context context) {
return this.text;
}
protected TextView onCreateTextView (@NonNull IDEEditor editor) {
final var context = editor.getContext ();
final var dp4 = SizeUtils.dp2px (4);
final var dp8 = dp4 * 2;
final var text = new TextView (context);
text.setBackground (createBackground ());
text.setTextColor (ContextCompat.getColor (context, R.color.primaryTextColor));
text.setTextSize (TypedValue.COMPLEX_UNIT_SP, 14);
text.setClickable (false);
text.setFocusable (false);
text.setPaddingRelative (dp8, dp4, dp8, dp4);
text.setLayoutParams (
new ViewGroup.LayoutParams (
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
return text;
}
protected View getRootView () {
return this.text;
}
public void displayWindow () {
this.getRootView ().measure (View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
setSize (this.getRootView ().getMeasuredWidth (), this.getRootView ().getMeasuredHeight ());
final var line = getEditor ().getCursor ().getLeftLine ();
final var column = getEditor ().getCursor ().getLeftColumn ();
int x = (int) ((getEditor ().getOffset (line, column) - (getWidth () / 2)));
int y =
(int) (getEditor ().getRowHeight () * line)
- getEditor ().getOffsetY ()
- getHeight ()
- 5;
setLocationAbsolutely (x, y);
show ();
}
protected Drawable createBackground () {
GradientDrawable background = new GradientDrawable ();
background.setShape (GradientDrawable.RECTANGLE);
background.setColor (0xff212121);
background.setStroke (1, 0xffffffff);
background.setCornerRadius (8);
return background;
}
}
@@ -0,0 +1,50 @@
<?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/>.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:orientation="vertical"
android:paddingStart="8dp"
android:paddingTop="4dp"
android:paddingEnd="8dp"
android:paddingBottom="4dp">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:textColor="@color/primaryTextColor"
android:textSize="14sp" />
<com.google.android.material.button.MaterialButton
android:id="@+id/fix"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_margin="8dp"
android:text="@string/msg_fix_diagnostic"
android:textColor="@color/secondaryColor"
android:textSize="10sp"
android:textStyle="bold"
app:strokeColor="@color/secondaryLightColor" />
</LinearLayout>
+2 -1
View File
@@ -331,7 +331,7 @@
<string name="msg_use_horizontal_popup">Make the floating text actions appear horizontally (restart required if changed).</string>
<string name="msg_apk_install_intent_failed">Unable to start intent for installing APK</string>
<string name="msg_viewaction_add_attr">Add attribute</string>
<string name="msg_search_attrs">Search attributes...</string>
<string name="msg_search_attrs">Search attributes…</string>
<string name="msg_attr_added">Attribute was added successfully</string>
<string name="msg_attr_not_updated">Unable to update this attribute</string>
<string name="hint_dimension_val">Dimension value</string>
@@ -343,4 +343,5 @@
<string name="hint_string_value">String value</string>
<string name="hint_string_resource">String resource</string>
<string name="hint_resource_reference">Any resource reference</string>
<string name="msg_fix_diagnostic">QuickFix</string>
</resources>