/*
 * Copyright 2011 Jon S Akhtar (Sylvanaar)
 *
 *   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.sylvanaar.idea.Lua.debugger;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.xdebugger.XSourcePosition;
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.concurrency.Promise;

/**
 * Created by IntelliJ IDEA.
 * User: Jon S Akhtar
 * Date: 5/15/11
 * Time: 5:07 AM
 */
public class LuaDebuggerEvaluator extends XDebuggerEvaluator {
    private static final Logger log = Logger.getInstance("Lua.LuaDebuggerEvaluator");

    private LuaDebuggerController myController;

    public LuaDebuggerEvaluator(LuaDebuggerController myController) {
        this.myController = myController;
    }

    @Override
    public void evaluate(@NotNull String expression, @NotNull XEvaluationCallback callback,
                         @Nullable XSourcePosition expressionPosition) {
        log.debug("evaluating: " + expression);
        Promise<LuaDebugValue> promise = myController.execute("return " + expression);
        promise.done(callback::evaluated);
    }

    @Override
    public boolean isCodeFragmentEvaluationSupported() {
        return false;
    }
}

@@ -720,24 +720,472 @@
 ic.Logger;%0D%0A
+import com.intellij.openapi.editor.Document;%0D%0Aimport com.intellij.openapi.project.IndexNotReadyException;%0D%0Aimport com.intellij.openapi.project.Project;%0D%0Aimport com.intellij.openapi.util.Pair;%0D%0Aimport com.intellij.openapi.util.Ref;%0D%0Aimport com.intellij.openapi.util.TextRange;%0D%0Aimport com.intellij.psi.PsiDocumentManager;%0D%0Aimport com.intellij.psi.PsiElement;%0D%0Aimport com.intellij.psi.util.PsiTreeUtil;%0D%0Aimport com.intellij.xdebugger.XDebuggerUtil;%0D%0A
 import com.i
@@ -1278,24 +1278,91 @@
 Evaluator;%0D%0A
+import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaExpression;%0D%0A
 import org.j
@@ -2381,12 +2381,1315 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    @Nullable%0D%0A    @Override%0D%0A    public TextRange getExpressionRangeAtOffset(final Project project, final Document document, final int offset, final boolean sideEffectsAllowed) %7B%0D%0A        final Ref%3CTextRange%3E currentRange = Ref.create(null);%0D%0A        PsiDocumentManager.getInstance(project).commitAndRunReadAction(() -%3E %7B%0D%0A            try %7B%0D%0A                PsiElement elementAtCursor = XDebuggerUtil.getInstance().findContextElement(PsiDocumentManager%0D%0A                        .getInstance(project).getPsiFile(document).getVirtualFile(), offset, project, false);%0D%0A                if (elementAtCursor == null) return;%0D%0A                Pair%3CPsiElement, TextRange%3E pair = findExpression(elementAtCursor, sideEffectsAllowed);%0D%0A                if (pair != null) %7B%0D%0A                    currentRange.set(pair.getSecond());%0D%0A                %7D%0D%0A            %7D catch (IndexNotReadyException ignored) %7B%7D%0D%0A        %7D);%0D%0A        return currentRange.get();%0D%0A    %7D%0D%0A%0D%0A    @Nullable%0D%0A    private static Pair%3CPsiElement, TextRange%3E findExpression(PsiElement element, boolean allowMethodCalls) %7B%0D%0A        LuaExpression expression = PsiTreeUtil.getParentOfType(element, LuaExpression.class);%0D%0A%0D%0A        if (expression == null) return null;%0D%0A%0D%0A%0D%0A        return Pair.create(expression, expression.getTextRange());%0D%0A    %7D%0D
 %0A%7D%0D%0A

Support mouseover evaluation