lean4monaco 1.1.8 → 1.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './editor';
2
2
  export * from './leanmonaco';
3
3
  export { RpcSessionAtPos } from './vscode-lean4/vscode-lean4/src/infoview';
4
+ export { DocumentPosition } from './vscode-lean4/lean4-infoview/src/infoview/util';
4
5
  export { LeanClient } from './vscode-lean4/vscode-lean4/src/leanclient';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './editor';
2
2
  export * from './leanmonaco';
3
3
  export { RpcSessionAtPos } from './vscode-lean4/vscode-lean4/src/infoview';
4
+ export { DocumentPosition } from './vscode-lean4/lean4-infoview/src/infoview/util';
4
5
  export { LeanClient } from './vscode-lean4/vscode-lean4/src/leanclient';
@@ -123,11 +123,7 @@ export class LeanMonaco {
123
123
  // Load fonts
124
124
  const fontFiles = [
125
125
  new FontFace("JuliaMono", `url(${new URL("./fonts/JuliaMono-Regular.ttf", import.meta.url)})`),
126
- new FontFace("Noto Color Emoji", `url(${new URL("./fonts/NotoColorEmoji-Regular.ttf", import.meta.url)})`),
127
- // new FontFace(
128
- // "LeanWeb",
129
- // `url(${new URL("./fonts/LeanWeb-Regular.otf", import.meta.url)})`,
130
- // )
126
+ new FontFace("Noto Color Emoji", `url(${new URL("./fonts/NotoColorEmoji-Subset.ttf", import.meta.url)})`),
131
127
  ];
132
128
  fontFiles.map(font => {
133
129
  document.fonts.add(font);
@@ -153,7 +149,7 @@ export class LeanMonaco {
153
149
  "editor.acceptSuggestionOnEnter": "off", // since there are plenty suggestions
154
150
  // other options
155
151
  "editor.renderWhitespace": "trailing",
156
- "editor.fontFamily": "'JuliaMono', 'Noto Color Emoji'",
152
+ "editor.fontFamily": "'Noto Color Emoji', 'JuliaMono'",
157
153
  "editor.wordWrap": "on",
158
154
  "editor.wrappingStrategy": "advanced",
159
155
  "workbench.colorTheme": "Visual Studio Light",
@@ -0,0 +1,45 @@
1
+ import * as React from 'react';
2
+ import { InfoviewConfig, LeanDiagnostic, LeanFileProgressProcessingInfo } from '@leanprover/infoview-api';
3
+ import { EditorConnection } from './editorConnection';
4
+ import { ServerVersion } from './serverVersion';
5
+ import { DocumentPosition } from './util';
6
+ export declare const EditorContext: React.Context<EditorConnection>;
7
+ export declare const VersionContext: React.Context<ServerVersion | undefined>;
8
+ export declare const ConfigContext: React.Context<InfoviewConfig>;
9
+ export declare const LspDiagnosticsContext: React.Context<Map<string, LeanDiagnostic[]>>;
10
+ export declare const ProgressContext: React.Context<Map<string, LeanFileProgressProcessingInfo[]>>;
11
+ /**
12
+ * Certain infoview components and widget instances
13
+ * utilize data that has been introduced above a specific position
14
+ * in a Lean source file.
15
+ *
16
+ * For instance, if we declare a global constant with `def foo` on line 10,
17
+ * we can immediately display it in a widget on line 11.
18
+ * To achieve this, the widget code needs to have access
19
+ * to the environment on line 11 as that environment contains `foo`.
20
+ *
21
+ * {@link EnvPosContext} stores the position in the file
22
+ * from which an appropriate environment can be retrieved.
23
+ * This is used to look up global constants,
24
+ * in particular RPC methods (`@[server_rpc_method]`)
25
+ * and widget modules (`@[widget_module]`).
26
+ *
27
+ * Note that {@link EnvPosContext} may, but need not,
28
+ * be equal to any of the positions which the infoview keeps track of
29
+ * (such as the editor cursor position).
30
+ *
31
+ * #### Infoview implementation details
32
+ *
33
+ * In the infoview, {@link EnvPosContext} is set as follows:
34
+ * - in an `<InfoDisplay>`,
35
+ * it is the position at which the info block is being displayed:
36
+ * either a recent editor cursor position
37
+ * (when shown in the at-cursor `<InfoDisplay>`,
38
+ * this will lag behind the current editor cursor position
39
+ * while the `<InfoDisplay>` is in the process of updating),
40
+ * or a pinned position.
41
+ * - in an `<InteractiveMessage>` that comes from a diagnostic
42
+ * emitted with a syntactic range,
43
+ * it is the start of the diagnostic's `fullRange`.
44
+ */
45
+ export declare const EnvPosContext: React.Context<DocumentPosition | undefined>;
@@ -0,0 +1,44 @@
1
+ import * as React from 'react';
2
+ import { defaultInfoviewConfig, } from '@leanprover/infoview-api';
3
+ // Type-unsafe initializers for contexts which we immediately set up at the top-level.
4
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
5
+ export const EditorContext = React.createContext(null);
6
+ export const VersionContext = React.createContext(undefined);
7
+ export const ConfigContext = React.createContext(defaultInfoviewConfig);
8
+ export const LspDiagnosticsContext = React.createContext(new Map());
9
+ export const ProgressContext = React.createContext(new Map());
10
+ /**
11
+ * Certain infoview components and widget instances
12
+ * utilize data that has been introduced above a specific position
13
+ * in a Lean source file.
14
+ *
15
+ * For instance, if we declare a global constant with `def foo` on line 10,
16
+ * we can immediately display it in a widget on line 11.
17
+ * To achieve this, the widget code needs to have access
18
+ * to the environment on line 11 as that environment contains `foo`.
19
+ *
20
+ * {@link EnvPosContext} stores the position in the file
21
+ * from which an appropriate environment can be retrieved.
22
+ * This is used to look up global constants,
23
+ * in particular RPC methods (`@[server_rpc_method]`)
24
+ * and widget modules (`@[widget_module]`).
25
+ *
26
+ * Note that {@link EnvPosContext} may, but need not,
27
+ * be equal to any of the positions which the infoview keeps track of
28
+ * (such as the editor cursor position).
29
+ *
30
+ * #### Infoview implementation details
31
+ *
32
+ * In the infoview, {@link EnvPosContext} is set as follows:
33
+ * - in an `<InfoDisplay>`,
34
+ * it is the position at which the info block is being displayed:
35
+ * either a recent editor cursor position
36
+ * (when shown in the at-cursor `<InfoDisplay>`,
37
+ * this will lag behind the current editor cursor position
38
+ * while the `<InfoDisplay>` is in the process of updating),
39
+ * or a pinned position.
40
+ * - in an `<InteractiveMessage>` that comes from a diagnostic
41
+ * emitted with a syntactic range,
42
+ * it is the start of the diagnostic's `fullRange`.
43
+ */
44
+ export const EnvPosContext = React.createContext(undefined);
@@ -0,0 +1,27 @@
1
+ import type { Location } from 'vscode-languageserver-protocol';
2
+ import { ContextMenuAction, EditorApi, InfoviewAction, InfoviewActionKind, InfoviewApi, PlainGoal, PlainTermGoal } from '@leanprover/infoview-api';
3
+ import { EventEmitter, Eventify } from './event';
4
+ import { DocumentPosition } from './util';
5
+ export type EditorEvents = Omit<Eventify<InfoviewApi>, 'requestedAction' | 'clickedContextMenu'> & {
6
+ requestedAction: EventEmitter<InfoviewAction, InfoviewActionKind>;
7
+ /**
8
+ * Must fire whenever the user clicks an infoview-specific context menu entry.
9
+ *
10
+ * Keys for this event are of the form `` `${action.entry}:${action.id}` ``.
11
+ */
12
+ clickedContextMenu: EventEmitter<ContextMenuAction, string>;
13
+ };
14
+ /** Provides higher-level wrappers around functionality provided by the editor,
15
+ * e.g. to insert a comment. See also {@link EditorApi}. */
16
+ export declare class EditorConnection {
17
+ readonly api: EditorApi;
18
+ readonly events: EditorEvents;
19
+ constructor(api: EditorApi, events: EditorEvents);
20
+ /** Highlights the given range in a document in the editor. */
21
+ revealLocation(loc: Location): Promise<void>;
22
+ revealPosition(pos: DocumentPosition): Promise<void>;
23
+ /** Copies the text to a comment at the cursor position. */
24
+ copyToComment(text: string): Promise<void>;
25
+ requestPlainGoal(pos: DocumentPosition): Promise<PlainGoal | undefined>;
26
+ requestPlainTermGoal(pos: DocumentPosition): Promise<PlainTermGoal | undefined>;
27
+ }
@@ -0,0 +1,41 @@
1
+ import { DocumentPosition } from './util';
2
+ /** Provides higher-level wrappers around functionality provided by the editor,
3
+ * e.g. to insert a comment. See also {@link EditorApi}. */
4
+ export class EditorConnection {
5
+ api;
6
+ events;
7
+ constructor(api, events) {
8
+ this.api = api;
9
+ this.events = events;
10
+ }
11
+ /** Highlights the given range in a document in the editor. */
12
+ async revealLocation(loc) {
13
+ const show = {
14
+ uri: loc.uri,
15
+ selection: loc.range,
16
+ };
17
+ await this.api.showDocument(show);
18
+ }
19
+ async revealPosition(pos) {
20
+ const loc = {
21
+ uri: pos.uri,
22
+ range: {
23
+ start: pos,
24
+ end: pos,
25
+ },
26
+ };
27
+ await this.revealLocation(loc);
28
+ }
29
+ /** Copies the text to a comment at the cursor position. */
30
+ async copyToComment(text) {
31
+ await this.api.insertText(`/-\n${text}\n-/`, 'above');
32
+ }
33
+ requestPlainGoal(pos) {
34
+ const params = DocumentPosition.toTdpp(pos);
35
+ return this.api.sendClientRequest(pos.uri, '$/lean/plainGoal', params);
36
+ }
37
+ requestPlainTermGoal(pos) {
38
+ const params = DocumentPosition.toTdpp(pos);
39
+ return this.api.sendClientRequest(pos.uri, '$/lean/plainTermGoal', params);
40
+ }
41
+ }
@@ -0,0 +1,33 @@
1
+ import type { Disposable } from 'vscode-languageserver-protocol';
2
+ export declare class EventEmitter<E, in out K> {
3
+ private freshId;
4
+ private handlers;
5
+ private handlersWithKey;
6
+ current?: E;
7
+ /**
8
+ * Register a handler that will receive events from this emitter
9
+ * and return a closure that removes the handler registration.
10
+ *
11
+ * If `key` is specified, only events fired with that key
12
+ * will be propagated to this handler.
13
+ */
14
+ on(handler: (_: E) => void, key?: K): Disposable;
15
+ /**
16
+ * Propagate the event to registered handlers.
17
+ *
18
+ * The event is propagated to all keyless handlers.
19
+ * Furthermore if `key` is provided,
20
+ * the event is also propagated to handlers registered with that key.
21
+ */
22
+ fire(event: E, key?: K): void;
23
+ }
24
+ type ExcludeNonEvent<T, U> = T extends (...args: any) => Promise<void> ? U : never;
25
+ /**
26
+ * Turn all fields in `T` which extend `(...args: As) => Promise<void>`
27
+ * into event emitter fields `f: EventEmitter<As>`.
28
+ * Other fields are removed.
29
+ */
30
+ export type Eventify<T> = {
31
+ [P in keyof T as ExcludeNonEvent<T[P], P>]: T[P] extends (arg: infer A) => Promise<void> ? EventEmitter<A, never> : T[P] extends (...args: infer As) => Promise<void> ? EventEmitter<As, never> : never;
32
+ };
33
+ export {};
@@ -0,0 +1,57 @@
1
+ export class EventEmitter {
2
+ freshId = 0;
3
+ handlers = new Map();
4
+ handlersWithKey = new Map();
5
+ current;
6
+ /**
7
+ * Register a handler that will receive events from this emitter
8
+ * and return a closure that removes the handler registration.
9
+ *
10
+ * If `key` is specified, only events fired with that key
11
+ * will be propagated to this handler.
12
+ */
13
+ on(handler, key) {
14
+ const id = this.freshId;
15
+ this.freshId += 1;
16
+ if (key) {
17
+ const handlersForKey = this.handlersWithKey.get(key) ?? [];
18
+ handlersForKey.push(handler);
19
+ this.handlersWithKey.set(key, handlersForKey);
20
+ }
21
+ else {
22
+ this.handlers.set(id, handler);
23
+ }
24
+ return {
25
+ dispose: () => {
26
+ if (key) {
27
+ const handlersForKey = this.handlersWithKey.get(key) ?? [];
28
+ // We assume that no key has so many handlers registered
29
+ // that the linear `filter` operation becomes a perf issue.
30
+ this.handlersWithKey.set(key, handlersForKey.filter(h => h !== handler));
31
+ }
32
+ else {
33
+ this.handlers.delete(id);
34
+ }
35
+ },
36
+ };
37
+ }
38
+ /**
39
+ * Propagate the event to registered handlers.
40
+ *
41
+ * The event is propagated to all keyless handlers.
42
+ * Furthermore if `key` is provided,
43
+ * the event is also propagated to handlers registered with that key.
44
+ */
45
+ fire(event, key) {
46
+ this.current = event;
47
+ for (const h of this.handlers.values()) {
48
+ h(event);
49
+ }
50
+ if (key) {
51
+ const handlersForKey = this.handlersWithKey.get(key) ?? [];
52
+ for (const h of handlersForKey) {
53
+ h(event);
54
+ }
55
+ }
56
+ }
57
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Keeps track of the Lean server version and available features.
3
+ * @module
4
+ */
5
+ export declare class ServerVersion {
6
+ major: number;
7
+ minor: number;
8
+ patch: number;
9
+ constructor(version: string);
10
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Keeps track of the Lean server version and available features.
3
+ * @module
4
+ */
5
+ export class ServerVersion {
6
+ major;
7
+ minor;
8
+ patch;
9
+ constructor(version) {
10
+ const vs = version.split('.');
11
+ if (vs.length === 2) {
12
+ this.major = parseInt(vs[0]);
13
+ this.minor = parseInt(vs[1]);
14
+ this.patch = 0;
15
+ }
16
+ else if (vs.length === 3) {
17
+ this.major = parseInt(vs[0]);
18
+ this.minor = parseInt(vs[1]);
19
+ this.patch = parseInt(vs[2]);
20
+ }
21
+ else {
22
+ throw new Error(`cannot parse Lean server version '${version}'`);
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,141 @@
1
+ import * as React from 'react';
2
+ import type { DocumentUri, Position, Range, TextDocumentPositionParams } from 'vscode-languageserver-protocol';
3
+ import { EventEmitter } from './event';
4
+ /** A document URI and a position in that document. */
5
+ export interface DocumentPosition extends Position {
6
+ uri: DocumentUri;
7
+ }
8
+ export declare namespace DocumentPosition {
9
+ function isEqual(p1: DocumentPosition, p2: DocumentPosition): boolean;
10
+ function toTdpp(p: DocumentPosition): TextDocumentPositionParams;
11
+ function toString(p: DocumentPosition): string;
12
+ }
13
+ export declare namespace PositionHelpers {
14
+ function isLessThanOrEqual(p1: Position, p2: Position): boolean;
15
+ }
16
+ export declare namespace RangeHelpers {
17
+ function contains(range: Range, pos: Position, ignoreCharacter?: boolean): boolean;
18
+ }
19
+ export declare function escapeHtml(s: string): string;
20
+ /** @deprecated (unused) */
21
+ export declare function colorizeMessage(goal: string): string;
22
+ export declare function basename(path: string): string;
23
+ /**
24
+ * A specialization of {@link React.useEffect} which executes `f` with the event data
25
+ * whenever `ev` fires.
26
+ * If `key` is provided, `f` is only invoked on events fired with that key.
27
+ */
28
+ export declare function useEvent<E, K>(ev: EventEmitter<E, K>, f: (_: E) => void, dependencies?: React.DependencyList, key?: K): void;
29
+ /**
30
+ * A piece of React {@link React.useState} which returns the data that `ev` most recently fired with.
31
+ * If `f` is provided, the data is mapped through `f` first. */
32
+ export declare function useEventResult<E, K>(ev: EventEmitter<E, K>): E | undefined;
33
+ export declare function useEventResult<E, K, T>(ev: EventEmitter<E, K>, f: (newVal: E) => T): T | undefined;
34
+ export declare function useServerNotificationEffect<T>(method: string, f: (params: T) => void, deps?: React.DependencyList): void;
35
+ /**
36
+ * Returns the same tuple as `setState` such that whenever a server notification with `method`
37
+ * arrives at the editor, the state will be updated according to `f`.
38
+ */
39
+ export declare function useServerNotificationState<S, T>(method: string, initial: S, f: (params: T) => Promise<(state: S) => S>, deps?: React.DependencyList): [S, React.Dispatch<React.SetStateAction<S>>];
40
+ export declare function useClientNotificationEffect<T>(method: string, f: (params: T) => void, deps?: React.DependencyList): void;
41
+ /**
42
+ * Like {@link useServerNotificationState} but for client->server notifications sent by the editor.
43
+ */
44
+ export declare function useClientNotificationState<S, T>(method: string, initial: S, f: (state: S, params: T) => S, deps?: React.DependencyList): [S, React.Dispatch<React.SetStateAction<S>>];
45
+ /** Useful for controlling {@link usePausableState} from child components. */
46
+ export interface PausableProps {
47
+ isPaused: boolean;
48
+ setPaused: React.Dispatch<React.SetStateAction<boolean>>;
49
+ }
50
+ /**
51
+ * Returns `[{ isPaused, setPaused }, tPausable, tRef]` s.t.
52
+ * - `[isPaused, setPaused]` are the paused status state
53
+ * - for as long as `isPaused` is set, `tPausable` holds its initial value (the `t` passed before pausing)
54
+ * rather than updates with changes to `t`.
55
+ * - `tRef` can be used to overwrite the paused state
56
+ *
57
+ * To pause child components, `startPaused` can be passed in their props.
58
+ */
59
+ export declare function usePausableState<T>(startPaused: boolean, t: T): [PausableProps, T, React.MutableRefObject<T>];
60
+ export type Keyed<T> = T & {
61
+ key: string;
62
+ };
63
+ /**
64
+ * Adds a unique `key` property to each element in `elems` using
65
+ * the values of (possibly non-injective) `getId`.
66
+ */
67
+ export declare function addUniqueKeys<T>(elems: T[], getId: (el: T) => string): Keyed<T>[];
68
+ export interface LogicalDomElement {
69
+ contains(el: Node): boolean;
70
+ }
71
+ export interface LogicalDomStorage {
72
+ /** Registers a descendant in the logical DOM.
73
+ * Returns a function which disposes of the registration. */
74
+ registerDescendant(el: LogicalDomElement): () => void;
75
+ }
76
+ export declare const LogicalDomContext: React.Context<LogicalDomStorage>;
77
+ /** Suppose a component B appears as a React descendant of the component A. For layout reasons,
78
+ * we sometimes don't want B to appear as a descendant of A in the DOM, so we use `createPortal`.
79
+ * We may still however want to carry out `contains` checks as if B were there, i.e. according to
80
+ * the React tree structure rather than the DOM structure. While React already correctly propagates
81
+ * DOM events up the React tree, other functionality such as `contains` is not provided. We provide
82
+ * it in this hook.
83
+ *
84
+ * Accepts a ref to the observed {@link HTMLElement} (A in the example). Returns:
85
+ * - a {@link LogicalDomElement} which provides `contains` checks for that {@link HTMLElement}; and
86
+ * - a {@link LogicalDomStorage} which MUST be passed to a {@link LogicalDomContext} enclosing
87
+ * the observed {@link HTMLElement}.
88
+ *
89
+ * Additionally, any component which introduces a portal MUST call `registerDescendant` in the
90
+ * {@link LogicalDomContext} with a ref to the portalled component (B in the example). */
91
+ export declare function useLogicalDomObserver(elt: React.RefObject<HTMLElement>): [LogicalDomElement, LogicalDomStorage];
92
+ /**
93
+ * An effect which calls `onClickOutside` whenever an element not logically descending from `ld`
94
+ * (see {@link useLogicalDomObserver}) is clicked. Note that `onClickOutside` is not called on clicks
95
+ * on the scrollbar since these should usually not impact the app's state.
96
+ * `isActive` controls whether the `onClickOutside` handler is active. This can be useful for performance,
97
+ * since having lots of `onClickOutside` handlers when they are not needed can be expensive.
98
+ */
99
+ export declare function useOnClickOutside(ld: LogicalDomElement, onClickOutside: (_: PointerEvent) => void, isActive?: boolean): void;
100
+ /** Sends an exception object to a throwable error.
101
+ * Maps JSON Rpc errors to throwable errors.
102
+ */
103
+ export declare function mapRpcError(err: unknown): Error;
104
+ /** Catch handler for RPC methods that just returns undefined if the method is not found.
105
+ * This is useful for compatibility with versions of Lean that do not yet have the given RPC method.
106
+ */
107
+ export declare function discardMethodNotFound(e: unknown): undefined;
108
+ export type AsyncState<T> = {
109
+ state: 'loading';
110
+ } | {
111
+ state: 'resolved';
112
+ value: T;
113
+ } | {
114
+ state: 'rejected';
115
+ error: any;
116
+ };
117
+ export type AsyncWithTriggerState<T> = {
118
+ state: 'notStarted';
119
+ } | AsyncState<T>;
120
+ export declare function useAsyncWithTrigger<T>(fn: () => Promise<T>, deps?: React.DependencyList): [AsyncWithTriggerState<T>, () => Promise<void>];
121
+ /** This React hook will run the given promise function `fn` whenever the deps change
122
+ * and use it to update the status and result when the promise resolves.
123
+ *
124
+ * This function prevents race conditions if the requests resolve in a
125
+ * different order to that which they were requested in:
126
+ *
127
+ * - Request 1 is sent with, say, line=42.
128
+ * - Request 2 is sent with line=90.
129
+ * - Request 2 returns with diags=[].
130
+ * - Request 1 returns with diags=['error'].
131
+ *
132
+ * Without `useAsync` we would now return the diagnostics for line 42 even though we're at line 90.
133
+ *
134
+ * When the deps change, the function immediately returns `{ state: 'loading' }`.
135
+ */
136
+ export declare function useAsync<T>(fn: () => Promise<T>, deps?: React.DependencyList): AsyncState<T>;
137
+ /** Like {@link useAsync} but never transitions from `resolved` to `loading` by internally storing
138
+ * the latest `resolved` state and continuing to return it while an update is in flight. The lower
139
+ * amount of re-renders tends to be less visually jarring.
140
+ */
141
+ export declare function useAsyncPersistent<T>(fn: () => Promise<T>, deps?: React.DependencyList): AsyncState<T>;