Skip to content

Add auto indentation to the current code editor - #59

Open
CX330Blake wants to merge 1 commit into
sysprog21:mainfrom
CX330Blake:c-branch-1
Open

CX330Blake wants to merge 1 commit into
sysprog21:mainfrom
CX330Blake:c-branch-1

Conversation

@CX330Blake

@CX330Blake CX330Blake commented Sep 17, 2026

Copy link
Copy Markdown

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

  • Trailing comments are not parsed syntax-aware, so block openers before
    a trailing comment like int func() { // may not trigger the expected
    indentation.
  • Continuation lines inside multi-line block comments are not detected
    as comment-only lines.

These cases are intentionally left for a follow-up to keep this change
lightweight.

@CX330Blake
CX330Blake marked this pull request as ready for review September 17, 2026 11:00
cubic-dev-ai[bot]

This comment was marked as resolved.

Comment thread web/editor.js Outdated
Comment thread tests/browser/editor.test.js Outdated
cubic-dev-ai[bot]

This comment was marked as resolved.

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Read https://chris.beams.io/git-commit carefully and enforce the rules.

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I think this is heading in the right direction, just left a few small suggestions and minor issues to address.

Comment thread web/editor.js
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);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine to handle as a follow-up, but I'd suggest mentioning it in the PR description for future reference.

Comment thread web/editor.js Outdated
const indentation = before.match(/^[ \t]*/)[0];
const opener = before.trimEnd().slice(-1);
const closer = { "{": "}", "[": "]", "(": ")" }[opener];
const comment = /^[ \t]*(#|\/\/|\/\*)/.test(before);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving forward, it might be better to have a dedicated helper that detects the programming language first, rather than trying to cover every edge case with a single regex.

Otherwise, if we also need to account for C++, #define might trigger a false positive.

);
}
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ColtenOuO left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, some of the intermediate review commits can be squashed together.

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.

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread web/editor.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants