2021-01-30 07:33:04 +08:00
|
|
|
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
|
2021-08-21 05:25:18 +08:00
|
|
|
import { CommandsRegistry } from "monaco-editor/esm/vs/platform/commands/common/commands";
|
2021-01-30 07:33:04 +08:00
|
|
|
import ElixirOnTypeFormattingEditProvider from "./elixir/on_type_formatting_edit_provider";
|
2021-04-21 01:34:17 +08:00
|
|
|
import theme from "./theme";
|
2021-01-30 07:33:04 +08:00
|
|
|
|
|
|
|
monaco.languages.registerOnTypeFormattingEditProvider(
|
|
|
|
"elixir",
|
|
|
|
ElixirOnTypeFormattingEditProvider
|
|
|
|
);
|
|
|
|
|
|
|
|
// Define custom theme
|
2021-04-21 01:34:17 +08:00
|
|
|
monaco.editor.defineTheme("custom", theme);
|
2021-01-30 07:33:04 +08:00
|
|
|
|
2021-03-26 00:39:18 +08:00
|
|
|
// See https://github.com/microsoft/monaco-editor/issues/648#issuecomment-564978560
|
|
|
|
// Without this selecting text with whitespace shrinks the whitespace.
|
|
|
|
document.fonts.addEventListener("loadingdone", (event) => {
|
|
|
|
const jetBrainsMonoLoaded = event.fontfaces.some(
|
2021-04-13 00:31:10 +08:00
|
|
|
// font-family may be either "JetBrains Mono" or "\"JetBrains Mono\""
|
|
|
|
(fontFace) => fontFace.family.includes("JetBrains Mono")
|
2021-03-26 00:39:18 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if (jetBrainsMonoLoaded) {
|
|
|
|
// We use JetBrains Mono in all instances of the editor,
|
|
|
|
// so we wait until it loads and then tell Monaco to remeasure
|
|
|
|
// fonts and updates its cache.
|
|
|
|
monaco.editor.remeasureFonts();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-21 03:30:53 +08:00
|
|
|
/**
|
|
|
|
* Define custom providers for various editor features.
|
|
|
|
*
|
|
|
|
* In our case, each cell has its own editor and behaviour
|
|
|
|
* of requests like completion and hover are cell dependent.
|
|
|
|
* For this reason we delegate the implementation to the
|
|
|
|
* specific cell by using its text model object.
|
|
|
|
*
|
|
|
|
* See cell/live_editor.js for more details.
|
|
|
|
*/
|
|
|
|
|
2021-04-21 01:34:17 +08:00
|
|
|
monaco.languages.registerCompletionItemProvider("elixir", {
|
2021-07-21 03:30:53 +08:00
|
|
|
provideCompletionItems: (model, position, context, token) => {
|
2021-04-21 01:34:17 +08:00
|
|
|
if (model.__getCompletionItems) {
|
|
|
|
return model.__getCompletionItems(model, position);
|
|
|
|
} else {
|
2021-07-21 03:30:53 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
monaco.languages.registerHoverProvider("elixir", {
|
|
|
|
provideHover: (model, position, token) => {
|
|
|
|
if (model.__getHover) {
|
|
|
|
return model.__getHover(model, position);
|
|
|
|
} else {
|
|
|
|
return null;
|
2021-04-21 01:34:17 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-12-04 04:57:21 +08:00
|
|
|
monaco.languages.registerSignatureHelpProvider("elixir", {
|
|
|
|
signatureHelpTriggerCharacters: ["(", ","],
|
|
|
|
provideSignatureHelp: (model, position, token, context) => {
|
|
|
|
if (model.__getSignatureHelp) {
|
|
|
|
return model.__getSignatureHelp(model, position);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-07-01 17:50:04 +08:00
|
|
|
monaco.languages.registerDocumentFormattingEditProvider("elixir", {
|
2021-07-21 03:30:53 +08:00
|
|
|
provideDocumentFormattingEdits: (model, options, token) => {
|
2021-07-01 17:50:04 +08:00
|
|
|
if (model.__getDocumentFormattingEdits) {
|
|
|
|
return model.__getDocumentFormattingEdits(model);
|
|
|
|
} else {
|
2021-07-21 03:30:53 +08:00
|
|
|
return null;
|
2021-07-01 17:50:04 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-01-30 07:33:04 +08:00
|
|
|
export default monaco;
|
2021-06-30 23:48:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Highlights the given code using the same rules as in the editor.
|
|
|
|
*
|
|
|
|
* Returns a promise resolving to HTML that renders as the highlighted code.
|
|
|
|
*/
|
|
|
|
export function highlight(code, language) {
|
|
|
|
return monaco.editor.colorize(code, language).then((result) => {
|
|
|
|
// `colorize` always adds additional newline, so we remove it
|
|
|
|
return result.replace(/<br\/>$/, "");
|
|
|
|
});
|
|
|
|
}
|
2021-08-21 05:25:18 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates keybinding for the given editor command.
|
|
|
|
*
|
|
|
|
* This uses an internal API, since there is no clean support
|
|
|
|
* for customizing keybindings.
|
|
|
|
* See https://github.com/microsoft/monaco-editor/issues/102#issuecomment-822981429
|
|
|
|
*/
|
|
|
|
export function addKeybinding(editor, id, newKeybinding) {
|
|
|
|
const { handler, when } = CommandsRegistry.getCommand(id) ?? {};
|
|
|
|
|
|
|
|
if (handler) {
|
|
|
|
editor._standaloneKeybindingService.addDynamicKeybinding(
|
|
|
|
id,
|
|
|
|
newKeybinding,
|
|
|
|
handler,
|
|
|
|
when
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|