@chordproject/editor 1.1.0 → 1.1.2

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
@@ -8,6 +8,19 @@ import { EditorView } from '@codemirror/view';
8
8
  */
9
9
  declare function getChordCompletionPriorities(content: string, cursor: number): Map<string, number>;
10
10
 
11
+ interface LintLabels {
12
+ duplicateDirective: (directive: string, firstLine: number) => string;
13
+ parserWarning: (warning: {
14
+ code: string;
15
+ message: string;
16
+ params: Record<string, string>;
17
+ }) => string;
18
+ sectionMessage: (endDirective: string) => string;
19
+ sectionAction: (endDirective: string) => string;
20
+ chordNotationMessage: (replacement: string, reason: string) => string;
21
+ chordNotationAction: (replacement: string) => string;
22
+ }
23
+
11
24
  interface ChordNotationSuggestion {
12
25
  original: string;
13
26
  replacement: string;
@@ -51,6 +64,7 @@ interface ChordProEditorOptions {
51
64
  onFocus?: () => void;
52
65
  onBlur?: () => void;
53
66
  searchLabels?: () => SearchLabels;
67
+ lintLabels?: () => LintLabels;
54
68
  }
55
69
  interface ChordProEditor {
56
70
  /** Underlying CodeMirror view, for advanced use cases not covered by this API. */
@@ -68,10 +82,12 @@ interface ChordProEditor {
68
82
  getChordNotationSuggestions(): ChordNotationSuggestion[];
69
83
  /** Replaces every suggested chord notation in one undoable CodeMirror transaction. */
70
84
  normalizeChordNotation(): ChordNotationSuggestion[];
85
+ /** Re-runs diagnostics, for example after changing their language. */
86
+ refreshLint(): void;
71
87
  focus(): void;
72
88
  destroy(): void;
73
89
  }
74
90
  /** Creates a standalone ChordPro editor instance. */
75
91
  declare function createChordProEditor(options: ChordProEditorOptions): ChordProEditor;
76
92
 
77
- export { type ChordNotationSuggestion, type ChordProEditor, type ChordProEditorOptions, type SearchLabels, type ThemeName, createChordProEditor, getChordCompletionPriorities, getChordNotationSuggestions, normalizeChordNotationText };
93
+ export { type ChordNotationSuggestion, type ChordProEditor, type ChordProEditorOptions, type LintLabels, type SearchLabels, type ThemeName, createChordProEditor, getChordCompletionPriorities, getChordNotationSuggestions, normalizeChordNotationText };
package/dist/index.js CHANGED
@@ -89,6 +89,35 @@ function chordCompletionSource(context) {
89
89
 
90
90
  // src/autocomplete/snippets.ts
91
91
  import { snippetCompletion } from "@codemirror/autocomplete";
92
+ function sectionShortcut(startDirective, endDirective, label, detail) {
93
+ return {
94
+ label,
95
+ type: "directive",
96
+ detail,
97
+ apply: (view, completion, from, to) => {
98
+ const line = view.state.doc.lineAt(from);
99
+ let closingAt = view.state.doc.length;
100
+ for (let lineNumber = line.number + 1; lineNumber <= view.state.doc.lines; lineNumber++) {
101
+ const nextLine = view.state.doc.line(lineNumber);
102
+ if (!nextLine.text.trim() || /^\s*\{\s*[A-Za-z_][\w]*\b/.test(nextLine.text)) {
103
+ closingAt = nextLine.from;
104
+ break;
105
+ }
106
+ }
107
+ const opening = `{${startDirective}}`;
108
+ const previousCharacter = closingAt > 0 ? view.state.doc.sliceString(closingAt - 1, closingAt) : "";
109
+ const closing = `${previousCharacter && previousCharacter !== "\n" ? "\n" : ""}{${endDirective}}
110
+ `;
111
+ view.dispatch({
112
+ changes: [
113
+ { from, to, insert: opening },
114
+ { from: closingAt, insert: closing }
115
+ ],
116
+ selection: { anchor: from + opening.length }
117
+ });
118
+ }
119
+ };
120
+ }
92
121
  var CHORDPRO_SNIPPETS = [
93
122
  snippetCompletion("{album: ${value}}", { label: "album", type: "directive" }),
94
123
  snippetCompletion("{arranger: ${value}}", { label: "arranger", type: "directive" }),
@@ -115,25 +144,25 @@ var CHORDPRO_SNIPPETS = [
115
144
  label: "chorus",
116
145
  type: "directive"
117
146
  }),
118
- snippetCompletion("{start_of_chorus}", { label: "soc", type: "directive", detail: "start_of_chorus" }),
147
+ sectionShortcut("start_of_chorus", "end_of_chorus", "soc", "start_of_chorus"),
119
148
  snippetCompletion("{end_of_chorus}", { label: "eoc", type: "directive", detail: "end_of_chorus" }),
120
149
  snippetCompletion("{start_of_verse: ${Verse} ${1}}\n${lyrics}\n{end_of_verse}", {
121
150
  label: "verse",
122
151
  type: "directive"
123
152
  }),
124
- snippetCompletion("{start_of_verse}", { label: "sov", type: "directive", detail: "start_of_verse" }),
153
+ sectionShortcut("start_of_verse", "end_of_verse", "sov", "start_of_verse"),
125
154
  snippetCompletion("{end_of_verse}", { label: "eov", type: "directive", detail: "end_of_verse" }),
126
155
  snippetCompletion("{start_of_bridge: ${Bridge}}\n${lyrics}\n{end_of_bridge}", {
127
156
  label: "bridge",
128
157
  type: "directive"
129
158
  }),
130
- snippetCompletion("{start_of_bridge}", { label: "sob", type: "directive", detail: "start_of_bridge" }),
159
+ sectionShortcut("start_of_bridge", "end_of_bridge", "sob", "start_of_bridge"),
131
160
  snippetCompletion("{end_of_bridge}", { label: "eob", type: "directive", detail: "end_of_bridge" }),
132
161
  snippetCompletion(
133
162
  "{start_of_tab}\ne|-${1}--------------------------------|\nB|----------------------------------|\nG|----------------------------------|\nD|----------------------------------|\nA|----------------------------------|\nE|----------------------------------|\n{end_of_tab}",
134
163
  { label: "tab", type: "directive" }
135
164
  ),
136
- snippetCompletion("{start_of_tab}", { label: "sot", type: "directive", detail: "start_of_tab" }),
165
+ sectionShortcut("start_of_tab", "end_of_tab", "sot", "start_of_tab"),
137
166
  snippetCompletion("{end_of_tab}", { label: "eot", type: "directive", detail: "end_of_tab" }),
138
167
  snippetCompletion("{define: ${Am} base-fret ${1} frets ${0 0 0 0 0 0} fingers ${0 0 0 0 0 0}}", {
139
168
  label: "define",
@@ -514,6 +543,14 @@ function normalizeChordNotationText(content) {
514
543
  }
515
544
 
516
545
  // src/linting.ts
546
+ var defaultLintLabels = {
547
+ duplicateDirective: (directive, firstLine) => `"${directive}" was already defined on line ${firstLine}; this value replaces it.`,
548
+ parserWarning: (warning) => warning.message,
549
+ sectionMessage: (endDirective) => `This section must be closed with {${endDirective}}.`,
550
+ sectionAction: (endDirective) => `Insert {${endDirective}}`,
551
+ chordNotationMessage: (replacement, reason) => `Use "${replacement}". ${reason}`,
552
+ chordNotationAction: (replacement) => `Replace with ${replacement}`
553
+ };
517
554
  var SINGULAR_DIRECTIVES = {
518
555
  title: "title",
519
556
  t: "title",
@@ -540,161 +577,184 @@ var SECTION_STARTS = {
540
577
  sot: "end_of_tab"
541
578
  };
542
579
  var SECTION_ENDS = /* @__PURE__ */ new Set(["end_of_chorus", "eoc", "end_of_verse", "eov", "end_of_bridge", "eob", "end_of_tab", "eot"]);
543
- var duplicateDirectiveLinter = linter((view) => {
544
- const diagnostics = [];
545
- const firstSeenAtLine = /* @__PURE__ */ new Map();
546
- for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
547
- const line = view.state.doc.line(lineNumber);
548
- const match = line.text.match(DIRECTIVE_LINE);
549
- if (!match) continue;
550
- const canonical = SINGULAR_DIRECTIVES[match[1].toLowerCase()];
551
- if (!canonical) continue;
552
- const firstLine = firstSeenAtLine.get(canonical);
553
- if (firstLine === void 0) {
554
- firstSeenAtLine.set(canonical, lineNumber);
580
+ function hasExplicitSectionEndAhead(view, lineNumber, endDirective) {
581
+ for (let nextLineNumber = lineNumber + 1; nextLineNumber <= view.state.doc.lines; nextLineNumber++) {
582
+ const nextLine = view.state.doc.line(nextLineNumber);
583
+ if (!nextLine.text.trim()) {
555
584
  continue;
556
585
  }
557
- diagnostics.push({
558
- from: line.from,
559
- to: line.from + match[0].length,
560
- severity: "warning",
561
- message: `"${canonical}" ya se defini\xF3 en la l\xEDnea ${firstLine}; este valor la reemplaza.`
562
- });
563
- }
564
- return diagnostics;
565
- });
566
- var parserWarningLinter = linter((view) => {
567
- const parser = new ChordProParser();
568
- parser.parse(view.state.doc.toString());
569
- return parser.warnings.flatMap((warning) => {
570
- if (warning.code === "empty_sheet") {
571
- return [];
586
+ const directive = nextLine.text.match(DIRECTIVE_LINE)?.[1].toLowerCase();
587
+ if (!directive) {
588
+ continue;
572
589
  }
573
- if (warning.lineNumber < 1 || warning.lineNumber > view.state.doc.lines) {
574
- return [];
590
+ return directive === endDirective || endDirective === "end_of_chorus" && directive === "eoc" || endDirective === "end_of_verse" && directive === "eov" || endDirective === "end_of_bridge" && directive === "eob" || endDirective === "end_of_tab" && directive === "eot";
591
+ }
592
+ return false;
593
+ }
594
+ function createLintExtensions(getLabels = () => defaultLintLabels) {
595
+ const duplicateDirectiveLinter = linter((view) => {
596
+ const diagnostics = [];
597
+ const firstSeenAtLine = /* @__PURE__ */ new Map();
598
+ const labels = getLabels();
599
+ for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
600
+ const line = view.state.doc.line(lineNumber);
601
+ const match = line.text.match(DIRECTIVE_LINE);
602
+ if (!match) continue;
603
+ const canonical = SINGULAR_DIRECTIVES[match[1].toLowerCase()];
604
+ if (!canonical) continue;
605
+ const firstLine = firstSeenAtLine.get(canonical);
606
+ if (firstLine === void 0) {
607
+ firstSeenAtLine.set(canonical, lineNumber);
608
+ continue;
609
+ }
610
+ diagnostics.push({
611
+ from: line.from,
612
+ to: line.from + match[0].length,
613
+ severity: "warning",
614
+ message: labels.duplicateDirective(canonical, firstLine)
615
+ });
575
616
  }
576
- const line = view.state.doc.line(warning.lineNumber);
577
- return [{
578
- from: line.from,
579
- to: line.to,
580
- severity: "warning",
581
- message: warning.message
582
- }];
617
+ return diagnostics;
583
618
  });
584
- }, { delay: 0 });
585
- var unclosedSectionLinter = linter((view) => {
586
- const diagnostics = [];
587
- let openSection = null;
588
- let sectionHasContent = false;
589
- for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
590
- const line = view.state.doc.line(lineNumber);
591
- const match = line.text.match(DIRECTIVE_LINE);
592
- const directive = match?.[1].toLowerCase();
593
- if (directive && SECTION_STARTS[directive]) {
594
- if (openSection) {
595
- openSection.insertionAt = line.from;
596
- openSection.insertionTo = line.from;
597
- diagnostics.push(sectionDiagnostic(openSection));
619
+ const parserWarningLinter = linter((view) => {
620
+ const parser = new ChordProParser();
621
+ parser.parse(view.state.doc.toString());
622
+ const labels = getLabels();
623
+ return parser.warnings.flatMap((warning) => {
624
+ if (warning.code === "empty_sheet") {
625
+ return [];
598
626
  }
599
- openSection = {
600
- endDirective: SECTION_STARTS[directive],
601
- lineFrom: line.from,
602
- lineTo: line.to,
603
- insertionAt: line.from,
604
- insertionTo: line.from
605
- };
606
- sectionHasContent = false;
607
- continue;
608
- }
609
- if (directive && SECTION_ENDS.has(directive)) {
610
- openSection = null;
611
- sectionHasContent = false;
612
- continue;
613
- }
614
- if (openSection && directive) {
615
- openSection.insertionAt = line.from;
616
- openSection.insertionTo = line.from;
617
- diagnostics.push(sectionDiagnostic(openSection));
618
- openSection = null;
619
- sectionHasContent = false;
620
- continue;
621
- }
622
- if (openSection && !line.text.trim()) {
623
- const nextDirective = nextNonBlankDirective(view, lineNumber);
624
- if (sectionHasContent) {
627
+ if (warning.lineNumber < 1 || warning.lineNumber > view.state.doc.lines) {
628
+ return [];
629
+ }
630
+ const line = view.state.doc.line(warning.lineNumber);
631
+ return [{
632
+ from: line.from,
633
+ to: line.to,
634
+ severity: "warning",
635
+ message: labels.parserWarning(warning)
636
+ }];
637
+ });
638
+ }, { delay: 0 });
639
+ const unclosedSectionLinter = linter((view) => {
640
+ const diagnostics = [];
641
+ let openSection = null;
642
+ let sectionHasContent = false;
643
+ for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
644
+ const line = view.state.doc.line(lineNumber);
645
+ const match = line.text.match(DIRECTIVE_LINE);
646
+ const directive = match?.[1].toLowerCase();
647
+ if (directive && SECTION_STARTS[directive]) {
648
+ if (openSection) {
649
+ openSection.insertionAt = line.from;
650
+ openSection.insertionTo = line.from;
651
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
652
+ }
653
+ openSection = {
654
+ endDirective: SECTION_STARTS[directive],
655
+ lineFrom: line.from,
656
+ lineTo: line.to,
657
+ insertionAt: line.from,
658
+ insertionTo: line.from
659
+ };
660
+ sectionHasContent = false;
661
+ continue;
662
+ }
663
+ if (directive && SECTION_ENDS.has(directive)) {
664
+ openSection = null;
665
+ sectionHasContent = false;
666
+ continue;
667
+ }
668
+ if (openSection && directive) {
625
669
  openSection.insertionAt = line.from;
626
- openSection.insertionTo = nextDirective ? nextNonBlankDirectiveLine(view, lineNumber) : line.from;
627
- diagnostics.push(sectionDiagnostic(openSection));
670
+ openSection.insertionTo = line.from;
671
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
628
672
  openSection = null;
629
673
  sectionHasContent = false;
674
+ continue;
630
675
  }
631
- continue;
632
- }
633
- if (openSection && lineNumber === view.state.doc.lines) {
634
- openSection.insertionAt = view.state.doc.length;
635
- openSection.insertionTo = view.state.doc.length;
636
- }
637
- if (openSection) {
638
- sectionHasContent = true;
639
- }
640
- }
641
- function nextNonBlankDirective(view2, lineNumber) {
642
- const directiveLine = nextNonBlankDirectiveLine(view2, lineNumber);
643
- return directiveLine < view2.state.doc.length ? view2.state.doc.lineAt(directiveLine).text.match(DIRECTIVE_LINE)?.[1].toLowerCase() ?? null : null;
644
- }
645
- function nextNonBlankDirectiveLine(view2, lineNumber) {
646
- for (let nextLineNumber = lineNumber + 1; nextLineNumber <= view2.state.doc.lines; nextLineNumber++) {
647
- const nextLine = view2.state.doc.line(nextLineNumber);
648
- if (!nextLine.text.trim()) {
676
+ if (openSection && !line.text.trim()) {
677
+ if (hasExplicitSectionEndAhead(view, lineNumber, openSection.endDirective)) {
678
+ continue;
679
+ }
680
+ const nextDirective = nextNonBlankDirective(view, lineNumber);
681
+ if (sectionHasContent) {
682
+ openSection.insertionAt = line.from;
683
+ openSection.insertionTo = nextDirective ? nextNonBlankDirectiveLine(view, lineNumber) : line.from;
684
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
685
+ openSection = null;
686
+ sectionHasContent = false;
687
+ }
649
688
  continue;
650
689
  }
651
- if (nextLine.text.match(DIRECTIVE_LINE)) {
652
- return nextLine.from;
690
+ if (openSection && lineNumber === view.state.doc.lines) {
691
+ openSection.insertionAt = view.state.doc.length;
692
+ openSection.insertionTo = view.state.doc.length;
693
+ }
694
+ if (openSection) {
695
+ sectionHasContent = true;
653
696
  }
654
- return view2.state.doc.length;
655
697
  }
656
- return view2.state.doc.length;
657
- }
658
- if (openSection) {
659
- diagnostics.push(sectionDiagnostic(openSection));
660
- }
661
- return diagnostics;
662
- }, { delay: 0 });
663
- function sectionDiagnostic(section) {
664
- return {
665
- from: section.lineFrom,
666
- to: section.lineTo,
667
- severity: "warning",
668
- message: `Falta cerrar esta secci\xF3n con {${section.endDirective}}.`,
669
- actions: [{
670
- name: `Insertar {${section.endDirective}}`,
671
- apply: (view, from, to) => {
672
- const previousCharacter = section.insertionAt > 0 ? view.state.doc.sliceString(section.insertionAt - 1, section.insertionAt) : "";
673
- const needsLeadingNewline = previousCharacter !== "\n";
674
- const insertion = `${needsLeadingNewline ? "\n" : ""}{${section.endDirective}}${section.insertionTo < view.state.doc.length ? "\n" : ""}`;
675
- view.dispatch({ changes: { from: section.insertionAt, to: section.insertionTo, insert: insertion } });
698
+ function nextNonBlankDirective(view2, lineNumber) {
699
+ const directiveLine = nextNonBlankDirectiveLine(view2, lineNumber);
700
+ return directiveLine < view2.state.doc.length ? view2.state.doc.lineAt(directiveLine).text.match(DIRECTIVE_LINE)?.[1].toLowerCase() ?? null : null;
701
+ }
702
+ function nextNonBlankDirectiveLine(view2, lineNumber) {
703
+ for (let nextLineNumber = lineNumber + 1; nextLineNumber <= view2.state.doc.lines; nextLineNumber++) {
704
+ const nextLine = view2.state.doc.line(nextLineNumber);
705
+ if (!nextLine.text.trim()) {
706
+ continue;
707
+ }
708
+ if (nextLine.text.match(DIRECTIVE_LINE)) {
709
+ return nextLine.from;
710
+ }
711
+ return view2.state.doc.length;
676
712
  }
677
- }]
678
- };
679
- }
680
- var chordNotationLinter = linter((view) => {
681
- const diagnostics = [];
682
- for (const replacement of findChordNotationReplacements(view.state.doc.toString())) {
683
- diagnostics.push({
684
- from: replacement.from,
685
- to: replacement.to,
686
- severity: "info",
687
- message: `Usa "${replacement.replacement}". ${replacement.reason}`,
713
+ return view2.state.doc.length;
714
+ }
715
+ if (openSection) {
716
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
717
+ }
718
+ return diagnostics;
719
+ }, { delay: 0 });
720
+ function sectionDiagnostic(section, labels) {
721
+ return {
722
+ from: section.lineFrom,
723
+ to: section.lineTo,
724
+ severity: "warning",
725
+ message: labels.sectionMessage(section.endDirective),
688
726
  actions: [{
689
- name: `Reemplazar por ${replacement.replacement}`,
690
- apply: (targetView, actionFrom, actionTo) => {
691
- targetView.dispatch({ changes: { from: actionFrom, to: actionTo, insert: `[${replacement.replacement}]` } });
727
+ name: labels.sectionAction(section.endDirective),
728
+ apply: (view, from, to) => {
729
+ const previousCharacter = section.insertionAt > 0 ? view.state.doc.sliceString(section.insertionAt - 1, section.insertionAt) : "";
730
+ const needsLeadingNewline = previousCharacter !== "\n";
731
+ const insertion = `${needsLeadingNewline ? "\n" : ""}{${section.endDirective}}${section.insertionTo < view.state.doc.length ? "\n" : ""}`;
732
+ view.dispatch({ changes: { from: section.insertionAt, to: section.insertionTo, insert: insertion } });
692
733
  }
693
734
  }]
694
- });
735
+ };
695
736
  }
696
- return diagnostics;
697
- });
737
+ const chordNotationLinter = linter((view) => {
738
+ const diagnostics = [];
739
+ const labels = getLabels();
740
+ for (const replacement of findChordNotationReplacements(view.state.doc.toString())) {
741
+ diagnostics.push({
742
+ from: replacement.from,
743
+ to: replacement.to,
744
+ severity: "info",
745
+ message: labels.chordNotationMessage(replacement.replacement, replacement.reason),
746
+ actions: [{
747
+ name: labels.chordNotationAction(replacement.replacement),
748
+ apply: (targetView, actionFrom, actionTo) => {
749
+ targetView.dispatch({ changes: { from: actionFrom, to: actionTo, insert: `[${replacement.replacement}]` } });
750
+ }
751
+ }]
752
+ });
753
+ }
754
+ return diagnostics;
755
+ });
756
+ return [duplicateDirectiveLinter, parserWarningLinter, unclosedSectionLinter, chordNotationLinter];
757
+ }
698
758
 
699
759
  // src/themes/base.ts
700
760
  import { EditorView as EditorView2 } from "@codemirror/view";
@@ -1302,16 +1362,14 @@ function createChordProEditor(options) {
1302
1362
  (update.view.hasFocus ? options.onFocus : options.onBlur)?.();
1303
1363
  }
1304
1364
  });
1365
+ const lintExtensions = createLintExtensions(options.lintLabels ?? (() => defaultLintLabels));
1305
1366
  const state = EditorState2.create({
1306
1367
  doc: options.doc ?? "",
1307
1368
  extensions: [
1308
1369
  basicSetup(),
1309
1370
  chordPro(),
1310
1371
  chordProFolding,
1311
- duplicateDirectiveLinter,
1312
- parserWarningLinter,
1313
- unclosedSectionLinter,
1314
- chordNotationLinter,
1372
+ ...lintExtensions,
1315
1373
  chordProAutocomplete(),
1316
1374
  themeCompartment.of(themeExtension(options.theme ?? "light")),
1317
1375
  search({ top: true, createPanel: (view2) => createSearchPanel(view2, options.searchLabels?.()) }),
@@ -1352,6 +1410,7 @@ function createChordProEditor(options) {
1352
1410
  }
1353
1411
  return normalization.suggestions;
1354
1412
  },
1413
+ refreshLint: () => view.dispatch({}),
1355
1414
  focus: () => view.focus(),
1356
1415
  destroy: () => view.destroy()
1357
1416
  };