/*
 * Copyright (c) 2017. tangzx(love.tangzx@qq.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tang.intellij.lua.debugger.attach;

import com.intellij.xdebugger.XSourcePosition;
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 *
 * Created by tangzx on 2017/4/2.
 */
public class LuaAttachDebuggerEvaluator extends XDebuggerEvaluator {
    private LuaAttachDebugProcess process;
    private LuaAttachStackFrame stackFrame;

    LuaAttachDebuggerEvaluator(LuaAttachDebugProcess process, LuaAttachStackFrame stackFrame) {
        this.process = process;
        this.stackFrame = stackFrame;
    }

    @Override
    public void evaluate(@NotNull String express, @NotNull XEvaluationCallback xEvaluationCallback, @Nullable XSourcePosition xSourcePosition) {
        process.getBridge().eval(express, stackFrame.getStack(), 1, result -> {
            if (result.isSuccess() && result.getXValue() != null) {
                xEvaluationCallback.evaluated(result.getXValue());
            } else {
                xEvaluationCallback.errorOccurred("error");
            }
        });
    }
}

@@ -653,16 +653,379 @@
 ttach;%0A%0A
+import com.intellij.debugger.impl.DebuggerUtilsEx;%0Aimport com.intellij.openapi.editor.Document;%0Aimport com.intellij.openapi.project.IndexNotReadyException;%0Aimport com.intellij.openapi.project.Project;%0Aimport com.intellij.openapi.util.Ref;%0Aimport com.intellij.openapi.util.TextRange;%0Aimport com.intellij.psi.PsiDocumentManager;%0Aimport com.intellij.psi.PsiElement;%0A
 import c
@@ -2071,11 +2071,769 @@
 );%0A    %7D
+%0A%0A    @Nullable%0A    @Override%0A    public TextRange getExpressionRangeAtOffset(Project project, Document document, int offset, boolean sideEffectsAllowed) %7B%0A        final Ref%3CTextRange%3E currentRange = Ref.create(null);%0A        PsiDocumentManager.getInstance(project).commitAndRunReadAction(() -%3E %7B%0A            try %7B%0A                PsiElement elementAtCursor = DebuggerUtilsEx.findElementAt(PsiDocumentManager.getInstance(project).getPsiFile(document), offset);%0A                if (elementAtCursor == null %7C%7C !elementAtCursor.isValid()) %7B%0A                    return;%0A                %7D%0A                currentRange.set(elementAtCursor.getTextRange());%0A            %7D catch (IndexNotReadyException ignored) %7B%7D%0A        %7D);%0A        return currentRange.get();%0A    %7D
 %0A%7D%0A

debug hint value