1
1
import * as vscode from "vscode" ;
2
2
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
+
3
20
const targetLineIsEmptyOrWhitespace = (
4
21
line : number ,
5
22
document : vscode . TextDocument
6
23
) => ! document . lineAt ( line ) . isEmptyOrWhitespace ;
7
24
8
- enum LineOperation {
9
- up = 1 ,
10
- down = 2 ,
11
- }
12
-
13
25
function getNextLine ( editor : vscode . TextEditor , op : LineOperation ) {
14
26
let document = editor . document ;
15
27
let line = editor . selection . active . line ;
@@ -34,32 +46,99 @@ function getNextLine(editor: vscode.TextEditor, op: LineOperation) {
34
46
return document . lineAt ( line ) ;
35
47
}
36
48
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
+ }
39
68
editor . selection = newCursorPosition ;
40
69
editor . revealRange ( new vscode . Range ( newPosition , newPosition ) ) ;
41
70
}
42
71
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
+
43
107
export function activate ( context : vscode . ExtensionContext ) {
44
108
let paragraphJumpUp = vscode . commands . registerTextEditorCommand (
45
109
"paragraphjump.up" ,
46
110
( 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 ) ;
50
112
}
51
113
) ;
52
114
53
115
let paragraphJumpDown = vscode . commands . registerTextEditorCommand (
54
116
"paragraphjump.down" ,
55
117
( 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 ) ;
59
119
}
60
120
) ;
61
121
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
+ ) ;
63
142
}
64
143
65
144
export function deactivate ( ) { }
0 commit comments