Skip to content

Instantly share code, notes, and snippets.

@lakhansamani
Created June 9, 2021 11:15
Show Gist options
  • Select an option

  • Save lakhansamani/60d83b48a0a947e81619e430b06747e0 to your computer and use it in GitHub Desktop.

Select an option

Save lakhansamani/60d83b48a0a947e81619e430b06747e0 to your computer and use it in GitHub Desktop.
// Configures two JSON schemas, with references.
var jsonCode = [
'{',
' "query": {',
' "match_all": {}',
' }',
"}"
].join('\n');
var modelUri = monaco.Uri.parse("a://b/query-schema.json"); // a made up unique URI for our model
var model = monaco.editor.createModel(jsonCode, "json", modelUri);
// consider we derived this from mappings
const fields = ["name","name.search", "name.keyword", "address.search", "address.keyword"];
function createDependencyProposals(range) {
// returning a static list of proposals, not even looking at the prefix (filtering is done by the Monaco editor),
// here you could do a server side lookup
return [
{
label: "aggs",
kind: monaco.languages.CompletionItemKind.Function,
documentation: "aggs",
insertText: '"aggs": {\n\t"__AGG__NAME__": {}\n}',
range: range
}
];
}
function getOptions(){
const matchOptions = {}
const aggOptions = []
fields.forEach(item => {
matchOptions[item] = {
oneOf: [
{ type: "string" },
{ $ref: "http://myserver/match-options-schema.json" },
]
}
if (item.includes('.keyword')) {
aggOptions.push(item)
}
})
return {
validate: true,
schemas: [{
uri: "http://myserver/query-schema.json", // id of the first schema
fileMatch: [modelUri.toString()], // associate with our model
schema: {
type: "object",
properties: {
size: {
type: "number"
},
query: {
type: "object",
properties: {
match: {
$ref: "http://myserver/match-schema.json"
},
match_all: {
type: "object"
}
}
},
aggs: {
$ref: "http://myserver/agg-schema.json"
},
}
}
}, {
uri: "http://myserver/match-schema.json", // id of the second schema
schema: {
type: "object",
properties: matchOptions
}
},{
uri: "http://myserver/match-options-schema.json", // id of the second schema
schema: {
type: "object",
properties: {
query: {
type: "string"
},
operator: {
type: "string",
enum: ["and", "or"]
}
}
}
}, {
uri: "http://myserver/agg-schema.json", // id of the second schema
schema: {
type: "object",
// $ref: "#",
patternProperties: {
".*": {
type: "object",
properties: {
term: {
type: "object",
properties: {
field: {
type: "string",
enum: aggOptions
}
}
}
}
}
}
}
}]
}
}
// configure the JSON language support with schemas and schema associations
monaco.languages.json.jsonDefaults.setDiagnosticsOptions(getOptions());
monaco.languages.registerCompletionItemProvider('json', {
provideCompletionItems: function(model, position) {
// find out if we are completing a property in the 'dependencies' object.
var textUntilPosition = model.getValueInRange({startLineNumber: 1, startColumn: 1, endLineNumber: position.lineNumber, endColumn: position.column});
if (textUntilPosition.includes('"aggs":')) {
return { suggestions: [] };
}
var word = model.getWordUntilPosition(position);
var range = {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: word.startColumn,
endColumn: word.endColumn
};
return {
suggestions: createDependencyProposals(range)
};
}
});
monaco.editor.create(document.getElementById("container"), {
model: model
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment