55import { forLines } from "./scopes"
66import { TextEditor , Selection } from "atom"
77
8- interface LineInfo {
9- scope : readonly string [ ]
10- line : string
11- }
8+ /**
9+ * interface LineInfo {
10+ * scope: readonly string[]
11+ * line: string
12+ * }
13+ */
1214
15+ /**
16+ *
17+ * @param {TextEditor } editor
18+ * @param {number } l
19+ * @returns {LineInfo }
20+ */
1321export function getLine ( editor : TextEditor , l : number ) : LineInfo {
1422 return {
1523 scope : editor . scopeDescriptorForBufferPosition ( [ l , 0 ] ) . getScopesArray ( ) ,
@@ -20,6 +28,12 @@ export function getLine(editor: TextEditor, l: number): LineInfo {
2028 }
2129}
2230
31+ /**
32+ *
33+ * @param {LineInfo.line } line
34+ * @param {LineInfo.scope } scope
35+ * @param {boolean } allowDocstrings
36+ */
2337function isBlank ( { line, scope } : LineInfo , allowDocstrings = false ) {
2438 for ( const s of scope ) {
2539 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) {
2943 return / ^ \s * ( # .* ) ? $ / . test ( line )
3044}
3145
46+ /**
47+ *
48+ * @param {LineInfo } lineInfo
49+ */
3250function isEnd ( lineInfo : LineInfo ) {
3351 if ( isStringEnd ( lineInfo ) ) {
3452 return true
3553 }
3654 return / ^ ( e n d \b | \) | ] | } ) / . test ( lineInfo . line )
3755}
3856
57+ /**
58+ *
59+ * @param {LineInfo } lineInfo
60+ */
3961function isStringEnd ( lineInfo : LineInfo ) {
4062 const scope = lineInfo . scope . join ( " " )
4163 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 ) )
4264}
4365
66+ /**
67+ *
68+ * @param {LineInfo } lineInfo
69+ */
4470function isCont ( lineInfo : LineInfo ) {
4571 const scope = lineInfo . scope . join ( " " )
4672 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) {
5076 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 / )
5177}
5278
79+ /**
80+ *
81+ * @param {LineInfo } lineInfo
82+ */
5383function isStart ( lineInfo : LineInfo ) {
5484 return ! ( / ^ \s / . test ( lineInfo . line ) || isBlank ( lineInfo ) || isEnd ( lineInfo ) || isCont ( lineInfo ) )
5585}
5686
57-
87+ /**
88+ *
89+ * @param {TextEditor } editor
90+ * @param {number } row
91+ */
5892function walkBack ( editor : TextEditor , row : number ) {
5993 while ( row > 0 && ! isStart ( getLine ( editor , row ) ) ) {
6094 row --
6195 }
6296 return row
6397}
6498
99+ /**
100+ *
101+ * @param {TextEditor } editor
102+ * @param {number } start
103+ */
65104function walkForward ( editor : TextEditor , start : number ) {
66105 let end = start
67106 let mark = start
@@ -86,6 +125,12 @@ function walkForward(editor: TextEditor, start: number) {
86125 return end
87126}
88127
128+ /**
129+ *
130+ * @param {TextEditor } editor
131+ * @param {number } row
132+ * @returns {[[number, number], [number, number]] | undefined }
133+ */
89134function getRange ( editor : TextEditor , row : number ) : [ [ number , number ] , [ number , number ] ] | undefined {
90135 const start = walkBack ( editor , row )
91136 const end = walkForward ( editor , start )
@@ -99,6 +144,11 @@ function getRange(editor: TextEditor, row: number): [[number, number], [number,
99144 }
100145}
101146
147+ /**
148+ *
149+ * @param {TextEditor } editor
150+ * @param {Selection } selection
151+ */
102152function getSelection ( editor : TextEditor , selection : Selection ) {
103153 const { start, end } = selection . getBufferRange ( )
104154 const range = [
@@ -116,6 +166,12 @@ function getSelection(editor: TextEditor, selection: Selection) {
116166 return range
117167}
118168
169+ /**
170+ *
171+ * @param {TextEditor } editor
172+ * @param {Selection } selection
173+ * @param {[[number, number], [number, number]] } range
174+ */
119175export function moveNext ( editor : TextEditor , selection : Selection , range : [ [ number , number ] , [ number , number ] ] ) {
120176 // Ensure enough room at the end of the buffer
121177 const row = range [ 1 ] [ 0 ]
@@ -142,6 +198,10 @@ export function moveNext(editor: TextEditor, selection: Selection, range: [[numb
142198 ] )
143199}
144200
201+ /**
202+ *
203+ * @param {TextEditor } editor
204+ */
145205function getRanges ( editor : TextEditor ) {
146206 const ranges = editor . getSelections ( ) . map ( selection => {
147207 return {
@@ -157,6 +217,10 @@ function getRanges(editor: TextEditor) {
157217 } )
158218}
159219
220+ /**
221+ *
222+ * @param {TextEditor } editor
223+ */
160224export function get ( editor : TextEditor ) {
161225 return getRanges ( editor ) . map ( ( { range, selection } ) => {
162226 return {
@@ -168,6 +232,11 @@ export function get(editor: TextEditor) {
168232 } )
169233}
170234
235+ /**
236+ *
237+ * @param {TextEditor } editor
238+ * @param {number } row
239+ */
171240export function getLocalContext ( editor : TextEditor , row : number ) {
172241 const range = getRange ( editor , row )
173242 const context = range ? editor . getTextInBufferRange ( range ) : ""
@@ -182,6 +251,10 @@ export function getLocalContext(editor: TextEditor, row: number) {
182251 }
183252}
184253
254+ /**
255+ *
256+ * @param {TextEditor | undefined } editor
257+ */
185258export function select ( editor = atom . workspace . getActiveTextEditor ( ) ) {
186259 if ( ! editor ) return
187260 return editor . mutateSelectedText ( selection => {
0 commit comments