@chordproject/editor 1.1.0 → 1.1.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.
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,167 @@ 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);
555
- continue;
556
- }
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 [];
572
- }
573
- if (warning.lineNumber < 1 || warning.lineNumber > view.state.doc.lines) {
574
- return [];
580
+ function createLintExtensions(getLabels = () => defaultLintLabels) {
581
+ const duplicateDirectiveLinter = linter((view) => {
582
+ const diagnostics = [];
583
+ const firstSeenAtLine = /* @__PURE__ */ new Map();
584
+ const labels = getLabels();
585
+ for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
586
+ const line = view.state.doc.line(lineNumber);
587
+ const match = line.text.match(DIRECTIVE_LINE);
588
+ if (!match) continue;
589
+ const canonical = SINGULAR_DIRECTIVES[match[1].toLowerCase()];
590
+ if (!canonical) continue;
591
+ const firstLine = firstSeenAtLine.get(canonical);
592
+ if (firstLine === void 0) {
593
+ firstSeenAtLine.set(canonical, lineNumber);
594
+ continue;
595
+ }
596
+ diagnostics.push({
597
+ from: line.from,
598
+ to: line.from + match[0].length,
599
+ severity: "warning",
600
+ message: labels.duplicateDirective(canonical, firstLine)
601
+ });
575
602
  }
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
- }];
603
+ return diagnostics;
583
604
  });
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));
605
+ const parserWarningLinter = linter((view) => {
606
+ const parser = new ChordProParser();
607
+ parser.parse(view.state.doc.toString());
608
+ const labels = getLabels();
609
+ return parser.warnings.flatMap((warning) => {
610
+ if (warning.code === "empty_sheet") {
611
+ return [];
598
612
  }
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) {
613
+ if (warning.lineNumber < 1 || warning.lineNumber > view.state.doc.lines) {
614
+ return [];
615
+ }
616
+ const line = view.state.doc.line(warning.lineNumber);
617
+ return [{
618
+ from: line.from,
619
+ to: line.to,
620
+ severity: "warning",
621
+ message: labels.parserWarning(warning)
622
+ }];
623
+ });
624
+ }, { delay: 0 });
625
+ const unclosedSectionLinter = linter((view) => {
626
+ const diagnostics = [];
627
+ let openSection = null;
628
+ let sectionHasContent = false;
629
+ for (let lineNumber = 1; lineNumber <= view.state.doc.lines; lineNumber++) {
630
+ const line = view.state.doc.line(lineNumber);
631
+ const match = line.text.match(DIRECTIVE_LINE);
632
+ const directive = match?.[1].toLowerCase();
633
+ if (directive && SECTION_STARTS[directive]) {
634
+ if (openSection) {
635
+ openSection.insertionAt = line.from;
636
+ openSection.insertionTo = line.from;
637
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
638
+ }
639
+ openSection = {
640
+ endDirective: SECTION_STARTS[directive],
641
+ lineFrom: line.from,
642
+ lineTo: line.to,
643
+ insertionAt: line.from,
644
+ insertionTo: line.from
645
+ };
646
+ sectionHasContent = false;
647
+ continue;
648
+ }
649
+ if (directive && SECTION_ENDS.has(directive)) {
650
+ openSection = null;
651
+ sectionHasContent = false;
652
+ continue;
653
+ }
654
+ if (openSection && directive) {
625
655
  openSection.insertionAt = line.from;
626
- openSection.insertionTo = nextDirective ? nextNonBlankDirectiveLine(view, lineNumber) : line.from;
627
- diagnostics.push(sectionDiagnostic(openSection));
656
+ openSection.insertionTo = line.from;
657
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
628
658
  openSection = null;
629
659
  sectionHasContent = false;
660
+ continue;
630
661
  }
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()) {
662
+ if (openSection && !line.text.trim()) {
663
+ const nextDirective = nextNonBlankDirective(view, lineNumber);
664
+ if (sectionHasContent) {
665
+ openSection.insertionAt = line.from;
666
+ openSection.insertionTo = nextDirective ? nextNonBlankDirectiveLine(view, lineNumber) : line.from;
667
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
668
+ openSection = null;
669
+ sectionHasContent = false;
670
+ }
649
671
  continue;
650
672
  }
651
- if (nextLine.text.match(DIRECTIVE_LINE)) {
652
- return nextLine.from;
673
+ if (openSection && lineNumber === view.state.doc.lines) {
674
+ openSection.insertionAt = view.state.doc.length;
675
+ openSection.insertionTo = view.state.doc.length;
676
+ }
677
+ if (openSection) {
678
+ sectionHasContent = true;
653
679
  }
654
- return view2.state.doc.length;
655
680
  }
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 } });
681
+ function nextNonBlankDirective(view2, lineNumber) {
682
+ const directiveLine = nextNonBlankDirectiveLine(view2, lineNumber);
683
+ return directiveLine < view2.state.doc.length ? view2.state.doc.lineAt(directiveLine).text.match(DIRECTIVE_LINE)?.[1].toLowerCase() ?? null : null;
684
+ }
685
+ function nextNonBlankDirectiveLine(view2, lineNumber) {
686
+ for (let nextLineNumber = lineNumber + 1; nextLineNumber <= view2.state.doc.lines; nextLineNumber++) {
687
+ const nextLine = view2.state.doc.line(nextLineNumber);
688
+ if (!nextLine.text.trim()) {
689
+ continue;
690
+ }
691
+ if (nextLine.text.match(DIRECTIVE_LINE)) {
692
+ return nextLine.from;
693
+ }
694
+ return view2.state.doc.length;
676
695
  }
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}`,
696
+ return view2.state.doc.length;
697
+ }
698
+ if (openSection) {
699
+ diagnostics.push(sectionDiagnostic(openSection, getLabels()));
700
+ }
701
+ return diagnostics;
702
+ }, { delay: 0 });
703
+ function sectionDiagnostic(section, labels) {
704
+ return {
705
+ from: section.lineFrom,
706
+ to: section.lineTo,
707
+ severity: "warning",
708
+ message: labels.sectionMessage(section.endDirective),
688
709
  actions: [{
689
- name: `Reemplazar por ${replacement.replacement}`,
690
- apply: (targetView, actionFrom, actionTo) => {
691
- targetView.dispatch({ changes: { from: actionFrom, to: actionTo, insert: `[${replacement.replacement}]` } });
710
+ name: labels.sectionAction(section.endDirective),
711
+ apply: (view, from, to) => {
712
+ const previousCharacter = section.insertionAt > 0 ? view.state.doc.sliceString(section.insertionAt - 1, section.insertionAt) : "";
713
+ const needsLeadingNewline = previousCharacter !== "\n";
714
+ const insertion = `${needsLeadingNewline ? "\n" : ""}{${section.endDirective}}${section.insertionTo < view.state.doc.length ? "\n" : ""}`;
715
+ view.dispatch({ changes: { from: section.insertionAt, to: section.insertionTo, insert: insertion } });
692
716
  }
693
717
  }]
694
- });
718
+ };
695
719
  }
696
- return diagnostics;
697
- });
720
+ const chordNotationLinter = linter((view) => {
721
+ const diagnostics = [];
722
+ const labels = getLabels();
723
+ for (const replacement of findChordNotationReplacements(view.state.doc.toString())) {
724
+ diagnostics.push({
725
+ from: replacement.from,
726
+ to: replacement.to,
727
+ severity: "info",
728
+ message: labels.chordNotationMessage(replacement.replacement, replacement.reason),
729
+ actions: [{
730
+ name: labels.chordNotationAction(replacement.replacement),
731
+ apply: (targetView, actionFrom, actionTo) => {
732
+ targetView.dispatch({ changes: { from: actionFrom, to: actionTo, insert: `[${replacement.replacement}]` } });
733
+ }
734
+ }]
735
+ });
736
+ }
737
+ return diagnostics;
738
+ });
739
+ return [duplicateDirectiveLinter, parserWarningLinter, unclosedSectionLinter, chordNotationLinter];
740
+ }
698
741
 
699
742
  // src/themes/base.ts
700
743
  import { EditorView as EditorView2 } from "@codemirror/view";
@@ -1302,16 +1345,14 @@ function createChordProEditor(options) {
1302
1345
  (update.view.hasFocus ? options.onFocus : options.onBlur)?.();
1303
1346
  }
1304
1347
  });
1348
+ const lintExtensions = createLintExtensions(options.lintLabels ?? (() => defaultLintLabels));
1305
1349
  const state = EditorState2.create({
1306
1350
  doc: options.doc ?? "",
1307
1351
  extensions: [
1308
1352
  basicSetup(),
1309
1353
  chordPro(),
1310
1354
  chordProFolding,
1311
- duplicateDirectiveLinter,
1312
- parserWarningLinter,
1313
- unclosedSectionLinter,
1314
- chordNotationLinter,
1355
+ ...lintExtensions,
1315
1356
  chordProAutocomplete(),
1316
1357
  themeCompartment.of(themeExtension(options.theme ?? "light")),
1317
1358
  search({ top: true, createPanel: (view2) => createSearchPanel(view2, options.searchLabels?.()) }),
@@ -1352,6 +1393,7 @@ function createChordProEditor(options) {
1352
1393
  }
1353
1394
  return normalization.suggestions;
1354
1395
  },
1396
+ refreshLint: () => view.dispatch({}),
1355
1397
  focus: () => view.focus(),
1356
1398
  destroy: () => view.destroy()
1357
1399
  };