@stepcode/codemirror 2.0.0
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/LICENSE +21 -0
- package/README.md +78 -0
- package/dist/index.d.ts +114 -0
- package/dist/index.js +1722 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 rolandoandrade
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @stepcode/codemirror
|
|
2
|
+
|
|
3
|
+
CodeMirror 6 language support for [StepCode](https://github.com/RolandoAndrade/stepcode),
|
|
4
|
+
built on the same parser and checker the runtime uses, plus the editor-side debugging
|
|
5
|
+
extensions. No worker, no interpreter: a host wires those.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { defaultHighlightStyle, syntaxHighlighting } from '@codemirror/language'
|
|
9
|
+
import { lintGutter } from '@codemirror/lint'
|
|
10
|
+
import { EditorState } from '@codemirror/state'
|
|
11
|
+
import { EditorView, lineNumbers } from '@codemirror/view'
|
|
12
|
+
import { breakpointLines, breakpointsChanged, debug, setCurrentLine, stepcode } from '@stepcode/codemirror'
|
|
13
|
+
import { profiles } from '@stepcode/profiles'
|
|
14
|
+
|
|
15
|
+
const view = new EditorView({
|
|
16
|
+
state: EditorState.create({
|
|
17
|
+
doc: 'Proceso hola\n Escribir "hola";\nFinProceso',
|
|
18
|
+
extensions: [
|
|
19
|
+
stepcode({ profile: profiles.es }),
|
|
20
|
+
debug(),
|
|
21
|
+
// the host's choices:
|
|
22
|
+
syntaxHighlighting(defaultHighlightStyle),
|
|
23
|
+
lintGutter(),
|
|
24
|
+
lineNumbers(),
|
|
25
|
+
EditorView.updateListener.of((update) => {
|
|
26
|
+
if (breakpointsChanged(update)) console.log(breakpointLines(update.state))
|
|
27
|
+
}),
|
|
28
|
+
],
|
|
29
|
+
}),
|
|
30
|
+
parent: document.body,
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
// from a paused run:
|
|
34
|
+
view.dispatch({ effects: setCurrentLine.of(2) })
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## What `stepcode()` bundles
|
|
38
|
+
|
|
39
|
+
One `LanguageSupport` per profile: the syntax tree (compiled by `stepcode`'s `compile` inside
|
|
40
|
+
a Lezer parser, so highlighting, diagnostics and completion never disagree), lint, folding,
|
|
41
|
+
indentation, block matching (`Si` ↔ `FinSi`), completion with block snippets, signature help,
|
|
42
|
+
hover, `F12` go to definition, and `←` for a typed `<-`. Switch profiles by wrapping it in a `Compartment`.
|
|
43
|
+
|
|
44
|
+
Every piece is also exported alone: `stepcodeLanguage`, `stepcodeLint`, `stepcodeCompletion`,
|
|
45
|
+
`stepcodeSignatureHelp`, `stepcodeHover`, `stepcodeBlockMatching`, `arrowInput`,
|
|
46
|
+
`stepcodeKeymap`.
|
|
47
|
+
`compileResultAt(state)` hands back the `CompileResult` the tree was built from, `treeDataAt(state)`
|
|
48
|
+
adds the offset maps the features use, and `stepcodeDiagnostics(state, options)` is the lint
|
|
49
|
+
mapping without the linter, for a host's own Problems panel.
|
|
50
|
+
|
|
51
|
+
Not included on purpose: a highlight style, the lint gutter, line numbers, history, the
|
|
52
|
+
default keymap.
|
|
53
|
+
|
|
54
|
+
## Debugging
|
|
55
|
+
|
|
56
|
+
`debug()` (or `breakpoints()` and `currentLine()` separately) is pure editor state:
|
|
57
|
+
|
|
58
|
+
- a breakpoint gutter — click to toggle; `breakpointLines(state)` reads the lines,
|
|
59
|
+
`setBreakpoints.of(lines)` / `toggleBreakpoint.of({ line })` change them,
|
|
60
|
+
`breakpointsChanged(update)` tells an update listener when to resend them;
|
|
61
|
+
- a current-line marker — `setCurrentLine.of(line | null)` highlights the line, marks the
|
|
62
|
+
gutter and scrolls it into view; `currentLineOf(state)` reads it back.
|
|
63
|
+
|
|
64
|
+
Markers follow their lines through edits and vanish when the line is deleted.
|
|
65
|
+
|
|
66
|
+
## Styling
|
|
67
|
+
|
|
68
|
+
The bundle ships a base theme, so the CSS class hooks are there to override: the breakpoint
|
|
69
|
+
gutter (`.cm-stepcode-breakpoints`, `.cm-stepcode-breakpoint`), the current line
|
|
70
|
+
(`.cm-stepcode-current-line`, `.cm-stepcode-current-line-marker`), the hover and signature
|
|
71
|
+
tooltips (`.cm-stepcode-hover`, `.cm-stepcode-signature`, `.cm-stepcode-signature-active`),
|
|
72
|
+
and the matching keyword pair (`.cm-matchingBracket`, `.cm-nonmatchingBracket`).
|
|
73
|
+
|
|
74
|
+
## Strings
|
|
75
|
+
|
|
76
|
+
Diagnostics render through `stepcode`'s catalogs; the few strings this package adds (symbol
|
|
77
|
+
kinds, "declared on line", snippet placeholders) come from its own table, which covers `es` and
|
|
78
|
+
`en` and falls back to `en`. `stepcode({ profile, locale })` defaults `locale` to the profile's.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { Language, LanguageSupport } from "@codemirror/language";
|
|
2
|
+
import { Command, EditorView, KeyBinding, ViewUpdate } from "@codemirror/view";
|
|
3
|
+
import "@codemirror/autocomplete";
|
|
4
|
+
import { ResolvedProfile } from "@stepcode/profiles";
|
|
5
|
+
import { BuiltinCall, Call, CompileResult, Identifier } from "stepcode";
|
|
6
|
+
import { NodeProp, NodeSet, Tree } from "@lezer/common";
|
|
7
|
+
import { EditorState, Extension } from "@codemirror/state";
|
|
8
|
+
import { Diagnostic } from "@codemirror/lint";
|
|
9
|
+
//#region src/arrow.d.ts
|
|
10
|
+
/**
|
|
11
|
+
* Spec §5.11: typing the second character of `<-` writes `←` instead, so the document keeps the
|
|
12
|
+
* spelling the profile prints while the keyboard keeps the one it can reach. Off for a profile
|
|
13
|
+
* that does not spell the arrow, or that assigns with `=`.
|
|
14
|
+
*/
|
|
15
|
+
declare function arrowInput(profile: ResolvedProfile): Extension;
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/options.d.ts
|
|
18
|
+
/** What every language feature needs: the profile the tree was built with and a locale. */
|
|
19
|
+
interface StepcodeOptions {
|
|
20
|
+
readonly profile: ResolvedProfile;
|
|
21
|
+
readonly locale: string;
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/completion.d.ts
|
|
25
|
+
/** The source, registered through the language's data so `autocompletion()` picks it up. */
|
|
26
|
+
declare function stepcodeCompletion(options: StepcodeOptions): Extension;
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/debug.d.ts
|
|
29
|
+
declare const toggleBreakpoint: import("@codemirror/state").StateEffectType<{
|
|
30
|
+
readonly line: number;
|
|
31
|
+
}>;
|
|
32
|
+
declare const setBreakpoints: import("@codemirror/state").StateEffectType<readonly number[]>;
|
|
33
|
+
declare const setCurrentLine: import("@codemirror/state").StateEffectType<number | null>;
|
|
34
|
+
/**
|
|
35
|
+
* Renders standalone: `stepcodeBaseTheme` rides along so its markers are visible even without
|
|
36
|
+
* `stepcode()`. `EditorState` dedupes identical extension values by identity, so combining this
|
|
37
|
+
* with `stepcode()` still installs the theme once.
|
|
38
|
+
*/
|
|
39
|
+
declare function breakpoints(): Extension;
|
|
40
|
+
/** Renders standalone; see `breakpoints()` on why `stepcodeBaseTheme` is included here too. */
|
|
41
|
+
declare function currentLine(): Extension;
|
|
42
|
+
declare function debug(): Extension;
|
|
43
|
+
/** 1-based, ascending; empty without the extension. */
|
|
44
|
+
declare function breakpointLines(state: EditorState): number[];
|
|
45
|
+
/** True when the update changed the breakpoint set — the host's cue to resend it. */
|
|
46
|
+
declare function breakpointsChanged(update: ViewUpdate): boolean;
|
|
47
|
+
/** 1-based, or null. */
|
|
48
|
+
declare function currentLineOf(state: EditorState): number | null;
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/definition.d.ts
|
|
51
|
+
declare const goToDefinition: Command;
|
|
52
|
+
/** F12 only; a mouse gesture is the host's choice (spec §5.10). */
|
|
53
|
+
declare const stepcodeKeymap: readonly KeyBinding[];
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/hover.d.ts
|
|
56
|
+
/** Spec §5.9. */
|
|
57
|
+
declare function stepcodeHover(options: StepcodeOptions): Extension;
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/lint.d.ts
|
|
60
|
+
/** The tree's compile diagnostics as CodeMirror diagnostics; empty before the first parse. */
|
|
61
|
+
declare function stepcodeDiagnostics(state: EditorState, options: StepcodeOptions): readonly Diagnostic[];
|
|
62
|
+
/** Lint from the tree, re-run after every completed parse. */
|
|
63
|
+
declare function stepcodeLint(options: StepcodeOptions): Extension;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/matching.d.ts
|
|
66
|
+
/**
|
|
67
|
+
* Spec §5.5: the stock matcher. Keyword, parenthesis, and bracket pairs all come from the
|
|
68
|
+
* `closedBy` / `openedBy` props on the leaves (spec §4.2); the `brackets` text config stays as
|
|
69
|
+
* the fallback for text the tree does not type.
|
|
70
|
+
*/
|
|
71
|
+
declare function stepcodeBlockMatching(): Extension;
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/tree.d.ts
|
|
74
|
+
/** What rides on the top node of every tree (spec §4.3). */
|
|
75
|
+
interface TreeData {
|
|
76
|
+
readonly result: CompileResult;
|
|
77
|
+
/** offset of an identifier leaf → its AST node, for hover, go to definition, signature help */
|
|
78
|
+
readonly identifiers: ReadonlyMap<number, Identifier>;
|
|
79
|
+
/** offset of a Call or BuiltinCall node → its AST node */
|
|
80
|
+
readonly calls: ReadonlyMap<number, Call | BuiltinCall>;
|
|
81
|
+
}
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/parser.d.ts
|
|
84
|
+
/**
|
|
85
|
+
* One `Language` per profile object, cached: `stepcodeCompletion` registers through its data
|
|
86
|
+
* facet, so every extension built for a profile must see the same instance.
|
|
87
|
+
*
|
|
88
|
+
* The cache keys on object identity (a `WeakMap`), so a profile must not be mutated after it is
|
|
89
|
+
* first passed here — a later mutation would silently apply to the cached `Language` too.
|
|
90
|
+
*/
|
|
91
|
+
declare function stepcodeLanguage(profile: ResolvedProfile): Language;
|
|
92
|
+
/** The data on the current tree, or `null` before a parse has produced one. */
|
|
93
|
+
declare function treeDataAt(state: EditorState): TreeData | null;
|
|
94
|
+
declare function compileResultAt(state: EditorState): CompileResult | null;
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/signature.d.ts
|
|
97
|
+
/** Spec §5.8: a tooltip field recomputed on selection, document and tree changes. */
|
|
98
|
+
declare function stepcodeSignatureHelp(options: StepcodeOptions): Extension;
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/stepcode.d.ts
|
|
101
|
+
/**
|
|
102
|
+
* Spec §7: everything for one profile. Deliberately absent: a highlight style, the lint
|
|
103
|
+
* gutter, line numbers, history and the default keymap — those are the host's.
|
|
104
|
+
*/
|
|
105
|
+
declare function stepcode(options: {
|
|
106
|
+
profile: ResolvedProfile;
|
|
107
|
+
locale?: string;
|
|
108
|
+
/** Include the autocompletion extension (default true); the editor's setting turns it off. */
|
|
109
|
+
completion?: boolean;
|
|
110
|
+
/** Rewrite a typed `<-` as `←` (default true) when the profile spells the arrow. */
|
|
111
|
+
arrow?: boolean;
|
|
112
|
+
}): LanguageSupport;
|
|
113
|
+
//#endregion
|
|
114
|
+
export { type StepcodeOptions, type TreeData, arrowInput, breakpointLines, breakpoints, breakpointsChanged, compileResultAt, currentLine, currentLineOf, debug, goToDefinition, setBreakpoints, setCurrentLine, stepcode, stepcodeBlockMatching, stepcodeCompletion, stepcodeDiagnostics, stepcodeHover, stepcodeKeymap, stepcodeLanguage, stepcodeLint, stepcodeSignatureHelp, toggleBreakpoint, treeDataAt };
|