Skip to content

Commit

Permalink
Make "..." clickable to reveal abbreviated name
Browse files Browse the repository at this point in the history
  • Loading branch information
asgerf committed Nov 14, 2024
1 parent 7de0bf6 commit 4a2e521
Showing 1 changed file with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fragment } from "react";
import { Fragment, useState } from "react";
import { styled } from "styled-components";

/**
Expand Down Expand Up @@ -29,6 +29,21 @@ interface QualifiedName {
args?: QualifiedName[];
}

function qnameToString(name: QualifiedName): string {
const parts: string[] = [];
if (name.prefix != null) {
parts.push(qnameToString(name.prefix));
parts.push("::");
}
parts.push(name.name);
if (name.args != null && name.args.length > 0) {
parts.push("<");
parts.push(name.args.map(qnameToString).join(","));
parts.push(">");
}
return parts.join("");
}

function tokeniseName(text: string) {
return Array.from(text.matchAll(/:+|<|>|,|"[^"]+"|`[^`]+`|[^:<>,"`]+/g));
}
Expand Down Expand Up @@ -137,7 +152,10 @@ class TrieBuilder {
if (args != null) {
result.push("<");
if (trieNodeBeforeArgs.children.size === 1) {
result.push("...");
const argsText = qname
.args!.map((arg) => qnameToString(arg))
.join(",");
result.push(<ExpandableNamePart>{argsText}</ExpandableNamePart>);
} else {
let first = true;
for (const arg of args) {
Expand All @@ -157,6 +175,30 @@ class TrieBuilder {
}
}

const ExpandableTextButton = styled.button`
background: none;
border: none;
cursor: pointer;
padding: 0;
color: inherit;
&:hover {
background-color: rgba(128, 128, 128, 0.2);
}
`;

interface ExpandableNamePartProps {
children: React.ReactNode;
}

function ExpandableNamePart(props: ExpandableNamePartProps) {
const [isExpanded, setExpanded] = useState(false);
return (
<ExpandableTextButton onClick={() => setExpanded(!isExpanded)}>
{isExpanded ? props.children : "..."}
</ExpandableTextButton>
);
}

/**
* Span enclosing an entire qualified name.
*
Expand Down

0 comments on commit 4a2e521

Please sign in to comment.