@nteract/monaco-editor
Version:
A React component for the monaco editor, tailored for nteract
40 lines • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.js_idx_to_char_idx = exports.completionRequest = void 0;
const messaging_1 = require("@nteract/messaging");
/**
* Create Jupyter messaging protocol's complete_request message.
* @param code Code of editor.
* @param cursorPos cursor position represented in the Jupyter messaging protocol (character position)
*/
exports.completionRequest = (code, cursorPos) => messaging_1.createMessage("complete_request", {
content: {
code,
cursor_pos: cursorPos
}
});
/**
* JavaScript stores text as utf16 and string indices use "code units",
* which stores high-codepoint characters as "surrogate pairs",
* which occupy two indices in the JavaScript string.
* We need to translate cursor_pos in the protocol (in characters)
* to js offset (with surrogate pairs taking two spots).
* @param js_idx JavaScript index
* @param text Text
*/
exports.js_idx_to_char_idx = (js_idx, text) => {
let char_idx = js_idx;
for (let i = 0; i + 1 < text.length && i < js_idx; i++) {
const char_code = text.charCodeAt(i);
// check for surrogate pair
if (char_code >= 0xd800 && char_code <= 0xdbff) {
const next_char_code = text.charCodeAt(i + 1);
if (next_char_code >= 0xdc00 && next_char_code <= 0xdfff) {
char_idx--;
i++;
}
}
}
return char_idx;
};
//# sourceMappingURL=editor-base.js.map