11import * 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+
320const 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-
1325function 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+
43107export 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
65144export function deactivate ( ) { }
0 commit comments