Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Libraries
import {LSPServer} from 'src/external/monaco.flux.server'
import FLUXLANGID from 'src/external/monaco.flux.syntax'
import {
MonacoToProtocolConverter,
ProtocolToMonacoConverter,
} from 'monaco-languageclient/lib/monaco-converter'
import {CompletionTriggerKind} from 'monaco-languageclient/lib/services'
// Types
import {MonacoType} from 'src/types'
const m2p = new MonacoToProtocolConverter(),
p2m = new ProtocolToMonacoConverter()
export function registerCompletion(monaco: MonacoType, server: LSPServer) {
monaco.languages.registerDocumentSymbolProvider(FLUXLANGID, {
provideDocumentSymbols: async (model, _token) => {
try {
const uri = model.uri.toString()
const symbols = await server.symbols(uri)
return p2m.asSymbolInformations(symbols)
} catch (e) {
return []
}
},
})
function createDocument(model: monaco.editor.IReadOnlyModel) {
return TextDocument.create(MODEL_URI, model.getModeId(), model.getVersionId(), model.getValue());
}
function resolveSchema(url: string): Promise {
const promise = new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = () => resolve(xhr.responseText);
xhr.onerror = () => reject(xhr.statusText);
xhr.open("GET", url, true);
xhr.send();
});
return promise;
}
const m2p = new MonacoToProtocolConverter();
const p2m = new ProtocolToMonacoConverter();
const jsonService = getLanguageService({
schemaRequestService: resolveSchema
});
const pendingValidationRequests = new Map();
monaco.languages.registerCompletionItemProvider(LANGUAGE_ID, {
provideCompletionItems(model, position, context, token): monaco.Thenable {
const document = createDocument(model);
const wordUntil = model.getWordUntilPosition(position);
const defaultRange = new monaco.Range(position.lineNumber, wordUntil.startColumn, position.lineNumber, wordUntil.endColumn);
const jsonDocument = jsonService.parseJSONDocument(document);
return jsonService.doComplete(document, m2p.asPosition(position.lineNumber, position.column), jsonDocument).then((list) => {
return p2m.asCompletionResult(list, defaultRange);
});
},