Add auto indentation to the current code editor - #59
CX330Blake wants to merge 1 commit into
Conversation
2fd3c0f to
34af192
Compare
76f3fc3 to
7ccb3eb
Compare
jserv
left a comment
There was a problem hiding this comment.
Read https://chris.beams.io/git-commit carefully and enforce the rules.
ColtenOuO
left a comment
There was a problem hiding this comment.
Overall, I think this is heading in the right direction, just left a few small suggestions and minor issues to address.
| const lineStart = start === 0 ? 0 : value.lastIndexOf("\n", start - 1) + 1; | ||
| const before = value.slice(lineStart, start); | ||
| const indentation = before.match(/^[ \t]*/)[0]; | ||
| const opener = before.trimEnd().slice(-1); |
There was a problem hiding this comment.
This is fine to handle as a follow-up, but I'd suggest mentioning it in the PR description for future reference.
| ); | ||
| } | ||
| }); | ||
|
|
There was a problem hiding this comment.
I'd suggest adding some tests for the newly added comment handling.
ex.
[" # Steps:", "python"]
[" // setup {", "javascript"]
["if ready: # explain", "python"]
["run(); // setup {", "javascript"]
ColtenOuO
left a comment
There was a problem hiding this comment.
By the way, some of the intermediate review commits can be squashed together.
7ccb3eb to
0164ead
Compare
ColtenOuO
left a comment
There was a problem hiding this comment.
Would it be worth adding a browser integration test (actually interact with the textarea via Playwright)? Right now, we can only verify whether individual methods work correctly, but we can't validate if the full end-to-end user behavior in the browser matches our expectations.
It might be worth adding because the entire flow actually involves multiple steps. Testing just one part of those steps might not give us comprehensive coverage.
Reduce manual formatting during interviews by preserving the current line indentation and adding one level after opening delimiters and Python block statements. Keep delimiter pairs readable by moving matching closing delimiters onto a separate line when appropriate. Avoid treating comment-only lines as block openers so comments do not introduce unexpected indentation. Add regression tests to cover indentation behavior, comments, and C++ preprocessor directives.
0164ead to
7121790
Compare
Added Playwright integration tests that interact with the actual editor textarea, press Enter, and verify indentation and caret position across nine cases. They also check syntax highlighting, line numbers, undo/redo, and code retention when switching languages. |
ColtenOuO
left a comment
There was a problem hiding this comment.
I think we are almost there.
| if (language === "python") { | ||
| return /^[ \t]*#/.test(line); | ||
| } | ||
| return /^[ \t]*(\/\/|\/\*)/.test(line); |
There was a problem hiding this comment.
isCommentOnlyLine() treats any C-like line starting with /* as comment-only, even when the comment closes and code follows.
For example, pressing Enter after /* guard */ if (ready) { preserves the current indentation instead of adding one level.
Could we either handle text after */ or explicitly track this case in #61?
There was a problem hiding this comment.
Since this is not a common case people are likely to encounter in normal coding, I think we can leave it as a follow-up. I've added this case to #61 for tracking. Thanks for reviewing!
ColtenOuO
left a comment
There was a problem hiding this comment.
LGTM, thanks for your contribution and patience!
| @@ -1,5 +1,41 @@ | |||
| const INDENT = " "; | |||
|
|
|||
| // TODO: The current implementation cannot handle cases like: | |||
There was a problem hiding this comment.
The repository carries no other TODO in first-party code, and this one records a limitation the pull request description already states, so once this merges it is a marker nothing tracks. Say what isCommentOnlyLine actually does instead: it classifies on the first token of a single line and carries no block comment state, which is why a continuation line inside /* ... */ reads as code.
| if (language === "python") { | ||
| return /^[ \t]*#/.test(line); | ||
| } | ||
| return /^[ \t]*(\/\/|\/\*)/.test(line); |
There was a problem hiding this comment.
This matches any line whose first token opens a block comment, even when the comment closes and real code follows, so /* seed */ if (ready) { is classified as a comment and the next line loses its level. Requiring the comment to stay open on the line keeps the intended cases and drops this one.
| return /^[ \t]*(\/\/|\/\*)/.test(line); | |
| return /^[ \t]*(\/\/|\/\*(?!.*\*\/))/.test(line); |
| const lineStart = start === 0 ? 0 : value.lastIndexOf("\n", start - 1) + 1; | ||
| const before = value.slice(lineStart, start); | ||
| const indentation = before.match(/^[ \t]*/)[0]; | ||
| const opener = before.trimEnd().slice(-1); |
There was a problem hiding this comment.
opener reads the raw line, so a trailing comment that ends in a delimiter or a colon adds a level the code never opened: work(); // { in JavaScript and value = 1 # note: in Python both indent the next line. The description covers the opposite direction (a real opener hidden before a trailing comment); this direction costs the candidate a deletion on every such line, so cutting the comment tail before reading the last token is worth doing here rather than in a follow-up.
Summary
Reduce manual formatting during interviews by preserving the current
line indentation and adding one level after opening delimiters and
Python block statements.
Keep delimiter pairs readable by moving matching closing delimiters onto
a separate line when appropriate. Avoid treating comment-only lines as
block openers so comments do not introduce unexpected indentation.
Add regression tests to cover indentation behavior, comments, and C++
preprocessor directives.
Known Limitations
a trailing comment like
int func() { //may not trigger the expectedindentation.
as comment-only lines.
These cases are intentionally left for a follow-up to keep this change
lightweight.