11'use babel'
22// TODO: docstrings
3-
3+ // TODO: Fix RangeCompatible types
44import { forLines } from './scopes'
55
6- export function getLine ( ed , l ) {
6+ export function getLine ( editor , l ) {
77 return {
8- scope : ed . scopeDescriptorForBufferPosition ( [ l , 0 ] ) . scopes ,
9- line : ed . getTextInBufferRange ( [ [ l , 0 ] , [ l , Infinity ] ] )
8+ scope : editor . scopeDescriptorForBufferPosition ( [ l , 0 ] ) . getScopesArray ( ) ,
9+ line : editor . getTextInBufferRange ( [
10+ [ l , 0 ] ,
11+ [ l , Infinity ]
12+ ] )
1013 }
1114}
1215
13- function isBlank ( { line, scope} , allowDocstrings = false ) {
16+ function isBlank ( { line, scope } , allowDocstrings = false ) {
1417 for ( const s of scope ) {
1518 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 ) ) ) {
1619 return true
1720 }
1821 }
1922 return / ^ \s * ( # .* ) ? $ / . test ( line )
2023}
21- function isEnd ( { line, scope } ) {
22- if ( isStringEnd ( { line, scope } ) ) {
24+
25+ function isEnd ( lineInfo ) {
26+ if ( isStringEnd ( lineInfo ) ) {
2327 return true
2428 }
25- return / ^ ( e n d \b | \) | \] | \ }) / . test ( line )
29+ return / ^ ( e n d \b | \) | ] | } ) / . test ( lineInfo . line )
2630}
27- function isStringEnd ( { line, scope } ) {
28- scope = scope . join ( ' ' )
31+
32+ function isStringEnd ( lineInfo ) {
33+ const scope = lineInfo . scope . join ( ' ' )
2934 return / \b s t r i n g \. m u l t i l i n e \. e n d \b / . test ( scope ) ||
3035 ( / \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 ) )
3136}
32- function isCont ( { line, scope } ) {
33- scope = scope . join ( ' ' )
34- 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 ) ) ) {
37+
38+ function isCont ( lineInfo ) {
39+ const scope = lineInfo . scope . join ( ' ' )
40+ 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 ) ) {
3541 return true
3642 }
37-
38- return 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 / )
43+ 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 / )
3944}
40- function isStart ( lineInfo ) {
45+
46+ function isStart ( lineInfo ) {
4147 return ! ( / ^ \s / . test ( lineInfo . line ) || isBlank ( lineInfo ) || isEnd ( lineInfo ) || isCont ( lineInfo ) )
4248}
4349
44- function walkBack ( ed , row ) {
45- while ( ( row > 0 ) && ! isStart ( getLine ( ed , row ) ) ) {
50+ function walkBack ( editor , row ) {
51+ while ( row > 0 && ! isStart ( getLine ( editor , row ) ) ) {
4652 row --
4753 }
4854 return row
4955}
5056
51- function walkForward ( ed , start ) {
57+ function walkForward ( editor , start ) {
5258 let end = start
5359 let mark = start
54- while ( mark < ed . getLastBufferRow ( ) ) {
60+ while ( mark < editor . getLastBufferRow ( ) ) {
5561 mark ++
56- const lineInfo = getLine ( ed , mark )
57-
62+ const lineInfo = getLine ( editor , mark )
5863 if ( isStart ( lineInfo ) ) {
5964 break
6065 }
@@ -63,7 +68,7 @@ function walkForward (ed, start) {
6368 // returning a non-empty array).
6469 // If the line closes a multiline string we also take that as ending the block.
6570 if (
66- ! ( forLines ( ed , start , mark - 1 ) . length === 0 ) ||
71+ ! ( forLines ( editor , start , mark - 1 ) . length === 0 ) ||
6772 isStringEnd ( lineInfo )
6873 ) {
6974 end = mark
@@ -75,74 +80,89 @@ function walkForward (ed, start) {
7580 return end
7681}
7782
78- function getRange ( ed , row ) {
79- const start = walkBack ( ed , row )
80- const end = walkForward ( ed , start )
83+ function getRange ( editor , row ) {
84+ const start = walkBack ( editor , row )
85+ const end = walkForward ( editor , start )
8186 if ( start <= row && row <= end ) {
82- return [ [ start , 0 ] , [ end , Infinity ] ]
87+ return [
88+ [ start , 0 ] ,
89+ [ end , Infinity ]
90+ ]
91+ } else {
92+ return undefined // TODO: make sure returned range from getRanges is not undefined
8393 }
8494}
8595
86- function getSelection ( ed , sel ) {
87- const { start, end} = sel . getBufferRange ( )
88- const range = [ [ start . row , start . column ] , [ end . row , end . column ] ]
89- while ( isBlank ( getLine ( ed , range [ 0 ] [ 0 ] ) , true ) && ( range [ 0 ] [ 0 ] <= range [ 1 ] [ 0 ] ) ) {
96+ function getSelection ( editor , selection ) {
97+ const { start, end } = selection . getBufferRange ( )
98+ const range = [
99+ [ start . row , start . column ] ,
100+ [ end . row , end . column ]
101+ ]
102+ while ( isBlank ( getLine ( editor , range [ 0 ] [ 0 ] ) , true ) && range [ 0 ] [ 0 ] <= range [ 1 ] [ 0 ] ) {
90103 range [ 0 ] [ 0 ] ++
91104 range [ 0 ] [ 1 ] = 0
92105 }
93- while ( isBlank ( getLine ( ed , range [ 1 ] [ 0 ] ) , true ) && ( range [ 1 ] [ 0 ] >= range [ 0 ] [ 0 ] ) ) {
106+ while ( isBlank ( getLine ( editor , range [ 1 ] [ 0 ] ) , true ) && range [ 1 ] [ 0 ] >= range [ 0 ] [ 0 ] ) {
94107 range [ 1 ] [ 0 ] --
95108 range [ 1 ] [ 1 ] = Infinity
96109 }
97110 return range
98111}
99112
100- export function moveNext ( ed , sel , range ) {
113+ export function moveNext ( editor , selection , range ) {
101114 // Ensure enough room at the end of the buffer
102115 const row = range [ 1 ] [ 0 ]
103116 let last
104- while ( ( last = ed . getLastBufferRow ( ) ) < ( row + 2 ) ) {
105- if ( ( last !== row ) && ! isBlank ( getLine ( ed , last ) ) ) {
117+ while ( ( last = editor . getLastBufferRow ( ) ) < row + 2 ) {
118+ if ( last !== row && ! isBlank ( getLine ( editor , last ) ) ) {
106119 break
107120 }
108- sel . setBufferRange ( [ [ last , Infinity ] , [ last , Infinity ] ] )
109- sel . insertText ( '\n' )
121+ selection . setBufferRange ( [
122+ [ last , Infinity ] ,
123+ [ last , Infinity ]
124+ ] )
125+ selection . insertText ( '\n' )
110126 }
111127 // Move the cursor
112128 let to = row + 1
113- while ( ( to < ed . getLastBufferRow ( ) ) && isBlank ( getLine ( ed , to ) ) ) {
129+ while ( to < editor . getLastBufferRow ( ) && isBlank ( getLine ( editor , to ) ) ) {
114130 to ++
115131 }
116- to = walkForward ( ed , to )
117- return sel . setBufferRange ( [ [ to , Infinity ] , [ to , Infinity ] ] )
132+ to = walkForward ( editor , to )
133+ return selection . setBufferRange ( [
134+ [ to , Infinity ] ,
135+ [ to , Infinity ]
136+ ] )
118137}
119138
120- function getRanges ( ed ) {
121- const ranges = ed . getSelections ( ) . map ( sel => {
139+ function getRanges ( editor ) {
140+ const ranges = editor . getSelections ( ) . map ( selection => {
122141 return {
123- selection : sel ,
124- range : sel . isEmpty ( ) ?
125- getRange ( ed , sel . getHeadBufferPosition ( ) . row ) :
126- getSelection ( ed , sel )
142+ selection : selection ,
143+ range : selection . isEmpty ( )
144+ ? getRange ( editor , selection . getHeadBufferPosition ( ) . row )
145+ : getSelection ( editor , selection )
127146 }
147+ // TODO: replace with getBufferRowRange? (getHeadBufferPosition isn't a public API)
128148 } )
129149 return ranges . filter ( ( { range } ) => {
130- return range && ed . getTextInBufferRange ( range ) . trim ( )
150+ return range && editor . getTextInBufferRange ( range ) . trim ( )
131151 } )
132152}
133153
134- export function get ( ed ) {
135- return getRanges ( ed ) . map ( ( { range, selection } ) => {
154+ export function get ( editor ) {
155+ return getRanges ( editor ) . map ( ( { range, selection } ) => {
136156 return {
137157 range,
138158 selection,
139159 line : range [ 0 ] [ 0 ] ,
140- text : ed . getTextInBufferRange ( range )
160+ text : editor . getTextInBufferRange ( range )
141161 }
142162 } )
143163}
144164
145- export function getLocalContext ( editor , row ) {
165+ export function getLocalContext ( editor , row ) {
146166 const range = getRange ( editor , row )
147167 const context = range ? editor . getTextInBufferRange ( range ) : ''
148168 // NOTE:
@@ -156,12 +176,13 @@ export function getLocalContext (editor, row) {
156176 }
157177}
158178
159- export function select ( ed = atom . workspace . getActiveTextEditor ( ) ) {
160- if ( ! ed ) return
161- return ed . mutateSelectedText ( selection => {
162- const range = getRange ( ed , selection . getHeadBufferPosition ( ) . row )
179+ export function select ( editor = atom . workspace . getActiveTextEditor ( ) ) {
180+ if ( ! editor ) return
181+ return editor . mutateSelectedText ( selection => {
182+ const range = getRange ( editor , selection . getHeadBufferPosition ( ) . row )
163183 if ( range ) {
164184 selection . setBufferRange ( range )
165185 }
166186 } )
187+ // TODO: replace with getBufferRowRange? (getHeadBufferPosition isn't a public API)
167188}
0 commit comments