@codemirror/language 6.0.0 → 6.2.1

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.
@@ -11,6 +11,6 @@ jobs:
11
11
  with:
12
12
  # You should create a personal access token and store it in your repository
13
13
  token: ${{ secrets.DISPATCH_AUTH }}
14
- repo: codemirror.next
14
+ repo: dev
15
15
  owner: codemirror
16
16
  event_type: push
package/CHANGELOG.md CHANGED
@@ -1,3 +1,27 @@
1
+ ## 6.2.1 (2022-07-21)
2
+
3
+ ### Bug fixes
4
+
5
+ Fix a bug where `bracketMatching` would incorrectly match nested brackets in syntax trees that put multiple pairs of brackets in the same parent node.
6
+
7
+ Fix a bug that could cause `indentRange` to loop infinitely.
8
+
9
+ ## 6.2.0 (2022-06-30)
10
+
11
+ ### Bug fixes
12
+
13
+ Fix a bug that prevented bracket matching to recognize plain brackets inside a language parsed as an overlay.
14
+
15
+ ### New features
16
+
17
+ The `indentRange` function provides an easy way to programatically auto-indent a range of the document.
18
+
19
+ ## 6.1.0 (2022-06-20)
20
+
21
+ ### New features
22
+
23
+ The `foldState` field is now public, and can be used to serialize and deserialize the fold state.
24
+
1
25
  ## 6.0.0 (2022-06-08)
2
26
 
3
27
  ### New features
package/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # @codemirror/language [![NPM version](https://img.shields.io/npm/v/@codemirror/language.svg)](https://www.npmjs.org/package/@codemirror/language)
2
2
 
3
- [ [**WEBSITE**](https://codemirror.net/6/) | [**DOCS**](https://codemirror.net/6/docs/ref/#language) | [**ISSUES**](https://github.com/codemirror/codemirror.next/issues) | [**FORUM**](https://discuss.codemirror.net/c/next/) | [**CHANGELOG**](https://github.com/codemirror/language/blob/main/CHANGELOG.md) ]
3
+ [ [**WEBSITE**](https://codemirror.net/) | [**DOCS**](https://codemirror.net/docs/ref/#language) | [**ISSUES**](https://github.com/codemirror/dev/issues) | [**FORUM**](https://discuss.codemirror.net/c/next/) | [**CHANGELOG**](https://github.com/codemirror/language/blob/main/CHANGELOG.md) ]
4
4
 
5
5
  This package implements the language support infrastructure for the
6
- [CodeMirror](https://codemirror.net/6/) code editor.
6
+ [CodeMirror](https://codemirror.net/) code editor.
7
7
 
8
- The [project page](https://codemirror.net/6/) has more information, a
9
- number of [examples](https://codemirror.net/6/examples/) and the
10
- [documentation](https://codemirror.net/6/docs/).
8
+ The [project page](https://codemirror.net/) has more information, a
9
+ number of [examples](https://codemirror.net/examples/) and the
10
+ [documentation](https://codemirror.net/docs/).
11
11
 
12
12
  This code is released under an
13
13
  [MIT license](https://github.com/codemirror/language/tree/main/LICENSE).
package/dist/index.cjs CHANGED
@@ -616,7 +616,10 @@ const parseWorker = view.ViewPlugin.fromClass(class ParseWorker {
616
616
  eventHandlers: { focus() { this.scheduleWork(); } }
617
617
  });
618
618
  /**
619
- The facet used to associate a language with an editor state.
619
+ The facet used to associate a language with an editor state. Used
620
+ by `Language` object's `extension` property (so you don't need to
621
+ manually wrap your languages in this). Can be used to access the
622
+ current language on a state.
620
623
  */
621
624
  const language = state.Facet.define({
622
625
  combine(languages) { return languages.length ? languages[0] : null; },
@@ -815,6 +818,31 @@ function getIndentation(context, pos) {
815
818
  return tree ? syntaxIndentation(context, tree, pos) : null;
816
819
  }
817
820
  /**
821
+ Create a change set that auto-indents all lines touched by the
822
+ given document range.
823
+ */
824
+ function indentRange(state, from, to) {
825
+ let updated = Object.create(null);
826
+ let context = new IndentContext(state, { overrideIndentation: start => { var _a; return (_a = updated[start]) !== null && _a !== void 0 ? _a : -1; } });
827
+ let changes = [];
828
+ for (let pos = from; pos <= to;) {
829
+ let line = state.doc.lineAt(pos);
830
+ pos = line.to + 1;
831
+ let indent = getIndentation(context, line.from);
832
+ if (indent == null)
833
+ continue;
834
+ if (!/\S/.test(line.text))
835
+ indent = 0;
836
+ let cur = /^\s*/.exec(line.text)[0];
837
+ let norm = indentString(state, indent);
838
+ if (cur != norm) {
839
+ updated[line.from] = indent;
840
+ changes.push({ from: line.from, to: line.from + cur.length, insert: norm });
841
+ }
842
+ }
843
+ return state.changes(changes);
844
+ }
845
+ /**
818
846
  Indentation contexts are used when calling [indentation
819
847
  services](https://codemirror.net/6/docs/ref/#language.indentService). They provide helper utilities
820
848
  useful in indentation logic, and can selectively override the
@@ -1204,6 +1232,13 @@ function selectedLines(view) {
1204
1232
  }
1205
1233
  return lines;
1206
1234
  }
1235
+ /**
1236
+ The state field that stores the folded ranges (as a [decoration
1237
+ set](https://codemirror.net/6/docs/ref/#view.DecorationSet)). Can be passed to
1238
+ [`EditorState.toJSON`](https://codemirror.net/6/docs/ref/#state.EditorState.toJSON) and
1239
+ [`fromJSON`](https://codemirror.net/6/docs/ref/#state.EditorState^fromJSON) to serialize the fold
1240
+ state.
1241
+ */
1207
1242
  const foldState = state.StateField.define({
1208
1243
  create() {
1209
1244
  return view.Decoration.none;
@@ -1231,7 +1266,24 @@ const foldState = state.StateField.define({
1231
1266
  }
1232
1267
  return folded;
1233
1268
  },
1234
- provide: f => view.EditorView.decorations.from(f)
1269
+ provide: f => view.EditorView.decorations.from(f),
1270
+ toJSON(folded, state) {
1271
+ let ranges = [];
1272
+ folded.between(0, state.doc.length, (from, to) => { ranges.push(from, to); });
1273
+ return ranges;
1274
+ },
1275
+ fromJSON(value) {
1276
+ if (!Array.isArray(value) || value.length % 2)
1277
+ throw new RangeError("Invalid JSON for fold state");
1278
+ let ranges = [];
1279
+ for (let i = 0; i < value.length;) {
1280
+ let from = value[i++], to = value[i++];
1281
+ if (typeof from != "number" || typeof to != "number")
1282
+ throw new RangeError("Invalid JSON for fold state");
1283
+ ranges.push(foldWidget.range(from, to));
1284
+ }
1285
+ return view.Decoration.set(ranges, true);
1286
+ }
1235
1287
  });
1236
1288
  /**
1237
1289
  Get a [range set](https://codemirror.net/6/docs/ref/#state.RangeSet) containing the folded ranges
@@ -1746,13 +1798,13 @@ function matchMarkedBrackets(_state, _pos, dir, token, matching, brackets) {
1746
1798
  depth++;
1747
1799
  }
1748
1800
  else if (matchingNodes(cursor.type, -dir, brackets)) {
1749
- depth--;
1750
1801
  if (depth == 0)
1751
1802
  return {
1752
1803
  start: firstToken,
1753
1804
  end: cursor.from == cursor.to ? undefined : { from: cursor.from, to: cursor.to },
1754
1805
  matched: false
1755
1806
  };
1807
+ depth--;
1756
1808
  }
1757
1809
  }
1758
1810
  } while (dir < 0 ? cursor.prevSibling() : cursor.nextSibling());
@@ -1772,7 +1824,7 @@ function matchPlainBrackets(state, pos, dir, tree, tokenType, maxScanDistance, b
1772
1824
  let basePos = pos + distance * dir;
1773
1825
  for (let pos = dir > 0 ? 0 : text.length - 1, end = dir > 0 ? text.length : -1; pos != end; pos += dir) {
1774
1826
  let found = brackets.indexOf(text[pos]);
1775
- if (found < 0 || tree.resolve(basePos + pos, 1).type != tokenType)
1827
+ if (found < 0 || tree.resolveInner(basePos + pos, 1).type != tokenType)
1776
1828
  continue;
1777
1829
  if ((found % 2 == 0) == (dir > 0)) {
1778
1830
  depth++;
@@ -2249,8 +2301,8 @@ for (let [legacyName, name] of [
2249
2301
  ["variable-2", "variableName.special"],
2250
2302
  ["string-2", "string.special"],
2251
2303
  ["def", "variableName.definition"],
2252
- ["tag", "typeName"],
2253
- ["attribute", "propertyName"],
2304
+ ["tag", "tagName"],
2305
+ ["attribute", "attributeName"],
2254
2306
  ["type", "typeName"],
2255
2307
  ["builtin", "variableName.standard"],
2256
2308
  ["qualifier", "modifier"],
@@ -2337,6 +2389,7 @@ exports.foldInside = foldInside;
2337
2389
  exports.foldKeymap = foldKeymap;
2338
2390
  exports.foldNodeProp = foldNodeProp;
2339
2391
  exports.foldService = foldService;
2392
+ exports.foldState = foldState;
2340
2393
  exports.foldable = foldable;
2341
2394
  exports.foldedRanges = foldedRanges;
2342
2395
  exports.forceParsing = forceParsing;
@@ -2345,6 +2398,7 @@ exports.getIndentation = getIndentation;
2345
2398
  exports.highlightingFor = highlightingFor;
2346
2399
  exports.indentNodeProp = indentNodeProp;
2347
2400
  exports.indentOnInput = indentOnInput;
2401
+ exports.indentRange = indentRange;
2348
2402
  exports.indentService = indentService;
2349
2403
  exports.indentString = indentString;
2350
2404
  exports.indentUnit = indentUnit;
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { NodeProp, Parser, Tree, TreeFragment, SyntaxNode, NodeType } from '@lezer/common';
2
2
  import { LRParser, ParserConfig } from '@lezer/lr';
3
3
  import * as _codemirror_state from '@codemirror/state';
4
- import { Facet, Extension, EditorState, Range } from '@codemirror/state';
4
+ import { Facet, Extension, EditorState, StateField, Range } from '@codemirror/state';
5
5
  import { EditorView, DecorationSet, Command, KeyBinding, ViewUpdate, BlockInfo, Decoration } from '@codemirror/view';
6
6
  import { Highlighter, Tag } from '@lezer/highlight';
7
7
  import { StyleModule, StyleSpec } from 'style-mod';
@@ -214,7 +214,10 @@ declare class ParseContext {
214
214
  static get(): ParseContext | null;
215
215
  }
216
216
  /**
217
- The facet used to associate a language with an editor state.
217
+ The facet used to associate a language with an editor state. Used
218
+ by `Language` object's `extension` property (so you don't need to
219
+ manually wrap your languages in this). Can be used to access the
220
+ current language on a state.
218
221
  */
219
222
  declare const language: Facet<Language, Language | null>;
220
223
  /**
@@ -380,6 +383,11 @@ be determined, and null otherwise.
380
383
  */
381
384
  declare function getIndentation(context: IndentContext | EditorState, pos: number): number | null;
382
385
  /**
386
+ Create a change set that auto-indents all lines touched by the
387
+ given document range.
388
+ */
389
+ declare function indentRange(state: EditorState, from: number, to: number): _codemirror_state.ChangeSet;
390
+ /**
383
391
  Indentation contexts are used when calling [indentation
384
392
  services](https://codemirror.net/6/docs/ref/#language.indentService). They provide helper utilities
385
393
  useful in indentation logic, and can selectively override the
@@ -617,6 +625,14 @@ State effect that unfolds the given range (if it was folded).
617
625
  */
618
626
  declare const unfoldEffect: _codemirror_state.StateEffectType<DocRange>;
619
627
  /**
628
+ The state field that stores the folded ranges (as a [decoration
629
+ set](https://codemirror.net/6/docs/ref/#view.DecorationSet)). Can be passed to
630
+ [`EditorState.toJSON`](https://codemirror.net/6/docs/ref/#state.EditorState.toJSON) and
631
+ [`fromJSON`](https://codemirror.net/6/docs/ref/#state.EditorState^fromJSON) to serialize the fold
632
+ state.
633
+ */
634
+ declare const foldState: StateField<DecorationSet>;
635
+ /**
620
636
  Get a [range set](https://codemirror.net/6/docs/ref/#state.RangeSet) containing the folded ranges
621
637
  in the given state.
622
638
  */
@@ -1061,4 +1077,4 @@ declare class StreamLanguage<State> extends Language {
1061
1077
  get allowsNesting(): boolean;
1062
1078
  }
1063
1079
 
1064
- export { Config, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, MatchResult, ParseContext, StreamLanguage, StreamParser, StringStream, TagStyle, TreeIndentContext, bracketMatching, codeFolding, continuedIndent, defaultHighlightStyle, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldAll, foldCode, foldEffect, foldGutter, foldInside, foldKeymap, foldNodeProp, foldService, foldable, foldedRanges, forceParsing, getIndentUnit, getIndentation, highlightingFor, indentNodeProp, indentOnInput, indentService, indentString, indentUnit, language, languageDataProp, matchBrackets, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, unfoldAll, unfoldCode, unfoldEffect };
1080
+ export { Config, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, MatchResult, ParseContext, StreamLanguage, StreamParser, StringStream, TagStyle, TreeIndentContext, bracketMatching, codeFolding, continuedIndent, defaultHighlightStyle, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldAll, foldCode, foldEffect, foldGutter, foldInside, foldKeymap, foldNodeProp, foldService, foldState, foldable, foldedRanges, forceParsing, getIndentUnit, getIndentation, highlightingFor, indentNodeProp, indentOnInput, indentRange, indentService, indentString, indentUnit, language, languageDataProp, matchBrackets, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, unfoldAll, unfoldCode, unfoldEffect };
package/dist/index.js CHANGED
@@ -612,7 +612,10 @@ const parseWorker = /*@__PURE__*/ViewPlugin.fromClass(class ParseWorker {
612
612
  eventHandlers: { focus() { this.scheduleWork(); } }
613
613
  });
614
614
  /**
615
- The facet used to associate a language with an editor state.
615
+ The facet used to associate a language with an editor state. Used
616
+ by `Language` object's `extension` property (so you don't need to
617
+ manually wrap your languages in this). Can be used to access the
618
+ current language on a state.
616
619
  */
617
620
  const language = /*@__PURE__*/Facet.define({
618
621
  combine(languages) { return languages.length ? languages[0] : null; },
@@ -811,6 +814,31 @@ function getIndentation(context, pos) {
811
814
  return tree ? syntaxIndentation(context, tree, pos) : null;
812
815
  }
813
816
  /**
817
+ Create a change set that auto-indents all lines touched by the
818
+ given document range.
819
+ */
820
+ function indentRange(state, from, to) {
821
+ let updated = Object.create(null);
822
+ let context = new IndentContext(state, { overrideIndentation: start => { var _a; return (_a = updated[start]) !== null && _a !== void 0 ? _a : -1; } });
823
+ let changes = [];
824
+ for (let pos = from; pos <= to;) {
825
+ let line = state.doc.lineAt(pos);
826
+ pos = line.to + 1;
827
+ let indent = getIndentation(context, line.from);
828
+ if (indent == null)
829
+ continue;
830
+ if (!/\S/.test(line.text))
831
+ indent = 0;
832
+ let cur = /^\s*/.exec(line.text)[0];
833
+ let norm = indentString(state, indent);
834
+ if (cur != norm) {
835
+ updated[line.from] = indent;
836
+ changes.push({ from: line.from, to: line.from + cur.length, insert: norm });
837
+ }
838
+ }
839
+ return state.changes(changes);
840
+ }
841
+ /**
814
842
  Indentation contexts are used when calling [indentation
815
843
  services](https://codemirror.net/6/docs/ref/#language.indentService). They provide helper utilities
816
844
  useful in indentation logic, and can selectively override the
@@ -1200,6 +1228,13 @@ function selectedLines(view) {
1200
1228
  }
1201
1229
  return lines;
1202
1230
  }
1231
+ /**
1232
+ The state field that stores the folded ranges (as a [decoration
1233
+ set](https://codemirror.net/6/docs/ref/#view.DecorationSet)). Can be passed to
1234
+ [`EditorState.toJSON`](https://codemirror.net/6/docs/ref/#state.EditorState.toJSON) and
1235
+ [`fromJSON`](https://codemirror.net/6/docs/ref/#state.EditorState^fromJSON) to serialize the fold
1236
+ state.
1237
+ */
1203
1238
  const foldState = /*@__PURE__*/StateField.define({
1204
1239
  create() {
1205
1240
  return Decoration.none;
@@ -1227,7 +1262,24 @@ const foldState = /*@__PURE__*/StateField.define({
1227
1262
  }
1228
1263
  return folded;
1229
1264
  },
1230
- provide: f => EditorView.decorations.from(f)
1265
+ provide: f => EditorView.decorations.from(f),
1266
+ toJSON(folded, state) {
1267
+ let ranges = [];
1268
+ folded.between(0, state.doc.length, (from, to) => { ranges.push(from, to); });
1269
+ return ranges;
1270
+ },
1271
+ fromJSON(value) {
1272
+ if (!Array.isArray(value) || value.length % 2)
1273
+ throw new RangeError("Invalid JSON for fold state");
1274
+ let ranges = [];
1275
+ for (let i = 0; i < value.length;) {
1276
+ let from = value[i++], to = value[i++];
1277
+ if (typeof from != "number" || typeof to != "number")
1278
+ throw new RangeError("Invalid JSON for fold state");
1279
+ ranges.push(foldWidget.range(from, to));
1280
+ }
1281
+ return Decoration.set(ranges, true);
1282
+ }
1231
1283
  });
1232
1284
  /**
1233
1285
  Get a [range set](https://codemirror.net/6/docs/ref/#state.RangeSet) containing the folded ranges
@@ -1742,13 +1794,13 @@ function matchMarkedBrackets(_state, _pos, dir, token, matching, brackets) {
1742
1794
  depth++;
1743
1795
  }
1744
1796
  else if (matchingNodes(cursor.type, -dir, brackets)) {
1745
- depth--;
1746
1797
  if (depth == 0)
1747
1798
  return {
1748
1799
  start: firstToken,
1749
1800
  end: cursor.from == cursor.to ? undefined : { from: cursor.from, to: cursor.to },
1750
1801
  matched: false
1751
1802
  };
1803
+ depth--;
1752
1804
  }
1753
1805
  }
1754
1806
  } while (dir < 0 ? cursor.prevSibling() : cursor.nextSibling());
@@ -1768,7 +1820,7 @@ function matchPlainBrackets(state, pos, dir, tree, tokenType, maxScanDistance, b
1768
1820
  let basePos = pos + distance * dir;
1769
1821
  for (let pos = dir > 0 ? 0 : text.length - 1, end = dir > 0 ? text.length : -1; pos != end; pos += dir) {
1770
1822
  let found = brackets.indexOf(text[pos]);
1771
- if (found < 0 || tree.resolve(basePos + pos, 1).type != tokenType)
1823
+ if (found < 0 || tree.resolveInner(basePos + pos, 1).type != tokenType)
1772
1824
  continue;
1773
1825
  if ((found % 2 == 0) == (dir > 0)) {
1774
1826
  depth++;
@@ -2245,8 +2297,8 @@ for (let [legacyName, name] of [
2245
2297
  ["variable-2", "variableName.special"],
2246
2298
  ["string-2", "string.special"],
2247
2299
  ["def", "variableName.definition"],
2248
- ["tag", "typeName"],
2249
- ["attribute", "propertyName"],
2300
+ ["tag", "tagName"],
2301
+ ["attribute", "attributeName"],
2250
2302
  ["type", "typeName"],
2251
2303
  ["builtin", "variableName.standard"],
2252
2304
  ["qualifier", "modifier"],
@@ -2307,4 +2359,4 @@ function docID(data) {
2307
2359
  return type;
2308
2360
  }
2309
2361
 
2310
- export { HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, ParseContext, StreamLanguage, StringStream, TreeIndentContext, bracketMatching, codeFolding, continuedIndent, defaultHighlightStyle, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldAll, foldCode, foldEffect, foldGutter, foldInside, foldKeymap, foldNodeProp, foldService, foldable, foldedRanges, forceParsing, getIndentUnit, getIndentation, highlightingFor, indentNodeProp, indentOnInput, indentService, indentString, indentUnit, language, languageDataProp, matchBrackets, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, unfoldAll, unfoldCode, unfoldEffect };
2362
+ export { HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, ParseContext, StreamLanguage, StringStream, TreeIndentContext, bracketMatching, codeFolding, continuedIndent, defaultHighlightStyle, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldAll, foldCode, foldEffect, foldGutter, foldInside, foldKeymap, foldNodeProp, foldService, foldState, foldable, foldedRanges, forceParsing, getIndentUnit, getIndentation, highlightingFor, indentNodeProp, indentOnInput, indentRange, indentService, indentString, indentUnit, language, languageDataProp, matchBrackets, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, unfoldAll, unfoldCode, unfoldEffect };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/language",
3
- "version": "6.0.0",
3
+ "version": "6.2.1",
4
4
  "description": "Language support infrastructure for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",