2023-09-27 23:36:51 +03:00
|
|
|
import { syntaxTree } from "@codemirror/language"
|
2023-09-25 23:50:41 +03:00
|
|
|
import { Extension } from '@codemirror/state';
|
|
|
|
import { hoverTooltip } from '@codemirror/view';
|
|
|
|
|
2023-09-27 23:36:51 +03:00
|
|
|
import { parseEntityReference, parseSyntacticReference } from '../../models/language';
|
2023-09-25 23:50:41 +03:00
|
|
|
import { IConstituenta } from '../../models/rsform';
|
2023-09-27 23:36:51 +03:00
|
|
|
import { domTooltipEntityReference, domTooltipSyntacticReference, findEnvelopingNodes } from '../../utils/codemirror';
|
|
|
|
import { IColorTheme } from '../../utils/color';
|
|
|
|
import { RefEntity, RefSyntactic } from './parse/parser.terms';
|
2023-09-25 23:50:41 +03:00
|
|
|
|
2023-09-27 23:36:51 +03:00
|
|
|
export const globalsHoverTooltip = (items: IConstituenta[], colors: IColorTheme) => {
|
|
|
|
return hoverTooltip((view, pos) => {
|
|
|
|
const nodes = findEnvelopingNodes(pos, pos, syntaxTree(view.state), [RefEntity, RefSyntactic]);
|
|
|
|
if (nodes.length !== 1) {
|
2023-09-25 23:50:41 +03:00
|
|
|
return null;
|
|
|
|
}
|
2023-09-27 23:36:51 +03:00
|
|
|
const start = nodes[0].from;
|
|
|
|
const end = nodes[0].to;
|
|
|
|
const text = view.state.doc.sliceString(start, end);
|
|
|
|
if (nodes[0].type.id === RefEntity) {
|
|
|
|
const ref = parseEntityReference(text);
|
|
|
|
const cst = items.find(cst => cst.alias === ref.entity);
|
|
|
|
return {
|
|
|
|
pos: start,
|
|
|
|
end: end,
|
|
|
|
above: false,
|
|
|
|
create: () => domTooltipEntityReference(ref, cst, colors)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const ref = parseSyntacticReference(text);
|
|
|
|
return {
|
|
|
|
pos: start,
|
|
|
|
end: end,
|
|
|
|
above: false,
|
|
|
|
create: () => domTooltipSyntacticReference(ref)
|
|
|
|
}
|
2023-09-25 23:50:41 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-27 23:36:51 +03:00
|
|
|
export function refsHoverTooltip(items: IConstituenta[], colors: IColorTheme): Extension {
|
|
|
|
return [globalsHoverTooltip(items, colors)];
|
2023-09-25 23:50:41 +03:00
|
|
|
}
|