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 12, 2022
1 parent 81b0288 commit 94eca88
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 21 deletions.
80 changes: 60 additions & 20 deletions src/NotebookProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,73 @@ export class NotebookProvider implements vscode.NotebookSerializer {
function parseClojure(content: string): vscode.NotebookCellData[] {
const cursor = tokenCursor.createStringCursor(content);
const topLevelRanges = cursor.rangesForTopLevelForms().flat();
if (topLevelRanges.length) {
topLevelRanges[0] = 0;
}

// grab only the ends of ranges, so we can include all of the file in the notebook
const fullRanges = _.filter(topLevelRanges, (_, index) => {
return index % 2 !== 0;
});

// last range should include end of file
fullRanges[fullRanges.length - 1] = content.length;

// start of file to end of top level sexp pairs
const allRanges = _.zip(_.dropRight([_.first(topLevelRanges), ...fullRanges], 1), fullRanges);
topLevelRanges.push(content.length);

const allRanges = _.zip(_.dropRight([0, ...topLevelRanges], 1), topLevelRanges);

const ranges = allRanges
.map(([start, end], index) => {
const isWhitespace = index % 2 === 0;
const rangeContent = content.substring(start, end);

if (isWhitespace) {
if (start === end) {
return {
value: '',
kind: vscode.NotebookCellKind.Markup,
languageId: 'markdown',
};
}

if (rangeContent.startsWith('\n\n;; ')) {
const startingWhitespace = rangeContent.indexOf('\n;; ');
const endingWhitespace = rangeContent.length - rangeContent.trimEnd().length;

return {
value: rangeContent.substring(startingWhitespace).trimEnd().replace(/\n;; /g, '\n'),
kind: vscode.NotebookCellKind.Markup,
languageId: 'markdown',
metadata: { asMarkdown: true, startingWhitespace, endingWhitespace },
};
}

return {
value: rangeContent,
kind: vscode.NotebookCellKind.Markup,
languageId: 'markdown',
};
} else {
return {
value: rangeContent,
kind: vscode.NotebookCellKind.Code,
languageId: 'clojure',
};
}
})
.filter((x) => x.value.length);

const ranges = allRanges.map(([start, end]) => {
return {
value: content.substring(start, end),
kind: vscode.NotebookCellKind.Code,
languageId: 'clojure',
};
});
return ranges;
}

function writeCellsToClojure(cells: vscode.NotebookCellData[]) {
return cells.map((x) => x.value).join('');
return cells
.map((x, index) => {
if (x.kind === vscode.NotebookCellKind.Code) {
return x.value;
} else {
if (x.metadata.asMarkdown) {
return (
'\n'.repeat(x.metadata.startingWhitespace) +
x.value.replace(/\n/g, '\n;; ') +
'\n'.repeat(x.metadata.endingWhitespace)
);
}
return x.value;
}
})
.join('');
}

export class NotebookKernel {
Expand Down
3 changes: 2 additions & 1 deletion test-data/notebook.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

(ns notebook)

"string1" "string"
;; A line comment
;; block

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

;; Line comment, line 1

;;
;; Line comment, line 2
(defn foo []
(println "bar"))
Expand Down

0 comments on commit 94eca88

Please sign in to comment.