11'use strict' ;
22
3- import * as fs from 'fs' ;
4- import * as os from 'os' ;
5- import * as path from 'path' ;
63import * as vscode from 'vscode' ;
74
8- import {
9- nREPLClient
10- } from './nreplClient' ;
5+ import { nREPLClient } from './nreplClient' ;
6+ import { getNamespace } from './clojureProvider' ;
117
12- import {
13- ClojureProvider
14- } from './clojureProvider' ;
8+ export function clojureEval ( context : vscode . ExtensionContext , outputChannel : vscode . OutputChannel ) {
9+ evaluate ( context , outputChannel , false ) ;
10+ }
1511
16- interface ErrorDescription {
17- position : vscode . Position ,
18- message : string
12+ export function clojureEvalAndShowResult ( context : vscode . ExtensionContext , outputChannel : vscode . OutputChannel ) {
13+ evaluate ( context , outputChannel , true ) ;
1914}
2015
21- export function clojureEval ( context : vscode . ExtensionContext , outputChannel ?: vscode . OutputChannel ) {
16+ function evaluate ( context : vscode . ExtensionContext , outputChannel : vscode . OutputChannel , showResults : boolean ) {
17+ const editor = vscode . window . activeTextEditor ;
18+ const selection = editor . selection ;
2219
23- let editor = vscode . window . activeTextEditor ;
2420 let text : string = editor . document . getText ( ) ;
25- let ns : string ;
26- let match = text . match ( / ^ [ \s \t ] * \( (?: [ \s \t \n ] * (?: i n - ) { 0 , 1 } n s ) [ \s \t \n ] + ' ? ( [ \w . \- \/ ] + ) [ \s \S ] * \) [ \s \S ] * / ) ;
27- match ? ns = match [ 1 ] : ns = 'user' ;
28- let selection = editor . selection ;
29- let isSelection = ! selection . isEmpty ;
30-
31- if ( isSelection ) {
32- text = `(ns ${ ns } ) ${ editor . document . getText ( selection ) } ` ;
21+ if ( ! selection . isEmpty ) {
22+ const ns : string = getNamespace ( text ) ;
23+ text = `(ns ${ ns } )\n${ editor . document . getText ( selection ) } ` ;
3324 }
3425
35- let port = context . workspaceState . get < number > ( 'port' ) ;
36- let host = context . workspaceState . get < string > ( 'host' ) ;
37-
26+ const port = context . workspaceState . get < number > ( 'port' ) ;
27+ const host = context . workspaceState . get < string > ( 'host' ) ;
3828 if ( ( ! port ) || ( ! host ) ) {
3929 vscode . window . showInformationMessage ( 'You should connect to nREPL first to evaluate code.' )
4030 return ;
4131 }
32+ const nrepl = new nREPLClient ( port , host ) ;
4233
43- let filename = editor . document . fileName ;
34+ const filename = editor . document . fileName ;
4435
45- let nrepl1 = new nREPLClient ( port , host ) ;
46- let diagnostics = vscode . languages . createDiagnosticCollection ( 'Compilation Errors' ) ;
47- diagnostics . clear ( ) ;
48- nrepl1 . evalFile ( text , filename ) . then ( respObjs => {
49- respObjs . forEach ( result => {
50- if ( result . out && outputChannel ) {
51- outputChannel . append ( result . out ) ;
52- outputChannel . show ( ) ;
53- }
54- if ( result . value ) {
55- if ( outputChannel ) {
56- outputChannel . appendLine ( `=> ${ result . value } ` ) ;
57- outputChannel . show ( ) ;
58- } else {
59- vscode . window . showInformationMessage ( 'Successfully compiled' ) ;
60- }
61- } else if ( result . ex ) {
62- let nrepl2 = new nREPLClient ( port , host ) ;
63- nrepl2 . stacktrace ( result . session , ( stackteace ) => {
64- vscode . window . showErrorMessage ( 'Compilation error' ) ;
65- let errLine = stackteace . line - 1 ;
66- let errChar = stackteace . column - 1 ;
67- let errFile = stackteace . file ;
68- let errFileUri : vscode . Uri ;
69- if ( errFile ) {
70- errFileUri = vscode . Uri . file ( errFile ) ;
71- } else {
72- errFileUri = vscode . window . activeTextEditor . document . uri ;
73- }
74- let errMsg = stackteace . message ;
75-
76- // Adjust an error position if a selection has been evaluated
77- if ( isSelection ) {
78- errLine = errLine + selection . start . line ;
79- errChar = errChar + selection . start . character ;
80- }
81-
82- let errPos = new vscode . Position ( errLine , errChar ) ;
83- editor . selection = new vscode . Selection ( errPos , errPos ) ;
84- let errLineLength = editor . document . lineAt ( errLine ) . text . length ;
85-
86- diagnostics . set ( errFileUri , [ new vscode . Diagnostic ( new vscode . Range ( errLine , errChar , errLine , errLineLength ) , errMsg , vscode . DiagnosticSeverity . Error ) ] ) ;
87- nrepl2 . close ( ( ) => { } ) ;
88- } )
36+ nrepl . evalFile ( text , filename )
37+ . then ( respObjs => {
38+ if ( ! ! respObjs [ 0 ] . ex )
39+ return handleError ( nrepl , outputChannel , selection , showResults , respObjs [ 0 ] . session ) ;
8940
90- }
41+ return handleSuccess ( outputChannel , showResults , respObjs ) ;
9142 } )
92- } ) ;
93- }
43+ . then ( ( ) => nrepl . close ( ) ) ;
44+ }
45+
46+ function handleError ( nrepl : nREPLClient , outputChannel : vscode . OutputChannel , selection : vscode . Selection , showResults : boolean , session : string ) {
47+ if ( ! showResults )
48+ vscode . window . showErrorMessage ( 'Compilation error' ) ;
49+
50+ return nrepl . stacktrace ( session )
51+ . then ( stacktraceObjs => {
52+ const stacktraceObj = stacktraceObjs [ 0 ] ;
53+
54+ let errLine = stacktraceObj . line - 1 ;
55+ let errChar = stacktraceObj . column - 1 ;
56+
57+ if ( ! selection . isEmpty ) {
58+ errLine += selection . start . line ;
59+ errChar += selection . start . character ;
60+ }
61+
62+ outputChannel . appendLine ( `${ stacktraceObj . class } ${ stacktraceObj . message } ` ) ;
63+ outputChannel . appendLine ( ` at ${ stacktraceObj . file } :${ errLine } :${ errChar } ` ) ;
64+
65+ stacktraceObj . stacktrace . forEach ( trace => {
66+ if ( trace . flags . indexOf ( 'tooling' ) > - 1 )
67+ outputChannel . appendLine ( ` ${ trace . class } .${ trace . method } (${ trace . file } :${ trace . line } )` ) ;
68+ } ) ;
69+
70+ outputChannel . show ( ) ;
71+ } ) ;
72+ }
73+
74+ function handleSuccess ( outputChannel : vscode . OutputChannel , showResults : boolean , respObjs : any [ ] ) {
75+ if ( ! showResults ) {
76+ vscode . window . showInformationMessage ( 'Successfully compiled' ) ;
77+ } else {
78+ respObjs . forEach ( respObj => {
79+ if ( respObj . out )
80+ outputChannel . append ( respObj . out ) ;
81+ if ( respObj . value )
82+ outputChannel . appendLine ( `=> ${ respObj . value } ` ) ;
83+ outputChannel . show ( ) ;
84+ } ) ;
85+ }
86+ }
0 commit comments