@coze-editor/preset-code 0.1.0-alpha.37c297 → 0.1.0-alpha.445000

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,13 +1,12 @@
1
1
  import * as _codemirror_view from '@codemirror/view';
2
2
  import * as _codemirror_state from '@codemirror/state';
3
3
  import { Extension } from '@codemirror/state';
4
- import * as _coze_editor_core_plugins from '@coze-editor/core-plugins';
5
4
  import * as _coze_editor_core from '@coze-editor/core';
6
5
  import { InferEditorAPIFromPlugins } from '@coze-editor/core';
7
6
  import { LanguageConfig, Transformer } from '@coze-editor/code-language-shared';
8
7
  export { Transformer } from '@coze-editor/code-language-shared';
9
8
  import { TagStyle } from '@codemirror/language';
10
- import { Text } from 'text-mapping';
9
+ import { Text } from '@coze-editor/utils';
11
10
  export { tags } from '@lezer/highlight';
12
11
 
13
12
  declare const themes: any;
@@ -90,7 +89,14 @@ declare const createTheme: ({ variant, settings, styles }: Theme) => Extension;
90
89
 
91
90
  declare const transformerCreator: (processor: (text: Text) => Text) => Transformer;
92
91
 
93
- declare const preset: (_coze_editor_core.ExtensionPluginSpec | _coze_editor_core.OptionPluginSpec<"fontSize", number> | _coze_editor_core.OptionPluginSpec<"placeholder", string | HTMLElement> | _coze_editor_core.OptionPluginSpec<"readOnly", boolean> | _coze_editor_core.OptionPluginSpec<"editable", boolean> | _coze_editor_core.OptionPluginSpec<"lineWrapping", boolean> | _coze_editor_core.OptionPluginSpec<"height", string | number> | _coze_editor_core.OptionPluginSpec<"minHeight", string | number> | _coze_editor_core.OptionPluginSpec<"maxHeight", string | number> | _coze_editor_core.APIPluginSpec<"getValue", [], string> | _coze_editor_core.APIPluginSpec<"setValue", [value: string], void> | _coze_editor_core.APIPluginSpec<"replaceText", [options: _coze_editor_core_plugins.ReplaceTextOptions], void> | _coze_editor_core.APIPluginSpec<"getCursorPosition", [], number> | _coze_editor_core.APIPluginSpec<"setCursorPosition", [pos: number], void> | _coze_editor_core.APIPluginSpec<"focus", [], void> | _coze_editor_core.APIPluginSpec<"blur", [], void> | _coze_editor_core.APIPluginSpec<"disableKeybindings", [keys: string[]], void> | _coze_editor_core.APIPluginSpec<"updateASTDecorations", [], void> | _coze_editor_core.APIPluginSpec<"undo", [], void> | _coze_editor_core.APIPluginSpec<"redo", [], void> | _coze_editor_core.APIPluginSpec<"transformTextInSelection", [transformer: (text: string) => string | undefined], void> | _coze_editor_core.APIPluginSpec<"getSelection", [], {
92
+ declare const preset: (_coze_editor_core.ExtensionPluginSpec | _coze_editor_core.OptionPluginSpec<"fontSize", number> | _coze_editor_core.OptionPluginSpec<"placeholder", string | HTMLElement> | _coze_editor_core.OptionPluginSpec<"readOnly", boolean> | _coze_editor_core.OptionPluginSpec<"editable", boolean> | _coze_editor_core.OptionPluginSpec<"lineWrapping", boolean> | _coze_editor_core.OptionPluginSpec<"height", string | number> | _coze_editor_core.OptionPluginSpec<"minHeight", string | number> | _coze_editor_core.OptionPluginSpec<"maxHeight", string | number> | _coze_editor_core.APIPluginSpec<"getValue", [], string> | _coze_editor_core.APIPluginSpec<"setValue", [value: string], void> | _coze_editor_core.APIPluginSpec<"replaceText", [options: {
93
+ from: number;
94
+ to: number;
95
+ text: string;
96
+ cursorOffset?: number;
97
+ scrollIntoView?: boolean;
98
+ userEvent?: string;
99
+ }], void> | _coze_editor_core.APIPluginSpec<"getCursorPosition", [], number> | _coze_editor_core.APIPluginSpec<"setCursorPosition", [pos: number], void> | _coze_editor_core.APIPluginSpec<"focus", [], void> | _coze_editor_core.APIPluginSpec<"blur", [], void> | _coze_editor_core.APIPluginSpec<"disableKeybindings", [keys: string[]], void> | _coze_editor_core.APIPluginSpec<"updateASTDecorations", [], void> | _coze_editor_core.APIPluginSpec<"undo", [], void> | _coze_editor_core.APIPluginSpec<"redo", [], void> | _coze_editor_core.APIPluginSpec<"transformTextInSelection", [transformer: (text: string) => string | undefined], void> | _coze_editor_core.APIPluginSpec<"getSelection", [], {
94
100
  from: number;
95
101
  to: number;
96
102
  anchor: number;
package/dist/index.js CHANGED
@@ -42,7 +42,7 @@ var import_extensions = require("@coze-editor/extensions");
42
42
  var import_extension_scrollbar = require("@coze-editor/extension-scrollbar");
43
43
  var import_extension_completion_icons = require("@coze-editor/extension-completion-icons");
44
44
  var import_core_plugins = require("@coze-editor/core-plugins");
45
- var import_core = require("@coze-editor/core");
45
+ var import_core2 = require("@coze-editor/core");
46
46
  var import_code_language_shared2 = require("@coze-editor/code-language-shared");
47
47
  var import_view6 = require("@codemirror/view");
48
48
  var import_state6 = require("@codemirror/state");
@@ -78,11 +78,12 @@ var vscodeDark = [
78
78
 
79
79
  // src/language-registry.ts
80
80
  var import_vscode_languageserver_types2 = require("vscode-languageserver-types");
81
- var import_codemirror_shiki = __toESM(require("codemirror-shiki"));
81
+ var import_microfuzz = __toESM(require("@nozbe/microfuzz"));
82
82
  var import_extension_lint = require("@coze-editor/extension-lint");
83
83
  var import_extension_links = require("@coze-editor/extension-links");
84
84
  var import_code_language_shared = require("@coze-editor/code-language-shared");
85
85
  var import_view3 = require("@codemirror/view");
86
+ var import_theme_one_dark = require("@codemirror/theme-one-dark");
86
87
  var import_state3 = require("@codemirror/state");
87
88
  var import_language2 = require("@codemirror/language");
88
89
  var import_autocomplete = require("@codemirror/autocomplete");
@@ -97,10 +98,21 @@ var import_marked_shiki = __toESM(require("marked-shiki"));
97
98
  var import_marked = require("marked");
98
99
 
99
100
  // src/highlighter.ts
100
- var import_shiki = require("shiki");
101
- var highlighterPromise = (0, import_shiki.createHighlighter)({
102
- langs: ["md", "js", "ts", "python"],
103
- themes: ["github-dark", "one-dark-pro"]
101
+ var import_oniguruma = require("shiki/engine/oniguruma");
102
+ var import_core = require("shiki/core");
103
+ var highlighterPromise = (0, import_core.createHighlighterCore)({
104
+ langs: [
105
+ () => import("@shikijs/langs/markdown"),
106
+ () => import("@shikijs/langs/javascript"),
107
+ () => import("@shikijs/langs/typescript"),
108
+ () => import("@shikijs/langs/python"),
109
+ () => import("@shikijs/langs/sql")
110
+ ],
111
+ themes: [
112
+ () => import("@shikijs/themes/github-dark"),
113
+ () => import("@shikijs/themes/one-dark-pro")
114
+ ],
115
+ engine: (0, import_oniguruma.createOnigurumaEngine)(import("shiki/wasm"))
104
116
  });
105
117
 
106
118
  // src/const.ts
@@ -392,6 +404,7 @@ function formatContents(contents) {
392
404
  }
393
405
  return "";
394
406
  }
407
+ var identRe = /^[\w$]+$/;
395
408
  var LanguagesRegistry = class {
396
409
  languages = {};
397
410
  register(id, options) {
@@ -413,15 +426,7 @@ var LanguagesRegistry = class {
413
426
  const { languageService, language, extensions } = this.languages[id];
414
427
  const langaugeExtensions = [];
415
428
  langaugeExtensions.push(new import_language2.LanguageSupport(language));
416
- if (id === "typescript" || id === "python") {
417
- langaugeExtensions.push(
418
- (0, import_codemirror_shiki.default)({
419
- highlighter: highlighterPromise,
420
- language: id,
421
- theme: DEFAULT_SYNTAX_THEME
422
- })
423
- );
424
- }
429
+ langaugeExtensions.push((0, import_language2.syntaxHighlighting)(import_theme_one_dark.oneDarkHighlightStyle));
425
430
  langaugeExtensions.push(
426
431
  import_state3.StateField.define({
427
432
  create(state) {
@@ -604,9 +609,19 @@ var LanguagesRegistry = class {
604
609
  }
605
610
  if (languageService == null ? void 0 : languageService.doComplete) {
606
611
  langaugeExtensions.push(
612
+ import_view3.EditorView.theme({
613
+ ".cm-completionInfo p": {
614
+ margin: 0
615
+ },
616
+ ".cm-completionMatchedText": {
617
+ textDecoration: "none",
618
+ color: "#4daafc"
619
+ }
620
+ }),
607
621
  (0, import_autocomplete.autocompletion)({
608
622
  override: [
609
623
  async function({ state, pos, view: view2 }) {
624
+ var _a2;
610
625
  const { textDocument, originalRangeFor, generatedRangeFor } = state.field(import_code_language_shared.textDocumentField);
611
626
  const range = generatedRangeFor({ from: pos, to: pos });
612
627
  const offset = range == null ? void 0 : range.from;
@@ -620,14 +635,73 @@ var LanguagesRegistry = class {
620
635
  if (!completionResult || !Array.isArray(completionResult.items) || completionResult.items.length === 0) {
621
636
  return null;
622
637
  }
638
+ let { items } = completionResult;
639
+ const content = textDocument.getText();
640
+ const charBefore = content.slice(offset - 1, offset);
641
+ const triggerCharacters = languageService.triggerCharacters ?? [];
642
+ if (!triggerCharacters.includes(charBefore)) {
643
+ let i = offset - 1;
644
+ let query = "";
645
+ while (i >= 0) {
646
+ const char = content.slice(i, i + 1);
647
+ if (char === "\n") {
648
+ break;
649
+ }
650
+ if (!identRe.test(char) && i + 1 <= offset) {
651
+ break;
652
+ }
653
+ i--;
654
+ }
655
+ query = content.slice(i + 1, offset);
656
+ if (query) {
657
+ const fuzzySearch = (0, import_microfuzz.default)(items, {
658
+ key: "label"
659
+ });
660
+ items = fuzzySearch(query).map((v) => {
661
+ const finalItem = {
662
+ ...v.item,
663
+ data: {
664
+ matches: v.matches.reduce((memo, current) => {
665
+ if (!current) {
666
+ return memo;
667
+ }
668
+ return [
669
+ ...memo,
670
+ ...current.reduce(
671
+ (m, c) => [...m, c[0], c[1] + 1],
672
+ []
673
+ )
674
+ ];
675
+ }, [])
676
+ }
677
+ };
678
+ if (!finalItem.textEdit) {
679
+ finalItem.textEdit = {
680
+ range: {
681
+ start: textDocument.positionAt(i + 1),
682
+ end: textDocument.positionAt(offset)
683
+ },
684
+ newText: v.item.label
685
+ };
686
+ }
687
+ return finalItem;
688
+ });
689
+ } else if (Array.isArray(languageService.triggerCharacters)) {
690
+ items = [];
691
+ }
692
+ }
623
693
  const completionOptions = [];
624
694
  const mapping = /* @__PURE__ */ new WeakMap();
625
- const resolveCompletion = createResolveCompletion(
626
- view2,
627
- languageService,
628
- mapping
629
- );
630
- for (const item of completionResult.items) {
695
+ let resolveCompletion = void 0;
696
+ if (typeof languageService.resolveCompletionItem === "function") {
697
+ resolveCompletion = createResolveCompletion(
698
+ view2,
699
+ languageService,
700
+ mapping
701
+ );
702
+ }
703
+ const matchesMapping = /* @__PURE__ */ new WeakMap();
704
+ for (const item of items) {
631
705
  const { kind, label, textEdit, textEditText } = item;
632
706
  const completion = {
633
707
  label,
@@ -635,6 +709,7 @@ var LanguagesRegistry = class {
635
709
  detail: item.detail ?? ""
636
710
  };
637
711
  mapping.set(completion, item);
712
+ matchesMapping.set(completion, (_a2 = item.data) == null ? void 0 : _a2.matches);
638
713
  if (textEdit) {
639
714
  const range2 = "range" in textEdit ? textEdit.range : textEdit.replace;
640
715
  const from = textDocument.offsetAt(range2.start);
@@ -675,7 +750,10 @@ var LanguagesRegistry = class {
675
750
  return {
676
751
  from: pos,
677
752
  options: completionOptions,
678
- filter: false
753
+ filter: false,
754
+ getMatch(completion) {
755
+ return matchesMapping.get(completion) ?? [];
756
+ }
679
757
  };
680
758
  }
681
759
  ]
@@ -975,9 +1053,9 @@ var import_dist = require("@coze-editor/extension-scrollbar/dist/index.css");
975
1053
  var import_dist2 = require("@coze-editor/extension-completion-icons/dist/index.css");
976
1054
 
977
1055
  // src/transformer.ts
978
- var import_text_mapping = require("text-mapping");
1056
+ var import_utils2 = require("@coze-editor/utils");
979
1057
  var transformerCreator = (processor) => (source) => {
980
- const text = processor(new import_text_mapping.Text(source));
1058
+ const text = processor(new import_utils2.Text(source));
981
1059
  return {
982
1060
  code: text.toString(),
983
1061
  mapping: {
@@ -995,7 +1073,7 @@ var SVG_FOLD_CLOSE = '<svg xmlns="http://www.w3.org/2000/svg" width="1em" height
995
1073
  var SVG_FOLD_OPEN = '<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 16 16"><path fill="currentColor" fill-rule="evenodd" d="m7.976 10.072l4.357-4.357l.62.618L8.284 11h-.618L3 6.333l.619-.618z" clip-rule="evenodd"/></svg>';
996
1074
  var preset = [
997
1075
  ...import_preset_universal.default,
998
- (0, import_core.extension)([
1076
+ (0, import_core2.extension)([
999
1077
  basicSetup,
1000
1078
  import_extensions.matchingBrackets,
1001
1079
  // indentGuides(),
@@ -1040,6 +1118,9 @@ var preset = [
1040
1118
  minWidth: "200px",
1041
1119
  margin: "0 2px"
1042
1120
  },
1121
+ ".cm-tooltip.cm-completionInfo": {
1122
+ padding: "6px 9px"
1123
+ },
1043
1124
  ".cm-completionInfo p:last-child": {
1044
1125
  display: "inline-block"
1045
1126
  },
@@ -1081,32 +1162,35 @@ var preset = [
1081
1162
  maxHeight: "360px",
1082
1163
  wordBreak: "break-word"
1083
1164
  },
1165
+ ".cm-tooltip-hover p": {
1166
+ margin: 0
1167
+ },
1084
1168
  ".cm-tooltip-section:not(:first-child)": {
1085
1169
  "border-top": "solid 0.5px #666666ab"
1086
1170
  }
1087
1171
  })
1088
1172
  )
1089
1173
  ]),
1090
- (0, import_core.option)("tabSize", tabSize),
1091
- (0, import_core.option)("height", import_core_plugins.height),
1092
- (0, import_core.option)("minHeight", import_core_plugins.minHeight),
1093
- (0, import_core.option)("maxHeight", import_core_plugins.maxHeight),
1094
- (0, import_core.option)(
1174
+ (0, import_core2.option)("tabSize", tabSize),
1175
+ (0, import_core2.option)("height", import_core_plugins.height),
1176
+ (0, import_core2.option)("minHeight", import_core_plugins.minHeight),
1177
+ (0, import_core2.option)("maxHeight", import_core_plugins.maxHeight),
1178
+ (0, import_core2.option)(
1095
1179
  "scrollBeyondLastLine",
1096
1180
  (use) => use ? (0, import_extensions.scrollBeyondLastLine)() : []
1097
1181
  ),
1098
- (0, import_core.option)("uri", (id) => import_code_language_shared2.uriFacet.of(id)),
1099
- (0, import_core.option)("theme", (themeName) => themes.get(themeName) ?? []),
1100
- (0, import_core.option)("languageId", (id) => [
1182
+ (0, import_core2.option)("uri", (id) => import_code_language_shared2.uriFacet.of(id)),
1183
+ (0, import_core2.option)("theme", (themeName) => themes.get(themeName) ?? []),
1184
+ (0, import_core2.option)("languageId", (id) => [
1101
1185
  import_code_language_shared2.languageIdFacet.of(id),
1102
1186
  languages.getExtension(id)
1103
1187
  ]),
1104
- (0, import_core.option)(
1188
+ (0, import_core2.option)(
1105
1189
  "transformer",
1106
1190
  (transformer) => import_code_language_shared2.transformerFacet.of(transformer)
1107
1191
  ),
1108
- (0, import_core.option)("overlayScrollbar", (enable = true) => enable ? [(0, import_extension_scrollbar.scrollbar)()] : []),
1109
- (0, import_core.option)(
1192
+ (0, import_core2.option)("overlayScrollbar", (enable = true) => enable ? [(0, import_extension_scrollbar.scrollbar)()] : []),
1193
+ (0, import_core2.option)(
1110
1194
  "lineNumbersGutter",
1111
1195
  (enable = true) => enable ? (0, import_view6.lineNumbers)({
1112
1196
  domEventHandlers: {
@@ -1118,7 +1202,7 @@ var preset = [
1118
1202
  }
1119
1203
  }) : []
1120
1204
  ),
1121
- (0, import_core.option)(
1205
+ (0, import_core2.option)(
1122
1206
  "foldGutter",
1123
1207
  (enable = true) => enable ? (0, import_language6.foldGutter)({
1124
1208
  markerDOM(open) {
@@ -1135,11 +1219,11 @@ var preset = [
1135
1219
  }
1136
1220
  }) : []
1137
1221
  ),
1138
- (0, import_core.option)(
1222
+ (0, import_core2.option)(
1139
1223
  "colorizeBrackets",
1140
1224
  (enable = true) => enable ? import_extensions.colorizationBrackets : []
1141
1225
  ),
1142
- (0, import_core.api)("validate", ({ view: view2 }) => () => {
1226
+ (0, import_core2.api)("validate", ({ view: view2 }) => () => {
1143
1227
  view2.dispatch({
1144
1228
  userEvent: LINT_REFRESH_USER_EVENT
1145
1229
  });