Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notebook markdown comments #1828

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 131 additions & 21 deletions src/NotebookProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { prettyPrint } from '../out/cljs-lib/cljs-lib';
import * as tokenCursor from './cursor-doc/token-cursor';
import * as repl from './api/repl';
import _ = require('lodash');
import { isInteger } from 'lodash';

export class NotebookProvider implements vscode.NotebookSerializer {
private readonly decoder = new TextDecoder();
Expand All @@ -30,36 +31,145 @@ 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;
function substring(content: string, [start, end]) {
if (isInteger(start) && isInteger(end)) {
return content.substring(start, end);
}
return '';
}

// 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;
function parseClojure(content: string): vscode.NotebookCellData[] {
const cursor = tokenCursor.createStringCursor(content);
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;
}

// start of file to end of top level sexp pairs
const allRanges = _.zip(_.dropRight([_.first(topLevelRanges), ...fullRanges], 1), fullRanges);
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: '',
},
});
}

const ranges = allRanges.map(([start, end]) => {
return {
value: content.substring(start, end),
cells.push({
value: substring(content, range),
kind: vscode.NotebookCellKind.Code,
languageId: 'clojure',
};
});
return ranges;
metadata: {
indent: 0,
range,
leading: '',
trailing: '',
},
});
}

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

console.log(cells);

return cells;
}

function writeCellsToClojure(cells: vscode.NotebookCellData[]) {
return cells.map((x) => x.value).join('');
return cells.reduce((acc, x, index) => {
if (x.kind === vscode.NotebookCellKind.Code) {
let result = '';

// created inside the notebook
if (undefined === x.metadata.leading) {
const indent = index > 0 ? _.repeat(' ', cells[index - 1].metadata.indent) : '';

result = '\n\n' + indent + x.value;
} else {
result = x.metadata.leading + x.value + x.metadata.trailing;
}

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);
}
}, '');
}

export class NotebookKernel {
Expand Down
12 changes: 12 additions & 0 deletions src/cursor-doc/token-cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,18 @@ export class LispTokenCursor extends TokenCursor {
return ranges;
}

rangesForCommentForms(offset: number): [number, number][] {
const cursor = this.doc.getTokenCursor(offset);
const ranges: [number, number][] = [];
while (cursor.forwardSexp()) {
const end = cursor.offsetStart;
cursor.backwardSexp();
ranges.push([cursor.offsetStart, end]);
cursor.forwardSexp();
}
return ranges;
}

isWhiteSpace(): boolean {
return tokenIsWhiteSpace(this.getToken());
}
Expand Down
79 changes: 79 additions & 0 deletions test-data/notebook2.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
(ns notebook)

"string1" "string2"
;; A line comment
;; block

(def hover-map
{:stuff "in-here"
:ohter "yeah"
;; Yada yada
:deeper {:a 1, "foo" :bar, [1 2 3] (vec (range 2000))}})
;; Foo

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

(foo)

;; A line comment

hover-map

"<html> <body>testing<h2>HTML Image</h2><img src='https://raw.githubusercontent.com/BetterThanTomorrow/calva/dev/assets/calva-64h.png'><iframe width='560' height='315' src='https://www.youtube.com/embed/dQw4w9WgXcQ' title='YouTube video player' frameborder='0' allow='accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe></body></html>"
; foo
(comment
(+ 40 2)

42

42/1

"forty-two")

(def forty-two
(+
40
1
1))

; c3

;c4

"one"

;c5

"two"

;c6
"three"
;c7

"four"
;c8
:five

;c9
six

:seven
"eight"
nine

ten :eleven "twelve"

; c10

:thirteen

"fourteen"

;c11
;c12

:fifteen "sixteen" seventeen