<commit_before>(:
 : Copyright 2006-2009 The FLWOR Foundation.
 :
 : 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.
:)
module namespace dmh = "http://www.zorba-xquery.com/modules/debugger/dbgp-message-handler";

import module namespace base64 = "http://www.zorba-xquery.com/modules/converters/base64";


declare variable $dmh:endl as xs:string := "
";

(:~
 : Set this variale to true if you want to have mode debug information when
 : an error occurs.
 :)
declare variable $dmh:debug as xs:boolean := fn:true();


declare %private function dmh:status($resp as element(response))
{
  fn:concat(
    $resp/@status,
    if ($resp/@status eq "break" and $resp/@reason ne "ok") then
      fn:concat(" (", $resp/@reason, ")")
    else
      ""
  )
};

declare %private function dmh:run($resp as element(response))
{
  if ($resp/@status eq "starting") then
    "Starting query"
  else
    dmh:status($resp)
};

declare %private function dmh:stop($resp as element(response))
{
  dmh:status($resp)
};

declare %private function dmh:breakpoint-set($resp as element(response))
{
  fn:concat("set breakpoint with id ", data($resp/@id), " and state ", data($resp/@state))
};

declare %private function dmh:breakpoint-get($resp as element(response))
{
  let $b := $resp/breakpoint
  return
    fn:concat(
      "Breakpoint ", $b/@id, $dmh:endl,
      "  type:  ", $b/@type, $dmh:endl,
      "  file:  ", $b/@filename, $dmh:endl,
      "  line:  ", $b/@lineno, $dmh:endl,
      "  state: ", $b/@state
    )
};

declare %private function dmh:breakpoint-list($resp as element(response))
{
  fn:string-join(
    let $bs := $resp/breakpoint
    return
      if (fn:exists($bs)) then
        fn:concat(
          "--------------------------------------", $dmh:endl,
          "|   ID   |   State   |  Line  | File", $dmh:endl,
          "|--------|-----------|--------|-------", $dmh:endl,
          fn:string-join(
            for $b in $bs
            return
              fn:concat(
                "| ", dmh:lpottl($b/@id, 6, " "), " ",
                "| ", if ($b/@state eq "enabled") then "enabled  " else "disabled ", " ",
                "| ", dmh:lpottl($b/@lineno, 6, " "), " ",
                "| ", $b/@filename, $dmh:endl),
            ""
          ),
          "--------------------------------------", $dmh:endl
        )
      else
        "No breakpoints set"
  )
};

(:~
 : left-pad-or-trim-to-length
 :)
declare %private function dmh:lpottl($value as xs:string, $length as xs:integer, $padChar as xs:string)
 as xs:string
{
  let $padChar := fn:substring($padChar, 1, 1)
  let $len := fn:string-length($value)
  return
    if ($len < $length) then
      let $pad :=
        fn:string-join(
          for $i in 1 to $length - $len
          return $padChar,
          ""
        )
      return
        fn:concat($pad, $value)
    else
      fn:substring($value, $len - $length + 1)
};

declare %private function dmh:breakpoint-remove($resp as element(response))
{
  "Breakpoint removed"
};

declare %private function dmh:stack-depth($resp as element(response))
{
  fn:concat("depth: ", $resp/@depth)
};

declare %private function dmh:stack-get($resp as element(response))
{
  fn:string-join(
    for $s in $resp/stack
    return
      fn:concat("level ", $s/@level, " at ", $s/@filename, ":", $s/@lineno),
    $dmh:endl
  )
};


declare %private function dmh:context-names($resp as element(response))
{
  fn:string-join(
    for $c in $resp/context
    return
      fn:concat("context ", $c/@id, ": ", $c/@name),
    $dmh:endl
  )
};

declare %private function dmh:context-get($resp as element(response))
{<commit_msg>making stack number be displayed like in gdb<commit_after>(:
 : Copyright 2006-2009 The FLWOR Foundation.
 :
 : 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.
:)
module namespace dmh = "http://www.zorba-xquery.com/modules/debugger/dbgp-message-handler";

import module namespace base64 = "http://www.zorba-xquery.com/modules/converters/base64";


declare variable $dmh:endl as xs:string := "
";

(:~
 : Set this variale to true if you want to have mode debug information when
 : an error occurs.
 :)
declare variable $dmh:debug as xs:boolean := fn:true();


declare %private function dmh:status($resp as element(response))
{
  fn:concat(
    $resp/@status,
    if ($resp/@status eq "break" and $resp/@reason ne "ok") then
      fn:concat(" (", $resp/@reason, ")")
    else
      ""
  )
};

declare %private function dmh:run($resp as element(response))
{
  if ($resp/@status eq "starting") then
    "Starting query"
  else
    dmh:status($resp)
};

declare %private function dmh:stop($resp as element(response))
{
  dmh:status($resp)
};

declare %private function dmh:breakpoint-set($resp as element(response))
{
  fn:concat("set breakpoint with id ", data($resp/@id), " and state ", data($resp/@state))
};

declare %private function dmh:breakpoint-get($resp as element(response))
{
  let $b := $resp/breakpoint
  return
    fn:concat(
      "Breakpoint ", $b/@id, $dmh:endl,
      "  type:  ", $b/@type, $dmh:endl,
      "  file:  ", $b/@filename, $dmh:endl,
      "  line:  ", $b/@lineno, $dmh:endl,
      "  state: ", $b/@state
    )
};

declare %private function dmh:breakpoint-list($resp as element(response))
{
  fn:string-join(
    let $bs := $resp/breakpoint
    return
      if (fn:exists($bs)) then
        fn:concat(
          "--------------------------------------", $dmh:endl,
          "|   ID   |   State   |  Line  | File", $dmh:endl,
          "|--------|-----------|--------|-------", $dmh:endl,
          fn:string-join(
            for $b in $bs
            return
              fn:concat(
                "| ", dmh:lpottl($b/@id, 6, " "), " ",
                "| ", if ($b/@state eq "enabled") then "enabled  " else "disabled ", " ",
                "| ", dmh:lpottl($b/@lineno, 6, " "), " ",
                "| ", $b/@filename, $dmh:endl),
            ""
          ),
          "--------------------------------------", $dmh:endl
        )
      else
        "No breakpoints set"
  )
};

(:~
 : left-pad-or-trim-to-length
 :)
declare %private function dmh:lpottl($value as xs:string, $length as xs:integer, $padChar as xs:string)
 as xs:string
{
  let $padChar := fn:substring($padChar, 1, 1)
  let $len := fn:string-length($value)
  return
    if ($len < $length) then
      let $pad :=
        fn:string-join(
          for $i in 1 to $length - $len
          return $padChar,
          ""
        )
      return
        fn:concat($pad, $value)
    else
      fn:substring($value, $len - $length + 1)
};

declare %private function dmh:breakpoint-remove($resp as element(response))
{
  "Breakpoint removed"
};

declare %private function dmh:stack-depth($resp as element(response))
{
  fn:concat("depth: ", $resp/@depth)
};

declare %private function dmh:stack-get($resp as element(response))
{
  fn:string-join(
    for $s in $resp/stack
    return
      fn:concat("#", $s/@level, " at ", $s/@filename, ":", $s/@lineno),
    $dmh:endl
  )
};


declare %private function dmh:context-names($resp as element(response))
{
  fn:string-join(
    for $c in $resp/context
    return
      fn:concat("context ", $c/@id, ": ", $c/@name),
    $dmh:endl
  )
};

declare %private function dmh:context-get($resp as element(response))
{
{'fraction_non_alphanumeric': 0.17824298160697, 'fraction_numerical': 0.005324298160696999, 'mean_word_length': 2.554838709677419, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, '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': '679723', 'n_tokens_mistral': 2796, 'n_tokens_neox': 2609, 'n_words': 715}