Merge pull request #2405 from continuedev/pe/code-tag-links
gui: update styling for filename lins
This commit is contained in:
commit
66805a9246
11 changed files with 105 additions and 209 deletions
|
|
@ -14,7 +14,7 @@ import {
|
|||
getMarkdownLanguageTagForFile,
|
||||
} from "../../util/";
|
||||
import {
|
||||
contextItemToRangeInFileWithContents,
|
||||
ctxItemToRifWithContents,
|
||||
type RangeInFileWithContents,
|
||||
} from "../util";
|
||||
|
||||
|
|
@ -250,7 +250,7 @@ const EditSlashCommand: SlashCommand = {
|
|||
}
|
||||
|
||||
const rif: RangeInFileWithContents =
|
||||
contextItemToRangeInFileWithContents(contextItemToEdit);
|
||||
ctxItemToRifWithContents(contextItemToEdit);
|
||||
|
||||
await ide.saveFile(rif.filepath);
|
||||
const fullFileContents = await ide.readFile(rif.filepath);
|
||||
|
|
|
|||
|
|
@ -9,20 +9,29 @@ export interface RangeInFileWithContents {
|
|||
contents: string;
|
||||
}
|
||||
|
||||
export function contextItemToRangeInFileWithContents(
|
||||
export function ctxItemToRifWithContents(
|
||||
item: ContextItemWithId,
|
||||
): RangeInFileWithContents {
|
||||
const lines = item.name.split("(")[1].split(")")[0].split("-");
|
||||
let startLine = 0;
|
||||
let endLine = 0;
|
||||
|
||||
const nameSplit = item.name.split("(");
|
||||
|
||||
if (nameSplit.length > 1) {
|
||||
const lines = nameSplit[1].split(")")[0].split("-");
|
||||
startLine = Number.parseInt(lines[0], 10);
|
||||
endLine = Number.parseInt(lines[1], 10);
|
||||
}
|
||||
|
||||
const rif: RangeInFileWithContents = {
|
||||
filepath: item.uri?.value || "",
|
||||
range: {
|
||||
start: {
|
||||
line: Number.parseInt(lines[0]),
|
||||
line: startLine,
|
||||
character: 0,
|
||||
},
|
||||
end: {
|
||||
line: Number.parseInt(lines[1]),
|
||||
line: endLine,
|
||||
character: 0,
|
||||
},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -4,14 +4,14 @@ import {
|
|||
ContextProviderExtras,
|
||||
ContextSubmenuItem,
|
||||
LoadSubmenuItemsArgs,
|
||||
} from "../../index.js";
|
||||
import { walkDir } from "../../indexing/walkDir.js";
|
||||
} from "../../";
|
||||
import { walkDir } from "../../indexing/walkDir";
|
||||
import {
|
||||
getBasename,
|
||||
getUniqueFilePath,
|
||||
groupByLastNPathParts,
|
||||
} from "../../util/index.js";
|
||||
import { BaseContextProvider } from "../index.js";
|
||||
} from "../../util/";
|
||||
import { BaseContextProvider } from "../";
|
||||
|
||||
const MAX_SUBMENU_ITEMS = 10_000;
|
||||
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ function StepContainer(props: StepContainerProps) {
|
|||
</ContentDiv>
|
||||
|
||||
{shouldRenderActions && (
|
||||
<div className="flex items-center justify-end gap-0.5 xs:flex text-xs text-gray-400 p-2 cursor-default">
|
||||
<div className="flex items-center justify-end gap-0.5 xs:flex text-xs text-gray-400 p-2 pb-0 cursor-default">
|
||||
{truncatedEarly && (
|
||||
<ButtonWithTooltip
|
||||
tabIndex={-1}
|
||||
|
|
|
|||
|
|
@ -57,8 +57,6 @@ export const vscBadgeBackground = `var(${VSC_BADGE_BACKGROUND_VAR}, #1bbe84)`;
|
|||
export const vscBadgeForeground = `var(${VSC_BADGE_FOREGROUND_VAR}, #fff)`;
|
||||
export const vscSidebarBorder = `var(${VSC_SIDEBAR_BORDER_VAR}, transparent)`;
|
||||
|
||||
// cececd
|
||||
|
||||
if (typeof document !== "undefined") {
|
||||
for (const colorVar of VSC_THEME_COLOR_VARS) {
|
||||
if (isJetBrains()) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { ChevronDownIcon, ChevronRightIcon } from "@heroicons/react/24/outline";
|
||||
import { ContextItemWithId } from "core";
|
||||
import { contextItemToRangeInFileWithContents } from "core/commands/util";
|
||||
import { ctxItemToRifWithContents } from "core/commands/util";
|
||||
import { useContext, useState } from "react";
|
||||
import { lightGray, vscBackground } from "..";
|
||||
import { IdeMessengerContext } from "../../context/IdeMessenger";
|
||||
|
|
@ -10,6 +10,7 @@ import SafeImg from "../SafeImg";
|
|||
import { INSTRUCTIONS_BASE_ITEM } from "core/context/providers/utils";
|
||||
import { getIconFromDropdownItem } from "./MentionList";
|
||||
import styled from "styled-components";
|
||||
import { getBasename } from "core/util";
|
||||
|
||||
interface ContextItemsPeekProps {
|
||||
contextItems?: ContextItemWithId[];
|
||||
|
|
@ -54,7 +55,7 @@ function ContextItemsPeekItem({ contextItem }: ContextItemsPeekItemProps) {
|
|||
const isRangeInFile = name.includes(" (") && name.endsWith(")");
|
||||
|
||||
if (isRangeInFile) {
|
||||
const rif = contextItemToRangeInFileWithContents(contextItem);
|
||||
const rif = ctxItemToRifWithContents(contextItem);
|
||||
ideMessenger.ide.showLines(
|
||||
rif.filepath,
|
||||
rif.range.start.line,
|
||||
|
|
@ -118,8 +119,7 @@ function ContextItemsPeekItem({ contextItem }: ContextItemsPeekItemProps) {
|
|||
return (
|
||||
<div
|
||||
onClick={openContextItem}
|
||||
className="cursor-pointer px-1.5 py-1 flex items-center rounded hover:bg-white/10 overflow-hidden whitespace-nowrap text-ellipsis mr-2"
|
||||
style={{ fontSize: `${getFontSize()}px` }}
|
||||
className="text-xs cursor-pointer px-1.5 py-1 flex items-center rounded hover:bg-white/10 overflow-hidden whitespace-nowrap text-ellipsis mr-2"
|
||||
>
|
||||
<div className="flex items-center w-full">
|
||||
{getContextItemIcon()}
|
||||
|
|
@ -128,11 +128,11 @@ function ContextItemsPeekItem({ contextItem }: ContextItemsPeekItemProps) {
|
|||
{contextItem.name}
|
||||
</div>
|
||||
<div
|
||||
className={`text-[${
|
||||
getFontSize() - 2
|
||||
}px] text-gray-400 overflow-hidden truncate whitespace-nowrap text-xs flex-1 min-w-0`}
|
||||
className={`text-xs text-gray-400 overflow-hidden truncate whitespace-nowrap text-xs flex-1 min-w-0`}
|
||||
>
|
||||
{contextItem.description}
|
||||
{contextItem.uri?.type === "file"
|
||||
? getBasename(contextItem.description)
|
||||
: contextItem.description}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -88,8 +88,6 @@ function ContinueInputBox(props: ContinueInputBoxProps) {
|
|||
[props.isMainInput],
|
||||
);
|
||||
|
||||
console.log({ isGatheringContext, isGatheringContextStore });
|
||||
|
||||
useEffect(() => {
|
||||
if (isGatheringContextStore && !isGatheringContext) {
|
||||
// 500ms delay when going from false -> true to prevent flashing loading indicator
|
||||
|
|
@ -107,10 +105,8 @@ function ContinueInputBox(props: ContinueInputBoxProps) {
|
|||
}, [isGatheringContextStore]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={`pt-1 bg-[${vscBackground}] ${props.hidden ? "hidden" : ""}`}
|
||||
>
|
||||
<div className={`flex px-2 relative bg-[${vscBackground}]`}>
|
||||
<div className={`mt-3 mb-1 ${props.hidden ? "hidden" : ""}`}>
|
||||
<div className={`flex px-2 relative`}>
|
||||
<GradientBorder
|
||||
loading={active && props.isLastUserInput ? 1 : 0}
|
||||
borderColor={
|
||||
|
|
@ -130,7 +126,7 @@ function ContinueInputBox(props: ContinueInputBoxProps) {
|
|||
<ContextItemsPeek
|
||||
contextItems={props.contextItems}
|
||||
isGatheringContext={isGatheringContext && props.isLastUserInput}
|
||||
></ContextItemsPeek>
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
36
gui/src/components/markdown/FilenameLink.tsx
Normal file
36
gui/src/components/markdown/FilenameLink.tsx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { RangeInFile } from "core";
|
||||
import { IdeMessengerContext } from "../../context/IdeMessenger";
|
||||
import { useContext } from "react";
|
||||
import { getBasename } from "core/util";
|
||||
import FileIcon from "../FileIcon";
|
||||
|
||||
interface FilenameLinkProps {
|
||||
rif: RangeInFile;
|
||||
}
|
||||
|
||||
function FilenameLink({ rif }: FilenameLinkProps) {
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
|
||||
function onClick() {
|
||||
debugger;
|
||||
ideMessenger.post("showLines", {
|
||||
filepath: rif.filepath,
|
||||
startLine: rif.range.start.line,
|
||||
endLine: rif.range.end.line,
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
className="inline-flex items-center align-middle hover:bg-stone-800 rounded-md py-0.5 pl-0 pr-1 mb-0.5 cursor-pointer gap-0.5"
|
||||
onClick={onClick}
|
||||
>
|
||||
<FileIcon filename={rif.filepath} height="20px" width="20px" />
|
||||
<span className="align-middle underline underline-offset-2 decoration-gray-600">
|
||||
{getBasename(rif.filepath)}
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default FilenameLink;
|
||||
|
|
@ -1,170 +0,0 @@
|
|||
import { RangeInFile } from "core";
|
||||
import { useContext, useEffect, useState } from "react";
|
||||
import { useSelector } from "react-redux";
|
||||
import styled from "styled-components";
|
||||
import { defaultBorderRadius, vscInputBackground } from "..";
|
||||
import { IdeMessengerContext } from "../../context/IdeMessenger";
|
||||
import { memoizedContextItemsSelector } from "../../redux/slices/stateSlice";
|
||||
|
||||
const StyledCode = styled.code<{ link: boolean }>`
|
||||
color: ${(props) => (props.link ? "#ff4343" : "#f78383")};
|
||||
word-wrap: break-word;
|
||||
border-radius: ${defaultBorderRadius};
|
||||
background-color: ${vscInputBackground};
|
||||
|
||||
${(props) => props.link && "cursor: pointer;"}
|
||||
&:hover {
|
||||
${(props) => props.link && "text-decoration: underline;"}
|
||||
}
|
||||
`;
|
||||
|
||||
function LinkableCode(props: any) {
|
||||
const ideMessenger = useContext(IdeMessengerContext);
|
||||
|
||||
const contextItems = useSelector(memoizedContextItemsSelector);
|
||||
|
||||
const [linkingDone, setLinkingDone] = useState(false);
|
||||
const [isLink, setIsLink] = useState(false);
|
||||
const [rif, setRif] = useState<undefined | RangeInFile>(undefined);
|
||||
|
||||
useEffect(() => {
|
||||
if (linkingDone) return;
|
||||
setLinkingDone(true);
|
||||
|
||||
// Get content of the code tag
|
||||
let content: string | undefined = undefined;
|
||||
if (typeof props.children === "string") {
|
||||
content = props.children;
|
||||
} else if (
|
||||
Array.isArray(props.children) &&
|
||||
props.children.length > 0 &&
|
||||
typeof props.children[0] === "string"
|
||||
) {
|
||||
content = props.children[0];
|
||||
}
|
||||
if (!content) return;
|
||||
|
||||
// Match filepath with context items
|
||||
const contextItemFileMatch = contextItems.find(
|
||||
(item) =>
|
||||
["file", "codebase", "folder"].includes(item.id.providerTitle) &&
|
||||
item.description.split(" (")[0].endsWith(content),
|
||||
);
|
||||
if (contextItemFileMatch) {
|
||||
setIsLink(true);
|
||||
if (contextItemFileMatch.description.includes(" (")) {
|
||||
// Get line number from filename
|
||||
const [startLine, _] = contextItemFileMatch.description
|
||||
.split(" (")[1]
|
||||
.split(")")[0]
|
||||
.split("-");
|
||||
const filepath = contextItemFileMatch.description.split(" (")[0];
|
||||
setRif({
|
||||
filepath,
|
||||
range: {
|
||||
start: {
|
||||
line: 0,
|
||||
character: 0,
|
||||
},
|
||||
end: {
|
||||
line: 0,
|
||||
character: 0,
|
||||
},
|
||||
},
|
||||
});
|
||||
return;
|
||||
} else {
|
||||
// If no line number, just open the file
|
||||
setRif({
|
||||
filepath: contextItemFileMatch.description.split(" (")[0],
|
||||
range: {
|
||||
start: {
|
||||
line: 0,
|
||||
character: 0,
|
||||
},
|
||||
end: {
|
||||
line: 0,
|
||||
character: 0,
|
||||
},
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to match the content with a line in a file
|
||||
if (content.length > 8) {
|
||||
const contextItemContentMatch = contextItems.find(
|
||||
(item) =>
|
||||
["file", "codebase", "folder"].includes(item.id.providerTitle) &&
|
||||
item.content.includes(content),
|
||||
);
|
||||
if (contextItemContentMatch) {
|
||||
const line = contextItemContentMatch.content
|
||||
.split("\n")
|
||||
.slice(1) // code block fence
|
||||
.findIndex((line) => line.includes(content));
|
||||
|
||||
const [filepath, rest] =
|
||||
contextItemContentMatch.description.split(" (");
|
||||
|
||||
setIsLink(true);
|
||||
|
||||
if (rest) {
|
||||
const [startLine, endLine] = rest.split(")")[0]?.split("-") || [
|
||||
"0",
|
||||
"0",
|
||||
];
|
||||
const start = parseInt(startLine);
|
||||
const end = parseInt(endLine);
|
||||
setRif({
|
||||
filepath,
|
||||
range: {
|
||||
start: {
|
||||
line: start + line,
|
||||
character: 0,
|
||||
},
|
||||
end: {
|
||||
line: start + line + 1,
|
||||
character: 0,
|
||||
},
|
||||
},
|
||||
});
|
||||
} else {
|
||||
setRif({
|
||||
filepath,
|
||||
range: {
|
||||
start: {
|
||||
line: 0,
|
||||
character: 0,
|
||||
},
|
||||
end: {
|
||||
line: 0,
|
||||
character: 0,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}, [linkingDone, contextItems]);
|
||||
|
||||
const onClick = () => {
|
||||
if (!isLink) return;
|
||||
if (!rif) return;
|
||||
ideMessenger.post("showLines", {
|
||||
filepath: rif.filepath,
|
||||
startLine: rif.range.start.line,
|
||||
endLine: rif.range.end.line,
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
{isLink}
|
||||
<StyledCode {...props} onClick={onClick} link={isLink} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default LinkableCode;
|
||||
|
|
@ -13,10 +13,13 @@ import {
|
|||
} from "..";
|
||||
import { getFontSize } from "../../util";
|
||||
import "./katex.css";
|
||||
import LinkableCode from "./LinkableCode";
|
||||
import FilenameLink from "./FilenameLink";
|
||||
import "./markdown.css";
|
||||
import PreWithToolbar from "./PreWithToolbar";
|
||||
import { SyntaxHighlightedPre } from "./SyntaxHighlightedPre";
|
||||
import { useSelector } from "react-redux";
|
||||
import { memoizedContextItemsSelector } from "../../redux/slices/stateSlice";
|
||||
import { ctxItemToRifWithContents } from "core/commands/util";
|
||||
|
||||
const StyledMarkdown = styled.div<{
|
||||
fontSize?: number;
|
||||
|
|
@ -29,6 +32,7 @@ const StyledMarkdown = styled.div<{
|
|||
overflow-x: scroll;
|
||||
overflow-y: hidden;
|
||||
|
||||
margin: 10px 0;
|
||||
padding: 6px 8px;
|
||||
}
|
||||
|
||||
|
|
@ -82,7 +86,7 @@ interface StyledMarkdownPreviewProps {
|
|||
|
||||
const HLJS_LANGUAGE_CLASSNAME_PREFIX = "language-";
|
||||
|
||||
const getLanuageFromClassName = (className: any): string | null => {
|
||||
function getLanuageFromClassName(className: any): string | null {
|
||||
if (!className || typeof className !== "string") {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -93,11 +97,27 @@ const getLanuageFromClassName = (className: any): string | null => {
|
|||
?.split("-")[1];
|
||||
|
||||
return language;
|
||||
};
|
||||
}
|
||||
|
||||
function getCodeChildrenContent(children: any) {
|
||||
if (typeof children === "string") {
|
||||
return children;
|
||||
} else if (
|
||||
Array.isArray(children) &&
|
||||
children.length > 0 &&
|
||||
typeof children[0] === "string"
|
||||
) {
|
||||
return children[0];
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
|
||||
props: StyledMarkdownPreviewProps,
|
||||
) {
|
||||
const contextItems = useSelector(memoizedContextItemsSelector);
|
||||
|
||||
const [reactContent, setMarkdownSource] = useRemark({
|
||||
remarkPlugins: [
|
||||
remarkMath,
|
||||
|
|
@ -167,15 +187,18 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
|
|||
);
|
||||
},
|
||||
code: ({ node, ...codeProps }) => {
|
||||
if (
|
||||
codeProps.className?.split(" ").includes("hljs") ||
|
||||
codeProps.children?.length > 1
|
||||
) {
|
||||
return <code {...codeProps}>{codeProps.children}</code>;
|
||||
}
|
||||
return (
|
||||
<LinkableCode {...codeProps}>{codeProps.children}</LinkableCode>
|
||||
const content = getCodeChildrenContent(codeProps.children);
|
||||
|
||||
const ctxItem = contextItems.find((ctxItem) =>
|
||||
ctxItem.uri?.value.includes(content),
|
||||
);
|
||||
|
||||
if (ctxItem) {
|
||||
const rif = ctxItemToRifWithContents(ctxItem);
|
||||
return <FilenameLink rif={rif} />;
|
||||
}
|
||||
|
||||
return <code {...codeProps}>{codeProps.children}</code>;
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
|||
|
|
@ -230,6 +230,10 @@ function useChatHandler(dispatch: Dispatch, ideMessenger: IIdeMessenger) {
|
|||
itemId: currentFilePath,
|
||||
providerTitle: "file",
|
||||
},
|
||||
uri: {
|
||||
type: "file",
|
||||
value: currentFilePath,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue