src/Pure/PIDE/command_span.ML
author wenzelm
Mon, 27 Oct 2025 15:36:47 +0100
changeset 83419 0ac8a8a3793b
parent 83291 f033ff36d9c8
permissions -rw-r--r--
clarified signature: prefer explicit type Editor.Output;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/PIDE/command_span.ML
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     3
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     4
Syntactic representation of command spans.
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     5
*)
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     6
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     7
signature COMMAND_SPAN =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
     8
sig
72754
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
     9
  val extensions: string list -> Path.T -> Path.T list
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    10
  datatype kind = Command_Span of string * Position.T | Ignored_Span | Malformed_Span
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    11
  datatype span = Span of kind * Token.T list
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    12
  val kind: span -> kind
83291
f033ff36d9c8 suppress command_timing for ignored command spans: reduce size of Document_Status.Command_Timings by factor 2;
wenzelm
parents: 82931
diff changeset
    13
  val is_ignored: span -> bool
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    14
  val content: span -> Token.T list
82931
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    15
  val range: span -> Position.range
68183
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    16
  val symbol_length: span -> int option
82679
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    17
  val eof: span
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    18
  val is_eof: span -> bool
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    19
  val stopper: span Scan.stopper
68183
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    20
  val adjust_offsets_kind: (int -> int option) -> kind -> kind
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    21
  val adjust_offsets: (int -> int option) -> span -> span
82931
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    22
  val command_ranges: span list -> Position.range list
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    23
end;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    24
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    25
structure Command_Span: COMMAND_SPAN =
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    26
struct
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    27
72754
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    28
(* loaded files *)
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    29
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    30
fun extensions exts path = map (fn ext => Path.ext ext path) exts;
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    31
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    32
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    33
(* span *)
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    34
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    35
datatype kind = Command_Span of string * Position.T | Ignored_Span | Malformed_Span;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    36
datatype span = Span of kind * Token.T list;
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    37
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    38
fun kind (Span (k, _)) = k;
83291
f033ff36d9c8 suppress command_timing for ignored command spans: reduce size of Document_Status.Command_Timings by factor 2;
wenzelm
parents: 82931
diff changeset
    39
fun is_ignored span = kind span = Ignored_Span;
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    40
fun content (Span (_, toks)) = toks;
82931
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    41
val range = Token.range_of o content;
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    42
val symbol_length = Position.distance_of o range;
68177
6e40f5d43936 clarified signature;
wenzelm
parents: 59689
diff changeset
    43
72754
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    44
82679
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    45
(* stopper *)
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    46
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    47
val eof = Span (Command_Span ("", Position.none), []);
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    48
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    49
fun is_eof (Span (Command_Span ("", _), _)) = true
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    50
  | is_eof _ = false;
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    51
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    52
val stopper = Scan.stopper (K eof) is_eof;
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    53
1dd29afaddda more generic parsing of command spans;
wenzelm
parents: 72754
diff changeset
    54
72754
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    55
(* presentation positions *)
1456c5747416 clarified modules;
wenzelm
parents: 68183
diff changeset
    56
68183
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    57
fun adjust_offsets_kind adjust k =
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    58
  (case k of
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    59
    Command_Span (name, pos) => Command_Span (name, Position.adjust_offsets adjust pos)
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    60
  | _ => k);
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    61
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    62
fun adjust_offsets adjust (Span (k, toks)) =
6560324b1e4d adjust position according to offset of command/exec id;
wenzelm
parents: 68177
diff changeset
    63
  Span (adjust_offsets_kind adjust k, map (Token.adjust_offsets adjust) toks);
68177
6e40f5d43936 clarified signature;
wenzelm
parents: 59689
diff changeset
    64
82931
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    65
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    66
(* command ranges, including trailing non-commands (whitespace etc.) *)
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    67
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    68
fun command_ranges spans =
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    69
  let
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    70
    fun ranges NONE [] result = rev result
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    71
      | ranges (SOME pos) [] result =
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    72
          let val end_pos = #2 (range (List.last spans))
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    73
          in rev (Position.range (pos, end_pos) :: result) end
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    74
      | ranges start_pos (span :: rest) result =
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    75
          (case (start_pos, kind span) of
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    76
            (NONE, Command_Span _) => ranges (SOME (#1 (range span))) rest result
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    77
          | (SOME pos, Command_Span _) =>
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    78
              let
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    79
                val pos' = #1 (range span);
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    80
                val result' = Position.range (pos, pos') :: result;
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    81
              in ranges (SOME pos') rest result' end
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    82
          | _ => ranges start_pos rest result);
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    83
  in ranges NONE spans [] end;
fa8067dc6787 more markup for batch build: command ranges with trailing whitespace;
wenzelm
parents: 82679
diff changeset
    84
57905
c0c5652e796e separate module Command_Span: mostly syntactic representation;
wenzelm
parents:
diff changeset
    85
end;