5
5
import { forLines } from "./scopes"
6
6
import { TextEditor , Selection } from "atom"
7
7
8
- interface LineInfo {
9
- scope : readonly string [ ]
10
- line : string
11
- }
8
+ /**
9
+ * interface LineInfo {
10
+ * scope: readonly string[]
11
+ * line: string
12
+ * }
13
+ */
12
14
15
+ /**
16
+ *
17
+ * @param {TextEditor } editor
18
+ * @param {number } l
19
+ * @returns {LineInfo }
20
+ */
13
21
export function getLine ( editor : TextEditor , l : number ) : LineInfo {
14
22
return {
15
23
scope : editor . scopeDescriptorForBufferPosition ( [ l , 0 ] ) . getScopesArray ( ) ,
@@ -20,6 +28,12 @@ export function getLine(editor: TextEditor, l: number): LineInfo {
20
28
}
21
29
}
22
30
31
+ /**
32
+ *
33
+ * @param {LineInfo.line } line
34
+ * @param {LineInfo.scope } scope
35
+ * @param {boolean } allowDocstrings
36
+ */
23
37
function isBlank ( { line, scope } : LineInfo , allowDocstrings = false ) {
24
38
for ( const s of scope ) {
25
39
if ( / \b c o m m e n t \b / . test ( s ) || ( ! allowDocstrings && / \b d o c s t r i n g \b / . test ( s ) ) ) {
@@ -29,18 +43,30 @@ function isBlank({ line, scope }: LineInfo, allowDocstrings = false) {
29
43
return / ^ \s * ( # .* ) ? $ / . test ( line )
30
44
}
31
45
46
+ /**
47
+ *
48
+ * @param {LineInfo } lineInfo
49
+ */
32
50
function isEnd ( lineInfo : LineInfo ) {
33
51
if ( isStringEnd ( lineInfo ) ) {
34
52
return true
35
53
}
36
54
return / ^ ( e n d \b | \) | ] | } ) / . test ( lineInfo . line )
37
55
}
38
56
57
+ /**
58
+ *
59
+ * @param {LineInfo } lineInfo
60
+ */
39
61
function isStringEnd ( lineInfo : LineInfo ) {
40
62
const scope = lineInfo . scope . join ( " " )
41
63
return / \b s t r i n g \. m u l t i l i n e \. e n d \b / . test ( scope ) || ( / \b s t r i n g \. e n d \b / . test ( scope ) && / \b b a c k t i c k \b / . test ( scope ) )
42
64
}
43
65
66
+ /**
67
+ *
68
+ * @param {LineInfo } lineInfo
69
+ */
44
70
function isCont ( lineInfo : LineInfo ) {
45
71
const scope = lineInfo . scope . join ( " " )
46
72
if ( / \b s t r i n g \b / . test ( scope ) && ! / \b p u n c t u a t i o n \. d e f i n i t i o n \. s t r i n g \b / . test ( scope ) ) {
@@ -50,18 +76,31 @@ function isCont(lineInfo: LineInfo) {
50
76
return lineInfo . line . match ( / ^ ( e l s e | e l s e i f | c a t c h | f i n a l l y ) \b / )
51
77
}
52
78
79
+ /**
80
+ *
81
+ * @param {LineInfo } lineInfo
82
+ */
53
83
function isStart ( lineInfo : LineInfo ) {
54
84
return ! ( / ^ \s / . test ( lineInfo . line ) || isBlank ( lineInfo ) || isEnd ( lineInfo ) || isCont ( lineInfo ) )
55
85
}
56
86
57
-
87
+ /**
88
+ *
89
+ * @param {TextEditor } editor
90
+ * @param {number } row
91
+ */
58
92
function walkBack ( editor : TextEditor , row : number ) {
59
93
while ( row > 0 && ! isStart ( getLine ( editor , row ) ) ) {
60
94
row --
61
95
}
62
96
return row
63
97
}
64
98
99
+ /**
100
+ *
101
+ * @param {TextEditor } editor
102
+ * @param {number } start
103
+ */
65
104
function walkForward ( editor : TextEditor , start : number ) {
66
105
let end = start
67
106
let mark = start
@@ -86,6 +125,12 @@ function walkForward(editor: TextEditor, start: number) {
86
125
return end
87
126
}
88
127
128
+ /**
129
+ *
130
+ * @param {TextEditor } editor
131
+ * @param {number } row
132
+ * @returns {[[number, number], [number, number]] | undefined }
133
+ */
89
134
function getRange ( editor : TextEditor , row : number ) : [ [ number , number ] , [ number , number ] ] | undefined {
90
135
const start = walkBack ( editor , row )
91
136
const end = walkForward ( editor , start )
@@ -99,6 +144,11 @@ function getRange(editor: TextEditor, row: number): [[number, number], [number,
99
144
}
100
145
}
101
146
147
+ /**
148
+ *
149
+ * @param {TextEditor } editor
150
+ * @param {Selection } selection
151
+ */
102
152
function getSelection ( editor : TextEditor , selection : Selection ) {
103
153
const { start, end } = selection . getBufferRange ( )
104
154
const range = [
@@ -116,6 +166,12 @@ function getSelection(editor: TextEditor, selection: Selection) {
116
166
return range
117
167
}
118
168
169
+ /**
170
+ *
171
+ * @param {TextEditor } editor
172
+ * @param {Selection } selection
173
+ * @param {[[number, number], [number, number]] } range
174
+ */
119
175
export function moveNext ( editor : TextEditor , selection : Selection , range : [ [ number , number ] , [ number , number ] ] ) {
120
176
// Ensure enough room at the end of the buffer
121
177
const row = range [ 1 ] [ 0 ]
@@ -142,6 +198,10 @@ export function moveNext(editor: TextEditor, selection: Selection, range: [[numb
142
198
] )
143
199
}
144
200
201
+ /**
202
+ *
203
+ * @param {TextEditor } editor
204
+ */
145
205
function getRanges ( editor : TextEditor ) {
146
206
const ranges = editor . getSelections ( ) . map ( selection => {
147
207
return {
@@ -157,6 +217,10 @@ function getRanges(editor: TextEditor) {
157
217
} )
158
218
}
159
219
220
+ /**
221
+ *
222
+ * @param {TextEditor } editor
223
+ */
160
224
export function get ( editor : TextEditor ) {
161
225
return getRanges ( editor ) . map ( ( { range, selection } ) => {
162
226
return {
@@ -168,6 +232,11 @@ export function get(editor: TextEditor) {
168
232
} )
169
233
}
170
234
235
+ /**
236
+ *
237
+ * @param {TextEditor } editor
238
+ * @param {number } row
239
+ */
171
240
export function getLocalContext ( editor : TextEditor , row : number ) {
172
241
const range = getRange ( editor , row )
173
242
const context = range ? editor . getTextInBufferRange ( range ) : ""
@@ -182,6 +251,10 @@ export function getLocalContext(editor: TextEditor, row: number) {
182
251
}
183
252
}
184
253
254
+ /**
255
+ *
256
+ * @param {TextEditor | undefined } editor
257
+ */
185
258
export function select ( editor = atom . workspace . getActiveTextEditor ( ) ) {
186
259
if ( ! editor ) return
187
260
return editor . mutateSelectedText ( selection => {
0 commit comments