-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Support resize and vertical resize command #9726
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
Woodii1998
wants to merge
8
commits into
VSCodeVim:master
Choose a base branch
from
Woodii1998:feat/supportVertical
base: master
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.
+880
−2
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4c0656e
feat: support resize and vertical
Woodii1998 251203d
update
Woodii1998 5167226
update
Woodii1998 1875ccf
You can't see it, but I'm making a very angry face right now
Woodii1998 59dd78c
Pro Tip: Use Copilot more
Woodii1998 2a8b01c
Enhance ResizeCommand: Add status message for unsupported usage witho…
Woodii1998 6962aa3
Refactor vertical command tests: Simplify error handling and assert s…
Woodii1998 9357683
Apply suggestion from @AzimovParviz
Woodii1998 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { Parser, optWhitespace, seq, regexp, alt } from 'parsimmon'; | ||
import * as vscode from 'vscode'; | ||
import { VimState } from '../../state/vimState'; | ||
import { ExCommand } from '../../vimscript/exCommand'; | ||
import { StatusBar } from '../../statusBar'; | ||
|
||
export interface IResizeCommandArguments { | ||
direction?: '+' | '-'; | ||
value?: number; | ||
absoluteValue?: number; | ||
} | ||
|
||
/** | ||
* Implements :resize command | ||
* The :resize command is used to change the height of the current window | ||
* | ||
* Examples: | ||
* :resize +5 - increase window height by 5 rows | ||
* :resize -3 - decrease window height by 3 rows | ||
*/ | ||
export class ResizeCommand extends ExCommand { | ||
public static readonly argParser: Parser<ResizeCommand> = seq( | ||
optWhitespace, | ||
alt( | ||
// Parse absolute values like ":resize 20" | ||
regexp(/\d+/).map((num) => ({ absoluteValue: parseInt(num, 10) })), | ||
// Parse relative values like ":resize +5" or ":resize -3" | ||
seq(regexp(/[+-]/), regexp(/\d+/)).map(([direction, num]) => ({ | ||
direction: direction as '+' | '-', | ||
value: parseInt(num, 10), | ||
})), | ||
// Empty args defaults to maximize | ||
optWhitespace.map(() => ({})), | ||
), | ||
).map(([, args]) => new ResizeCommand(args)); | ||
|
||
private readonly arguments: IResizeCommandArguments; | ||
|
||
constructor(args: IResizeCommandArguments) { | ||
super(); | ||
this.arguments = args; | ||
} | ||
|
||
async execute(vimState: VimState): Promise<void> { | ||
const { direction, value, absoluteValue } = this.arguments; | ||
|
||
// Handle absolute resize | ||
if (absoluteValue !== undefined) { | ||
// VSCode doesn't support setting absolute window heights | ||
StatusBar.setText( | ||
vimState, | ||
`VSCode doesn't support setting exact row heights. Use relative resize (+/-) instead.`, | ||
); | ||
return; | ||
} | ||
|
||
// Handle relative resize | ||
if (direction && value !== undefined) { | ||
// A value of 0 should be a no-op | ||
if (value === 0) { | ||
return; | ||
} | ||
const command = | ||
direction === '+' | ||
? 'workbench.action.increaseViewHeight' | ||
: 'workbench.action.decreaseViewHeight'; | ||
|
||
// Use runCommands for better performance with multiple executions | ||
if (value > 1) { | ||
const commands = Array(value).fill(command); | ||
await vscode.commands.executeCommand('runCommands', { commands }); | ||
} else { | ||
await vscode.commands.executeCommand(command); | ||
} | ||
return; | ||
} | ||
|
||
// TODO: Default behavior (no arguments) - toggle panel to maximize editor height | ||
StatusBar.setText( | ||
vimState, | ||
'resize does not currently support running without parameters. Please use :resize +N or :resize -N to adjust the height.', | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { Parser, all, optWhitespace } from 'parsimmon'; | ||
import { VimState } from '../../state/vimState'; | ||
import { ExCommand } from '../../vimscript/exCommand'; | ||
import { FileCommand } from './file'; | ||
import * as vscode from 'vscode'; | ||
import { StatusBar } from '../../statusBar'; | ||
import { VimError, ErrorCode } from '../../error'; | ||
|
||
export interface IVerticalCommandArguments { | ||
/** The command following :vertical (e.g., "split", "new filename", "resize +5") */ | ||
command: string; | ||
} | ||
|
||
/** | ||
* Implements :vertical command | ||
* The :vertical command modifier forces the following command to be executed | ||
* in a vertical split manner instead of horizontal. | ||
* | ||
* Currently supported commands: | ||
* - split: Create vertical split instead of horizontal split | ||
* - new: Create new file in vertical split instead of horizontal split | ||
* - resize: Adjust window width instead of height | ||
* | ||
* Examples: | ||
* :vertical split - Create vertical split of current file | ||
* :vertical split filename - Create vertical split and open filename | ||
* :vertical new - Create vertical split with new untitled file | ||
* :vertical new filename - Create vertical split with new file named filename | ||
* :vertical resize +5 - Increase current window width by 5 columns | ||
* :vertical resize -3 - Decrease current window width by 3 columns | ||
* | ||
* Note: For other commands (like help), :vertical sets a modifier flag that | ||
* compatible commands can check, but many commands are not yet implemented | ||
* in this Vim extension. | ||
*/ | ||
export class VerticalCommand extends ExCommand { | ||
public static readonly argParser: Parser<VerticalCommand> = optWhitespace | ||
.then(all) | ||
.map((command) => new VerticalCommand({ command })); | ||
|
||
private readonly arguments: IVerticalCommandArguments; | ||
|
||
constructor(args: IVerticalCommandArguments) { | ||
super(); | ||
this.arguments = args; | ||
} | ||
|
||
async execute(vimState: VimState): Promise<void> { | ||
const command = this.arguments.command.trim(); | ||
|
||
if (!command) { | ||
// :vertical without a command is not meaningful | ||
StatusBar.displayError(vimState, VimError.fromCode(ErrorCode.ArgumentRequired)); | ||
return; | ||
} | ||
|
||
// Handle specific commands that we know support vertical modification | ||
if (command === 'split' || command.startsWith('split ')) { | ||
// Parse as a split command but force vertical behavior | ||
const splitArgs = command.substring(5).trim(); // Remove 'split' | ||
let fileCommand: FileCommand; | ||
|
||
if (splitArgs === '') { | ||
// :vertical split (no file) | ||
fileCommand = new FileCommand({ name: 'vsplit', opt: [] }); | ||
} else { | ||
// :vertical split filename | ||
fileCommand = new FileCommand({ name: 'vsplit', opt: [], file: splitArgs }); | ||
} | ||
|
||
await fileCommand.execute(vimState); | ||
return; | ||
} | ||
|
||
if (command === 'new' || command.startsWith('new ')) { | ||
// Parse as a new command but force vertical behavior | ||
const newArgs = command.substring(3).trim(); // Remove 'new' | ||
let fileCommand: FileCommand; | ||
|
||
if (newArgs === '') { | ||
// :vertical new (no file) | ||
fileCommand = new FileCommand({ name: 'vnew', opt: [] }); | ||
} else { | ||
// :vertical new filename | ||
fileCommand = new FileCommand({ name: 'vnew', opt: [], file: newArgs }); | ||
} | ||
|
||
await fileCommand.execute(vimState); | ||
return; | ||
} | ||
|
||
if (command === 'resize' || command.startsWith('resize ')) { | ||
// Handle :vertical resize - change window width instead of height | ||
const resizeArgs = command.substring(6).trim(); // Remove 'resize' | ||
|
||
// Parse resize arguments | ||
let direction: '+' | '-' | undefined; | ||
let value: number | undefined; | ||
let absoluteValue: number | undefined; | ||
|
||
if (resizeArgs === '') { | ||
// :vertical resize (no args) - maximize width | ||
// Use editor group commands instead of panel commands | ||
await vscode.commands.executeCommand('workbench.action.toggleEditorWidths'); | ||
return; | ||
} | ||
|
||
// Parse arguments | ||
const match = resizeArgs.match(/^([+-]?)(\d+)$/); | ||
if (match) { | ||
const [, dir, num] = match; | ||
if (dir) { | ||
direction = dir as '+' | '-'; | ||
value = parseInt(num, 10); | ||
} else { | ||
absoluteValue = parseInt(num, 10); | ||
} | ||
} else { | ||
// Invalid argument (e.g., non-numeric or unexpected chars) | ||
StatusBar.displayError( | ||
vimState, | ||
VimError.fromCode(ErrorCode.InvalidArgument474, resizeArgs), | ||
); | ||
return; | ||
} | ||
|
||
// Execute width resize commands | ||
if (absoluteValue !== undefined) { | ||
// VSCode doesn't support setting absolute window widths | ||
StatusBar.setText( | ||
vimState, | ||
`VSCode doesn't support setting exact column widths. Use relative resize (+/-) instead.`, | ||
); | ||
return; | ||
} else if (direction && value !== undefined) { | ||
// A value of 0 should be a no-op | ||
if (value === 0) { | ||
return; | ||
} | ||
const resizeCommand = | ||
direction === '+' | ||
? 'workbench.action.increaseViewWidth' | ||
: 'workbench.action.decreaseViewWidth'; | ||
|
||
// Use runCommands for better performance with multiple executions | ||
if (value > 1) { | ||
const commands = Array(value).fill(resizeCommand); | ||
await vscode.commands.executeCommand('runCommands', { commands }); | ||
} else { | ||
await vscode.commands.executeCommand(resizeCommand); | ||
} | ||
} | ||
|
||
return; | ||
} | ||
|
||
// For other commands that we don't explicitly support | ||
StatusBar.displayError( | ||
vimState, | ||
VimError.fromCode(ErrorCode.NotAnEditorCommand, `vertical ${command}`), | ||
); | ||
} | ||
} |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.