Skip to content

Commit 9080d44

Browse files
Initial select up and down functionality
1 parent 6c4d5f8 commit 9080d44

File tree

3 files changed

+78
-11
lines changed

3 files changed

+78
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ None at the time.
1919

2020
### 0.0.1
2121

22-
Initial release including move up and down features.
22+
Initial release including move up and down features

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
],
2626
"activationEvents": [
2727
"onCommand:paragraphjump.up",
28-
"onCommand:paragraphjump.down"
28+
"onCommand:paragraphjump.down",
29+
"onCommand:paragraphjump.selectup",
30+
"onCommand:paragraphjump.selectdown"
2931
],
3032
"main": "./out/extension.js",
3133
"contributes": {
@@ -37,6 +39,14 @@
3739
{
3840
"command": "paragraphjump.down",
3941
"title": "ParagraphJump: Down"
42+
},
43+
{
44+
"command": "paragraphjump.selectup",
45+
"title": "ParagraphJump: Select Up"
46+
},
47+
{
48+
"command": "paragraphjump.selectdown",
49+
"title": "ParagraphJump: Select Down"
4050
}
4151
]
4252
},

src/extension.ts

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const targetLineIsEmptyOrWhitespace = (
66
) => !document.lineAt(line).isEmptyOrWhitespace;
77

88
enum LineOperation {
9-
up = 1,
10-
down = 2,
9+
up = 0,
10+
down = 1,
1111
}
1212

1313
function getNextLine(editor: vscode.TextEditor, op: LineOperation) {
@@ -34,8 +34,30 @@ function getNextLine(editor: vscode.TextEditor, op: LineOperation) {
3434
return document.lineAt(line);
3535
}
3636

37-
function moveCursor(editor: vscode.TextEditor, newPosition: vscode.Position) {
38-
let newCursorPosition = new vscode.Selection(newPosition, newPosition);
37+
enum MoveOperation {
38+
move = 0,
39+
select = 1,
40+
}
41+
42+
function moveCursor(
43+
editor: vscode.TextEditor,
44+
newPosition: vscode.Position,
45+
op: MoveOperation
46+
) {
47+
let newCursorPosition: vscode.Selection;
48+
switch (op) {
49+
case MoveOperation.move:
50+
{
51+
newCursorPosition = new vscode.Selection(newPosition, newPosition);
52+
}
53+
break;
54+
case MoveOperation.select:
55+
{
56+
let anchor = editor.selection.anchor;
57+
newCursorPosition = new vscode.Selection(anchor, newPosition);
58+
}
59+
break;
60+
}
3961
editor.selection = newCursorPosition;
4062
editor.revealRange(new vscode.Range(newPosition, newPosition));
4163
}
@@ -44,22 +66,57 @@ export function activate(context: vscode.ExtensionContext) {
4466
let paragraphJumpUp = vscode.commands.registerTextEditorCommand(
4567
"paragraphjump.up",
4668
(editor: vscode.TextEditor) => {
47-
let targetLine: vscode.TextLine = getNextLine(editor, LineOperation.up);
69+
const targetLine: vscode.TextLine = getNextLine(editor, LineOperation.up);
4870
const newPosition = new vscode.Position(targetLine.lineNumber, 0);
49-
moveCursor(editor, newPosition);
71+
moveCursor(editor, newPosition, MoveOperation.move);
5072
}
5173
);
5274

5375
let paragraphJumpDown = vscode.commands.registerTextEditorCommand(
5476
"paragraphjump.down",
5577
(editor: vscode.TextEditor) => {
56-
let targetLine: vscode.TextLine = getNextLine(editor, LineOperation.down);
78+
const targetLine: vscode.TextLine = getNextLine(
79+
editor,
80+
LineOperation.down
81+
);
82+
const newPosition = new vscode.Position(
83+
targetLine.lineNumber,
84+
targetLine.text.length
85+
);
86+
moveCursor(editor, newPosition, MoveOperation.move);
87+
}
88+
);
89+
90+
let paragraphSelectUp = vscode.commands.registerTextEditorCommand(
91+
"paragraphjump.selectup",
92+
(editor: vscode.TextEditor) => {
93+
const targetLine: vscode.TextLine = getNextLine(editor, LineOperation.up);
5794
const newPosition = new vscode.Position(targetLine.lineNumber, 0);
58-
moveCursor(editor, newPosition);
95+
moveCursor(editor, newPosition, MoveOperation.select);
5996
}
6097
);
6198

62-
context.subscriptions.push(paragraphJumpUp, paragraphJumpDown);
99+
let paragraphSelectDown = vscode.commands.registerTextEditorCommand(
100+
"paragraphjump.selectdown",
101+
(editor: vscode.TextEditor) => {
102+
const targetLine: vscode.TextLine = getNextLine(
103+
editor,
104+
LineOperation.down
105+
);
106+
const newPosition = new vscode.Position(
107+
targetLine.lineNumber,
108+
targetLine.text.length
109+
);
110+
moveCursor(editor, newPosition, MoveOperation.select);
111+
}
112+
);
113+
114+
context.subscriptions.push(
115+
paragraphJumpUp,
116+
paragraphJumpDown,
117+
paragraphSelectUp,
118+
paragraphSelectDown
119+
);
63120
}
64121

65122
export function deactivate() {}

0 commit comments

Comments
 (0)