Compare commits

...

65 commits
v3.2.0 ... main

Author SHA1 Message Date
Remco Haszing
4ab8e671e6
4.0.0-alpha.1 2022-02-15 14:04:19 +01:00
Remco Haszing
0e42b40a65
Merge pull request #151 from remcohaszing/update-dependencies
Update dependencies
2022-02-15 14:02:59 +01:00
Remco Haszing
45d8d1ae57
Update dependencies
This adds support for marker tags. This is used for unused anchors.
2022-02-15 11:24:56 +01:00
Remco Haszing
629aaaa2a8
Merge pull request #147 from remcohaszing/update-dependencies
Update dependencies
2022-02-15 11:13:47 +01:00
Remco Haszing
5efe47e449
Fix type issue 2022-01-18 10:19:41 +01:00
Remco Haszing
e28f7fec9f
Update dependencies
Most notably this updates yaml-language-server to 1.3.0.
2022-01-17 17:01:12 +01:00
Remco Haszing
c4ae02d2ef
Merge pull request #145 from remcohaszing/ci-test-example-builds
Build examples in CI
2021-11-27 15:22:46 +01:00
Remco Haszing
18cfa0a008
Build examples in CI
This doesn’t verify they actually work, but at least this verified they
can be built.
2021-11-25 21:29:34 +01:00
Remco Haszing
d35b72e1fe
Merge pull request #144 from domsew/fixExample
fix monaco-editor-webpack-plugin
2021-11-24 18:40:50 +01:00
Dominik Seweryn
0518312cac fix monaco-editor-webpack-plugin
after library output change
2021-11-24 18:37:48 +01:00
Remco Haszing
326a253813
Merge pull request #136 from remcohaszing/flatten-output-paths
Flatten output paths
2021-11-23 15:36:00 +01:00
Remco Haszing
928d33cdc3
Resolve merge isue 2021-11-23 15:12:20 +01:00
Remco Haszing
ef35d1be2b
Merge branch 'main' into flatten-output-paths 2021-11-23 15:10:34 +01:00
Remco Haszing
a959e028ce
Merge pull request #140 from remcohaszing/esm-import-extensions
Use extensions for directly importing files
2021-11-23 15:07:08 +01:00
Remco Haszing
38e61fc7be
Remove unused eslint-disable comment 2021-11-23 15:00:08 +01:00
Remco Haszing
f42c5e97e0
Merge pull request #139 from remcohaszing/fix-type-issue
Fix a small type issue
2021-11-23 14:51:45 +01:00
Remco Haszing
af88581fbb
Merge pull request #141 from remcohaszing/support-document-symbol-detail
Add support for document symbol details
2021-11-23 14:51:32 +01:00
Remco Haszing
a18a75d137
Merge pull request #142 from remcohaszing/remove-prefix-support
Remove support for the prefix option
2021-11-23 14:51:19 +01:00
Remco Haszing
9a2be95116
Merge pull request #135 from remcohaszing/contributing-docs
Enhance contribution instructions
2021-11-23 14:50:18 +01:00
Remco Haszing
bd0ae90018
Remove support for the prefix option
If it turns out people use this, we can support a custom schema request
service implementation instead.

Also error responses for schema requests are now handled.
2021-11-20 12:39:43 +01:00
Remco Haszing
934eb8d1e3
Fix ESLint issues 2021-11-20 12:23:11 +01:00
Remco Haszing
3a873e083c
Add support for document symbol details
This is set to the raw value for primitives by the YAML language service.

The demo now uses this value as the title attribute on the breadcrumbs.

Closes #137
2021-11-20 12:21:51 +01:00
Remco Haszing
ccfbbbe9d8
Use extensions for directly importing files
For better ESM compatibility, this adds a file extension for nested
package files.

This also removes the nested special handling of
`vscode-languageserver-types` and `vscode-languageserver-document`
imports. This is unnecessary, because they use the `"module"` field in
`package.json`.
2021-11-20 12:05:44 +01:00
Remco Haszing
5bb55b6e96
Fix a small type issue
The language server completion kind and monaco completion kind are
compatible by coincidence, not by definition.
2021-11-19 16:21:07 +01:00
Remco Haszing
90d895a214
Flatten output paths
`lib/esm/monaco.contribution.js` → `index.js`
`lib/esm/yaml.worker.js` → `yaml.worker.js`

Closes #132
2021-11-15 18:33:26 +01:00
Remco Haszing
72fc069c6d
Enhance contribution instructions
For both the project and all exmaples the prerequisites, setup steps,
and running instructions have been added.
2021-11-15 18:09:40 +01:00
Remco Haszing
545e6a6bc4
Merge pull request #126 from remcohaszing/update-dependencies
Update dependencies
2021-11-15 17:57:36 +01:00
Remco Haszing
f44c4e8e34
Update dependencies 2021-11-15 17:54:04 +01:00
Remco Haszing
56372c1c2a
Merge branch 'main' into update-dependencies 2021-11-15 17:31:25 +01:00
Remco Haszing
c008952c87
Merge pull request #116 from remcohaszing/vite-example
Add minimal vite example
2021-11-15 17:29:29 +01:00
Remco Haszing
2a80dc8913
Merge branch 'main' into vite-example 2021-11-15 17:09:35 +01:00
Remco Haszing
e0d2f4ade6
Merge branch 'main' into vite-example 2021-11-15 17:05:21 +01:00
Remco Haszing
f2948443e1
Merge pull request #128 from domsew/monaco-editor-webpack-plugin-example
Add monaco-editor-webpack-plugin-example
2021-11-15 17:03:45 +01:00
Dominik Seweryn
ed001fdc1c Add monaco-editor-webpack-plugin-example 2021-11-11 17:20:19 +01:00
Remco Haszing
786cdb5dbb
Implement definition provider 2021-11-10 22:28:06 +01:00
Remco Haszing
edf8623b78
Update dependencies 2021-11-10 21:59:05 +01:00
Remco Haszing
5507d53dee
Merge branch 'main' into update-dependencies 2021-11-10 21:40:56 +01:00
Remco Haszing
4f06c90e34
4.0.0-alpha.0 2021-11-09 21:56:55 +01:00
Remco Haszing
d15459091c
Disable automated publishing
It’s not working currently. It’s disabled while we’re in the pre-release
stage.
2021-11-09 21:53:29 +01:00
Remco Haszing
44e5c7b6b5
Merge pull request #134 from Nivl/ml/update-monaco-0.30
Update monaco to 0.30
2021-11-09 21:52:19 +01:00
Melvin Laplanche
f3decc20dd
Update monaco to 0.30 2021-11-09 10:43:15 -08:00
Remco Haszing
7fd3bc973b
Merge pull request #133 from remcohaszing/faq
Add FAQ entry in readme
2021-11-09 08:55:39 +01:00
Remco Haszing
eb0ce6dd0a
Merge pull request #130 from remcohaszing/specify-lockfile-version
Use lockfile version 3
2021-11-09 08:55:23 +01:00
Remco Haszing
fc405ef1b2
Add FAQ entry in readme
Closes #118
Closes #119
Closes #121
2021-11-07 15:00:01 +01:00
Remco Haszing
1b5fa06cc9
Use lockfile version 3
This saves half of the `package-lock.json` file.
2021-11-07 13:58:42 +01:00
Remco Haszing
c5d83c4543
Remove webpack buffer fallback
This was needed for `yaml-languagerserver-parser`, which is no longer
used.
2021-10-18 21:16:18 +02:00
Remco Haszing
90a7bcda0a
Fix ESLint issue 2021-10-17 13:23:08 +02:00
Remco Haszing
f7fce45714
Sync package-lock.json 2021-10-17 13:21:36 +02:00
Remco Haszing
554699887d
Update dependencies
- Both `js-yaml` and `yaml-languageserver-parser` have been replaced with
  `yaml`.
- `jsonc-parser` has been externalized.
- All defaults are now specified explicitly.
- The new property `yamlVersion` has been added to match
  `yaml-language-server`. The default is `1.2`.
- `DiagnosticsOptions` properties are now sorted alphabetically and
  documentation has been enhanced.

Closes #117
2021-10-17 12:32:19 +02:00
Remco Haszing
4e30f4cf16
Merge pull request #123 from remcohaszing/demo-source-map
Enable source maps for the demo
2021-10-06 09:09:27 +02:00
Remco Haszing
8d75a459ee
Enable source maps for the demo 2021-10-02 16:20:32 +02:00
Remco Haszing
0f2e5536bf
Merge pull request #114 from remcohaszing/remove-hover-content-checks
Remove checks for hover content
2021-10-02 16:16:00 +02:00
Remco Haszing
9987fbc7d5
Merge pull request #113 from remcohaszing/constant-language-id
Extract languageId into constants
2021-10-02 16:15:47 +02:00
Remco Haszing
4affa0963c
Merge pull request #112 from remcohaszing/demo-dispose-old-model
Dispose the old model before switching in demo
2021-10-02 16:15:41 +02:00
Remco Haszing
11f4465ee8
Add minimal vite example
Closes #115
2021-09-17 18:09:59 +02:00
Remco Haszing
da51564fbb
Remove checks for hover content
The hover implementation of the YAML language service always returns
`MarkupContent`.
2021-09-17 16:33:13 +02:00
Remco Haszing
7313780510
Extract languageId into constants
It was defined once, but it was passed into various functions. This
added unnecessary complexity.

It is now defined in a constants file, which is imported where
necessary.
2021-09-17 16:21:27 +02:00
Remco Haszing
3a0b164c51
Dispose the old model before switching in demo
This prevents errors.
2021-09-17 16:02:53 +02:00
Remco Haszing
979ed62d6f
3.2.1 2021-09-15 21:59:27 +02:00
Remco Haszing
65e91a1d43
Merge pull request #109 from patrickshipe/restore-options-updates
Restore ability to change options after loading
2021-09-15 21:52:57 +02:00
Remco Haszing
7bf8137107
Merge pull request #110 from patrickshipe/prevent-null-model-crash
Fix crashes from null model
2021-09-15 21:52:35 +02:00
Patrick Shipe
2bece31f56 Fix crashes from null model 2021-09-15 08:59:40 -06:00
Patrick Shipe
7a96d822b1 Restore ability to change options after loading 2021-09-14 13:37:14 -06:00
Remco Haszing
8d278d3d19
Merge pull request #101 from remcohaszing/stylistic-fixes
Apply various stylistic fixes
2021-09-06 12:11:42 +02:00
Remco Haszing
9739d143d3
Apply various stylistic fixes
- ESLint config `remcohaszing/typechecking` is extended.
- Various previously disabled ESLint rules have now been enabled.
- Various `any` types have been fixed.
- Removed useless type check of diagnostic code.
- The diagnostics adapter listener has been turned into an actual map.
2021-09-02 22:11:00 +02:00
35 changed files with 3028 additions and 15587 deletions

View file

@ -1,24 +1,19 @@
extends: remcohaszing
extends:
- remcohaszing
- remcohaszing/typechecking
rules:
class-methods-use-this: off
max-classes-per-file: off
no-console: off
no-restricted-globals: off
no-underscore-dangle: off
no-useless-constructor: off
'@typescript-eslint/naming-convention': off
'@typescript-eslint/no-parameter-properties': off
'@typescript-eslint/no-misused-promises': off
'@typescript-eslint/no-shadow': off
'@typescript-eslint/prefer-optional-chain': off
'@typescript-eslint/no-unnecessary-condition': off
import/extensions: off
import/no-extraneous-dependencies: off
import/no-unresolved: off
import/no-webpack-loader-syntax: off
jsdoc/require-jsdoc: off
node/no-extraneous-import: off
node/no-unpublished-import: off
node/no-unsupported-features/es-syntax: off
node/no-unsupported-features/node-builtins: off

View file

@ -16,6 +16,16 @@ jobs:
- run: npm ci
- run: npx eslint .
examples:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with: { node-version: 16 }
- run: npm ci
- run: npm run prepack
- run: npm run build --workspaces
pack:
runs-on: ubuntu-latest
steps:
@ -42,16 +52,15 @@ jobs:
with: { node-version: 16 }
- run: npm ci
- run: npx tsc
release:
runs-on: ubuntu-latest
needs: [eslint, pack, prettier, tsc]
if: startsWith(github.ref, 'refs/tags/')
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with: { node-version: 16 }
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# release:
# runs-on: ubuntu-latest
# needs: [eslint, pack, prettier, tsc]
# if: startsWith(github.ref, 'refs/tags/')
# steps:
# - uses: actions/checkout@v2
# - uses: actions/setup-node@v2
# with: { node-version: 16 }
# - run: npm ci
# - run: npm publish
# env:
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

4
.gitignore vendored
View file

@ -1,5 +1,7 @@
dist/
node_modules/
/lib/
/index.js
/yaml.worker.js
*.log
*.map
*.tgz

1
.npmrc Normal file
View file

@ -0,0 +1 @@
lockfile-version = 3

View file

@ -1,3 +1,3 @@
/dist/
/lib/
/out/
dist/
/index.js
/yaml.worker.js

View file

@ -1,5 +1,15 @@
# Contributing
## Prerequisites
The following are required to start working on this project:
- [Git](https://git-scm.com)
- [NodeJS](https://nodejs.org) 16 or higher
- [npm](https://github.com/npm/cli) 8.1.2 or higher
## Getting started
To get started with contributing, clone the repository and install its dependencies.
```sh
@ -8,16 +18,19 @@ cd monaco-yaml
npm ci
```
## Building
To build the repository, run:
```sh
npm prepack
npm run prepack
```
## Running
To test it, run one of the
[examples](https://github.com/remcohaszing/monaco-yaml/tree/main/examples).
```sh
cd examples/webpack-worker-loader
npm start
npm --workspace demo start
```

101
README.md
View file

@ -16,6 +16,7 @@ files:
- Document Symbols
- Automatically load remote schema files (by enabling DiagnosticsOptions.enableSchemaRequest)
- Links from JSON references.
- Links and hover effects from YAML anchors.
Schemas can also be provided by configuration. See
[here](https://github.com/remcohaszing/monaco-yaml/blob/main/index.d.ts) for the API that the plugin
@ -31,7 +32,7 @@ npm install monaco-yaml
Import `monaco-yaml` and configure it before an editor instance is created.
```ts
```typescript
import { editor, Uri } from 'monaco-editor';
import { setDiagnosticsOptions } from 'monaco-yaml';
@ -85,7 +86,43 @@ editor.create(document.createElement('editor'), {
});
```
Also make sure to register the web worker.
Also make sure to register the web worker. When using Webpack 5, this looks like the code below.
Other bundlers may use a different syntax, but the idea is the same. Languages you dont used can be
omitted.
```js
window.MonacoEnvironment = {
getWorker(moduleId, label) {
switch (label) {
case 'editorWorkerService':
return new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker', import.meta.url));
case 'css':
case 'less':
case 'scss':
return new Worker(new URL('monaco-editor/esm/vs/language/css/css.worker', import.meta.url));
case 'handlebars':
case 'html':
case 'razor':
return new Worker(
new URL('monaco-editor/esm/vs/language/html/html.worker', import.meta.url),
);
case 'json':
return new Worker(
new URL('monaco-editor/esm/vs/language/json/json.worker', import.meta.url),
);
case 'javascript':
case 'typescript':
return new Worker(
new URL('monaco-editor/esm/vs/language/typescript/ts.worker', import.meta.url),
);
case 'yaml':
return new Worker(new URL('monaco-yaml/yaml.worker', import.meta.url));
default:
throw new Error(`Unknown label ${label}`);
}
},
};
```
## Examples
@ -96,6 +133,66 @@ A running example: ![demo-image](test-demo.png)
Some usage examples can be found in the
[examples](https://github.com/remcohaszing/monaco-yaml/tree/main/examples) directory.
## FAQ
### Does this work with the Monaco UMD bundle?
No. Only ESM is supported.
### Does this work with Monaco Editor from a CDN?
No, because these use a UMD bundle, which isnt supported.
### Does this work with `@monaco-editor/loader` or `@monaco-editor/react`?
No. These packages pull in the Monaco UMD bundle from a CDN. Because UMD isnt supported, neither
are these packages.
### Is the web worker necessary?
Yes. The web worker provides the core functionality of `monaco-yaml`.
### Does it work without a bundler?
No. `monaco-yaml` uses dependencies from `node_modules`, so they can be deduped and your bundle size
is decreased. This comes at the cost of not being able to use it without a bundler.
### How do I integrate `monaco-yaml` with a framework? (Angular, React, Vue, etc.)
`monaco-yaml` only uses the Monaco Editor. Its not tied to a framework, all thats needed is a DOM
node to attach the Monaco Editor to. See the
[Monaco Editor examples](https://github.com/microsoft/monaco-editor/tree/main/monaco-editor-samples)
for examples on how to integrate Monaco Editor in your project, then configure `monaco-yaml` as
described above.
### Does `monaco-yaml` work with `create-react-app`?
Yes, but youll have to eject. See
[#92 (comment)](https://github.com/remcohaszing/monaco-yaml/issues/92#issuecomment-905836058) for
details.
### Why isnt `monaco-yaml` working? Official Monaco language extensions do work.
This is most likely due to the fact that `monaco-yaml` is using a different instance of the
`monaco-editor` package than you are. This is something youll want to avoid regardless of
`monaco-editor`, because it means your bundle is significantly larger than it needs to be. This is
likely caused by one of the following issues:
- A code splitting misconfiguration
To solve this, try inspecting your bundle using for example
[webpack-bundle-analyzer](https://github.com/webpack-contrib/webpack-bundle-analyzer). If
`monaco-editor` is in there twice, this is the issue. Its up to you to solve this, as its
project-specific.
- Youre using a package which imports `monaco-editor` for you, but its using a different version.
You can find out why the `monaco-editor` is installed using `npm ls monaco-editor` or
`yarn why monaco-editor`. It should exist only once, but its ok if its deduped.
You may be able to solve this by deleting your `node_modules` folder and `package-lock.json` or
`yarn.lock`, then running `npm install` or `yarn install` respectively.
## Contributing
Please see our [contributing guidelines](CONTRIBUTING.md)

121
build.js
View file

@ -1,71 +1,58 @@
const { promises: fs } = require('fs');
const { join } = require('path');
const { build } = require('esbuild');
const { dependencies, peerDependencies } = require('./package.json');
fs.rm(join(__dirname, 'lib'), { force: true, recursive: true })
.then(() =>
build({
entryPoints: ['src/monaco.contribution.ts', 'src/yaml.worker.ts'],
bundle: true,
external: Object.keys({ ...dependencies, ...peerDependencies }),
logLevel: 'info',
outdir: 'lib/esm',
sourcemap: true,
format: 'esm',
target: ['es2019'],
plugins: [
{
name: 'alias',
setup({ onResolve }) {
// The yaml language service only imports re-exports of vscode-languageserver-types from
// vscode-languageserver.
onResolve({ filter: /^vscode-languageserver-textdocument$/ }, () => ({
path: 'vscode-languageserver-textdocument/lib/esm/main.js',
external: true,
}));
// The yaml language service only imports re-exports of vscode-languageserver-types from
// vscode-languageserver.
onResolve({ filter: /^vscode-languageserver$/ }, () => ({
path: 'vscode-languageserver-types/lib/esm/main.js',
external: true,
}));
// The yaml language service only imports re-exports of vscode-languageserver-types from
// vscode-languageserver.
onResolve({ filter: /^vscode-languageserver-types$/ }, () => ({
path: 'vscode-languageserver-types/lib/esm/main.js',
external: true,
}));
// The yaml language service uses path. We can stub it using path-browserify.
onResolve({ filter: /^path$/ }, () => ({
path: 'path-browserify',
external: true,
}));
// The main prettier entry point contains all of Prettier.
// The standalone bundle is smaller and works fine for us.
onResolve({ filter: /^prettier$/ }, () => ({
path: 'prettier/standalone',
external: true,
}));
// This tiny filler implementation serves all our needs.
onResolve({ filter: /vscode-nls/ }, () => ({
path: require.resolve('./src/fillers/vscode-nls.ts'),
}));
// The language server dependencies tend to write both ESM and UMD output alongside each
// other, then use UMD for imports. We prefer ESM.
onResolve({ filter: /\/umd\// }, (args) => ({
path: require.resolve(args.path.replace(/\/umd\//, '/esm/'), {
paths: [args.resolveDir],
}),
}));
},
},
],
}),
)
.catch((error) => {
console.error(error);
process.exit(1);
});
build({
entryPoints: ['src/index.ts', 'src/yaml.worker.ts'],
bundle: true,
external: Object.keys({ ...dependencies, ...peerDependencies }),
logLevel: 'info',
outdir: '.',
sourcemap: true,
format: 'esm',
target: ['es2019'],
plugins: [
{
name: 'alias',
setup({ onResolve }) {
// The file monaco-yaml/lib/esm/schemaSelectionHandlers.js imports code from the language
// server part that we dont want.
onResolve({ filter: /\/schemaSelectionHandlers$/ }, () => ({
path: require.resolve('./src/fillers/schemaSelectionHandlers.ts'),
}));
// The yaml language service only imports re-exports of vscode-languageserver-types from
// vscode-languageserver.
onResolve({ filter: /^vscode-languageserver(\/node|-protocol)?$/ }, () => ({
path: 'vscode-languageserver-types',
external: true,
}));
// The yaml language service uses path. We can stub it using path-browserify.
onResolve({ filter: /^path$/ }, () => ({
path: 'path-browserify',
external: true,
}));
// The main prettier entry point contains all of Prettier.
// The standalone bundle is smaller and works fine for us.
onResolve({ filter: /^prettier/ }, ({ path }) => ({
path: path === 'prettier' ? 'prettier/standalone.js' : `${path}.js`,
external: true,
}));
// This tiny filler implementation serves all our needs.
onResolve({ filter: /vscode-nls/ }, () => ({
path: require.resolve('./src/fillers/vscode-nls.ts'),
}));
// The language server dependencies tend to write both ESM and UMD output alongside each
// other, then use UMD for imports. We prefer ESM.
onResolve({ filter: /\/umd\// }, (args) => ({
path: require.resolve(args.path.replace(/\/umd\//, '/esm/'), {
paths: [args.resolveDir],
}),
}));
},
},
],
}).catch((error) => {
// eslint-disable-next-line no-console
console.error(error);
process.exit(1);
});

View file

@ -2,10 +2,30 @@
This demo is deployed to [monaco-yaml.js.org](https://monaco-yaml.js.org). It shows how
`monaco-editor` and `monaco-yaml` can be used with
[Webpack 5](https://webpack.js.org/concepts/entry-points). To start it, simply run:
[Webpack 5](https://webpack.js.org/concepts/entry-points).
## Prerequisites
- [NodeJS](https://nodejs.org) 16 or higher
- [npm](https://github.com/npm/cli) 8.1.2 or higher
## Setup
To run the project locally, clone the repository and set it up:
```sh
npm start
git clone https://github.com/remcohaszing/monaco-yaml
cd monaco-yaml
npm ci
npm run prepack
```
## Running
To start it, simply run:
```sh
npm --workspace demo start
```
The demo will open in your browser.

View file

@ -7,13 +7,13 @@
"build": "webpack --mode production"
},
"dependencies": {
"@fortawesome/fontawesome-free": "^5.0.0",
"@fortawesome/fontawesome-free": "^6.0.0",
"@schemastore/schema-catalog": "^0.0.5",
"css-loader": "^6.0.0",
"css-minimizer-webpack-plugin": "^3.0.0",
"html-webpack-plugin": "^5.0.0",
"mini-css-extract-plugin": "^2.0.0",
"monaco-editor": "^0.27.0",
"monaco-editor": "^0.31.0",
"monaco-yaml": "file:../..",
"ts-loader": "^9.0.0",
"typescript": "^4.0.0",

View file

@ -31,7 +31,7 @@ window.MonacoEnvironment = {
case 'editorWorkerService':
return new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker', import.meta.url));
case 'yaml':
return new Worker(new URL('monaco-yaml/lib/esm/yaml.worker', import.meta.url));
return new Worker(new URL('monaco-yaml/yaml.worker', import.meta.url));
default:
throw new Error(`Unknown label ${label}`);
}
@ -44,11 +44,6 @@ const defaultSchema: SchemasSettings = {
};
setDiagnosticsOptions({
validate: true,
enableSchemaRequest: true,
format: true,
hover: true,
completion: true,
schemas: [defaultSchema],
});
@ -69,6 +64,10 @@ markdown: hover me to get a markdown based description 😮
enum:
# Unused anchors will be reported
unused anchor: &unused anchor
# Of course numbers are supported!
number: 12
@ -100,6 +99,14 @@ pointer:
$ref: '#/array'
# This anchor can be referenced
anchorRef: &anchor can be clicked as well
# Press control while hovering over the anchor
anchorPointer: *anchor
formatting: Formatting is supported too! Under the hood this is powered by Prettier. Just press Ctrl+Shift+I or right click and press Format to format this document.
@ -121,7 +128,7 @@ fetch('https://www.schemastore.org/api/json/catalog.json').then(async (response)
if (!response.ok) {
return;
}
const catalog: JSONSchemaForSchemaStoreOrgCatalogFiles = await response.json();
const catalog = (await response.json()) as JSONSchemaForSchemaStoreOrgCatalogFiles;
const schemas = [defaultSchema];
catalog.schemas.sort((a, b) => a.name.localeCompare(b.name));
for (const { fileMatch, name, url } of catalog.schemas) {
@ -152,7 +159,10 @@ fetch('https://www.schemastore.org/api/json/catalog.json').then(async (response)
});
select.addEventListener('change', () => {
ed.setModel(editor.createModel(ed.getValue(), 'yaml', Uri.parse(select.value)));
const oldModel = ed.getModel();
const newModel = editor.createModel(oldModel.getValue(), 'yaml', Uri.parse(select.value));
ed.setModel(newModel);
oldModel.dispose();
});
function* iterateSymbols(
@ -178,6 +188,7 @@ ed.onDidChangeCursorPosition(async (event) => {
breadcrumb.setAttribute('role', 'button');
breadcrumb.classList.add('breadcrumb');
breadcrumb.textContent = symbol.name;
breadcrumb.title = symbol.detail;
if (symbol.kind === languages.SymbolKind.Array) {
breadcrumb.classList.add('array');
} else if (symbol.kind === languages.SymbolKind.Module) {

View file

@ -1,4 +1,29 @@
declare module 'monaco-editor/esm/vs/base/common/cancellation' {
export enum CancellationToken {
None,
}
}
declare module 'monaco-editor/esm/vs/editor/contrib/documentSymbols/documentSymbols' {
import { ITextModel, languages } from 'monaco-editor';
import { CancellationToken } from 'monaco-editor/esm/vs/base/common/cancellation';
export function getDocumentSymbols(
model: ITextModel,
flat: boolean,
token: CancellationToken,
): Promise<languages.DocumentSymbol[]>;
}
declare module 'monaco-editor/esm/vs/editor/editor.worker.js' {
import { worker } from 'monaco-editor/esm/vs/editor/editor.api';
export function initialize(
fn: (ctx: worker.IWorkerContext, createData: unknown) => unknown,
): void;
}
declare module '*.json' {
declare const uri;
declare const uri: string;
export default uri;
}

View file

@ -6,12 +6,9 @@ module.exports = {
output: {
filename: '[contenthash].js',
},
devtool: 'source-map',
resolve: {
extensions: ['.mjs', '.js', '.ts'],
fallback: {
// Yaml-ast-parser-custom-tags imports buffer. This can be omitted safely.
buffer: false,
},
},
module: {
rules: [

View file

@ -0,0 +1,35 @@
# Monaco Editor Webpack Loader Plugin Example
This demo demonstrates how bundle `monaco-editor` and `monaco-yaml` with
[monaco-editor-webpack-plugin](https://github.com/microsoft/monaco-editor/tree/main/webpack-plugin).
The build output is
[esm library](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules). Example is
based on
[link](https://github.com/microsoft/monaco-editor/tree/main/samples/browser-esm-webpack-monaco-plugin).
To start it, simply run:
## Prerequisites
- [NodeJS](https://nodejs.org) 16 or higher
- [npm](https://github.com/npm/cli) 8.1.2 or higher
## Setup
To run the project locally, clone the repository and set it up:
```sh
git clone https://github.com/remcohaszing/monaco-yaml
cd monaco-yaml
npm ci
npm run prepack
```
## Running
To start it, simply run:
```sh
npm --workspace monaco-editor-webpack-plugin-example start
```
The demo will open in your browser.

View file

@ -0,0 +1,4 @@
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js';
export { setDiagnosticsOptions } from 'monaco-yaml';
export default monaco;

View file

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Monaco Editor Webpack Plugin Example</title>
<style>
.editor {
width: 800px;
height: 600px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="editor"></div>
<script src="index.js" type="module"></script>
</body>
</html>

View file

@ -0,0 +1,37 @@
const value = `
number: 0xfe
boolean: true
`;
async function create() {
// Dynamic import is possible
const { default: monaco } = await import('./main.js');
// Define schema first
monaco.languages.yaml.yamlDefaults.setDiagnosticsOptions({
schemas: [
{
fileMatch: ['*'],
uri: 'my-schema.json',
schema: {
type: 'object',
properties: {
number: {
description: 'number property',
type: 'number',
},
},
},
},
],
});
// Create editor
monaco.editor.create(document.querySelector('.editor'), {
language: 'yaml',
tabSize: 2,
value,
});
}
create();

View file

@ -0,0 +1,20 @@
{
"name": "monaco-editor-webpack-plugin-example",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"start": "webpack serve --open --mode development",
"build": "webpack --mode production"
},
"dependencies": {
"css-loader": "^6.0.0",
"monaco-editor": "^0.31.0",
"monaco-editor-webpack-plugin": "^7.0.0",
"monaco-yaml": "file:../..",
"style-loader": "^3.0.0",
"webpack": "^5.0.0",
"webpack-cli": "^4.0.0",
"webpack-dev-server": "^4.0.0"
}
}

View file

@ -0,0 +1,49 @@
import MonacoWebpackPlugin from 'monaco-editor-webpack-plugin';
export default {
entry: './editor.js',
output: {
filename: '[name].js',
library: {
type: 'module',
},
clean: true,
},
target: 'es2020',
experiments: {
outputModule: true,
},
devtool: 'source-map',
module: {
rules: [
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.ttf$/,
type: 'asset',
},
],
},
plugins: [
new MonacoWebpackPlugin({
languages: [],
customLanguages: [
{
label: 'yaml',
entry: ['monaco-yaml', 'vs/basic-languages/yaml/yaml.contribution'],
worker: {
id: 'monaco-yaml/yamlWorker',
entry: 'monaco-yaml/yaml.worker',
},
},
],
}),
],
devServer: {
static: {
directory: './',
},
},
};

View file

@ -0,0 +1,29 @@
# Vite Example
This minimal example shows how `monaco-yaml` can be used with [Vite](https://vitejs.dev).
## Prerequisites
- [NodeJS](https://nodejs.org) 16 or higher
- [npm](https://github.com/npm/cli) 8.1.2 or higher
## Setup
To run the project locally, clone the repository and set it up:
```sh
git clone https://github.com/remcohaszing/monaco-yaml
cd monaco-yaml
npm ci
npm run prepack
```
## Running
To start it, simply run:
```sh
npm --workspace vite-example start
```
The demo will be available on http://localhost:3000.

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Monaco YAML</title>
</head>
<body>
<div id="editor" style="width: 800px; height: 600px;"></div>
<script type="module" src="/index.js"></script>
</body>
</html>

View file

@ -0,0 +1,67 @@
import { editor, Uri } from 'monaco-editor';
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
import { setDiagnosticsOptions } from 'monaco-yaml';
import YamlWorker from 'monaco-yaml/yaml.worker?worker';
window.MonacoEnvironment = {
getWorker(moduleId, label) {
switch (label) {
case 'editorWorkerService':
return new EditorWorker();
case 'yaml':
return new YamlWorker();
default:
throw new Error(`Unknown label ${label}`);
}
},
};
// The uri is used for the schema file match.
const modelUri = Uri.parse('a://b/foo.yaml');
setDiagnosticsOptions({
enableSchemaRequest: true,
hover: true,
completion: true,
validate: true,
format: true,
schemas: [
{
// Id of the first schema
uri: 'http://myserver/foo-schema.json',
// Associate with our model
fileMatch: [String(modelUri)],
schema: {
type: 'object',
properties: {
p1: {
enum: ['v1', 'v2'],
},
p2: {
// Reference the second schema
$ref: 'http://myserver/bar-schema.json',
},
},
},
},
{
// Id of the first schema
uri: 'http://myserver/bar-schema.json',
schema: {
type: 'object',
properties: {
q1: {
enum: ['x1', 'x2'],
},
},
},
},
],
});
const value = 'p1: \np2: \n';
editor.create(document.getElementById('editor'), {
automaticLayout: true,
model: editor.createModel(value, 'yaml', modelUri),
});

View file

@ -0,0 +1,14 @@
{
"name": "vite-example",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "vite",
"build": "vite build"
},
"dependencies": {
"monaco-editor": "^0.31.0",
"monaco-yaml": "file:../..",
"vite": "^2.0.0"
}
}

70
index.d.ts vendored
View file

@ -29,44 +29,68 @@ declare module 'monaco-editor/esm/vs/editor/editor.api' {
export interface DiagnosticsOptions {
/**
* If set, enable schema based autocompletion.
*
* @default true
*/
readonly completion?: boolean;
/**
* A list of custom tags.
*
* @default []
*/
readonly customTags?: string[];
/**
* If set, the schema service would load schema content on-demand with 'fetch' if available
*
* @default false
*/
readonly enableSchemaRequest?: boolean;
/**
* If true, formatting using Prettier is enabled. Setting this to `false` does **not** exclude
* Prettier from the bundle.
*
* @default true
*/
readonly format?: boolean;
/**
* If set, enable hover typs based the JSON schema.
*
* @default true
*/
readonly hover?: boolean;
/**
* If set, the validator will be enabled and perform syntax validation as well as schema
* based validation.
*/
readonly validate?: boolean;
/**
* A list of known schemas and/or associations of schemas to file names.
*/
readonly schemas?: SchemasSettings[];
/**
* If set, the schema service would load schema content on-demand with 'fetch' if available
*/
readonly enableSchemaRequest?: boolean;
/**
* If specified, this prefix will be added to all on demand schema requests
*/
readonly prefix?: string;
/**
* Whether or not kubernetes yaml is supported
* If true, a different diffing algorithm is used to generate error messages.
*
* @default false
*/
readonly isKubernetes?: boolean;
/**
* A list of custom tags.
* A list of known schemas and/or associations of schemas to file names.
*
* @default []
*/
readonly customTags?: string[];
readonly schemas?: SchemasSettings[];
readonly format?: boolean;
/**
* If set, the validator will be enabled and perform syntax validation as well as schema
* based validation.
*
* @default true
*/
readonly validate?: boolean;
/**
* The YAML version to use for parsing.
*
* @default '1.2'
*/
readonly yamlVersion?: '1.1' | '1.2';
}
export interface LanguageServiceDefaults {

17608
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,18 +1,17 @@
{
"name": "monaco-yaml",
"version": "3.2.0",
"version": "4.0.0-alpha.1",
"description": "YAML plugin for the Monaco Editor",
"homepage": "https://monaco-yaml.js.org",
"scripts": {
"prepack": "node build.js",
"prepare": "husky install"
},
"main": "./lib/esm/monaco.contribution.js",
"module": "./lib/esm/monaco.contribution.js",
"typings": "./index.d.ts",
"files": [
"lib",
"index.d.ts"
"index.js",
"index.d.ts",
"yaml.worker.js"
],
"workspaces": [
"examples/*"
@ -39,30 +38,28 @@
],
"dependencies": {
"@types/json-schema": "^7.0.0",
"js-yaml": "^4.0.0",
"jsonc-parser": "^3.0.0",
"path-browserify": "^1.0.0",
"prettier": "2.0.5",
"vscode-languageserver-textdocument": "^1.0.0",
"vscode-languageserver-types": "^3.0.0",
"yaml-language-server-parser": "^0.1.0"
"yaml": "2.0.0-10"
},
"peerDependencies": {
"monaco-editor": ">=0.22"
"monaco-editor": ">=0.30"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.0.0",
"@typescript-eslint/parser": "^4.0.0",
"esbuild": "^0.12.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"esbuild": "^0.14.0",
"eslint": "^7.0.0",
"eslint-config-remcohaszing": "^3.0.0",
"husky": "^7.0.0",
"lint-staged": "^11.0.0",
"monaco-editor": "^0.27.0",
"lint-staged": "^12.0.0",
"monaco-editor": "^0.31.0",
"type-fest": "^2.0.0",
"typescript": "^4.0.0",
"yaml-language-server": "^0.22.0"
"yaml-language-server": "^1.0.0"
},
"resolutions": {},
"lint-staged": {
"*.{css,json,md,html,yaml}": [
"prettier --write"

1
src/constants.ts Normal file
View file

@ -0,0 +1 @@
export const languageId = 'yaml';

View file

@ -0,0 +1,5 @@
/**
* This is a stub for `monaco-yaml/lib/esm/schemaSelectionHandlers.js`.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
export function JSONSchemaSelection(): void {}

View file

@ -16,7 +16,7 @@ export type LoadFunc = (file?: string) => LocalizeFunc;
function format(message: string, args: string[]): string {
return args.length === 0
? message
: message.replace(/{(\d+)}/g, (match, rest) => {
: message.replace(/{(\d+)}/g, (match, rest: number[]) => {
const [index] = rest;
return typeof args[index] === 'undefined' ? match : args[index];
});

View file

@ -1,11 +1,23 @@
import { Emitter, languages } from 'monaco-editor/esm/vs/editor/editor.api';
import { Emitter, languages } from 'monaco-editor/esm/vs/editor/editor.api.js';
import { languageId } from './constants';
import { setupMode } from './yamlMode';
// --- YAML configuration and defaults ---------
const diagnosticDefault: languages.yaml.DiagnosticsOptions = {
completion: true,
customTags: [],
enableSchemaRequest: false,
format: true,
isKubernetes: false,
hover: true,
schemas: [],
validate: true,
yamlVersion: '1.2',
};
export function createLanguageServiceDefaults(
languageId: string,
initialDiagnosticsOptions: languages.yaml.DiagnosticsOptions,
): languages.yaml.LanguageServiceDefaults {
const onDidChange = new Emitter<languages.yaml.LanguageServiceDefaults>();
@ -25,7 +37,7 @@ export function createLanguageServiceDefaults(
},
setDiagnosticsOptions(options) {
diagnosticsOptions = options || {};
diagnosticsOptions = { ...diagnosticDefault, ...options };
onDidChange.fire(languageServiceDefaults);
},
};
@ -33,13 +45,7 @@ export function createLanguageServiceDefaults(
return languageServiceDefaults;
}
const diagnosticDefault: languages.yaml.DiagnosticsOptions = {
validate: true,
schemas: [],
enableSchemaRequest: false,
};
const yamlDefaults = createLanguageServiceDefaults('yaml', diagnosticDefault);
const yamlDefaults = createLanguageServiceDefaults(diagnosticDefault);
// Export API
function createAPI(): typeof languages.yaml {
@ -52,7 +58,7 @@ languages.yaml = createAPI();
// --- Registration to monaco editor ---
languages.register({
id: 'yaml',
id: languageId,
extensions: ['.yaml', '.yml'],
aliases: ['YAML', 'yaml', 'YML', 'yml'],
mimetypes: ['application/x-yaml'],

View file

@ -1,16 +1,17 @@
import {
editor,
IDisposable,
IMarkdownString,
languages,
MarkerSeverity,
MarkerTag,
Position,
Range,
Uri,
} from 'monaco-editor/esm/vs/editor/editor.api';
} from 'monaco-editor/esm/vs/editor/editor.api.js';
import * as ls from 'vscode-languageserver-types';
import { CustomFormatterOptions } from 'yaml-language-server/lib/esm/languageservice/yamlLanguageService';
import { CustomFormatterOptions } from 'yaml-language-server/lib/esm/languageservice/yamlLanguageService.js';
import { languageId } from './constants';
import { YAMLWorker } from './yamlWorker';
export type WorkerAccessor = (...more: Uri[]) => PromiseLike<YAMLWorker>;
@ -32,9 +33,17 @@ function toSeverity(lsSeverity: ls.DiagnosticSeverity): MarkerSeverity {
}
}
function toDiagnostics(resource: Uri, diag: ls.Diagnostic): editor.IMarkerData {
const code = typeof diag.code === 'number' ? String(diag.code) : (diag.code as string);
function toMarkerDataTag(tag: ls.DiagnosticTag): MarkerTag {
switch (tag) {
case ls.DiagnosticTag.Deprecated:
return MarkerTag.Deprecated;
case ls.DiagnosticTag.Unnecessary:
return MarkerTag.Unnecessary;
default:
}
}
function toDiagnostics(diag: ls.Diagnostic): editor.IMarkerData {
return {
severity: toSeverity(diag.severity),
startLineNumber: diag.range.start.line + 1,
@ -42,55 +51,59 @@ function toDiagnostics(resource: Uri, diag: ls.Diagnostic): editor.IMarkerData {
endLineNumber: diag.range.end.line + 1,
endColumn: diag.range.end.character + 1,
message: diag.message,
code,
code: String(diag.code),
source: diag.source,
tags: diag.tags?.map(toMarkerDataTag),
};
}
export function createDiagnosticsAdapter(
languageId: string,
getWorker: WorkerAccessor,
defaults: languages.yaml.LanguageServiceDefaults,
): void {
const listeners: Record<string, IDisposable> = Object.create(null);
const listeners = new Map<string, IDisposable>();
const resetSchema = async (resource: Uri): Promise<void> => {
const worker = await getWorker();
worker.resetSchema(String(resource));
};
const doValidate = async (resource: Uri, languageId: string): Promise<void> => {
const doValidate = async (resource: Uri): Promise<void> => {
const worker = await getWorker(resource);
const diagnostics = await worker.doValidation(String(resource));
const markers = diagnostics.map((d) => toDiagnostics(resource, d));
const markers = diagnostics.map(toDiagnostics);
const model = editor.getModel(resource);
if (model.getModeId() === languageId) {
// Return value from getModel can be null if model not found
// (e.g. if user navigates away from editor)
if (model && model.getLanguageId() === languageId) {
editor.setModelMarkers(model, languageId, markers);
}
};
const onModelAdd = (model: editor.IModel): void => {
const modeId = model.getModeId();
if (modeId !== languageId) {
if (model.getLanguageId() !== languageId) {
return;
}
let handle: ReturnType<typeof setTimeout>;
listeners[String(toString)] = model.onDidChangeContent(() => {
clearTimeout(handle);
handle = setTimeout(() => doValidate(model.uri, modeId), 500);
});
listeners.set(
String(model.uri),
model.onDidChangeContent(() => {
clearTimeout(handle);
handle = setTimeout(() => doValidate(model.uri), 500);
}),
);
doValidate(model.uri, modeId);
doValidate(model.uri);
};
const onModelRemoved = (model: editor.IModel): void => {
editor.setModelMarkers(model, languageId, []);
const uriStr = String(model.uri);
const listener = listeners[uriStr];
const listener = listeners.get(uriStr);
if (listener) {
listener.dispose();
delete listeners[uriStr];
listeners.delete(uriStr);
}
};
@ -106,7 +119,7 @@ export function createDiagnosticsAdapter(
});
defaults.onDidChange(() => {
for (const model of editor.getModels()) {
if (model.getModeId() === languageId) {
if (model.getLanguageId() === languageId) {
onModelRemoved(model);
onModelAdd(model);
}
@ -139,7 +152,7 @@ function toRange(range: ls.Range): Range {
);
}
function toCompletionItemKind(kind: languages.CompletionItemKind): languages.CompletionItemKind {
function toCompletionItemKind(kind: ls.CompletionItemKind): languages.CompletionItemKind {
const mItemKind = languages.CompletionItemKind;
switch (kind) {
@ -251,40 +264,24 @@ export function createCompletionItemProvider(
};
}
function isMarkupContent(thing: unknown): thing is ls.MarkupContent {
return thing && typeof thing === 'object' && typeof (thing as ls.MarkupContent).kind === 'string';
}
// --- definition ------
function toMarkdownString(entry: ls.MarkedString | ls.MarkupContent): IMarkdownString {
if (typeof entry === 'string') {
return {
value: entry,
};
}
if (isMarkupContent(entry)) {
if (entry.kind === 'plaintext') {
return {
value: entry.value.replace(/[!#()*+.[\\\]_`{}-]/g, '\\$&'),
};
}
return {
value: entry.value,
};
}
export function createDefinitionProvider(getWorker: WorkerAccessor): languages.DefinitionProvider {
return {
async provideDefinition(model, position) {
const resource = model.uri;
return { value: `\`\`\`${entry.language}\n${entry.value}\n\`\`\`\n` };
}
const worker = await getWorker(resource);
const definitions = await worker.doDefinition(String(resource), fromPosition(position));
function toMarkedStringArray(
contents: ls.MarkedString | ls.MarkedString[] | ls.MarkupContent,
): IMarkdownString[] {
if (!contents) {
return;
}
if (Array.isArray(contents)) {
return contents.map(toMarkdownString);
}
return [toMarkdownString(contents)];
return definitions?.map((definition) => ({
originSelectionRange: definition.originSelectionRange,
range: toRange(definition.targetRange),
targetSelectionRange: definition.targetSelectionRange,
uri: Uri.parse(definition.targetUri),
}));
},
};
}
// --- hover ------
@ -301,7 +298,7 @@ export function createHoverProvider(getWorker: WorkerAccessor): languages.HoverP
}
return {
range: toRange(info.range),
contents: toMarkedStringArray(info.contents),
contents: [{ value: (info.contents as ls.MarkupContent).value }],
};
},
};
@ -356,12 +353,12 @@ function toSymbolKind(kind: ls.SymbolKind): languages.SymbolKind {
function toDocumentSymbol(item: ls.DocumentSymbol): languages.DocumentSymbol {
return {
detail: '',
detail: item.detail || '',
range: toRange(item.range),
name: item.name,
kind: toSymbolKind(item.kind),
selectionRange: toRange(item.selectionRange),
children: item.children.map((child) => toDocumentSymbol(child)),
children: item.children.map(toDocumentSymbol),
tags: [],
};
}
@ -378,7 +375,7 @@ export function createDocumentSymbolProvider(
if (!items) {
return;
}
return items.map((item) => toDocumentSymbol(item));
return items.map(toDocumentSymbol);
},
};
}

View file

@ -1,4 +1,4 @@
import { editor, languages } from 'monaco-editor/esm/vs/editor/editor.api';
import { editor, languages } from 'monaco-editor/esm/vs/editor/editor.api.js';
import { WorkerAccessor } from './languageFeatures';
import { YAMLWorker } from './yamlWorker';
@ -13,18 +13,27 @@ export function createWorkerManager(
let client: Promise<YAMLWorker>;
let lastUsedTime = 0;
const stopWorker = (): void => {
if (worker) {
worker.dispose();
worker = undefined;
}
client = undefined;
};
setInterval(() => {
if (!worker) {
return;
}
const timePassedSinceLastUsed = Date.now() - lastUsedTime;
if (timePassedSinceLastUsed > STOP_WHEN_IDLE_FOR) {
worker.dispose();
worker = undefined;
client = undefined;
stopWorker();
}
}, 30 * 1000);
// This is necessary to have updated language options take effect (e.g. schema changes)
defaults.onDidChange(() => stopWorker());
const getClient = (): Promise<YAMLWorker> => {
lastUsedTime = Date.now();
@ -38,9 +47,7 @@ export function createWorkerManager(
// Passed in to the create() method
createData: {
languageSettings: defaults.diagnosticsOptions,
languageId: defaults.languageId,
enableSchemaRequest: defaults.diagnosticsOptions.enableSchemaRequest,
prefix: defaults.diagnosticsOptions.prefix,
isKubernetes: defaults.diagnosticsOptions.isKubernetes,
customTags: defaults.diagnosticsOptions.customTags,
},

View file

@ -1,7 +1,7 @@
import { initialize } from 'monaco-editor/esm/vs/editor/editor.worker';
import { initialize } from 'monaco-editor/esm/vs/editor/editor.worker.js';
import { createYAMLWorker } from './yamlWorker';
import { createYAMLWorker, ICreateData } from './yamlWorker';
self.onmessage = () => {
initialize((ctx, createData) => Object.create(createYAMLWorker(ctx, createData)));
initialize((ctx, createData: ICreateData) => Object.create(createYAMLWorker(ctx, createData)));
};

View file

@ -1,7 +1,9 @@
import { languages } from 'monaco-editor/esm/vs/editor/editor.api';
import { languages } from 'monaco-editor/esm/vs/editor/editor.api.js';
import { languageId } from './constants';
import {
createCompletionItemProvider,
createDefinitionProvider,
createDiagnosticsAdapter,
createDocumentFormattingEditProvider,
createDocumentSymbolProvider,
@ -45,16 +47,15 @@ const richEditConfiguration: languages.LanguageConfiguration = {
export function setupMode(defaults: languages.yaml.LanguageServiceDefaults): void {
const worker = createWorkerManager(defaults);
const { languageId } = defaults;
languages.registerCompletionItemProvider(languageId, createCompletionItemProvider(worker));
languages.registerHoverProvider(languageId, createHoverProvider(worker));
languages.registerDefinitionProvider(languageId, createDefinitionProvider(worker));
languages.registerDocumentSymbolProvider(languageId, createDocumentSymbolProvider(worker));
languages.registerDocumentFormattingEditProvider(
languageId,
createDocumentFormattingEditProvider(worker),
);
languages.registerLinkProvider(languageId, createLinkProvider(worker));
createDiagnosticsAdapter(languageId, worker, defaults);
createDiagnosticsAdapter(worker, defaults);
languages.setLanguageConfiguration(languageId, richEditConfiguration);
}

View file

@ -1,4 +1,4 @@
import { worker } from 'monaco-editor/esm/vs/editor/editor.api';
import { worker } from 'monaco-editor/esm/vs/editor/editor.api.js';
import { Promisable } from 'type-fest';
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as ls from 'vscode-languageserver-types';
@ -6,12 +6,16 @@ import {
CustomFormatterOptions,
getLanguageService,
LanguageSettings,
} from 'yaml-language-server/lib/esm/languageservice/yamlLanguageService';
} from 'yaml-language-server/lib/esm/languageservice/yamlLanguageService.js';
let defaultSchemaRequestService: (url: string) => Promise<string>;
import { languageId } from './constants';
if (typeof fetch !== 'undefined') {
defaultSchemaRequestService = (url) => fetch(url).then((response) => response.text());
async function schemaRequestService(uri: string): Promise<string> {
const response = await fetch(uri);
if (response.ok) {
return response.text();
}
throw new Error(`Schema request failed for ${uri}`);
}
export interface YAMLWorker {
@ -19,6 +23,8 @@ export interface YAMLWorker {
doComplete: (uri: string, position: ls.Position) => Promisable<ls.CompletionList>;
doDefinition: (uri: string, position: ls.Position) => Promisable<ls.LocationLink[]>;
doHover: (uri: string, position: ls.Position) => Promisable<ls.Hover>;
format: (uri: string, options: CustomFormatterOptions) => Promisable<ls.TextEdit[]>;
@ -32,21 +38,16 @@ export interface YAMLWorker {
export function createYAMLWorker(
ctx: worker.IWorkerContext,
{
enableSchemaRequest,
isKubernetes = false,
languageId,
languageSettings,
prefix = '',
}: ICreateData,
{ enableSchemaRequest, languageSettings }: ICreateData,
): YAMLWorker {
const service = (url: string): Promise<string> => defaultSchemaRequestService(`${prefix}${url}`);
const languageService = getLanguageService(enableSchemaRequest && service, null, null, null);
languageService.configure({
...languageSettings,
hover: true,
isKubernetes,
});
const languageService = getLanguageService(
enableSchemaRequest ? schemaRequestService : null,
null,
null,
null,
null,
);
languageService.configure(languageSettings);
const getTextDocument = (uri: string): TextDocument => {
const models = ctx.getMirrorModels();
@ -62,14 +63,19 @@ export function createYAMLWorker(
doValidation(uri) {
const document = getTextDocument(uri);
if (document) {
return languageService.doValidation(document, isKubernetes);
return languageService.doValidation(document, languageSettings.isKubernetes);
}
return [];
},
doComplete(uri, position) {
const document = getTextDocument(uri);
return languageService.doComplete(document, position, isKubernetes);
return languageService.doComplete(document, position, languageSettings.isKubernetes);
},
doDefinition(uri, position) {
const document = getTextDocument(uri);
return languageService.doDefinition(document, { position, textDocument: { uri } });
},
doHover(uri, position) {
@ -93,15 +99,13 @@ export function createYAMLWorker(
findLinks(uri) {
const document = getTextDocument(uri);
return Promise.resolve(languageService.findLinks(document));
return languageService.findLinks(document);
},
};
}
export interface ICreateData {
languageId: string;
languageSettings: LanguageSettings;
enableSchemaRequest: boolean;
prefix?: string;
isKubernetes?: boolean;
}