@jupyter-lsp/jupyterlab-lsp
Version:
Language Server Protocol integration for JupyterLab
85 lines • 3.53 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.hasLspSupportedFile &&
document.fileExtension &&
path.endsWith(document.fileExtension)) {
path = path.slice(0, -document.fileExtension.length - 1);
}
const fullPath = 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: fullPath }, path));
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const virtualLines = document.virtualLines;
if (!virtualLines.size) {
return React.createElement("span", { key: document.uri }, "Empty document");
}
try {
if (adapter.hasMultipleEditors) {
let firstLine = virtualLines.get(0);
let lastLine = virtualLines.get(document.lastVirtualLine - 1);
let firstCell = adapter.getEditorIndex(firstLine.editor);
let lastCell = adapter.getEditorIndex(lastLine.editor);
let cellLocator = firstCell === lastCell
? trans.__('cell %1', firstCell + 1)
: trans.__('cells: %1-%2', firstCell + 1, lastCell + 1);
return (React.createElement("span", { key: document.uri },
document.language,
" (",
cellLocator,
")"));
}
}
catch (e) {
console.warn('LSP: could not display document cell location', e);
}
return React.createElement("span", { key: document.uri }, document.language);
});
}
async function focusEditor(accessor) {
if (!accessor) {
return;
}
await accessor.reveal();
const editor = accessor.getEditor();
editor.focus();
}
export function DocumentLocator(props) {
let { document, adapter } = props;
let target = null;
if (adapter.hasMultipleEditors) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
let firstLine = document.virtualLines.get(0);
if (firstLine) {
target = firstLine.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 ? focusEditor(target) : null) }, breadcrumbs));
}
export function ServerLinksList(props) {
var _a;
return (React.createElement("ul", { className: 'lsp-server-links-list' }, Object.entries(((_a = props.specification) === null || _a === void 0 ? void 0 : _a.urls) || {}).map(([name, url]) => (React.createElement("li", { key: props.specification.serverId + '-url-' + name },
name,
":",
' ',
React.createElement("a", { href: url, target: "_blank", rel: "noreferrer" }, url))))));
}
//# sourceMappingURL=utils.js.map