Skip to content

Commit

Permalink
notebook markdown comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyrik committed Aug 20, 2022
1 parent fb35441 commit 7fcc022
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions src/NotebookProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,109 @@ function parseClojure(content: string): vscode.NotebookCellData[] {
// start of file to end of top level sexp pairs
const allRanges = _.zip(_.dropRight([_.first(topLevelRanges), ...fullRanges], 1), fullRanges);

let offset = 0;
let cells = [];

while (cursor.forwardSexp()) {
const start = offset;
const end = cursor.offsetStart;
offset = end;

const endForm = cursor.doc.getTokenCursor(end - 1);
const afterForm = cursor.doc.getTokenCursor(end);

if (endForm.getFunctionName() === 'comment') {
const commentRange = afterForm.rangeForCurrentForm(0);
const commentStartCursor = cursor.doc.getTokenCursor(commentRange[0]);
const commentCells = [];
let previouseEnd = start;

commentStartCursor.downList();
commentStartCursor.forwardSexp();

while (commentStartCursor.forwardSexp()) {
const range = commentStartCursor.rangeForDefun(commentStartCursor.offsetStart);

let leading = '';
const indent = commentStartCursor.doc.getRowCol(range[0])[1]; // will break with tabs?

leading = content.substring(previouseEnd, range[0]);
previouseEnd = range[1];

commentCells.push({
value: substring(content, range),
kind: vscode.NotebookCellKind.Code,
languageId: 'clojure',
metadata: {
leading: leading,
indent,
range,
richComment: true,
trailing: '',
},
});
}

_.last(commentCells).metadata.trailing = content.substring(previouseEnd, end);

cells = cells.concat(commentCells);

continue;
}

const range = cursor.rangeForDefun(cursor.offsetStart);

const leading = content.substring(start, range[0]);

if (leading.indexOf(';; ') === -1) {
cells.push({
value: leading,
kind: vscode.NotebookCellKind.Markup,
languageId: 'markdown',
metadata: {
indent: 0,
range,
leading: '',
trailing: '',
},
});
} else {
cells.push({
value: leading.replace(/;; /g, ''),
kind: vscode.NotebookCellKind.Markup,
languageId: 'markdown',
metadata: {
indent: 0,
range,
markdownComment: true,
leading: '',
trailing: '',
},
});
}

cells.push({
value: substring(content, range),
kind: vscode.NotebookCellKind.Code,
languageId: 'clojure',
metadata: {
indent: 0,
range,
leading: '',
trailing: '',
},
});
}

_.last(cells).metadata.trailing = content.substring(
_.last(cells).metadata.range[1],
content.length
);

console.log(cells);

return cells;

const ranges = allRanges.flatMap(([start, end]) => {
const endForm = cursor.doc.getTokenCursor(end - 1);
const afterForm = cursor.doc.getTokenCursor(end);
Expand Down Expand Up @@ -128,6 +231,13 @@ function writeCellsToClojure(cells: vscode.NotebookCellData[]) {

return acc.concat(result);
} else {
if (x.metadata.markdownComment) {
let result = x.value.replace(/\n(?!$)/g, '\n;; ');
if (index === 0) {
result = ';; ' + result;
}
return acc.concat(result);
}
return acc.concat(x.value);
}
}, '');
Expand Down

0 comments on commit 7fcc022

Please sign in to comment.