Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import FLUXLANGID from 'src/external/monaco.flux.syntax'
import THEME_NAME from 'src/external/monaco.flux.theme'
import loadServer, {LSPServer} from 'src/external/monaco.flux.server'
import {comments, submit} from 'src/external/monaco.flux.hotkeys'
import {registerAutogrow} from 'src/external/monaco.autogrow'
import {isFlagEnabled} from 'src/shared/utils/featureFlag'
// Types
import {OnChangeScript} from 'src/types/flux'
import {EditorType} from 'src/types'
import './FluxMonacoEditor.scss'
import {editor as monacoEditor} from 'monaco-editor'
import {Diagnostic} from 'monaco-languageclient/lib/services'
const p2m = new ProtocolToMonacoConverter()
export interface EditorProps {
script: string
onChangeScript: OnChangeScript
onSubmitScript?: () => void
autogrow?: boolean
}
interface Props extends EditorProps {
setEditorInstance?: (editor: EditorType) => void
}
const FluxEditorMonaco: FC = ({
script,
onChangeScript,
onSubmitScript,
// 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 []
}
},
})
monaco.languages.registerDocumentFormattingEditProvider(FLUXLANGID, {
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);
});
},