@@ -33,33 +33,73 @@ export class NotebookProvider implements vscode.NotebookSerializer {
3333function parseClojure ( content : string ) : vscode . NotebookCellData [ ] {
3434 const cursor = tokenCursor . createStringCursor ( content ) ;
3535 const topLevelRanges = cursor . rangesForTopLevelForms ( ) . flat ( ) ;
36- if ( topLevelRanges . length ) {
37- topLevelRanges [ 0 ] = 0 ;
38- }
39-
40- // grab only the ends of ranges, so we can include all of the file in the notebook
41- const fullRanges = _ . filter ( topLevelRanges , ( _ , index ) => {
42- return index % 2 !== 0 ;
43- } ) ;
4436
4537 // last range should include end of file
46- fullRanges [ fullRanges . length - 1 ] = content . length ;
47-
48- // start of file to end of top level sexp pairs
49- const allRanges = _ . zip ( _ . dropRight ( [ _ . first ( topLevelRanges ) , ...fullRanges ] , 1 ) , fullRanges ) ;
38+ topLevelRanges . push ( content . length ) ;
39+
40+ const allRanges = _ . zip ( _ . dropRight ( [ 0 , ...topLevelRanges ] , 1 ) , topLevelRanges ) ;
41+
42+ const ranges = allRanges
43+ . map ( ( [ start , end ] , index ) => {
44+ const isWhitespace = index % 2 === 0 ;
45+ const rangeContent = content . substring ( start , end ) ;
46+
47+ if ( isWhitespace ) {
48+ if ( start === end ) {
49+ return {
50+ value : '' ,
51+ kind : vscode . NotebookCellKind . Markup ,
52+ languageId : 'markdown' ,
53+ } ;
54+ }
55+
56+ if ( rangeContent . startsWith ( '\n\n;; ' ) ) {
57+ const startingWhitespace = rangeContent . indexOf ( '\n;; ' ) ;
58+ const endingWhitespace = rangeContent . length - rangeContent . trimEnd ( ) . length ;
59+
60+ return {
61+ value : rangeContent . substring ( startingWhitespace ) . trimEnd ( ) . replace ( / \n ; ; / g, '\n' ) ,
62+ kind : vscode . NotebookCellKind . Markup ,
63+ languageId : 'markdown' ,
64+ metadata : { asMarkdown : true , startingWhitespace, endingWhitespace } ,
65+ } ;
66+ }
67+
68+ return {
69+ value : rangeContent ,
70+ kind : vscode . NotebookCellKind . Markup ,
71+ languageId : 'markdown' ,
72+ } ;
73+ } else {
74+ return {
75+ value : rangeContent ,
76+ kind : vscode . NotebookCellKind . Code ,
77+ languageId : 'clojure' ,
78+ } ;
79+ }
80+ } )
81+ . filter ( ( x ) => x . value . length ) ;
5082
51- const ranges = allRanges . map ( ( [ start , end ] ) => {
52- return {
53- value : content . substring ( start , end ) ,
54- kind : vscode . NotebookCellKind . Code ,
55- languageId : 'clojure' ,
56- } ;
57- } ) ;
5883 return ranges ;
5984}
6085
6186function writeCellsToClojure ( cells : vscode . NotebookCellData [ ] ) {
62- return cells . map ( ( x ) => x . value ) . join ( '' ) ;
87+ return cells
88+ . map ( ( x , index ) => {
89+ if ( x . kind === vscode . NotebookCellKind . Code ) {
90+ return x . value ;
91+ } else {
92+ if ( x . metadata . asMarkdown ) {
93+ return (
94+ '\n' . repeat ( x . metadata . startingWhitespace ) +
95+ x . value . replace ( / \n / g, '\n;; ' ) +
96+ '\n' . repeat ( x . metadata . endingWhitespace )
97+ ) ;
98+ }
99+ return x . value ;
100+ }
101+ } )
102+ . join ( '' ) ;
63103}
64104
65105export class NotebookKernel {
0 commit comments