Skip to content

Commit

Permalink
Merge pull request #4 from chordbook/linter
Browse files Browse the repository at this point in the history
Add a basic syntax linter
  • Loading branch information
bkeepers authored Mar 22, 2024
2 parents 551ecf2 + b1b3e6d commit 7df1cd7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A web-based editor for editing chord sheets in the [ChordPro](https://chordpro.o
* ✅ ChordPro Syntax Highlighting
* ✅ Chord autocomplete - type "[" and you will see autocomplete of previously used chords
* ✅ Snippets - type "title", "start_of…", "tab" or any other ChordPro directive
* ✅ Error checking - Shows syntax errors in the editor

## Installation

Expand Down
2 changes: 2 additions & 0 deletions demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ This is the [G]chorus
# ✅ Syntax Highlighting
# ✅ Chord autocomplete - type "[" and you will see autocomplete of previously used chords
# ✅ Snippets - type "title", "start_of…", "tab" or any other ChordPro directive
# ✅ Error checking - Shows syntax errors in the editor
{
`

const el = document.querySelector('#editor')!
Expand Down
5 changes: 3 additions & 2 deletions src/extensions/baseTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { EditorView } from "@codemirror/view"

export default EditorView.baseTheme({
"&": {
minHeight: '100%'
height: '100%',
},
".cm-editor": {
minHeight: '100%'
height: '100%',
overflow: 'auto'
},
".cm-scroller": {
flex: '1'
Expand Down
11 changes: 8 additions & 3 deletions src/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import { defaultKeymap, history, historyKeymap } from '@codemirror/commands'
import { bracketMatching } from '@codemirror/language'
import { lintKeymap } from '@codemirror/lint'
import { highlightSelectionMatches, searchKeymap } from '@codemirror/search'
import { drawSelection, highlightActiveLine, highlightActiveLineGutter, highlightSpecialChars, keymap, lineNumbers } from '@codemirror/view'
import { drawSelection, highlightActiveLine, highlightActiveLineGutter, highlightSpecialChars, scrollPastEnd, keymap, lineNumbers } from '@codemirror/view'
import { ChordPro } from '@chordbook/codemirror-lang-chordpro'
import { oneDark } from '@codemirror/theme-one-dark'
import baseTheme from './baseTheme'
import linter from './linter'
import { lintGutter } from "@codemirror/lint"

export default [
baseTheme,
oneDark,
ChordPro(),
linter,
lintGutter(),
lineNumbers(),
highlightActiveLineGutter(),
highlightSpecialChars(),
Expand All @@ -39,6 +43,7 @@ export default [
if (completionStatus(e.state)) return acceptCompletion(e);
return false
},
}
])
},
]),
scrollPastEnd(),
]
28 changes: 28 additions & 0 deletions src/extensions/linter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { EditorView } from "@codemirror/view";
import { syntaxTree } from "@codemirror/language";
import { linter, Diagnostic } from "@codemirror/lint";

// A basic syntax linter that will highlight syntax errors in the editor.
// Unfortuantely, codemirror/lezer does not provide a way to get the error message,
// so we just return a generic "Syntax Error" message for now.
// See: https://discuss.codemirror.net/t/how-should-i-get-the-error-message/6327
function lintSyntax(view: EditorView): readonly Diagnostic[] {
const diagnostics: Diagnostic[] = [];

syntaxTree(view.state).iterate({
enter(node) {
if (node.type.isError) {
diagnostics.push({
from: node.from,
to: node.to,
severity: "error",
message: "Syntax Error",
});
}
},
});

return diagnostics;
}

export default linter(lintSyntax)

0 comments on commit 7df1cd7

Please sign in to comment.