Skip to content

Commit 71e4918

Browse files
committed
better whitespace handling in rich comments
1 parent 511ef43 commit 71e4918

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/NotebookProvider.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,33 +65,44 @@ function parseClojure(content: string): vscode.NotebookCellData[] {
6565
const commentRange = afterForm.rangeForCurrentForm(0);
6666
const commentStartCursor = cursor.doc.getTokenCursor(commentRange[0]);
6767
const commentCells = [];
68-
let count = 0;
68+
let previouseEnd = start;
6969

7070
commentStartCursor.downList();
7171
commentStartCursor.forwardSexp();
7272

7373
while (commentStartCursor.forwardSexp()) {
74+
const range = commentStartCursor.rangeForDefun(commentStartCursor.offsetStart);
75+
let leading = '';
76+
77+
leading = content.substring(previouseEnd, range[0]);
78+
previouseEnd = range[1];
79+
7480
commentCells.push({
75-
value: substring(
76-
content,
77-
commentStartCursor.rangeForDefun(commentStartCursor.offsetStart)
78-
),
81+
value: substring(content, range),
7982
kind: vscode.NotebookCellKind.Code,
8083
languageId: 'clojure',
8184
metadata: {
82-
richComment: { index: count },
85+
leading: leading,
86+
range,
87+
richComment: true,
88+
trailing: '',
8389
},
8490
});
85-
count++;
8691
}
87-
commentCells.forEach((x) => (x.metadata.richComment.count = count));
92+
93+
_.last(commentCells).metadata.trailing = content.substring(previouseEnd, end);
8894

8995
return commentCells;
9096
}
97+
9198
return {
9299
value: content.substring(start, end),
93100
kind: vscode.NotebookCellKind.Code,
94101
languageId: 'clojure',
102+
metadata: {
103+
leading: '',
104+
trailing: '',
105+
},
95106
};
96107
});
97108

@@ -101,17 +112,13 @@ function parseClojure(content: string): vscode.NotebookCellData[] {
101112
function writeCellsToClojure(cells: vscode.NotebookCellData[]) {
102113
return cells.reduce((acc, x) => {
103114
if (x.kind === vscode.NotebookCellKind.Code) {
104-
let result: string = x.value;
105-
if (x.metadata?.richComment) {
106-
if (x.metadata.richComment.index === 0) {
107-
result = '\n(comment\n'.concat(result);
108-
}
109-
110-
if (x.metadata.richComment.index === x.metadata.richComment.count - 1) {
111-
result = result.concat(')');
112-
} else {
113-
result = result.concat('\n');
114-
}
115+
let result = '';
116+
117+
// created inside the notebook
118+
if (undefined === x.metadata.leading) {
119+
result = '\n\n' + x.value;
120+
} else {
121+
result = x.metadata.leading + x.value + x.metadata.trailing;
115122
}
116123

117124
return acc.concat(result);

0 commit comments

Comments
 (0)