[debugger] Convert XQuickEvaluateHandler to Kotlin

GitOrigin-RevId: e8792643c2f4a306853de14fbe9ed8d551384d4b
This commit is contained in:
Nikolay Rykunov
2024-09-19 10:32:05 +02:00
committed by intellij-monorepo-bot
parent 760af9ca4f
commit bf81387373

View File

@@ -1,106 +1,99 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.xdebugger.impl.evaluate.quick;
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.xdebugger.impl.evaluate.quick
import com.intellij.codeInsight.TargetElementUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.util.ui.UIUtil;
import com.intellij.xdebugger.XDebugSession;
import com.intellij.xdebugger.XDebuggerManager;
import com.intellij.xdebugger.evaluation.ExpressionInfo;
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator;
import com.intellij.xdebugger.impl.evaluate.quick.common.AbstractValueHint;
import com.intellij.xdebugger.impl.evaluate.quick.common.QuickEvaluateHandler;
import com.intellij.xdebugger.impl.evaluate.quick.common.ValueHintType;
import com.intellij.xdebugger.settings.XDebuggerSettingsManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.concurrency.AsyncPromise;
import org.jetbrains.concurrency.Promise;
import org.jetbrains.concurrency.Promises;
import com.intellij.codeInsight.TargetElementUtil
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiDocumentManager
import com.intellij.util.Function
import com.intellij.util.ui.UIUtil
import com.intellij.xdebugger.XDebuggerManager
import com.intellij.xdebugger.evaluation.ExpressionInfo
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator
import com.intellij.xdebugger.impl.evaluate.quick.common.AbstractValueHint
import com.intellij.xdebugger.impl.evaluate.quick.common.QuickEvaluateHandler
import com.intellij.xdebugger.impl.evaluate.quick.common.ValueHintType
import com.intellij.xdebugger.settings.XDebuggerSettingsManager
import org.jetbrains.concurrency.AsyncPromise
import org.jetbrains.concurrency.Promise
import org.jetbrains.concurrency.resolvedPromise
import java.awt.Point
import java.awt.*;
private val LOG = Logger.getInstance(XQuickEvaluateHandler::class.java)
public class XQuickEvaluateHandler extends QuickEvaluateHandler {
private static final Logger LOG = Logger.getInstance(XQuickEvaluateHandler.class);
@Override
public boolean isEnabled(@NotNull final Project project) {
XDebugSession session = XDebuggerManager.getInstance(project).getCurrentSession();
return session != null && session.getDebugProcess().getEvaluator() != null;
class XQuickEvaluateHandler : QuickEvaluateHandler() {
override fun isEnabled(project: Project): Boolean {
val session = XDebuggerManager.getInstance(project).getCurrentSession()
return session != null && session.getDebugProcess().getEvaluator() != null
}
@Nullable
@Override
public AbstractValueHint createValueHint(@NotNull Project project, @NotNull Editor editor, @NotNull Point point, ValueHintType type) {
return null;
override fun createValueHint(project: Project, editor: Editor, point: Point, type: ValueHintType?): AbstractValueHint? {
return null
}
@NotNull
@Override
public CancellableHint createValueHintAsync(@NotNull final Project project, @NotNull final Editor editor, @NotNull final Point point, final ValueHintType type) {
final XDebugSession session = XDebuggerManager.getInstance(project).getCurrentSession();
override fun createValueHintAsync(project: Project, editor: Editor, point: Point, type: ValueHintType): CancellableHint {
val session = XDebuggerManager.getInstance(project).getCurrentSession()
if (session == null) {
return CancellableHint.resolved(null);
return CancellableHint.resolved(null)
}
final XDebuggerEvaluator evaluator = session.getDebugProcess().getEvaluator();
val evaluator = session.getDebugProcess().getEvaluator()
if (evaluator == null) {
return CancellableHint.resolved(null);
return CancellableHint.resolved(null)
}
int offset = AbstractValueHint.calculateOffset(editor, point);
Document document = editor.getDocument();
var offset = AbstractValueHint.calculateOffset(editor, point)
val document = editor.getDocument()
// adjust offset to match with other actions, like go to declaration
offset = TargetElementUtil.adjustOffset(PsiDocumentManager.getInstance(project).getPsiFile(document), document, offset);
Promise<ExpressionInfo> infoPromise = getExpressionInfo(evaluator, project, type, editor, offset);
Promise<AbstractValueHint> hintPromise = infoPromise
.thenAsync(expressionInfo -> {
AsyncPromise<AbstractValueHint> resultPromise = new AsyncPromise<>();
UIUtil.invokeLaterIfNeeded(() -> {
int textLength = document.getTextLength();
offset = TargetElementUtil.adjustOffset(PsiDocumentManager.getInstance(project).getPsiFile(document), document, offset)
val infoPromise: Promise<ExpressionInfo?> = getExpressionInfo(evaluator, project, type, editor, offset)
val hintPromise = infoPromise
.thenAsync<AbstractValueHint?>(Function { expressionInfo: ExpressionInfo? ->
val resultPromise = AsyncPromise<AbstractValueHint?>()
UIUtil.invokeLaterIfNeeded {
val textLength = document.textLength
if (expressionInfo == null) {
resultPromise.setResult(null);
return;
resultPromise.setResult(null)
return@invokeLaterIfNeeded
}
TextRange range = expressionInfo.getTextRange();
if (range.getStartOffset() > range.getEndOffset() || range.getStartOffset() < 0 || range.getEndOffset() > textLength) {
LOG.error("invalid range: " + range + ", text length = " + textLength + ", evaluator: " + evaluator);
resultPromise.setResult(null);
return;
val range = expressionInfo.textRange
if (range.startOffset > range.endOffset || range.startOffset < 0 || range.endOffset > textLength) {
LOG.error("invalid range: $range, text length = $textLength, evaluator: $evaluator")
resultPromise.setResult(null)
return@invokeLaterIfNeeded
}
resultPromise.setResult(new XValueHint(project, editor, point, type, expressionInfo, evaluator, session, false));
});
return resultPromise;
});
return new CancellableHint(hintPromise, infoPromise);
resultPromise.setResult(XValueHint(project, editor, point, type, expressionInfo, evaluator, session, false))
}
resultPromise
})
return CancellableHint(hintPromise, infoPromise)
}
@NotNull
private static Promise<ExpressionInfo> getExpressionInfo(final XDebuggerEvaluator evaluator, final Project project,
final ValueHintType type, @NotNull Editor editor, final int offset) {
SelectionModel selectionModel = editor.getSelectionModel();
int selectionStart = selectionModel.getSelectionStart();
int selectionEnd = selectionModel.getSelectionEnd();
if ((type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT) && selectionModel.hasSelection()
&& selectionStart <= offset && offset <= selectionEnd) {
return Promises.resolvedPromise(new ExpressionInfo(new TextRange(selectionStart, selectionEnd)));
override fun canShowHint(project: Project): Boolean {
return isEnabled(project)
}
override fun getValueLookupDelay(project: Project?): Int {
return XDebuggerSettingsManager.getInstance().getDataViewSettings().getValueLookupDelay()
}
companion object {
private fun getExpressionInfo(
evaluator: XDebuggerEvaluator, project: Project,
type: ValueHintType?, editor: Editor, offset: Int,
): Promise<ExpressionInfo?> {
val selectionModel = editor.getSelectionModel()
val selectionStart = selectionModel.selectionStart
val selectionEnd = selectionModel.selectionEnd
if ((type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT) && selectionModel.hasSelection()
&& selectionStart <= offset && offset <= selectionEnd
) {
return resolvedPromise<ExpressionInfo?>(ExpressionInfo(TextRange(selectionStart, selectionEnd)))
}
return evaluator.getExpressionInfoAtOffsetAsync(project, editor.getDocument(), offset,
type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT)
}
return evaluator.getExpressionInfoAtOffsetAsync (project, editor.getDocument(), offset, type == ValueHintType.MOUSE_CLICK_HINT || type == ValueHintType.MOUSE_ALT_OVER_HINT);
}
@Override
public boolean canShowHint(@NotNull final Project project) {
return isEnabled(project);
}
@Override
public int getValueLookupDelay(Project project) {
return XDebuggerSettingsManager.getInstance().getDataViewSettings().getValueLookupDelay();
}
}