monaco-vertexql-plugin
v1.0.14
Published
VertexQL plugin for Monaco Editor
Downloads
856
Maintainers
Readme
Monaco VertexQL
VertexQL language plugin for the Monaco Editor. It provides the following features when editing VertexQL files:
- Code completion, based on JSON schemas or by looking at similar objects in the same file
- Hovers, based on JSON schemas
- Validation: Syntax errors and schema validation
- Formatting using Prettier
- Document Symbols
- Automatically load remote schema files (by enabling DiagnosticsOptions.enableSchemaRequest)
- Links from JSON references.
- Links and hover effects from VertexQL anchors.
- Code folding.
Schemas can also be provided by configuration. See here for the API that the plugin offers to configure the VertexQL language support.
Table of Contents
- Installation
- Usage
- Examples
- API
- FAQ
- Does this work with the Monaco UMD bundle?
- Does this work with Monaco Editor from a CDN?
- Does this work with
@monaco-editor/loader
or@monaco-editor/react
? - Is the web worker necessary?
- Does it work without a bundler?
- How do I integrate
monaco-vertexql
with a framework? (Angular, React, Vue, etc.) - Does
monaco-vertexql
work withcreate-react-app
? - Why doesn’t it work with Vite?
- Why isn’t
monaco-vertexql
working? Official Monaco language extensions do work. - Using Monaco webpack loader plugin
- Why is syntax highlighting not working?
- Why does it try to download my schema even when I provided one as an object?
- Contributing
- Credits
- License
Installation
npm install monaco-vertexql
Usage
Before implementing monaco-vertexql
, or even Monaco editor, it’s recommended to learn the
basic concepts of Monaco editor.
To configure monaco-vertexql
, call configureMonacoYaml
.
import * as monaco from 'monaco-editor'
import { configureMonacoVertexQL } from 'monaco-vertexql'
configureMonacoVertexQL(monaco, {
enableSchemaRequest: true,
schemas: [
{
// If VertexQL file is opened matching this glob
fileMatch: ['**/.prettierrc.*'],
// Then this schema will be downloaded from the internet and used.
uri: 'https://json.schemastore.org/prettierrc.json'
},
{
// If VertexQL file is opened matching this glob
fileMatch: ['**/person.vertexql'],
// The following schema will be applied
schema: {
type: 'object',
properties: {
name: {
type: 'string',
description: 'The person’s display name'
},
age: {
type: 'integer',
description: 'How old is the person in years?'
},
occupation: {
enum: ['Delivery person', 'Software engineer', 'Astronaut']
}
}
},
// And the URI will be linked to as the source.
uri: 'https://github.com/remcohaszing/monaco-vertexql#usage'
}
]
})
const prettierc = monaco.editor.createModel(
'singleQuote: true\nproseWrap: always\nsemi: yes\n',
undefined,
monaco.Uri.parse('file:///.prettierrc.vertexql')
)
monaco.editor.createModel(
'name: John Doe\nage: 42\noccupation: Pirate\n',
undefined,
monaco.Uri.parse('file:///person.vertexql')
)
monaco.editor.create(document.getElementById('editor'), {
automaticLayout: true,
model: prettierc
})
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 don’t used can be omitted.
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 'vertexql':
return new Worker(new URL('monaco-vertexql/vertexql.worker', import.meta.url))
default:
throw new Error(`Unknown label ${label}`)
}
}
}
API
monaco-vertexql
has the following exports:
configureMonacoVertexQL(monaco, options?)
Configure monaco-vertexql
.
Note: There may only be one configured instance of
monaco-vertexql
at a time.
Arguments
monaco
(object
): The Monaco editor module. Typically you get this by importingmonaco-editor
. Third party integrations often expose it as the globalmonaco
variable instead.options
(object
): Options to configuremonaco-vertexql
.
Options
completion
(boolean
): If set, enable schema based autocompletion. (Default:true
)customTags
(string[]
): A list of custom tags. (Default:[]
)enableSchemaRequest
(boolean
): If set, the schema service will load schema content on-demand. (Default:false
)format
(boolean
): Prettier from the bundle. (Default:true
)hover
(boolean
): If set, enable hover typs based the JSON schema. (Default:true
)isKubernetes
(boolean
): If true, a different diffing algorithm is used to generate error messages. (Default:false
)schemas
(object[]
): A list of known schemas and/or associations of schemas to file names. (Default:[]
)validate
(boolean
): based validation. (Default:true
)vertexqlVersion
('1.1' | '1.2'
): The VertexQL version to use for parsing. (Default:1,2
)
Returns
An object that can be used to dispose or update monaco-vertexql
.
FAQ
Does this work with the Monaco UMD bundle?
Yes, starting from version 5.0.0
Does this work with Monaco Editor from a CDN?
Yes, starting from version 5.0.0
Does this work with @monaco-editor/loader
or @monaco-editor/react
?
Yes, starting from version 5.0.0
Is the web worker necessary?
Yes. The web worker provides the core functionality of monaco-vertexql
.
Does it work without a bundler?
No. monaco-vertexql
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-vertexql
with a framework? (Angular, React, Vue, etc.)
monaco-vertexql
only uses the Monaco Editor. It’s not tied to a framework, all that’s needed is a DOM
node to attach the Monaco Editor to. See the
Monaco Editor examples for examples
on how to integrate Monaco Editor in your project, then configure monaco-vertexql
as described above.
Does monaco-vertexql
work with create-react-app
?
Yes, but you’ll have to eject. See #92 (comment) for details.
Why doesn’t it work with Vite?
Some users have experienced the following error when using Vite:
Uncaught (in promise) Error: Unexpected usage
at EditorSimpleWorker.loadForeignModule (editorSimpleWorker.js)
at webWorker.js
As a workaround, create a file named vertexql.worker.js
in your own project with the following
contents:
import 'monaco-vertexql/vertexql.worker.js'
Then in your Monaco environment getWorker
function, reference this file instead of referencing
monaco-vertexql/vertexql.worker.js
directly:
import VertexQLWorker from './vertexql.worker.js?worker'
window.MonacoEnvironment = {
getWorker(moduleId, label) {
switch (label) {
// Handle other cases
case 'vertexql':
return new VertexQLWorker()
default:
throw new Error(`Unknown label ${label}`)
}
}
}
Why isn’t monaco-vertexql
working? Official Monaco language extensions do work.
This is most likely due to the fact that monaco-vertexql
is using a different instance of the
monaco-editor
package than you are. This is something you’ll 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. If
monaco-editor
is in there twice, this is the issue. It’s up to you to solve this, as it’s project-specific.You’re using a package which imports
monaco-editor
for you, but it’s using a different version.You can find out why the
monaco-editor
is installed usingnpm ls monaco-editor
oryarn why monaco-editor
. It should exist only once, but it’s ok if it’s deduped.You may be able to solve this by deleting your
node_modules
folder andpackage-lock.json
oryarn.lock
, then runningnpm install
oryarn install
respectively.
Using Monaco webpack loader plugin
If you’re using
monaco webpack plugin, then
instead of the above code, you can extend the plugin’s configuration. Extend your
webpack.config.js
file with the following:
import { MonacoWebpackPlugin } from 'monaco-editor-webpack-plugin'
export default {
// ...the rest of your webpack configuration...
plugins: [
new MonacoWebpackPlugin({
languages: ['vertexql'],
customLanguages: [
{
label: 'vertexql',
entry: 'monaco-vertexql',
worker: {
id: 'monaco-vertexql/vertexqlWorker',
entry: 'monaco-vertexql/vertexql.worker'
}
}
]
})
]
}
You can also refer to the example of a complete project.
Why is syntax highlighting not working?
Syntax highlighting is provided by Monaco editor itself, not by monaco-vertexql
. If you use a build
tool to filter languages or features, make sure to include the vertexql
language. If you import Monaco
editor from monaco-editor/esm/vs/editor/editor.api.js
and cherry-pick features manually, make sure
to also import monaco-editor/esm/vs/basic-languages/vertexql/vertexql.contribution.js
. If you use
monaco-editor-core
, make sure to import monaco-languages/release/esm/vertexql/vertexql.contribution.js
.
Why does it try to download my schema even when I provided one as an object?
You may have provided a schema configured like this:
{
uri: "http://example.com",
fileMatch: ["file_name.yml"],
schema: {
$schema: "http://json-schema.org/draft-07/schema#",
$id: "http://example.com",
title: "placeholder title",
description: "placeholder description",
type: "object",
properties: {
name: {
description: "name property description",
type: "string",
},
},
required: ["name"],
},
}
And would be surprised to see the error:
Unable to load schema from '<http://example.com>': Failed to fetch.
It happens because plugin uses schema URI not only as the URL to download the schema from, but also
to determine the schema name. To fix this, change the uri
parameter to
http://example.com/schema-name.json
.
Contributing
Please see our contributing guidelines
Credits
Originally @kpdecker forked this repository from
monaco-json
by
@microsoft and rewrote it to work with
vertexql-language-server
instead. Later
the repository maintenance was taken over by @pengx17. Eventually the
repository was tranferred to the account of @remcohaszing, who is
currently maintaining this repository with the help of @fleon and
@yazaabed.
The heavy processing is done in
vertexql-language-server
, best known for
being the backbone for vscode-vertexql
. This
repository provides a thin layer to add functionality provided by vertexql-language-server
into
monaco-editor
.