Skip to content

Commit

Permalink
Merge pull request continuedev#2617 from todd-xander/fix-code-context…
Browse files Browse the repository at this point in the history
…-meta-parsing

fix code context description parsing to filename & range
  • Loading branch information
sestinj authored Nov 9, 2024
2 parents be1a79b + 56caf96 commit d3f0a57
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
7 changes: 5 additions & 2 deletions gui/src/components/mainInput/resolveInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ async function resolveEditorContent(
}
} else if (p.type === "codeBlock") {
if (!p.attrs.item.editing) {
let meta = p.attrs.item.description.split(" ");
let relativePath = meta[0] || "";
let extName = relativePath.split(".").slice(-1)[0];
const text =
"```" +
p.attrs.item.description +
"\n\n" +
"```" + extName + " " + p.attrs.item.description +
"\n" +
p.attrs.item.content +
"\n```";
Expand Down
8 changes: 6 additions & 2 deletions gui/src/components/markdown/CodeBlockToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from "..";
import { IdeMessengerContext } from "../../context/IdeMessenger";
import { useWebviewListener } from "../../hooks/useWebviewListener";
import { defaultModelSelector } from "../../redux/selectors/modelSelectors";
import {
incrementNextCodeBlockToApplyIndex,
updateApplyState,
Expand All @@ -29,7 +30,6 @@ import ButtonWithTooltip from "../ButtonWithTooltip";
import FileIcon from "../FileIcon";
import { CopyButton as CopyButtonHeader } from "./CopyButton";
import { ToolbarButtonWithTooltip } from "./ToolbarButtonWithTooltip";
import { defaultModelSelector } from "../../redux/selectors/modelSelectors";

const ToolbarDiv = styled.div`
display: flex;
Expand Down Expand Up @@ -87,6 +87,7 @@ interface CodeBlockToolBarProps {
language: string | undefined;
isNextCodeBlock: boolean;
filepath?: string;
range?: string;
}

const terminalLanguages = ["bash", "sh"];
Expand Down Expand Up @@ -265,7 +266,10 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) {
onClick={onClickHeader}
>
<FileIcon height="20px" width="20px" filename={props.filepath} />
<span className="truncate">{getBasename(props.filepath)}</span>
<span className="truncate">
{getBasename(props.filepath)}
{props.range && ` ${props.range}`}
</span>
</div>

<div className="flex items-center gap-1">
Expand Down
6 changes: 3 additions & 3 deletions gui/src/components/markdown/CodeSnippetPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) {
.split(")")[0]
.split("-");
ideMessenger.ide.showLines(
props.item.description,
props.item.description.split(" ")[0],
parseInt(lines[0]) - 1,
parseInt(lines[1]) - 1,
);
Expand Down Expand Up @@ -145,8 +145,8 @@ function CodeSnippetPreview(props: CodeSnippetPreviewProps) {
>
<StyledMarkdownPreview
source={`${fence}${getMarkdownLanguageTagForFile(
props.item.description,
)}\n${content}\n${fence}`}
props.item.description.split(" ")[0],
)} ${props.item.description}\n${content}\n${fence}`}
showCodeBorder={false}
/>
</div>
Expand Down
3 changes: 3 additions & 0 deletions gui/src/components/markdown/PreWithToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function PreWithToolbar(props: {
language: string | null;
codeBlockIndex: number;
filepath?: string | undefined;
range?: string;
}) {
const uiConfig = useUIConfig();
const toolbarBottom = uiConfig?.codeBlockToolbarPosition == "bottom";
Expand Down Expand Up @@ -76,6 +77,7 @@ function PreWithToolbar(props: {
bottom={toolbarBottom}
language={props.language}
filepath={props.filepath}
range={props.range}
/>
) : (
hovering && (
Expand All @@ -84,6 +86,7 @@ function PreWithToolbar(props: {
text={copyValue}
bottom={toolbarBottom}
language={props.language}
range={props.range}
/>
)
)}
Expand Down
10 changes: 6 additions & 4 deletions gui/src/components/markdown/StyledMarkdownPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,15 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
visit(tree, "code", (node: any) => {
if (!node.lang) {
node.lang === "javascript";
} else if (node.lang.includes(".")) {
node.lang = node.lang.split(".").slice(-1)[0];
}

if (node.meta) {
node.data = node.data || {};
node.data.hProperties = node.data.hProperties || {};
node.data.hProperties.filepath = node.meta;

let meta = node.meta.split(" ");
node.data.hProperties.filepath = meta[0];
node.data.hProperties.range = meta[1];
}
});
};
Expand Down Expand Up @@ -172,13 +173,14 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
);
},
pre: ({ node, ...preProps }) => {
const { className, filepath } = preProps?.children?.[0]?.props;
const { className, filepath, range } = preProps?.children?.[0]?.props;

return props.showCodeBorder ? (
<PreWithToolbar
codeBlockIndex={preProps.codeBlockIndex}
language={getLanuageFromClassName(className)}
filepath={filepath}
range={range}
>
<SyntaxHighlightedPre {...preProps}></SyntaxHighlightedPre>
</PreWithToolbar>
Expand Down

0 comments on commit d3f0a57

Please sign in to comment.