From 4dc3065ce4d3712b9db142e6b987dfc857e96ea0 Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Sun, 21 Jan 2024 20:32:22 +0100 Subject: [PATCH] Initial add --- .github/workflows/dispatch.yml | 16 +++++++++++ .gitignore | 7 +++++ .npmignore | 6 ++++ LICENSE | 21 ++++++++++++++ README.md | 37 ++++++++++++++++++++++++ package.json | 42 +++++++++++++++++++++++++++ src/README.md | 26 +++++++++++++++++ src/yaml.ts | 52 ++++++++++++++++++++++++++++++++++ 8 files changed, 207 insertions(+) create mode 100644 .github/workflows/dispatch.yml create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 package.json create mode 100644 src/README.md create mode 100644 src/yaml.ts diff --git a/.github/workflows/dispatch.yml b/.github/workflows/dispatch.yml new file mode 100644 index 0000000..d050072 --- /dev/null +++ b/.github/workflows/dispatch.yml @@ -0,0 +1,16 @@ +name: Trigger CI +on: push + +jobs: + build: + name: Dispatch to main repo + runs-on: ubuntu-latest + steps: + - name: Emit repository_dispatch + uses: mvasigh/dispatch-action@main + with: + # You should create a personal access token and store it in your repository + token: ${{ secrets.DISPATCH_AUTH }} + repo: dev + owner: codemirror + event_type: push diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eebad72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +/node_modules +package-lock.json +/dist +/test/*.js +/test/*.d.ts +/test/*.d.ts.map +.tern-* diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..62c7fc6 --- /dev/null +++ b/.npmignore @@ -0,0 +1,6 @@ +/src +/test +/node_modules +.tern-* +rollup.config.js +tsconfig.json diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..463ee6f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (C) 2024 by Marijn Haverbeke and others + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e450be6 --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ + + +# @codemirror/lang-yaml [![NPM version](https://img.shields.io/npm/v/@codemirror/lang-yaml.svg)](https://www.npmjs.org/package/@codemirror/lang-yaml) + +[ [**WEBSITE**](https://codemirror.net/) | [**ISSUES**](https://github.com/codemirror/dev/issues) | [**FORUM**](https://discuss.codemirror.net/c/next/) | [**CHANGELOG**](https://github.com/codemirror/lang-yaml/blob/main/CHANGELOG.md) ] + +This package implements YAML language support for the +[CodeMirror](https://codemirror.net/) code editor. + +The [project page](https://codemirror.net/) has more information, a +number of [examples](https://codemirror.net/examples/) and the +[documentation](https://codemirror.net/docs/). + +This code is released under an +[MIT license](https://github.com/codemirror/lang-yaml/tree/main/LICENSE). + +We aim to be an inclusive, welcoming community. To make that explicit, +we have a [code of +conduct](http://contributor-covenant.org/version/1/1/0/) that applies +to communication around the project. + +## API Reference + +
+
+ yaml() → LanguageSupport
+ +

Language support for YAML.

+
+
+ yamlLanguage: LRLanguage
+ +

A language provider based on the Lezer YAML +parser, extended with +highlighting and indentation information.

+
+
diff --git a/package.json b/package.json new file mode 100644 index 0000000..213ab27 --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "@codemirror/lang-yaml", + "version": "6.2.1", + "description": "YAML language support for the CodeMirror code editor", + "scripts": { + "test": "cm-runtests", + "prepare": "cm-buildhelper src/yaml.ts" + }, + "keywords": [ + "editor", + "code" + ], + "author": { + "name": "Marijn Haverbeke", + "email": "marijn@haverbeke.berlin", + "url": "http://marijnhaverbeke.nl" + }, + "type": "module", + "main": "dist/index.cjs", + "exports": { + "import": "./dist/index.js", + "require": "./dist/index.cjs" + }, + "types": "dist/index.d.ts", + "module": "dist/index.js", + "sideEffects": false, + "license": "MIT", + "dependencies": { + "@codemirror/autocomplete": "^6.0.0", + "@codemirror/language": "^6.0.0", + "@codemirror/state": "^6.0.0", + "@lezer/common": "^1.0.2", + "@lezer/yaml": "^1.0.0" + }, + "devDependencies": { + "@codemirror/buildhelper": "^1.0.0" + }, + "repository": { + "type": "git", + "url": "https://github.com/codemirror/lang-yaml.git" + } +} diff --git a/src/README.md b/src/README.md new file mode 100644 index 0000000..c8ef32a --- /dev/null +++ b/src/README.md @@ -0,0 +1,26 @@ + + +# @codemirror/lang-yaml [![NPM version](https://img.shields.io/npm/v/@codemirror/lang-yaml.svg)](https://www.npmjs.org/package/@codemirror/lang-yaml) + +[ [**WEBSITE**](https://codemirror.net/) | [**ISSUES**](https://github.com/codemirror/dev/issues) | [**FORUM**](https://discuss.codemirror.net/c/next/) | [**CHANGELOG**](https://github.com/codemirror/lang-yaml/blob/main/CHANGELOG.md) ] + +This package implements YAML language support for the +[CodeMirror](https://codemirror.net/) code editor. + +The [project page](https://codemirror.net/) has more information, a +number of [examples](https://codemirror.net/examples/) and the +[documentation](https://codemirror.net/docs/). + +This code is released under an +[MIT license](https://github.com/codemirror/lang-yaml/tree/main/LICENSE). + +We aim to be an inclusive, welcoming community. To make that explicit, +we have a [code of +conduct](http://contributor-covenant.org/version/1/1/0/) that applies +to communication around the project. + +## API Reference + +@yaml + +@yamlLanguage diff --git a/src/yaml.ts b/src/yaml.ts new file mode 100644 index 0000000..d7f02c5 --- /dev/null +++ b/src/yaml.ts @@ -0,0 +1,52 @@ +import {parser} from "@lezer/yaml" +import {LRLanguage, delimitedIndent, indentNodeProp, + foldNodeProp, foldInside, LanguageSupport} from "@codemirror/language" +import {SyntaxNode} from "@lezer/common" + +/// A language provider based on the [Lezer YAML +/// parser](https://github.com/lezer-parser/yaml), extended with +/// highlighting and indentation information. +export const yamlLanguage = LRLanguage.define({ + name: "yaml", + parser: parser.configure({ + props: [ + indentNodeProp.add({ + Stream: cx => { + for (let before = cx.node.resolve(cx.pos, -1) as SyntaxNode | null; + before && before.to >= cx.pos; before = before.parent) { + if (before.name == "BlockLiteralContent" && before.from < before.to) + return cx.baseIndentFor(before) + if (before.name == "BlockLiteral") + return cx.baseIndentFor(before) + cx.unit + if (before.name == "BlockSequence" || before.name == "BlockMapping") + return cx.column(before.from, 1) + if (before.name == "QuotedLiteral") + return null + if (before.name == "Literal") { + let col = cx.column(before.from, 1) + if (col == cx.lineIndent(before.from, 1)) return col // Start on own line + if (before.to > cx.pos) return null + } + } + return null + }, + FlowMapping: delimitedIndent({closing: "}"}), + FlowSequence: delimitedIndent({closing: "]"}), + }), + foldNodeProp.add({ + "FlowMapping FlowSequence": foldInside, + "BlockSequence BlockMapping BlockLiteral": + (node, state) => ({from: state.doc.lineAt(node.from).to, to: node.to}) + }) + ] + }), + languageData: { + commentTokens: {line: "#"}, + indentOnInput: /^\s*[\]\}]$/, + } +}) + +/// Language support for YAML. +export function yaml() { + return new LanguageSupport(yamlLanguage) +}