<commit_before>/*
 * Copyright (C) 2020 <NAME>
 *
 * 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 uk.co.reecedunn.intellij.plugin.marklogic.query.rest.debugger

import com.intellij.lang.Language
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.xdebugger.XSourcePosition
import com.intellij.xdebugger.breakpoints.XBreakpointHandler
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator
import com.intellij.xdebugger.frame.XStackFrame
import uk.co.reecedunn.intellij.plugin.core.xml.XmlDocument
import uk.co.reecedunn.intellij.plugin.xquery.intellij.lang.XQuery
import uk.co.reecedunn.intellij.plugin.marklogic.intellij.resources.MarkLogicQueries
import uk.co.reecedunn.intellij.plugin.marklogic.query.rest.MarkLogicQueryProcessor
import uk.co.reecedunn.intellij.plugin.marklogic.query.rest.debugger.breakpoints.MarkLogicXQueryBreakpointHandler
import uk.co.reecedunn.intellij.plugin.processor.debug.DebugSession
import uk.co.reecedunn.intellij.plugin.processor.debug.DebugSessionListener
import uk.co.reecedunn.intellij.plugin.processor.query.QueryProcessState
import uk.co.reecedunn.intellij.plugin.xquery.intellij.xdebugger.breakpoints.XQueryExpressionBreakpointType
import java.lang.RuntimeException

internal class MarkLogicDebugSession(
    private val processor: MarkLogicQueryProcessor,
    private val query: VirtualFile
) : XDebuggerEvaluator(), DebugSession {
    private var state: QueryProcessState = QueryProcessState.Starting
    private var requestId: String? = null

    // region XDebuggerEvaluator

    override fun evaluate(expression: String, callback: XEvaluationCallback, expressionPosition: XSourcePosition?) {
        val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Value, XQuery)
        query.bindVariable("requestId", requestId, "xs:unsignedLong")
        query.bindVariable("expression", expression, "xs:string")
        try {
            val results = query.run().results
            callback.evaluated(MarkLogicValue(results))
        } catch (e: Throwable) {
            e.message?.let {
                callback.errorOccurred(it)
            }
        }
    }

    // endregion
    // region DebugSession

    override fun getBreakpointHandlers(language: Language): Array<XBreakpointHandler<*>> {
        return arrayOf(
            MarkLogicXQueryBreakpointHandler(XQueryExpressionBreakpointType::class.java)
        )
    }

    override var listener: DebugSessionListener? = null

    override fun suspend() {
        if (state === QueryProcessState.Running) {
            state = QueryProcessState.Suspending

            val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Break, XQuery)
            query.bindVariable("requestId", requestId, "xs:unsignedLong")
            query.run()
        }
    }

    override fun resume() {
        if (state === QueryProcessState.Suspended) {
            state = QueryProcessState.Resuming

            val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Continue, XQuery)
            query.bindVariable("requestId", requestId, "xs:unsignedLong")
            query.run()
        }
    }

    override val stackFrames: List<XStackFrame>
        get() {
            val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Stack, XQuery)
            query.bindVariable("requestId", requestId, "xs:unsignedLong")

            val stack = XmlDocument.parse(query.run().results.first().value as String, DBG_STACK_NAMESPACES)
            return stack.root.children("dbg:frame").map {
                MarkLogicDebugFrame.create(it, this.query, this)
            }.toList()
        }

    // endregion

    fun registerBreakpoint(uri: String, line: Int, column: Int): Boolean {
        val query = processor.createRunnableQuery(MarkLogicQueries.Debug.AddBreakpoint, XQuery)
        query.bindVariable("requestId", requestId, "xs:unsignedLong")
        query.bindVariable("exprUri", uri, "xs:string")
        query.bindVariable("exprLine", line.toString(), "xs:nonNegativeInteger")
        query.bindVariable("exprColumn", column.toString(), "xs:nonNegativeInteger")
        return query.run().results.first().value == "true"
    }

    fun run(requestId: String) {
        this.requestId = requestId
        state = QueryProcessState.Suspended // MarkLogic requests are suspended at the start of the first expression.

        resume()
        while (state !== QueryProcessState.Stopped) {
            Thread.sleep(100)

            val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Status, XQuery)
            query.bindVariable("requestId", requestId, "xs:unsignedLong")
            state = when (val status = query.run().results.first().value as String) {
                "none" -> QueryProcessState.Stopped
                "running" -> QueryProcessState.Running
                "stopped" -> {
                    if (state !== QueryProcessState.Suspended) {
                        state = QueryProcessState.Suspended
                        listener?.onsuspended(this.query.name)
                    }
                    QueryProcessState.Suspended
                }
                else -> throw RuntimeException(status)
            }
        }
    }

    fun stop() {
        val query = processor.createRunnableQuery(MarkLogicQueries.Request.Cancel, XQuery)
        query.bindVariable("requestId", requestId, "xs:unsignedLong")
        query.run()
    }

    companion object {
        private val DBG_STACK_NAMESPACES = mapOf("dbg" to "http://marklogic.com/xdmp/debug")
    }
}
<commit_msg>Store the breakpoint handlers in a property.<commit_after>/*
 * Copyright (C) 2020 <NAME>
 *
 * 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 uk.co.reecedunn.intellij.plugin.marklogic.query.rest.debugger

import com.intellij.lang.Language
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.xdebugger.XSourcePosition
import com.intellij.xdebugger.breakpoints.XBreakpointHandler
import com.intellij.xdebugger.evaluation.XDebuggerEvaluator
import com.intellij.xdebugger.frame.XStackFrame
import uk.co.reecedunn.intellij.plugin.core.xml.XmlDocument
import uk.co.reecedunn.intellij.plugin.xquery.intellij.lang.XQuery
import uk.co.reecedunn.intellij.plugin.marklogic.intellij.resources.MarkLogicQueries
import uk.co.reecedunn.intellij.plugin.marklogic.query.rest.MarkLogicQueryProcessor
import uk.co.reecedunn.intellij.plugin.marklogic.query.rest.debugger.breakpoints.MarkLogicXQueryBreakpointHandler
import uk.co.reecedunn.intellij.plugin.processor.debug.DebugSession
import uk.co.reecedunn.intellij.plugin.processor.debug.DebugSessionListener
import uk.co.reecedunn.intellij.plugin.processor.query.QueryProcessState
import uk.co.reecedunn.intellij.plugin.xquery.intellij.xdebugger.breakpoints.XQueryExpressionBreakpointType
import java.lang.RuntimeException

internal class MarkLogicDebugSession(
    private val processor: MarkLogicQueryProcessor,
    private val query: VirtualFile
) : XDebuggerEvaluator(), DebugSession {
    private var state: QueryProcessState = QueryProcessState.Starting
    private var requestId: String? = null

    private val breakpointHandlers: Array<XBreakpointHandler<*>> = arrayOf(
        MarkLogicXQueryBreakpointHandler(XQueryExpressionBreakpointType::class.java)
    )

    // region XDebuggerEvaluator

    override fun evaluate(expression: String, callback: XEvaluationCallback, expressionPosition: XSourcePosition?) {
        val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Value, XQuery)
        query.bindVariable("requestId", requestId, "xs:unsignedLong")
        query.bindVariable("expression", expression, "xs:string")
        try {
            val results = query.run().results
            callback.evaluated(MarkLogicValue(results))
        } catch (e: Throwable) {
            e.message?.let {
                callback.errorOccurred(it)
            }
        }
    }

    // endregion
    // region DebugSession

    override fun getBreakpointHandlers(language: Language): Array<XBreakpointHandler<*>> = breakpointHandlers

    override var listener: DebugSessionListener? = null

    override fun suspend() {
        if (state === QueryProcessState.Running) {
            state = QueryProcessState.Suspending

            val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Break, XQuery)
            query.bindVariable("requestId", requestId, "xs:unsignedLong")
            query.run()
        }
    }

    override fun resume() {
        if (state === QueryProcessState.Suspended) {
            state = QueryProcessState.Resuming

            val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Continue, XQuery)
            query.bindVariable("requestId", requestId, "xs:unsignedLong")
            query.run()
        }
    }

    override val stackFrames: List<XStackFrame>
        get() {
            val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Stack, XQuery)
            query.bindVariable("requestId", requestId, "xs:unsignedLong")

            val stack = XmlDocument.parse(query.run().results.first().value as String, DBG_STACK_NAMESPACES)
            return stack.root.children("dbg:frame").map {
                MarkLogicDebugFrame.create(it, this.query, this)
            }.toList()
        }

    // endregion

    fun registerBreakpoint(uri: String, line: Int, column: Int): Boolean {
        val query = processor.createRunnableQuery(MarkLogicQueries.Debug.AddBreakpoint, XQuery)
        query.bindVariable("requestId", requestId, "xs:unsignedLong")
        query.bindVariable("exprUri", uri, "xs:string")
        query.bindVariable("exprLine", line.toString(), "xs:nonNegativeInteger")
        query.bindVariable("exprColumn", column.toString(), "xs:nonNegativeInteger")
        return query.run().results.first().value == "true"
    }

    fun run(requestId: String) {
        this.requestId = requestId
        state = QueryProcessState.Suspended // MarkLogic requests are suspended at the start of the first expression.

        resume()
        while (state !== QueryProcessState.Stopped) {
            Thread.sleep(100)

            val query = processor.createRunnableQuery(MarkLogicQueries.Debug.Status, XQuery)
            query.bindVariable("requestId", requestId, "xs:unsignedLong")
            state = when (val status = query.run().results.first().value as String) {
                "none" -> QueryProcessState.Stopped
                "running" -> QueryProcessState.Running
                "stopped" -> {
                    if (state !== QueryProcessState.Suspended) {
                        state = QueryProcessState.Suspended
                        listener?.onsuspended(this.query.name)
                    }
                    QueryProcessState.Suspended
                }
                else -> throw RuntimeException(status)
            }
        }
    }

    fun stop() {
        val query = processor.createRunnableQuery(MarkLogicQueries.Request.Cancel, XQuery)
        query.bindVariable("requestId", requestId, "xs:unsignedLong")
        query.run()
    }

    companion object {
        private val DBG_STACK_NAMESPACES = mapOf("dbg" to "http://marklogic.com/xdmp/debug")
    }
}

{'fraction_non_alphanumeric': 0.09704848336194914, 'fraction_numerical': 0.001798708200474205, 'mean_word_length': 3.046311610982468, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2150226', 'n_tokens_mistral': 3439, 'n_tokens_neox': 3318, 'n_words': 690}