-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature:
increment/decrementNumber
(#56)
Adds command to increment and decrement numbers.
- Loading branch information
1 parent
05371f8
commit 4bd2bf6
Showing
7 changed files
with
227 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,66 @@ | ||
import * as vscode from 'vscode'; | ||
|
||
interface NumberUpdateArgs { | ||
by?: number; | ||
stepEachSelection?: boolean; | ||
} | ||
|
||
function changeNumberHelper(text: string, index: number, args: NumberUpdateArgs) { | ||
let number = parseInt(text); | ||
const step = args.by || 1; | ||
if (args.stepEachSelection) { | ||
number += step * index; | ||
} else { | ||
number += step; | ||
} | ||
return number.toString(); | ||
} | ||
|
||
function changeNumber(args: NumberUpdateArgs) { | ||
const editor = vscode.window.activeTextEditor; | ||
if (editor) { | ||
const selTexts = editor.selections.map(s => editor.document.getText(s)); | ||
const badTexts = selTexts.filter(s => !/[0-9]/.test(s)); | ||
if (badTexts.length > 0) { | ||
vscode.window.showErrorMessage( | ||
`The selected text '${badTexts[0]}' is not a number.` | ||
); | ||
return; | ||
} | ||
|
||
editor.edit(edit => { | ||
for (let i = 0; i < editor.selections.length; i++) { | ||
edit.replace( | ||
editor.selections[i], | ||
changeNumberHelper(selTexts[i], i, args) | ||
); | ||
} | ||
return edit; | ||
}); | ||
} | ||
} | ||
|
||
export function registerTextModifiers(context: vscode.ExtensionContext) { | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand('selection-utilities.incrementNumber', () => | ||
changeNumber({by: 1}) | ||
) | ||
); | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand('selection-utilities.decrementNumber', () => | ||
changeNumber({by: -1}) | ||
) | ||
); | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand( | ||
'selection-utilities.incrementNumberPerSelection', | ||
() => changeNumber({by: 1, stepEachSelection: true}) | ||
) | ||
); | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand( | ||
'selection-utilities.decrementNumberPerSelection', | ||
() => changeNumber({by: -1, stepEachSelection: true}) | ||
) | ||
); | ||
} |
This file contains 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,129 @@ | ||
import '@wdio/globals'; | ||
import 'wdio-vscode-service'; | ||
import {cleanWhitespace, setupEditor, storeCoverageStats} from './utils.mts'; | ||
import {sleep, TextEditor} from 'wdio-vscode-service'; | ||
|
||
describe('Number changes', () => { | ||
let editor: TextEditor; | ||
|
||
async function setupCursors() { | ||
await browser.executeWorkbench(vscode => { | ||
vscode.commands.executeCommand('selection-utilities.cancelSelection'); | ||
}); | ||
|
||
await editor.moveCursor(1, 1); | ||
|
||
for (let i = 0; i < 3; i++) { | ||
await browser.executeWorkbench(async vscode => { | ||
await vscode.commands.executeCommand('editor.action.insertCursorBelow'); | ||
}); | ||
sleep(100); | ||
} | ||
|
||
await browser.executeWorkbench(vscode => { | ||
vscode.commands.executeCommand('selection-utilities.moveBy', { | ||
unit: 'word', | ||
value: 1, | ||
boundary: 'both', | ||
selectWhole: true, | ||
}); | ||
}); | ||
} | ||
|
||
before(async () => { | ||
editor = await setupEditor(`1 | ||
1 | ||
1 | ||
1 | ||
`); | ||
|
||
await setupCursors(); | ||
}); | ||
|
||
it('can increment numbers', async () => { | ||
await browser.executeWorkbench(async vscode => { | ||
await vscode.commands.executeCommand('selection-utilities.incrementNumber'); | ||
}); | ||
await sleep(100); | ||
expect(await editor.getText()).toEqual( | ||
cleanWhitespace(`2 | ||
2 | ||
2 | ||
2 | ||
`) | ||
); | ||
}); | ||
|
||
it('can decrement numbers', async () => { | ||
await setupCursors(); | ||
await browser.executeWorkbench(async vscode => { | ||
await vscode.commands.executeCommand('selection-utilities.decrementNumber'); | ||
}); | ||
await sleep(100); | ||
expect(await editor.getText()).toEqual( | ||
cleanWhitespace(`1 | ||
1 | ||
1 | ||
1 | ||
`) | ||
); | ||
}); | ||
|
||
it('can increment numbers per selection', async () => { | ||
await setupCursors(); | ||
await browser.executeWorkbench(async vscode => { | ||
await vscode.commands.executeCommand( | ||
'selection-utilities.incrementNumberPerSelection' | ||
); | ||
}); | ||
expect(await editor.getText()).toEqual( | ||
cleanWhitespace(`1 | ||
2 | ||
3 | ||
4 | ||
`) | ||
); | ||
}); | ||
|
||
it('can decrement numbers per selection', async () => { | ||
await setupCursors(); | ||
await browser.executeWorkbench(async vscode => { | ||
await vscode.commands.executeCommand( | ||
'selection-utilities.decrementNumberPerSelection' | ||
); | ||
}); | ||
expect(await editor.getText()).toEqual( | ||
cleanWhitespace(`1 | ||
1 | ||
1 | ||
1 | ||
`) | ||
); | ||
}); | ||
|
||
it('errors with bad selection', async () => { | ||
editor = await setupEditor('a\n'); | ||
await browser.executeWorkbench(async vscode => { | ||
await vscode.commands.executeCommand('selection-utilities.moveBy', { | ||
unit: 'word', | ||
value: 1, | ||
boundary: 'both', | ||
selectWhole: true, | ||
}); | ||
}); | ||
|
||
await browser.executeWorkbench(async vscode => { | ||
await vscode.commands.executeCommand('selection-utilities.incrementNumber'); | ||
}); | ||
|
||
const workbench = await browser.getWorkbench(); | ||
const notifs = await Promise.all( | ||
(await workbench.getNotifications()).map(n => n.getMessage()) | ||
); | ||
expect(notifs).toContain("The selected text 'a' is not a number."); | ||
}); | ||
|
||
after(async () => { | ||
await storeCoverageStats('changeNumbers'); | ||
}); | ||
}); |
This file contains 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