@krassowski/jupyterlab-lsp
Version:
Language Server Protocol integration for JupyterLab
77 lines • 3.1 kB
JavaScript
import { nullTranslator } from '@jupyterlab/translation';
import React from 'react';
export function getBreadcrumbs(document, adapter, trans, collapse = true) {
if (!trans) {
trans = nullTranslator.load('');
}
return document.ancestry.map((document) => {
if (!document.parent) {
let path = document.path;
if (!document.has_lsp_supported_file &&
document.file_extension &&
path.endsWith(document.file_extension)) {
path = path.slice(0, -document.file_extension.length - 1);
}
const full_path = path;
if (collapse) {
let parts = path.split('/');
if (parts.length > 2) {
path = parts[0] + '/.../' + parts[parts.length - 1];
}
}
return (React.createElement("span", { key: document.uri, title: full_path }, path));
}
if (!document.virtual_lines.size) {
return React.createElement("span", { key: document.uri }, "Empty document");
}
try {
if (adapter.has_multiple_editors) {
let first_line = document.virtual_lines.get(0);
let last_line = document.virtual_lines.get(document.last_virtual_line - 1);
let first_cell = adapter.get_editor_index(first_line.editor);
let last_cell = adapter.get_editor_index(last_line.editor);
let cell_locator = first_cell === last_cell
? trans.__('cell %1', first_cell + 1)
: trans.__('cells: %1-%2', first_cell + 1, last_cell + 1);
return (React.createElement("span", { key: document.uri },
document.language,
" (",
cell_locator,
")"));
}
}
catch (e) {
console.warn('LSP: could not display document cell location', e);
}
return React.createElement("span", { key: document.uri }, document.language);
});
}
/**
* @deprecated please use getBreadcrumbs instead; `get_breadcrumbs` will be removed in 4.0
*/
export function get_breadcrumbs(document, adapter, collapse = true) {
return getBreadcrumbs(document, adapter, undefined, collapse);
}
export function focus_on(node) {
if (!node) {
return;
}
node.scrollIntoView();
node.focus();
}
export function DocumentLocator(props) {
let { document, adapter } = props;
let target = null;
if (adapter.has_multiple_editors) {
let first_line = document.virtual_lines.get(0);
if (first_line) {
target = adapter.get_editor_wrapper(first_line.editor);
}
else {
console.warn('Could not get first line of ', document);
}
}
let breadcrumbs = getBreadcrumbs(document, adapter, props.trans);
return (React.createElement("div", { className: 'lsp-document-locator', onClick: () => (target ? focus_on(target) : null) }, breadcrumbs));
}
//# sourceMappingURL=utils.js.map