-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjs-colon.ts
53 lines (48 loc) · 1.23 KB
/
js-colon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import traverse from '@babel/traverse'
import { Selection } from 'vscode'
import type { Handler } from '../types'
/**
* `:` to the value.
*
* ```js
* ▽
* { foo: { bar } }
* └─────┘
* ```
*
* @name js-colon
* @category js
*/
export const jsColonHandler: Handler = {
name: 'js-colon',
handle({ doc, getAst, anchor }) {
const asts = getAst('js')
if (!asts.length)
return
const range = doc.getWordRangeAtPosition(anchor, /\s*:\s*/)
if (!range)
return
for (const ast of getAst('js')) {
const relativeIndex = doc.offsetAt(range.end) - ast.start
let result: Selection | undefined
traverse(ast.root, {
enter(path) {
if (result)
return path.skip()
if (path.node.start == null || path.node.end == null)
return
if (relativeIndex > path.node.end || path.node.start > relativeIndex)
return path.skip()
if (path.node.start !== relativeIndex)
return
result = new Selection(
doc.positionAt(ast.start + path.node.start),
doc.positionAt(ast.start + path.node.end),
)
},
})
if (result)
return result
}
},
}