-
Notifications
You must be signed in to change notification settings - Fork 8
Add auto indentation to the current code editor #59
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: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -1,5 +1,41 @@ | ||
| const INDENT = " "; | ||
|
|
||
| // TODO: The current implementation cannot handle cases like: | ||
| // /* | ||
| // * comment | ||
| // */ | ||
|
CX330Blake marked this conversation as resolved.
|
||
| function isCommentOnlyLine(line, language) { | ||
| if (language === "python") { | ||
| return /^[ \t]*#/.test(line); | ||
| } | ||
| return /^[ \t]*(\/\/|\/\*)/.test(line); | ||
| } | ||
|
|
||
| export function indentNewline(value, start, end, language) { | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: When Enter is pressed after a Python block with a trailing comment, this loses the required nested indentation because Prompt for AI agents
Author
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. Good catch. This is indeed an edge case, but I think it's acceptable to leave it as-is for now. Supporting trailing Python comments correctly would require more syntax-aware parsing, and I'd prefer to avoid adding that complexity for a relatively uncommon case in this PR.
Collaborator
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. This is fine to handle as a follow-up, but I'd suggest mentioning it in the PR description for future reference. |
||
| const closer = { "{": "}", "[": "]", "(": ")" }[opener]; | ||
| const comment = isCommentOnlyLine(before, language); | ||
| const nested = !comment && (Boolean(closer) || (language === "python" && opener === ":")); | ||
| const innerIndent = indentation + (nested ? INDENT : ""); | ||
| let insertion = `\n${innerIndent}`; | ||
| const caret = start + insertion.length; | ||
| const after = value.slice(end); | ||
| const trailingSpace = after.match(/^[ \t]*/)[0].length; | ||
| // If the caret is between an opener and a closer, the indentation will be like this: | ||
| // | ||
| // if (a != b) { | ||
| // | <------- caret | ||
| // } | ||
| if (!comment && closer && after[trailingSpace] === closer) { | ||
| insertion += `\n${indentation}`; | ||
| end += trailingSpace; | ||
| } | ||
| return { value: value.slice(0, start) + insertion + value.slice(end), start: caret, end: caret }; | ||
| } | ||
|
|
||
| export function indentSelection(value, start, end, outdent = false) { | ||
| // Not lastIndexOf alone: a negative fromIndex clamps to 0 and still matches | ||
| // there, so a document that opens with a blank line would resolve the | ||
|
|
||
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.
I'd suggest adding some tests for the newly added comment handling.
ex.