1
- /** @babel */
1
+ import { Point , PointCompatible , Range , RangeCompatible , TextEditor } from "atom"
2
2
3
- import { Point , Range } from 'atom'
4
-
5
- const juliaScopes = [ 'source.julia' , 'source.embedded.julia' ]
3
+ const juliaScopes = [ "source.julia" , "source.embedded.julia" ]
6
4
const openers = [
7
- 'if' , 'while' , 'for' , 'begin' , 'function' , 'macro' , 'module' , 'baremodule' , 'type' , 'immutable' ,
8
- 'struct' , 'mutable struct' , 'try' , 'let' , 'do' , 'quote' , 'abstract type' , 'primitive type'
5
+ "if" ,
6
+ "while" ,
7
+ "for" ,
8
+ "begin" ,
9
+ "function" ,
10
+ "macro" ,
11
+ "module" ,
12
+ "baremodule" ,
13
+ "type" ,
14
+ "immutable" ,
15
+ "struct" ,
16
+ "mutable struct" ,
17
+ "try" ,
18
+ "let" ,
19
+ "do" ,
20
+ "quote" ,
21
+ "abstract type" ,
22
+ "primitive type" ,
9
23
]
10
- const reopeners = [ ' else' , ' elseif' , ' catch' , ' finally' ]
24
+ const reopeners = [ " else" , " elseif" , " catch" , " finally" ]
11
25
12
- function isKeywordScope ( scopes ) {
26
+ function isKeywordScope ( scopes : string [ ] ) {
13
27
// Skip 'source.julia'
14
28
return scopes . slice ( 1 ) . some ( scope => {
15
- return scope . indexOf ( ' keyword' ) > - 1
29
+ return scope . indexOf ( " keyword" ) > - 1
16
30
} )
17
31
}
18
32
19
- export function isStringScope ( scopes ) {
33
+ export function isStringScope ( scopes : readonly string [ ] ) {
20
34
let isString = false
21
35
let isInterp = false
22
36
for ( const scope of scopes ) {
23
- if ( scope . indexOf ( ' string' ) > - 1 ) {
37
+ if ( scope . indexOf ( " string" ) > - 1 ) {
24
38
isString = true
25
39
}
26
- if ( scope . indexOf ( ' interpolation' ) > - 1 ) {
40
+ if ( scope . indexOf ( " interpolation" ) > - 1 ) {
27
41
isInterp = true
28
42
}
29
43
}
30
44
return isString && ! isInterp
31
45
}
32
46
33
- function forRange ( editor , range ) {
47
+ function forRange ( editor : TextEditor , range : RangeCompatible ) {
34
48
// this should happen here and not a top-level so that we aren't relying on
35
49
// Atom to load packages in a specific order:
36
- const juliaGrammar = atom . grammars . grammarForScopeName ( ' source.julia' )
50
+ const juliaGrammar = atom . grammars . grammarForScopeName ( " source.julia" )
37
51
38
52
if ( juliaGrammar === undefined ) return [ ]
39
53
40
- const scopes = [ ]
54
+ const scopes : string [ ] = [ ]
41
55
let n_parens = 0
42
56
let n_brackets = 0
43
57
const text = editor . getTextInBufferRange ( range )
44
58
juliaGrammar . tokenizeLines ( text ) . forEach ( lineTokens => {
45
59
lineTokens . forEach ( token => {
46
60
const { value } = token
47
61
if ( ! isStringScope ( token . scopes ) ) {
48
- if ( n_parens > 0 && value === ')' ) {
62
+ if ( n_parens > 0 && value === ")" ) {
49
63
n_parens -= 1
50
- scopes . splice ( scopes . lastIndexOf ( ' paren' ) , 1 )
64
+ scopes . splice ( scopes . lastIndexOf ( " paren" ) , 1 )
51
65
return
52
- } else if ( n_brackets > 0 && value === ']' ) {
66
+ } else if ( n_brackets > 0 && value === "]" ) {
53
67
n_brackets -= 1
54
- scopes . splice ( scopes . lastIndexOf ( ' bracket' ) , 1 )
68
+ scopes . splice ( scopes . lastIndexOf ( " bracket" ) , 1 )
55
69
return
56
- } else if ( value === '(' ) {
70
+ } else if ( value === "(" ) {
57
71
n_parens += 1
58
- scopes . push ( ' paren' )
72
+ scopes . push ( " paren" )
59
73
return
60
- } else if ( value === '[' ) {
74
+ } else if ( value === "[" ) {
61
75
n_brackets += 1
62
- scopes . push ( ' bracket' )
76
+ scopes . push ( " bracket" )
63
77
return
64
78
}
65
79
}
66
- if ( ! ( isKeywordScope ( token . scopes ) ) ) return
80
+ if ( ! isKeywordScope ( token . scopes ) ) return
67
81
if ( ! ( n_parens === 0 && n_brackets === 0 ) ) return
68
82
69
83
const reopen = reopeners . includes ( value )
70
- if ( value === ' end' || reopen ) scopes . pop ( )
84
+ if ( value === " end" || reopen ) scopes . pop ( )
71
85
if ( openers . includes ( value ) || reopen ) scopes . push ( value )
72
86
} )
73
87
} )
74
88
return scopes
75
89
}
76
90
77
- export function forLines ( editor , start , end ) {
91
+ export function forLines ( editor : TextEditor , start : number , end : number ) {
78
92
const startPoint = new Point ( start , 0 )
79
93
const endPoint = new Point ( end , Infinity )
80
94
const range = new Range ( startPoint , endPoint )
81
95
return forRange ( editor , range )
82
96
}
83
97
84
- export function isCommentScope ( scopes ) {
98
+ export function isCommentScope ( scopes : readonly string [ ] ) {
85
99
// Skip 'source.julia'
86
100
return scopes . slice ( 1 ) . some ( scope => {
87
- return scope . indexOf ( ' comment' ) > - 1
101
+ return scope . indexOf ( " comment" ) > - 1
88
102
} )
89
103
}
90
104
@@ -93,13 +107,13 @@ export function isCommentScope (scopes) {
93
107
* Supposed to be used within Atom-IDE integrations, whose `grammarScopes` setting doesn't support
94
108
* embedded scopes by default.
95
109
*/
96
- export function isValidScopeToInspect ( editor , bufferPosition ) {
110
+ export function isValidScopeToInspect ( editor : TextEditor , bufferPosition : PointCompatible ) {
97
111
const scopes = editor
98
112
. scopeDescriptorForBufferPosition ( bufferPosition )
99
113
. getScopesArray ( )
100
114
return scopes . some ( scope => {
101
115
return juliaScopes . includes ( scope )
102
- } ) ?
103
- ! isCommentScope ( scopes ) && ! isStringScope ( scopes ) :
104
- false
116
+ } )
117
+ ? ! isCommentScope ( scopes ) && ! isStringScope ( scopes )
118
+ : false
105
119
}
0 commit comments