Overview
Gupshup's internal developer tools require a lightweight, performant code editor for quick script authoring and testing. This problem focuses on building the core editing experience, providing immediate visual feedback to the user through syntax highlighting and basic real-time linting. This component is crucial for improving developer efficiency and reducing common syntax errors in small code snippets.
What you need to do / Task
Your task is to build a CodeEditor React component that offers a basic yet functional editing experience with visual feedback.
- Editor Structure: Create a resizable or fixed-size editor area that displays code. It should consist of two main synchronized parts: line numbers on the left and the editable code content on the right.
- Line Numbers: Dynamically display line numbers that correspond to the code lines in the editor. These numbers must scroll synchronously with the code content.
- Basic Syntax Highlighting: Implement real-time syntax highlighting for common JavaScript-like constructs:
- Keywords:
const, let, function, return, if, else, for, while, class
- Strings: Text enclosed in single (
') or double (") quotes.
- Comments: Single-line comments starting with
//.
- Real-time Linting: As the user types, identify and highlight lines that are missing a semicolon (
;) at the end. A line is considered to be missing a semicolon if it is not empty, does not end with { or }, and does not end with // (start of a comment).
- Error Indicators: For each line identified with a linting error, display a small, visually distinct indicator (e.g., a red dot or icon) next to its corresponding line number.
- Synchronization: Ensure smooth and accurate synchronization of scrolling, line height, and content display between the editable
textarea, the line numbers, and the syntax-highlighted overlay.
UI/UX expectations
- The editor should present a clean, minimalist interface.
- Line numbers should be visually distinct (e.g., a lighter grey color) from the code content.
- Syntax highlighting should use at least three distinct colors: one for keywords, one for strings, and one for comments. (Feel free to choose your own palette).
- Linting error indicators should be clearly visible and positioned next to the line numbers.
- The editor should use a monospace font for consistent character and line spacing.
- The entire editor component (line numbers + code area) should have a reasonable fixed height with vertical scrolling for overflow.
Acceptance criteria
- The editor component renders successfully with initial code.
- Line numbers are correctly displayed for all lines of code and update as lines are added/removed.
- Scrolling the code area also scrolls the line numbers and highlighted content synchronously.
- Keywords (
const, function), strings ("hello", 'world'), and comments (// a comment) are highlighted with distinct colors.
- Typing
let x = 1 immediately shows a linting error indicator next to that line's number.
- Modifying
let x = 1 to let x = 1; immediately removes the linting error indicator.
- Lines ending with
{ (e.g., function foo() {), } (e.g., }), or consisting solely of a comment (e.g., // This is fine) do not show a linting error.
- Empty lines do not show a linting error.
- A line like
const x = 1 // some comment does show a linting error, as the statement const x = 1 is missing a semicolon.