Skip to content

Complete Home key toggle functionality between line start and indentation#199

Merged
lhecker merged 3 commits intomicrosoft:mainfrom
achaljhawar:main
May 21, 2025
Merged

Complete Home key toggle functionality between line start and indentation#199
lhecker merged 3 commits intomicrosoft:mainfrom
achaljhawar:main

Conversation

@achaljhawar
Copy link
Contributor

Complete Home key toggle functionality

Problem (#81)

Edit has a "smart home" feature that moves from text to indentation to column 0, but lacks the ability to move from column 0 back to indentation when pressing Home.

Solution

This PR completes the bidirectional toggle behavior by adding the missing functionality to move from column 0 to the first non-whitespace character, matching VSCode and Visual Studio behavior.

Implementation

  • Added condition in Home key handler to check if cursor is at column 0
  • Reuses existing indent_end_logical_pos() function
  • Maintains Shift+Home selection support

Files Changed

  • src/tui.rs: Added missing condition in Home key handler

Testing

Verified toggle works in both directions with and without selection.

src/tui.rs Outdated
Comment on lines +2421 to +2422
// If the cursor is at the start of the line (column 0), move it to the indentation position
else if logical_after.x == 0 && indent_end.x > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Now that I'm looking at this code, I notice that indent_end is only needed if logical_after.x == 0. Do you mind wrapping both if conditions into a single block like this?

if logical_after.x == 0 {
    let indent_end = tb.indent_end_logical_pos();

    if logical_before > indent_end {
        // ...
    } else if indent_end.x > 0 {
        // ...
    }
}

The reason for this is that seeking backwards in the buffer is a lot more expensive than seeking forwards. This means that indent_end_logical_pos is an "expensive" call (relatively speaking) if the current cursor is not at the line start. logical_after.x == 0 means that the cursor is at the line start and so that means the call is cheap.

Copy link
Member

@lhecker lhecker left a comment

Choose a reason for hiding this comment

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

Wait, I just tested your changes, and they unfortunately don't work. 😅

// we move it back to the end of the indentation.
if logical_after.x == 0
&& let indent_end = tb.indent_end_logical_pos()
&& (logical_before > indent_end || logical_before.x == 0)
Copy link
Member

Choose a reason for hiding this comment

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

Because I was curious about it, I tried implementing it myself. The correct approach is checking for logical_before.x == 0.

I also made the optimization I described above, by moving the indent_end_logical_pos call into the let chain.

@lhecker lhecker merged commit 3a47f47 into microsoft:main May 21, 2025
1 check passed
diabloproject pushed a commit to diabloproject/edit that referenced this pull request May 29, 2025
…tion (microsoft#199)

Closes microsoft#81

Co-authored-by: Leonard Hecker <leonard@hecker.io>
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