Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/playwright-core/src/server/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ function isScreenshotCommand(metadata: CallMetadata) {
return metadata.method.toLowerCase().includes('screenshot');
}

function languageForFile(file: string) {
function languageForFile(file: string): Language {
if (file.endsWith('.py'))
return 'python';
if (file.endsWith('.java'))
Expand Down
18 changes: 9 additions & 9 deletions packages/playwright-core/src/server/recorder/recorderApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ export class RecorderApp {
private _actions: actions.ActionInContext[] = [];
private _userSources: Source[] = [];
private _recorderSources: Source[] = [];
private _primaryLanguage: string;
private _selectedLanguageId: string;
private _primaryGeneratorId: string;
private _selectedGeneratorId: string;

private constructor(recorder: Recorder, params: RecorderAppParams, page: Page, wsEndpointForTest: string | undefined) {
this._page = page;
Expand All @@ -70,8 +70,8 @@ export class RecorderApp {
};

this._throttledOutputFile = params.outputFile ? new ThrottledFile(params.outputFile) : null;
this._primaryLanguage = process.env.TEST_INSPECTOR_LANGUAGE || params.language || determinePrimaryLanguage(params.sdkLanguage);
this._selectedLanguageId = this._primaryLanguage;
this._primaryGeneratorId = process.env.TEST_INSPECTOR_LANGUAGE || params.language || determinePrimaryGeneratorId(params.sdkLanguage);
this._selectedGeneratorId = this._primaryGeneratorId;
}

private async _init(inspectedContext: BrowserContext) {
Expand Down Expand Up @@ -133,7 +133,7 @@ export class RecorderApp {
const source = [...this._recorderSources, ...this._userSources].find(s => s.id === data.params.fileId);
if (source) {
if (source.isRecorded)
this._selectedLanguageId = source.id;
this._selectedGeneratorId = source.id;
this._recorder.setLanguage(source.language);
}
return;
Expand Down Expand Up @@ -358,9 +358,9 @@ export class RecorderApp {
};
source.revealLine = text.split('\n').length - 1;
recorderSources.push(source);
if (languageGenerator.id === this._primaryLanguage)
if (languageGenerator.id === this._primaryGeneratorId)
this._throttledOutputFile?.setContent(source.text);
if (reveal === 'reveal' && source.id === this._selectedLanguageId)
if (reveal === 'reveal' && source.id === this._selectedGeneratorId)
revealSourceId = source.id;
}

Expand All @@ -370,8 +370,8 @@ export class RecorderApp {
}
}

// For example, if the SDK language is 'javascript', we return 'playwright-test' as the primary language.
function determinePrimaryLanguage(sdkLanguage: Language): string {
// For example, if the SDK language is 'javascript', this returns 'playwright-test'.
function determinePrimaryGeneratorId(sdkLanguage: Language): string {
for (const language of languageSet()) {
if (language.highlighter === sdkLanguage)
return language.id;
Expand Down
6 changes: 3 additions & 3 deletions packages/recorder/src/recorder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,14 @@ export const Recorder: React.FC<RecorderProps> = ({
</Toolbar>
<SplitView
sidebarSize={200}
main={<CodeMirrorWrapper text={source.text} language={source.language} highlight={source.highlight} revealLine={source.revealLine} readOnly={true} lineNumbers={true} />}
main={<CodeMirrorWrapper text={source.text} highlighter={source.language} highlight={source.highlight} revealLine={source.revealLine} readOnly={true} lineNumbers={true} />}
sidebar={<TabbedPane
rightToolbar={selectedTab === 'locator' || selectedTab === 'aria' ? [<ToolbarButton key={1} icon='files' title='Copy' onClick={() => copy((selectedTab === 'locator' ? locator : ariaSnapshot) || '')} />] : []}
tabs={[
{
id: 'locator',
title: 'Locator',
render: () => <CodeMirrorWrapper text={locator} placeholder='Type locator to inspect' language={source.language} focusOnChange={true} onChange={onEditorChange} wrapLines={true} />
render: () => <CodeMirrorWrapper text={locator} placeholder='Type locator to inspect' highlighter={source.language} focusOnChange={true} onChange={onEditorChange} wrapLines={true} />
},
{
id: 'log',
Expand All @@ -199,7 +199,7 @@ export const Recorder: React.FC<RecorderProps> = ({
{
id: 'aria',
title: 'Aria',
render: () => <CodeMirrorWrapper text={ariaSnapshot || ''} placeholder='Type aria template to match' language={'yaml'} onChange={onAriaEditorChange} highlight={ariaSnapshotErrors} wrapLines={true} />
render: () => <CodeMirrorWrapper text={ariaSnapshot || ''} placeholder='Type aria template to match' highlighter={'yaml'} onChange={onAriaEditorChange} highlight={ariaSnapshotErrors} wrapLines={true} />
},
]}
selectedTab={selectedTab}
Expand Down
7 changes: 4 additions & 3 deletions packages/trace-viewer/src/ui/inspectorTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
*/

import { CodeMirrorWrapper } from '@web/components/codeMirrorWrapper';
import type { Language, SourceHighlight } from '@web/components/codeMirrorWrapper';
import type { SourceHighlight } from '@web/components/codeMirrorWrapper';
import { ToolbarButton } from '@web/components/toolbarButton';
import { copy } from '@web/uiUtils';
import * as React from 'react';
import type { HighlightedElement } from './snapshotTab';
import './sourceTab.css';
import { parseAriaSnapshot } from '@isomorphic/ariaSnapshot';
import yaml from 'yaml';
import type { Language } from '@isomorphic/locatorGenerators';

export const InspectorTab: React.FunctionComponent<{
sdkLanguage: Language,
Expand Down Expand Up @@ -55,7 +56,7 @@ export const InspectorTab: React.FunctionComponent<{
}}></ToolbarButton>
</div>
<div style={{ height: 50 }}>
<CodeMirrorWrapper text={highlightedElement.locator || ''} language={sdkLanguage} isFocused={true} wrapLines={true} onChange={text => {
<CodeMirrorWrapper text={highlightedElement.locator || ''} highlighter={sdkLanguage} isFocused={true} wrapLines={true} onChange={text => {
// Updating text needs to go first - react can squeeze a render between the state updates.
setHighlightedElement({ ...highlightedElement, locator: text, lastEdited: 'locator' });
setIsInspecting(false);
Expand All @@ -71,7 +72,7 @@ export const InspectorTab: React.FunctionComponent<{
<div style={{ height: 150 }}>
<CodeMirrorWrapper
text={highlightedElement.ariaSnapshot || ''}
language='yaml'
highlighter='yaml'
wrapLines={false}
highlight={ariaSnapshotErrors}
onChange={onAriaEditorChange} />
Expand Down
2 changes: 1 addition & 1 deletion packages/trace-viewer/src/ui/sourceTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const SourceTab: React.FunctionComponent<{
<CopyToClipboard description='Copy filename' value={shortFileName}/>
{location && <ToolbarButton icon='link-external' title='Open in VS Code' onClick={openExternally}></ToolbarButton>}
</Toolbar> }
<CodeMirrorWrapper text={source.content || ''} language='javascript' highlight={highlight} revealLine={targetLine} readOnly={true} lineNumbers={true} dataTestId='source-code-mirror'/>
<CodeMirrorWrapper text={source.content || ''} highlighter='javascript' highlight={highlight} revealLine={targetLine} readOnly={true} lineNumbers={true} dataTestId='source-code-mirror'/>
</div>}
sidebar={<StackTraceView stack={stack} selectedFrame={selectedFrame} setSelectedFrame={setSelectedFrame} />}
/>;
Expand Down
10 changes: 5 additions & 5 deletions packages/web/src/components/codeMirrorWrapper.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,27 @@ class Program
`;

test('highlight JavaScript', async ({ mount }) => {
const component = await mount(<CodeMirrorWrapper text={javascriptSnippet} language='javascript' />);
const component = await mount(<CodeMirrorWrapper text={javascriptSnippet} highlighter='javascript' />);
await expect(component.locator('text="async"').first()).toHaveClass('cm-keyword');
});

test('highlight Python', async ({ mount }) => {
const component = await mount(<CodeMirrorWrapper text={pythonSnippet} language='python' />);
const component = await mount(<CodeMirrorWrapper text={pythonSnippet} highlighter='python' />);
await expect(component.locator('text="async"').first()).toHaveClass('cm-keyword');
});

test('highlight Java', async ({ mount }) => {
const component = await mount(<CodeMirrorWrapper text={javaSnippet} language='java' />);
const component = await mount(<CodeMirrorWrapper text={javaSnippet} highlighter='java' />);
await expect(component.locator('text="public"').first()).toHaveClass('cm-keyword');
});

test('highlight C#', async ({ mount }) => {
const component = await mount(<CodeMirrorWrapper text={csharpSnippet} language='csharp' />);
const component = await mount(<CodeMirrorWrapper text={csharpSnippet} highlighter='csharp' />);
await expect(component.locator('text="public"').first()).toHaveClass('cm-keyword');
});

test('highlight lines', async ({ mount }) => {
const component = await mount(<CodeMirrorWrapper text={javascriptSnippet} language='javascript' highlight={[
const component = await mount(<CodeMirrorWrapper text={javascriptSnippet} highlighter='javascript' highlight={[
{ line: 4, type: 'running' },
{ line: 5, type: 'paused' },
{ line: 6, type: 'error' },
Expand Down
16 changes: 8 additions & 8 deletions packages/web/src/components/codeMirrorWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export type SourceHighlight = {
message?: string;
};

export type Language = 'javascript' | 'python' | 'java' | 'csharp' | 'jsonl' | 'html' | 'css' | 'markdown' | 'yaml';
type CodeMirrorHighlighter = 'javascript' | 'python' | 'java' | 'csharp' | 'jsonl' | 'html' | 'css' | 'markdown' | 'yaml';

export const lineHeight = 20;

export interface SourceProps {
text: string;
language?: Language;
highlighter?: CodeMirrorHighlighter;
mimeType?: string;
linkify?: boolean;
readOnly?: boolean;
Expand All @@ -51,7 +51,7 @@ export interface SourceProps {

export const CodeMirrorWrapper: React.FC<SourceProps> = ({
text,
language,
highlighter,
mimeType,
linkify,
readOnly,
Expand Down Expand Up @@ -85,7 +85,7 @@ export const CodeMirrorWrapper: React.FC<SourceProps> = ({
if (!element)
return;

const mode = languageToMode(language) || mimeTypeToMode(mimeType) || (linkify ? 'text/linkified' : '');
const mode = highlighterToMode(highlighter) || mimeTypeToMode(mimeType) || (linkify ? 'text/linkified' : '');

if (codemirrorRef.current
&& mode === codemirrorRef.current.cm.getOption('mode')
Expand Down Expand Up @@ -113,7 +113,7 @@ export const CodeMirrorWrapper: React.FC<SourceProps> = ({
setCodemirror(cm);
return cm;
})();
}, [modulePromise, codemirror, codemirrorElement, language, mimeType, linkify, lineNumbers, wrapLines, readOnly, isFocused, placeholder]);
}, [modulePromise, codemirror, codemirrorElement, highlighter, mimeType, linkify, lineNumbers, wrapLines, readOnly, isFocused, placeholder]);

React.useEffect(() => {
if (codemirrorRef.current)
Expand Down Expand Up @@ -244,8 +244,8 @@ function mimeTypeToMode(mimeType: string | undefined): string | undefined {
return 'css';
}

function languageToMode(language: Language | undefined): string | undefined {
if (!language)
function highlighterToMode(highlighter: CodeMirrorHighlighter | undefined): string | undefined {
if (!highlighter)
return;
return {
javascript: 'javascript',
Expand All @@ -257,5 +257,5 @@ function languageToMode(language: Language | undefined): string | undefined {
html: 'htmlmixed',
css: 'css',
yaml: 'yaml',
}[language];
}[highlighter];
}
Loading