Skip to content

Commit

Permalink
feat(trace-viewer) add copy to clipboard on the Source > Stacktrace t…
Browse files Browse the repository at this point in the history
…ab (#31394)
  • Loading branch information
ryanrosello-og committed Jul 2, 2024
1 parent 9dc7e40 commit 262586a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
7 changes: 4 additions & 3 deletions packages/trace-viewer/src/ui/copyToClipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import * as React from 'react';

export const CopyToClipboard: React.FunctionComponent<{
value: string,
}> = ({ value }) => {
description?: string,
}> = ({ value, description }) => {
const [iconClassName, setIconClassName] = React.useState('codicon-clippy');

const handleCopy = React.useCallback(() => {
Expand All @@ -32,5 +33,5 @@ export const CopyToClipboard: React.FunctionComponent<{
});

}, [value]);
return <span className={`copy-icon codicon ${iconClassName}`} onClick={handleCopy}/>;
};
return <span title={description ? description : 'Copy'} className={`copy-icon codicon ${iconClassName}`} onClick={handleCopy}/>;
};
10 changes: 10 additions & 0 deletions packages/trace-viewer/src/ui/sourceTab.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@
box-shadow: var(--vscode-scrollbar-shadow) 0 6px 6px -6px;
z-index: 10;
}

.source-tab-file-name .copy-icon.codicon {
display: block;
cursor: pointer;
}

.source-copy-to-clipboard {
display: block;
padding-left: 4px;
}
18 changes: 17 additions & 1 deletion packages/trace-viewer/src/ui/sourceTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { CodeMirrorWrapper } from '@web/components/codeMirrorWrapper';
import type { SourceHighlight } from '@web/components/codeMirrorWrapper';
import type { SourceLocation, SourceModel } from './modelUtil';
import type { StackFrame } from '@protocol/channels';
import { CopyToClipboard } from './copyToClipboard';

export const SourceTab: React.FunctionComponent<{
stack: StackFrame[] | undefined,
Expand Down Expand Up @@ -82,7 +83,14 @@ export const SourceTab: React.FunctionComponent<{

return <SplitView sidebarSize={200} orientation={stackFrameLocation === 'bottom' ? 'vertical' : 'horizontal'} sidebarHidden={!showStackFrames}>
<div className='vbox' data-testid='source-code'>
{fileName && <div className='source-tab-file-name'>{fileName}</div>}
{fileName && (
<div className='source-tab-file-name'>
{fileName}
<span className='source-copy-to-clipboard'>
<CopyToClipboard description='Copy filename' value={getFileName(fileName, targetLine)}/>
</span>
</div>
)}
<CodeMirrorWrapper text={source.content || ''} language='javascript' highlight={highlight} revealLine={targetLine} readOnly={true} lineNumbers={true} />
</div>
<StackTraceView stack={stack} selectedFrame={selectedFrame} setSelectedFrame={setSelectedFrame} />
Expand All @@ -100,3 +108,11 @@ export async function calculateSha1(text: string): Promise<string> {
}
return hexCodes.join('');
}

function getFileName(fullPath?: string, lineNum?: number): string {
if (!fullPath)
return '';
const pathSep = fullPath?.includes('/') ? '/' : '\\';
const fileName = fullPath?.split(pathSep).pop() ?? '';
return lineNum ? `${fileName}:${lineNum}` : fileName;
}

0 comments on commit 262586a

Please sign in to comment.