-
Notifications
You must be signed in to change notification settings - Fork 194
Switch to Monaco Editor #3857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Switch to Monaco Editor #3857
Changes from 5 commits
6bf2545
e8ab897
88f2ca8
c33e5cb
465ca01
05ef3f7
8bbc714
3cfe30b
9bede5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { Card } from '@blueprintjs/core'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import MonacoReactEditor from '@monaco-editor/react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { useCallback } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { EditorProps } from './Editor'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { SOURCE_MONACO_THEME } from './monaco/setupMonaco'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const languageByExtension: Record<string, string> = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c: 'c', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cc: 'cpp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cpp: 'cpp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| css: 'css', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| h: 'cpp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| html: 'html', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| java: 'java', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| js: 'javascript', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| json: 'json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| jsx: 'javascript', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| py: 'python', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ts: 'typescript', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tsx: 'typescript' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const getLanguage = ({ filePath, mode }: Pick<EditorProps, 'filePath' | 'mode'>): string => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (mode) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return mode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const extension = filePath?.split('.').pop()?.toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (extension && languageByExtension[extension]) || 'javascript'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const MonacoEditor: React.FC<EditorProps> = props => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const handleChange = useCallback( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (value: string | undefined, event: unknown) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const newValue = value ?? ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| props.handleEditorValueChange(props.editorTabIndex, newValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| props.handleUpdateHasUnsavedChanges?.(true); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| props.onChange?.(newValue, event); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [props] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <Card className="Editor"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className="row editor-react-ace" data-testid="Editor" id="editor-react-ace"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a hardcoded
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <MonacoReactEditor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className="react-ace" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| height="100%" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| language={getLanguage(props)} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| onChange={handleChange} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| options={{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fontFamily: "'Inconsolata', 'Consolas', monospace", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fontSize: 17, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| folding: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| glyphMargin: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hover: { enabled: false }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lineHeight: 17, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| lineNumbersMinChars: 4, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| minimap: { enabled: false }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| readOnly: props.sessionDetails?.readOnly ?? false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| renderLineHighlight: 'none', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scrollBeyondLastLine: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+51
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| theme={SOURCE_MONACO_THEME} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value={props.editorValue} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| width="100%" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </div> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </Card> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default MonacoEditor; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { fireEvent, render, screen } from '@testing-library/react'; | ||
| import { Provider } from 'react-redux'; | ||
| import { mockInitialStore } from 'src/commons/mocks/StoreMocks'; | ||
| import { | ||
| defaultWorkspaceSettings, | ||
| WorkspaceSettingsContext | ||
| } from 'src/commons/WorkspaceSettingsContext'; | ||
| import { flagMonacoEditorEnable } from 'src/features/monaco/flagMonacoEditorEnable'; | ||
| import { vi } from 'vitest'; | ||
|
|
||
| import EditorContainer, { EditorContainerProps } from '../EditorContainer'; | ||
|
|
||
| vi.mock('../MonacoEditor', () => ({ | ||
| default: (props: any) => ( | ||
| <textarea | ||
| data-testid="MonacoEditorMock" | ||
| onChange={event => props.handleEditorValueChange(props.editorTabIndex, event.target.value)} | ||
| readOnly={props.sessionDetails?.readOnly ?? false} | ||
| value={props.editorValue} | ||
| /> | ||
| ) | ||
| })); | ||
|
|
||
| const createProps = (overrides: Partial<EditorContainerProps> = {}): EditorContainerProps => ({ | ||
| activeEditorTabIndex: 0, | ||
| editorSessionId: '', | ||
| editorTabs: [ | ||
| { | ||
| breakpoints: [], | ||
| editorTabIndex: 0, | ||
| editorValue: 'const x = 1;', | ||
| filePath: '/program.js', | ||
| highlightedLines: [] | ||
| } | ||
| ], | ||
| editorVariant: 'normal', | ||
| handleDeclarationNavigate: () => {}, | ||
| handleEditorEval: () => {}, | ||
| handleEditorUpdateBreakpoints: () => {}, | ||
| handleEditorValueChange: () => {}, | ||
| handlePromptAutocomplete: () => {}, | ||
| isEditorAutorun: false, | ||
| isFolderModeEnabled: false, | ||
| removeEditorTabByIndex: () => {}, | ||
| sessionDetails: null, | ||
| setActiveEditorTabIndex: () => {}, | ||
| ...overrides | ||
| }); | ||
|
|
||
| const renderEditorContainer = ( | ||
| props: EditorContainerProps, | ||
| featureFlags: Record<string, any> = {} | ||
| ) => { | ||
| const store = mockInitialStore({ | ||
| featureFlags: { | ||
| modifiedFlags: featureFlags | ||
| } | ||
| }); | ||
|
|
||
| return render( | ||
| <Provider store={store}> | ||
| <WorkspaceSettingsContext.Provider value={[defaultWorkspaceSettings, () => {}]}> | ||
| <EditorContainer {...props} /> | ||
| </WorkspaceSettingsContext.Provider> | ||
| </Provider> | ||
| ); | ||
| }; | ||
|
|
||
| test('EditorContainer renders Ace editor path when Monaco flag is off', () => { | ||
| renderEditorContainer(createProps()); | ||
| expect(screen.getByTestId('Editor')).toBeTruthy(); | ||
| expect(screen.queryByTestId('MonacoEditorMock')).toBeNull(); | ||
| }); | ||
|
|
||
| test('EditorContainer renders Monaco editor path when Monaco flag is on', async () => { | ||
| renderEditorContainer(createProps(), { | ||
| [flagMonacoEditorEnable.flagName]: true | ||
| }); | ||
| expect(await screen.findByTestId('MonacoEditorMock')).toBeTruthy(); | ||
| }); | ||
|
|
||
| test('Monaco editor path forwards value changes with the active editor tab index', async () => { | ||
| const handleEditorValueChange = vi.fn(); | ||
| renderEditorContainer( | ||
| createProps({ | ||
| handleEditorValueChange | ||
| }), | ||
| { | ||
| [flagMonacoEditorEnable.flagName]: true | ||
| } | ||
| ); | ||
|
|
||
| fireEvent.change(await screen.findByTestId('MonacoEditorMock'), { | ||
| target: { value: 'const y = 2;' } | ||
| }); | ||
|
|
||
| expect(handleEditorValueChange).toHaveBeenCalledWith(0, 'const y = 2;'); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import { fireEvent, render, screen } from '@testing-library/react'; | ||
| import { vi } from 'vitest'; | ||
|
|
||
| import { EditorProps } from '../Editor'; | ||
| import MonacoEditor from '../MonacoEditor'; | ||
|
|
||
| vi.mock('monaco-editor', () => ({ | ||
| editor: { | ||
| defineTheme: vi.fn() | ||
| } | ||
| })); | ||
|
|
||
| vi.mock('@monaco-editor/react', () => ({ | ||
| default: (props: any) => ( | ||
| <textarea | ||
| data-theme={props.theme} | ||
| data-testid="MonacoReactEditorMock" | ||
| onChange={event => props.onChange(event.target.value, { source: 'test' })} | ||
| readOnly={props.options?.readOnly ?? false} | ||
| value={props.value} | ||
| /> | ||
| ), | ||
| loader: { | ||
| config: vi.fn() | ||
| } | ||
| })); | ||
|
|
||
| const createProps = (overrides: Partial<EditorProps> = {}): EditorProps => ({ | ||
| breakpoints: [], | ||
| editorSessionId: '', | ||
| editorTabIndex: 0, | ||
| editorValue: 'const x = 1;', | ||
| handleDeclarationNavigate: () => {}, | ||
| handleEditorEval: () => {}, | ||
| handleEditorUpdateBreakpoints: () => {}, | ||
| handleEditorValueChange: () => {}, | ||
| handlePromptAutocomplete: () => {}, | ||
| highlightedLines: [], | ||
| isEditorAutorun: false, | ||
| sessionDetails: null, | ||
| ...overrides | ||
| }); | ||
|
|
||
| test('MonacoEditor renders the Monaco React editor wrapper', () => { | ||
| render(<MonacoEditor {...createProps()} />); | ||
| const editor = screen.getByTestId('MonacoReactEditorMock') as HTMLTextAreaElement; | ||
| expect(screen.getByTestId('Editor')).toBeTruthy(); | ||
| expect(editor.value).toBe('const x = 1;'); | ||
| expect(editor.dataset.theme).toBe('source'); | ||
| }); | ||
|
|
||
| test('MonacoEditor forwards changes to workspace handlers', () => { | ||
| const handleEditorValueChange = vi.fn(); | ||
| const handleUpdateHasUnsavedChanges = vi.fn(); | ||
| const onChange = vi.fn(); | ||
|
|
||
| render( | ||
| <MonacoEditor | ||
| {...createProps({ | ||
| handleEditorValueChange, | ||
| handleUpdateHasUnsavedChanges, | ||
| onChange | ||
| })} | ||
| /> | ||
| ); | ||
|
|
||
| fireEvent.change(screen.getByTestId('MonacoReactEditorMock'), { | ||
| target: { value: 'const y = 2;' } | ||
| }); | ||
|
|
||
| expect(handleEditorValueChange).toHaveBeenCalledWith(0, 'const y = 2;'); | ||
| expect(handleUpdateHasUnsavedChanges).toHaveBeenCalledWith(true); | ||
| expect(onChange).toHaveBeenCalledWith('const y = 2;', { source: 'test' }); | ||
| }); | ||
|
|
||
| test('MonacoEditor passes session readonly state to Monaco', () => { | ||
| render( | ||
| <MonacoEditor | ||
| {...createProps({ | ||
| sessionDetails: { | ||
| docId: 'doc-id', | ||
| owner: false, | ||
| readOnly: true | ||
| } | ||
| })} | ||
| /> | ||
| ); | ||
|
|
||
| expect(screen.getByTestId('MonacoReactEditorMock').hasAttribute('readonly')).toBe(true); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
useMemoto the React imports to support memoizing the editor options.