src/Tools/VSCode/extension/src/symbol.ts
author wenzelm
Sat, 02 Sep 2017 17:21:52 +0200
changeset 66598 e2671e8c476f
parent 66097 ee4c2d5b650e
child 73168 6d37836c4329
permissions -rw-r--r--
VSCode extension for official Isabelle release;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
66052
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
     1
'use strict';
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
     2
66070
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
     3
import * as library from './library'
66073
50a28b3cceb2 clarified message;
wenzelm
parents: 66072
diff changeset
     4
import { Disposable, DocumentSelector, ExtensionContext, extensions, window } from 'vscode'
66070
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
     5
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
     6
66060
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
     7
/* ASCII characters */
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
     8
66071
wenzelm
parents: 66070
diff changeset
     9
export type Symbol = string
wenzelm
parents: 66070
diff changeset
    10
wenzelm
parents: 66070
diff changeset
    11
export function is_char(s: Symbol): boolean
66060
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    12
{ return s.length == 1 }
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    13
66071
wenzelm
parents: 66070
diff changeset
    14
export function is_ascii_letter(s: Symbol): boolean
66060
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    15
{ return is_char(s) && "A" <= s && s <= "Z" || "a" <= s && s <= "z" }
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    16
66071
wenzelm
parents: 66070
diff changeset
    17
export function is_ascii_digit(s: Symbol): boolean
66060
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    18
{ return is_char(s) && "0" <= s && s <= "9" }
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    19
66071
wenzelm
parents: 66070
diff changeset
    20
export function is_ascii_quasi(s: Symbol): boolean
66060
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    21
{ return s == "_" || s == "'" }
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    22
66071
wenzelm
parents: 66070
diff changeset
    23
export function is_ascii_letdig(s: Symbol): boolean
66060
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    24
{ return is_ascii_letter(s) || is_ascii_digit(s) || is_ascii_quasi(s) }
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    25
66071
wenzelm
parents: 66070
diff changeset
    26
export function is_ascii_identifier(s: Symbol): boolean
66060
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    27
{
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    28
  const n = s.length
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    29
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    30
  let all_letdig = true
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    31
  for (const c of s) { all_letdig = all_letdig && is_ascii_letdig(c) }
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    32
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    33
  return n > 0 && is_ascii_letter(s.charAt(0)) && all_letdig
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    34
}
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    35
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    36
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    37
/* named symbols */
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    38
66052
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    39
export interface Entry
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    40
{
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    41
  symbol: Symbol,
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    42
  name: string,
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    43
  code: number
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    44
}
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    45
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    46
let symbol_entries: [Entry]
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    47
const names = new Map<Symbol, string>()
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    48
const codes = new Map<Symbol, number>()
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    49
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    50
export function get_name(sym: Symbol): string | undefined
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    51
{
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    52
  return names.get(sym)
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    53
}
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    54
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    55
export function get_code(sym: Symbol): number | undefined
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    56
{
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    57
  return codes.get(sym)
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    58
}
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    59
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    60
export function get_unicode(sym: Symbol): string
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    61
{
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    62
  const code = get_code(sym)
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    63
  return code ? String.fromCharCode(code) : ""
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    64
}
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    65
66070
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    66
function update_entries(entries: [Entry])
66052
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    67
{
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    68
  symbol_entries = entries
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    69
  names.clear
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    70
  codes.clear
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    71
  for (const entry of entries) {
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    72
    names.set(entry.symbol, entry.name)
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    73
    codes.set(entry.symbol, entry.code)
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
    74
  }
66060
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    75
}
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    76
b2bfbefd354f symbol completion that bypasses the LS protocol, and thus observes the range properly;
wenzelm
parents: 66052
diff changeset
    77
66070
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    78
/* prettify symbols mode */
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    79
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    80
interface PrettyStyleProperties
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    81
{
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    82
  border?: string
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    83
  textDecoration?: string
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    84
  color?: string
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    85
  backgroundColor?: string
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    86
}
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    87
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    88
interface PrettyStyle extends PrettyStyleProperties
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    89
{
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    90
  dark?: PrettyStyleProperties
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    91
  light?: PrettyStyleProperties
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    92
}
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    93
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    94
interface Substitution
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    95
{
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    96
  ugly: string
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    97
  pretty: string
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    98
  pre?: string
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
    99
  post?: string
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   100
  style?: PrettyStyle
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   101
}
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   102
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   103
interface LanguageEntry
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   104
{
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   105
  language: DocumentSelector
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   106
  substitutions: Substitution[]
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   107
}
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   108
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   109
interface PrettifySymbolsMode
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   110
{
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   111
  onDidEnabledChange: (handler: (enabled: boolean) => void) => Disposable
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   112
  isEnabled: () => boolean,
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   113
  registerSubstitutions: (substitutions: LanguageEntry) => Disposable
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   114
}
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   115
66097
ee4c2d5b650e tuned signature;
wenzelm
parents: 66077
diff changeset
   116
export function setup(context: ExtensionContext, entries: [Entry])
66070
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   117
{
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   118
  update_entries(entries)
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   119
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   120
  const prettify_symbols_mode =
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   121
    extensions.getExtension<PrettifySymbolsMode>("siegebell.prettify-symbols-mode")
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   122
  if (prettify_symbols_mode) {
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   123
    prettify_symbols_mode.activate().then(() =>
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   124
    {
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   125
      const substitutions = [] as [Substitution]
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   126
      for (const entry of names) {
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   127
        const sym = entry[0]
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   128
        substitutions.push(
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   129
          {
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   130
            ugly: library.escape_regex(sym),
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   131
            pretty: library.escape_regex(get_unicode(sym))
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   132
          })
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   133
      }
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   134
      for (const language of ["isabelle", "isabelle-ml", "isabelle-output"]) {
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   135
        context.subscriptions.push(
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   136
          prettify_symbols_mode.exports.registerSubstitutions(
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   137
            {
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   138
              language: language,
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   139
              substitutions: substitutions
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   140
            }))
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   141
      }
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   142
    })
65a68dcd95c3 dynamic configuration of prettify-symbols-mode, similar to VSCoq;
wenzelm
parents: 66060
diff changeset
   143
  }
66073
50a28b3cceb2 clarified message;
wenzelm
parents: 66072
diff changeset
   144
  else {
50a28b3cceb2 clarified message;
wenzelm
parents: 66072
diff changeset
   145
    window.showWarningMessage("Please install extension \"Prettify Symbols Model\" and restart!")
50a28b3cceb2 clarified message;
wenzelm
parents: 66072
diff changeset
   146
  }
66052
39eb61b1fa51 provide information about Isabelle symbols within VSCode;
wenzelm
parents:
diff changeset
   147
}