-
-
Notifications
You must be signed in to change notification settings - Fork 74
cycle-spacing
#2513
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
Open
whitphx
wants to merge
2
commits into
main
Choose a base branch
from
feature/cycle-spacing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
cycle-spacing
#2513
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -74,6 +74,145 @@ export class DeleteHorizontalSpace extends EmacsCommand { | |||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| export class CycleSpacing extends EmacsCommand { | ||||||||||
| public readonly id = "cycleSpacing"; | ||||||||||
|
|
||||||||||
| private cycleState = 0; // 0: one space, 1: no space, 2: restore | ||||||||||
| private originalSpacing: Array<{ before: string; after: string; from: number }> = []; | ||||||||||
| private latestTextEditor: TextEditor | null = null; | ||||||||||
| private latestSelections: readonly vscode.Selection[] = []; | ||||||||||
|
|
||||||||||
| public run(textEditor: TextEditor, isInMarkMode: boolean, prefixArgument: number | undefined): Thenable<void> { | ||||||||||
| // Check if this is a continuation of the previous cycle-spacing call | ||||||||||
| const isContinuation = | ||||||||||
| this.latestTextEditor === textEditor && | ||||||||||
| this.latestSelections.length === textEditor.selections.length && | ||||||||||
| this.latestSelections.every((latestSelection, i) => { | ||||||||||
| const activeSelection = textEditor.selections[i]; | ||||||||||
| return activeSelection && latestSelection.isEqual(activeSelection); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| if (!isContinuation) { | ||||||||||
| // Start a new cycle | ||||||||||
| this.cycleState = 0; | ||||||||||
| this.originalSpacing = []; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const currentState = this.cycleState; | ||||||||||
|
|
||||||||||
| return textEditor | ||||||||||
| .edit((editBuilder) => { | ||||||||||
| textEditor.selections.forEach((selection, index) => { | ||||||||||
| const line = selection.active.line; | ||||||||||
| const lineText = textEditor.document.lineAt(line).text; | ||||||||||
| const cursorChar = selection.active.character; | ||||||||||
|
|
||||||||||
| if (currentState === 0 || currentState === 1) { | ||||||||||
| // Find the range of spaces/tabs around the cursor for states 0 and 1 | ||||||||||
| let from = cursorChar; | ||||||||||
| while (from > 0) { | ||||||||||
| const char = lineText[from - 1]; | ||||||||||
| if (char !== " " && char !== "\t") { | ||||||||||
| break; | ||||||||||
| } | ||||||||||
| from -= 1; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| let to = cursorChar; | ||||||||||
| const lineEnd = lineText.length; | ||||||||||
| while (to < lineEnd) { | ||||||||||
| const char = lineText[to]; | ||||||||||
| if (char !== " " && char !== "\t") { | ||||||||||
| break; | ||||||||||
| } | ||||||||||
| to += 1; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Store original spacing on first call of the cycle | ||||||||||
| if (currentState === 0) { | ||||||||||
| const beforeSpacing = lineText.substring(from, cursorChar); | ||||||||||
| const afterSpacing = lineText.substring(cursorChar, to); | ||||||||||
| this.originalSpacing[index] = { before: beforeSpacing, after: afterSpacing, from }; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const range = new Range(line, from, line, to); | ||||||||||
|
|
||||||||||
| if (currentState === 0) { | ||||||||||
| // First call: delete all but one space | ||||||||||
| editBuilder.replace(range, " "); | ||||||||||
| } else { | ||||||||||
| // Second call: delete all spaces | ||||||||||
| editBuilder.delete(range); | ||||||||||
| } | ||||||||||
| } else { | ||||||||||
| // Third call (state 2): restore original spacing | ||||||||||
| const original = this.originalSpacing[index]; | ||||||||||
| if (original) { | ||||||||||
| // Always use insert at the original position | ||||||||||
| editBuilder.insert(new vscode.Position(line, original.from), original.before + original.after); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| }); | ||||||||||
| }) | ||||||||||
| .then((success) => { | ||||||||||
| if (!success) { | ||||||||||
| logger.warn("cycleSpacing failed"); | ||||||||||
| } | ||||||||||
| }) | ||||||||||
| .then(() => { | ||||||||||
| // Update cursor positions based on the state | ||||||||||
| textEditor.selections = textEditor.selections.map((selection, index) => { | ||||||||||
| const line = selection.active.line; | ||||||||||
| const original = this.originalSpacing[index]; | ||||||||||
|
|
||||||||||
| if (currentState === 0) { | ||||||||||
| // After replacing with one space, cursor should be after the space | ||||||||||
| if (original) { | ||||||||||
| const newPos = new vscode.Position(line, original.from + 1); | ||||||||||
| return new Selection(newPos, newPos); | ||||||||||
| } | ||||||||||
| } else if (currentState === 1) { | ||||||||||
| // After deleting all spaces, cursor at the position where spaces were | ||||||||||
| if (original) { | ||||||||||
| const newPos = new vscode.Position(line, original.from); | ||||||||||
| return new Selection(newPos, newPos); | ||||||||||
| } | ||||||||||
| } else { | ||||||||||
| // After restoring original spacing, place cursor at original position | ||||||||||
| if (original) { | ||||||||||
| const newPos = new vscode.Position(line, original.from + original.before.length); | ||||||||||
| return new Selection(newPos, newPos); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
||||||||||
| } | |
| } | |
| // This fallback should never be reached because all three states (0, 1, 2) are explicitly handled above. | |
| // If reached, it indicates an unexpected state; returning the current selection unchanged as a safeguard. |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
The whitespace detection logic is duplicated in two while loops. Consider extracting this into a helper function like
isWhitespace(char: string): booleanto improve maintainability and make the code more DRY.