fix(indent): re-indent dedent keywords as they are typed (#2582) - #2644
Open
sinelaw wants to merge 1 commit into
Open
fix(indent): re-indent dedent keywords as they are typed (#2582)#2644sinelaw wants to merge 1 commit into
sinelaw wants to merge 1 commit into
Conversation
Live "electric" dedent only fired for closing brackets (`}`): typing a keyword that matches a language's `decrease_indent_pattern` — Python `else:`/`elif`/`except`/`finally`/`case`, Ruby/Lua/Fish/Pascal `end`, Bash `fi`/`done`/`esac`, or a user's custom token — left the line stuck at its inherited indent until the next Enter or save. The mis-indent then compounded: pressing Enter after the over-indented keyword pushed the whole block another level right. Add `IndentRules::calculate_dedent_for_typed_char`, the keyword counterpart of the existing `calculate_dedent_for_delimiter`, and hook it into the character-insert path so a completed dedent keyword snaps the line one level shallower — the same correction brackets already got. The hook is gated to the unambiguous case (no active selection, cursor at end of the line's content) and is idempotent, so it adds no redundant undo step once the indent is correct. Covered by unit tests on the rules method across families and by end-to-end typing tests that drive the production insert pipeline.
sinelaw
force-pushed
the
claude/compassionate-newton-5qlbzs
branch
from
July 17, 2026 11:56
e1934a7 to
eac3c32
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2582.
Problem
Live "electric" dedent only fired for closing brackets (
}). Typing a keyword that matches a language'sdecrease_indent_pattern— Pythonelse:/elif/except/finally/case, Ruby/Lua/Fish/Pascalend, Bashfi/done/esac, or a user's custom token — left the line stuck at its inherited indent:The error then compounded: pressing Enter after the mis-indented
else:indented the next line another level deeper, so the whole block drifted right. Dedent rules were applied only when Enter split text onto a new line, never when the keyword was typed — even though Fresh already gave}this treatment, and the docs' owndecrease_indent_patternexamples (end,CLOSE) are tokens you type at the end of a block, never Enter-split.Fix
IndentRules::calculate_dedent_for_typed_charinindent_rules.rs— the keyword counterpart of the existingcalculate_dedent_for_delimiter(which handles brackets). It matches the line's leading content (after the just-typed char) against the language'sdecreaserule and returns the target indent, aligned to the reference line one level up.insert_char_events) so a completed dedent keyword snaps the line one level shallower, mirroring the existing closing-brackethandle_auto_dedent.The hook is deliberately conservative:
Only the per-language regex rules tier is touched; curly-brace languages keep their bracket-only decrease rule (their
}is handled by the existing path), so nothing changes for them.Testing
indent_rules.rs):else/except/finally/case(Python, incl. nested),end(Ruby/Lua),fi/done(Bash); plus negatives — an identifier likeelsewhere, an ordinary statement, a curly-brace language, and a first line with no reference above.actions.rs): drive the productionAction::InsertCharpipeline key-by-key; verifyelse:dedents to column 0 and that the following Enter indents the body to column 4 (not the old drifted 8).freshbinary in tmux on a Python file, typedelse:, and confirmed viacapture-panethat the line dedents to column 0 and its body lands at column 4.cargo fmt,cargo clippy, andcargo check --all-targetsall clean; existing indent / auto-pair / auto-indent scenario suites pass.🤖 Generated with Claude Code
https://claude.ai/code/session_01DvjnQtGxsMn3UKywougFDd
Generated by Claude Code