Skip to content

Commit 582dfd6

Browse files
Merge pull request #2 from brokenprogrammer/feature/paragraph-select
Feature/paragraph select
2 parents 6c4d5f8 + 14b1b72 commit 582dfd6

File tree

4 files changed

+155
-24
lines changed

4 files changed

+155
-24
lines changed

CHANGELOG.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
All notable changes to the "paragraphjump" extension will be documented in this file.
44

5-
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
5+
## 1.0.0
66

7-
## [Unreleased]
7+
### Added
88

9-
- Initial release
9+
- Added support for selecting paragraphs up and down.
10+
11+
## 0.0.1
12+
13+
### Added
14+
15+
- Initial release with only jump up and down functionality.

README.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,53 @@ ParagraphJump allows the user to navigate swiftly between paragraphs.
66

77
* `paragraphjump.up`: Move up a paragraph
88
* `paragraphjump.down`: Move down a paragraph
9-
10-
## Extension Settings
11-
12-
Include if your extension adds any VS Code settings through the `contributes.configuration` extension point.
9+
* `paragraphjump.selectup`: Select one paragraph up
10+
* `paragraphjump.selectdown`: Select one paragraph down
11+
12+
## Gettings started
13+
14+
This plugin will automatically be enabled after using one of the commands specified above.
15+
16+
### Keyboard shortcuts
17+
18+
To optimally use this plugin it is recommended to re-map your keybindings. You can do this through the **Keyboard Shortcuts** editor available
19+
through the **Preferences** -> **Keyboard Shortcuts** menu.
20+
21+
My personal bindings for this plugin is displayed below as an example:
22+
23+
```JSON
24+
{
25+
"key": "ctrl+up",
26+
"command": "paragraphjump.up",
27+
"when": "textInputFocus"
28+
},
29+
{
30+
"key": "ctrl+down",
31+
"command": "paragraphjump.down",
32+
"when": "textInputFocus"
33+
},
34+
{
35+
"key": "ctrl+shift+down",
36+
"command": "paragraphjump.selectdown",
37+
"when": "textInputFocus"
38+
},
39+
{
40+
"key": "ctrl+shift+up",
41+
"command": "paragraphjump.selectup",
42+
"when": "textInputFocus"
43+
}
44+
```
1345

1446
## Known Issues
1547

1648
None at the time.
1749

1850
## Release Notes
1951

52+
### 1.0.0
53+
54+
First full release with all the intended functionality for the plugin.
55+
2056
### 0.0.1
2157

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

package.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "paragraphjump",
33
"displayName": "ParagraphJump",
44
"description": "Allows the user to navigate swiftly between paragraphs",
5-
"version": "0.0.1",
5+
"version": "1.0.0",
66
"publisher": "brokenprogrammer",
77
"repository": {
88
"type": "git",
@@ -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: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import * as vscode from "vscode";
22

3+
enum LineOperation {
4+
up = 0,
5+
down = 1,
6+
}
7+
8+
enum MoveOperation {
9+
move = 0,
10+
select = 1,
11+
}
12+
13+
enum ParagraphJumpOperation {
14+
moveUp = 0,
15+
moveDown = 1,
16+
selectUp = 2,
17+
selectDown = 3,
18+
}
19+
320
const targetLineIsEmptyOrWhitespace = (
421
line: number,
522
document: vscode.TextDocument
623
) => !document.lineAt(line).isEmptyOrWhitespace;
724

8-
enum LineOperation {
9-
up = 1,
10-
down = 2,
11-
}
12-
1325
function getNextLine(editor: vscode.TextEditor, op: LineOperation) {
1426
let document = editor.document;
1527
let line = editor.selection.active.line;
@@ -34,32 +46,99 @@ function getNextLine(editor: vscode.TextEditor, op: LineOperation) {
3446
return document.lineAt(line);
3547
}
3648

37-
function moveCursor(editor: vscode.TextEditor, newPosition: vscode.Position) {
38-
let newCursorPosition = new vscode.Selection(newPosition, newPosition);
49+
function moveCursor(
50+
editor: vscode.TextEditor,
51+
newPosition: vscode.Position,
52+
op: MoveOperation
53+
) {
54+
let newCursorPosition: vscode.Selection;
55+
switch (op) {
56+
case MoveOperation.move:
57+
{
58+
newCursorPosition = new vscode.Selection(newPosition, newPosition);
59+
}
60+
break;
61+
case MoveOperation.select:
62+
{
63+
const anchor = editor.selection.anchor;
64+
newCursorPosition = new vscode.Selection(anchor, newPosition);
65+
}
66+
break;
67+
}
3968
editor.selection = newCursorPosition;
4069
editor.revealRange(new vscode.Range(newPosition, newPosition));
4170
}
4271

72+
function performParagraphJumpOperation(
73+
editor: vscode.TextEditor,
74+
op: ParagraphJumpOperation
75+
) {
76+
switch (op) {
77+
case ParagraphJumpOperation.moveUp:
78+
case ParagraphJumpOperation.selectUp:
79+
{
80+
const targetLine = getNextLine(editor, LineOperation.up);
81+
const newPosition = new vscode.Position(targetLine.lineNumber, 0);
82+
const moveOp =
83+
op === ParagraphJumpOperation.moveUp
84+
? MoveOperation.move
85+
: MoveOperation.select;
86+
moveCursor(editor, newPosition, moveOp);
87+
}
88+
break;
89+
case ParagraphJumpOperation.moveDown:
90+
case ParagraphJumpOperation.selectDown:
91+
{
92+
const targetLine = getNextLine(editor, LineOperation.down);
93+
const newPosition = new vscode.Position(
94+
targetLine.lineNumber,
95+
targetLine.text.length
96+
);
97+
const moveOp =
98+
op === ParagraphJumpOperation.moveDown
99+
? MoveOperation.move
100+
: MoveOperation.select;
101+
moveCursor(editor, newPosition, moveOp);
102+
}
103+
break;
104+
}
105+
}
106+
43107
export function activate(context: vscode.ExtensionContext) {
44108
let paragraphJumpUp = vscode.commands.registerTextEditorCommand(
45109
"paragraphjump.up",
46110
(editor: vscode.TextEditor) => {
47-
let targetLine: vscode.TextLine = getNextLine(editor, LineOperation.up);
48-
const newPosition = new vscode.Position(targetLine.lineNumber, 0);
49-
moveCursor(editor, newPosition);
111+
performParagraphJumpOperation(editor, ParagraphJumpOperation.moveUp);
50112
}
51113
);
52114

53115
let paragraphJumpDown = vscode.commands.registerTextEditorCommand(
54116
"paragraphjump.down",
55117
(editor: vscode.TextEditor) => {
56-
let targetLine: vscode.TextLine = getNextLine(editor, LineOperation.down);
57-
const newPosition = new vscode.Position(targetLine.lineNumber, 0);
58-
moveCursor(editor, newPosition);
118+
performParagraphJumpOperation(editor, ParagraphJumpOperation.moveDown);
59119
}
60120
);
61121

62-
context.subscriptions.push(paragraphJumpUp, paragraphJumpDown);
122+
let paragraphSelectUp = vscode.commands.registerTextEditorCommand(
123+
"paragraphjump.selectup",
124+
(editor: vscode.TextEditor) => {
125+
performParagraphJumpOperation(editor, ParagraphJumpOperation.selectUp);
126+
}
127+
);
128+
129+
let paragraphSelectDown = vscode.commands.registerTextEditorCommand(
130+
"paragraphjump.selectdown",
131+
(editor: vscode.TextEditor) => {
132+
performParagraphJumpOperation(editor, ParagraphJumpOperation.selectDown);
133+
}
134+
);
135+
136+
context.subscriptions.push(
137+
paragraphJumpUp,
138+
paragraphJumpDown,
139+
paragraphSelectUp,
140+
paragraphSelectDown
141+
);
63142
}
64143

65144
export function deactivate() {}

0 commit comments

Comments
 (0)