Skip to content

Commit 94eca88

Browse files
committed
notebook markdown comments
1 parent 81b0288 commit 94eca88

File tree

2 files changed

+62
-21
lines changed

2 files changed

+62
-21
lines changed

src/NotebookProvider.ts

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,73 @@ export class NotebookProvider implements vscode.NotebookSerializer {
3333
function 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

6186
function 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

65105
export class NotebookKernel {

test-data/notebook.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
(ns notebook)
44

5+
"string1" "string"
56
;; A line comment
67
;; block
78

@@ -12,7 +13,7 @@
1213
:deeper {:a 1, "foo" :bar, [1 2 3] (vec (range 2000))}})
1314

1415
;; Line comment, line 1
15-
16+
;;
1617
;; Line comment, line 2
1718
(defn foo []
1819
(println "bar"))

0 commit comments

Comments
 (0)