1
- import * as os from 'os' ;
2
1
import * as fs from 'fs' ;
2
+ import * as os from 'os' ;
3
3
import * as path from 'path' ;
4
4
import * as vscode from 'vscode' ;
5
5
6
6
const windows : boolean = os . platform ( ) == 'win32' ;
7
- const linux : boolean = os . platform ( ) == 'linux' ;
8
- const pathSeparator : string = windows ? ';' : ':' ;
9
7
10
- const janetCommand : string = windows ? 'janet.exe' : 'janet -s ' ;
8
+ const janetBinary : string = windows ? 'janet.exe' : 'janet' ;
11
9
const terminalName : string = 'Janet REPL' ;
12
10
13
-
14
11
function janetExists ( ) : boolean {
15
- return process . env [ 'PATH' ] . split ( pathSeparator )
16
- . some ( ( x ) => fs . existsSync ( path . resolve ( x , janetCommand ) ) ) ;
12
+ return process . env [ 'PATH' ] . split ( path . delimiter )
13
+ . some ( ( x ) => fs . existsSync ( path . resolve ( x , janetBinary ) ) ) ;
17
14
}
18
15
19
- function newREPL ( ) : vscode . Terminal {
20
- let terminal = vscode . window . createTerminal ( terminalName ) ;
21
- terminal . sendText ( janetCommand , true ) ;
22
-
23
- vscode . window . withProgress ( {
16
+ function newREPL ( ) : Thenable < vscode . Terminal > {
17
+ const terminal = vscode . window . createTerminal ( terminalName ) ;
18
+ terminal . sendText ( janetBinary + ' -s' , true ) ;
19
+ return vscode . window . withProgress ( {
24
20
location : vscode . ProgressLocation . Notification ,
25
21
title : "Running Janet REPL..." ,
26
22
cancellable : false
27
23
} , ( progress , token ) => {
28
- var p = new Promise ( resolve => {
24
+ return new Promise < vscode . Terminal > ( resolve => {
29
25
setTimeout ( ( ) => {
30
26
terminal . show ( ) ;
31
27
thenFocusTextEditor ( ) ;
32
- resolve ( ) ;
28
+ resolve ( terminal ) ;
33
29
} , 2000 ) ;
34
30
} ) ;
35
-
36
- return p ;
37
31
} ) ;
38
-
39
- return terminal ;
40
32
}
41
33
42
- function getREPL ( show : boolean ) : vscode . Terminal {
34
+ function getREPL ( show : boolean ) : Thenable < vscode . Terminal > {
43
35
let terminal : vscode . Terminal = ( < any > vscode . window ) . terminals . find ( x => x . _name === terminalName ) ;
36
+ let terminalP = ( terminal ) ? Promise . resolve ( terminal ) : newREPL ( ) ;
37
+ return terminalP . then ( t => {
38
+ if ( show ) {
39
+ t . show ( ) ;
40
+ }
41
+ return t ;
42
+ } ) ;
43
+ }
44
44
45
- if ( terminal == null ) {
46
- newREPL ( ) ;
47
- return null ;
48
- }
49
-
50
- if ( show ) terminal . show ( ) ;
51
-
52
- return terminal ;
45
+ function sendSource ( terminal : vscode . Terminal , text : string ) {
46
+ terminal . sendText ( text , true ) ;
53
47
}
54
48
55
49
function thenFocusTextEditor ( ) {
@@ -77,18 +71,16 @@ export function activate(context: vscode.ExtensionContext) {
77
71
( ) => {
78
72
let editor = vscode . window . activeTextEditor ;
79
73
if ( editor == null ) return ;
80
-
81
- let terminal = getREPL ( true ) ;
82
- if ( terminal == null ) return ;
83
-
84
- function send ( terminal : vscode . Terminal ) {
85
- terminal . sendText ( editor . document . getText ( editor . selection ) , true ) ;
86
- thenFocusTextEditor ( ) ;
87
- }
88
- if ( editor . selection . isEmpty )
89
- vscode . commands . executeCommand ( 'editor.action.selectToBracket' ) . then ( ( ) => send ( terminal ) ) ;
90
- else
91
- send ( terminal ) ;
74
+ getREPL ( true ) . then ( terminal => {
75
+ function send ( terminal : vscode . Terminal ) {
76
+ sendSource ( terminal , editor . document . getText ( editor . selection ) ) ;
77
+ thenFocusTextEditor ( ) ;
78
+ }
79
+ if ( editor . selection . isEmpty )
80
+ vscode . commands . executeCommand ( 'editor.action.selectToBracket' ) . then ( ( ) => send ( terminal ) ) ;
81
+ else
82
+ send ( terminal ) ;
83
+ } ) ;
92
84
}
93
85
) ) ;
94
86
@@ -97,12 +89,10 @@ export function activate(context: vscode.ExtensionContext) {
97
89
( ) => {
98
90
let editor = vscode . window . activeTextEditor ;
99
91
if ( editor == null ) return ;
100
-
101
- let terminal = getREPL ( true ) ;
102
- if ( terminal == null ) return ;
103
-
104
- terminal . sendText ( editor . document . getText ( ) , true ) ;
105
- thenFocusTextEditor ( ) ;
92
+ getREPL ( true ) . then ( terminal => {
93
+ sendSource ( terminal , editor . document . getText ( ) ) ;
94
+ thenFocusTextEditor ( ) ;
95
+ } ) ;
106
96
}
107
97
) ) ;
108
98
}
0 commit comments