Skip to content

Commit 7fcc022

Browse files
committed
notebook markdown comments
1 parent fb35441 commit 7fcc022

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

src/NotebookProvider.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,109 @@ function parseClojure(content: string): vscode.NotebookCellData[] {
5757
// start of file to end of top level sexp pairs
5858
const allRanges = _.zip(_.dropRight([_.first(topLevelRanges), ...fullRanges], 1), fullRanges);
5959

60+
let offset = 0;
61+
let cells = [];
62+
63+
while (cursor.forwardSexp()) {
64+
const start = offset;
65+
const end = cursor.offsetStart;
66+
offset = end;
67+
68+
const endForm = cursor.doc.getTokenCursor(end - 1);
69+
const afterForm = cursor.doc.getTokenCursor(end);
70+
71+
if (endForm.getFunctionName() === 'comment') {
72+
const commentRange = afterForm.rangeForCurrentForm(0);
73+
const commentStartCursor = cursor.doc.getTokenCursor(commentRange[0]);
74+
const commentCells = [];
75+
let previouseEnd = start;
76+
77+
commentStartCursor.downList();
78+
commentStartCursor.forwardSexp();
79+
80+
while (commentStartCursor.forwardSexp()) {
81+
const range = commentStartCursor.rangeForDefun(commentStartCursor.offsetStart);
82+
83+
let leading = '';
84+
const indent = commentStartCursor.doc.getRowCol(range[0])[1]; // will break with tabs?
85+
86+
leading = content.substring(previouseEnd, range[0]);
87+
previouseEnd = range[1];
88+
89+
commentCells.push({
90+
value: substring(content, range),
91+
kind: vscode.NotebookCellKind.Code,
92+
languageId: 'clojure',
93+
metadata: {
94+
leading: leading,
95+
indent,
96+
range,
97+
richComment: true,
98+
trailing: '',
99+
},
100+
});
101+
}
102+
103+
_.last(commentCells).metadata.trailing = content.substring(previouseEnd, end);
104+
105+
cells = cells.concat(commentCells);
106+
107+
continue;
108+
}
109+
110+
const range = cursor.rangeForDefun(cursor.offsetStart);
111+
112+
const leading = content.substring(start, range[0]);
113+
114+
if (leading.indexOf(';; ') === -1) {
115+
cells.push({
116+
value: leading,
117+
kind: vscode.NotebookCellKind.Markup,
118+
languageId: 'markdown',
119+
metadata: {
120+
indent: 0,
121+
range,
122+
leading: '',
123+
trailing: '',
124+
},
125+
});
126+
} else {
127+
cells.push({
128+
value: leading.replace(/;; /g, ''),
129+
kind: vscode.NotebookCellKind.Markup,
130+
languageId: 'markdown',
131+
metadata: {
132+
indent: 0,
133+
range,
134+
markdownComment: true,
135+
leading: '',
136+
trailing: '',
137+
},
138+
});
139+
}
140+
141+
cells.push({
142+
value: substring(content, range),
143+
kind: vscode.NotebookCellKind.Code,
144+
languageId: 'clojure',
145+
metadata: {
146+
indent: 0,
147+
range,
148+
leading: '',
149+
trailing: '',
150+
},
151+
});
152+
}
153+
154+
_.last(cells).metadata.trailing = content.substring(
155+
_.last(cells).metadata.range[1],
156+
content.length
157+
);
158+
159+
console.log(cells);
160+
161+
return cells;
162+
60163
const ranges = allRanges.flatMap(([start, end]) => {
61164
const endForm = cursor.doc.getTokenCursor(end - 1);
62165
const afterForm = cursor.doc.getTokenCursor(end);
@@ -128,6 +231,13 @@ function writeCellsToClojure(cells: vscode.NotebookCellData[]) {
128231

129232
return acc.concat(result);
130233
} else {
234+
if (x.metadata.markdownComment) {
235+
let result = x.value.replace(/\n(?!$)/g, '\n;; ');
236+
if (index === 0) {
237+
result = ';; ' + result;
238+
}
239+
return acc.concat(result);
240+
}
131241
return acc.concat(x.value);
132242
}
133243
}, '');

0 commit comments

Comments
 (0)